Extract list of specific frames using ffmpeg
Solution 1
Use
ffmpeg -i in.mp4 -vf select='eq(n\,100)+eq(n\,184)+eq(n\,213)' -vsync 0 frames%d.jpg
FFmpeg is primarily a processor of timed video i.e. media with a cycle rate such as framerate or sample rate. It assumes that the output should be at the same rate as the source. If inadequate frames are supplied, it duplicates unless told not to. -vsync 0
is added which, in this case, tells it to suppress duplication.
Solution 2
A very naive answer...
#! /bin/sh -e
# framegrab v0.1
frames="$1"
filename="$2"
printf '%s' "$frames" |
sed 's:\[\|\]::g; s:[, ]\+:\n:g' |
xargs printf '%03d\n' |
xargs -IFRAME ffmpeg -i "$filename" -vf "select=eq(n\,FRAME)" -vframes 1 out_imageFRAME.jpg
Call it with your argument list:
framegrab '[100, 110, 127, 270, 300]' test_video.mp4
John Allard
Updated on June 08, 2022Comments
-
John Allard about 2 years
I'm trying to use ffmpeg on a video to extract a list of specific frames, denoted by their frame numbers. So lets say I want to extract just one frame from 'test_video.mp4', frame number 150 to be exact. I can use the following command
ffmpeg -i test_video.mp4 -vf "select=gte(n\, 150)" -vframes 1 ~/test_image.jpg
But what if I want to exact a list of frames, such as
[100, 110, 127, 270, 300]
? I have been looking at this page (https://ffmpeg.org/ffmpeg-filters.html#select_002c-aselect) and can see that there are ways to extract multiple frames based on parity of frame number, timestamps, etc, but I can't find the syntax for doing what I need to do.Ideally I would extract the list of given frames under the naming convention
out_image%03d.jpg
where the%03d
is replaced by the given frame number, although a timestamp would also work.Is there a way to do this?
-
John Allard almost 8 yearsthanks for your help! I'm having trouble figuring out on my own though, does this run one ffmpeg process per frame number or is it getting all of the desired frames through a single ffmpeg call?
-
John Allard almost 8 yearsI couldn't get this to work, it only outputs a single image file even when given a list of 5 frame numbers.
-
John Allard almost 8 yearsSo I fixed it by changing
sed 's:\[::g; s:\]::g; s/, */\n/g' |
tosed 's:\[::g; s:\]::g; s/, */ /g' |
, but I can see now that it is just running ffmpeg many times over over. This technically works but is not ideal for the embedded system I'm running on, I was hoping there was a way for ffmpeg to handle it in one fell swoop -
Michael Back almost 8 yearsAbsolutely is not ideal.... It is just something to get you going for now. I have just looked at the man page for ffmpeg -- look at the following example: ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg
-
Michael Back almost 8 yearsAs far as
' '
verses\n
-- it may be our versions of xargs don't work the same? I tested this on Linux and' '
delimiting would not work for me... but\n
did. -
Ngoral over 6 yearsIs that really sets output frames numbers to 100, 184 and 213? Mine not. It sets just 1, 2, 3
-
Gyan over 6 years@Ngoral - it selects input frames # 100,184,213. For output filenames, add
-frame_pts 1
with a recent ffmpeg version. -
devwannabe almost 4 yearsWorked really well! Thank you!