How to extract 1 screenshot for a video with ffmpeg at a given time?
Solution 1
Use the -ss
option:
ffmpeg -ss 01:23:45 -i input -frames:v 1 -q:v 2 output.jpg
-
For JPEG output use
-q:v
to control output quality. Full range is a linear scale of 1-31 where a lower value results in a higher quality. 2-5 is a good range to try. -
The select filter provides an alternative method for more complex needs such as selecting only certain frame types, or 1 per 100, etc.
-
Placing
-ss
before the input will be faster. See FFmpeg Wiki: Seeking and this excerpt from theffmpeg
cli tool documentation:
-ss
position (input/output)When used as an input option (before
-i
), seeks in this input file to position. Note the in most formats it is not possible to seek exactly, soffmpeg
will seek to the closest seek point before position. When transcoding and-accurate_seek
is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when-noaccurate_seek
is used, it will be preserved.When used as an output option (before an output filename), decodes but discards input until the timestamps reach position.
position may be either in seconds or in
hh:mm:ss[.xxx]
form.
Solution 2
FFMpeg can do this by seeking to the given timestamp and extracting exactly one frame as an image, see for instance:
ffmpeg -i input_file.mp4 -ss 01:23:45 -vframes 1 output.jpg
Let's explain the options:
-i input file the path to the input file
-ss 01:23:45 seek the position to the specified timestamp
-vframes 1 only handle one video frame
output.jpg output filename, should have a well-known extension
The -ss
parameter accepts a value in the form HH:MM:SS[.xxx]
or as a number in seconds. If you need a percentage, you need to compute the video duration beforehand.
Related videos on Youtube
Peter Bengtsson
Updated on July 08, 2022Comments
-
Peter Bengtsson almost 2 years
There are many tutorials and stuff showing how to extract multiple screenshots from a video using ffmpeg. You set -r and you can even start a certain amount in.
But I just want 1 screenshot at, say 01:23:45 in. Or 1 screenshot at 86% in.
This is all possible with ffmpegthumbnailer but it's another dependency I don't want to depend on. I want to be able to do it with ffmpeg.
-
Peter Bengtsson over 9 yearsSeems to work as expected. I'm currently benchmarking if this is the same speed as using ffmpegthumbnailer.
-
Peter Bengtsson over 9 yearsYeah, this approach is super slow. See peterbe.com/plog/fastest-way-to-take-screencaps-out-of-videos
-
SirDarius over 9 yearstry using the -ss option before -i since it will skip the decoding part of the pipeline. This might speedup the process.
-
Peter Bengtsson over 9 yearsThis is MUCH faster. Thank you so much. Putting the -ss first makes a huge difference.
-
dorien almost 7 yearsis there any way to get the n-th frame after the specified time?
-
Stefan Reich almost 5 years@GuntherPiez I don't see any mention anywhere about possible artifacts.
-
Nicke Manarin over 4 yearsIs there any way to output as PNG to the stream instead of a file?
-
Glenn Slayden over 4 years@NickeManarin Yes..
ffmpeg -ss 01:23:45 -i video.mp4 -c:v png -frames:v 1 image.jpg
. Notice that I also put the-ss
"seek" option before the input file on the command line, as others have recommended. -
Nicke Manarin over 4 years@GlennSlayden Hmm, your example will output 1 frame to 1 file, not multiple frames to the output stream.
-
Glenn Slayden over 4 years@NickeManarin Oops, sorry, I misread that you just wanted a PNG instead of JPG version of what's shown in this answer. To output entire file as PNG to
stdout
, use this:ffmpeg -i video.mp4 -c:v png -f image2pipe -
(notice the bare hyphen at the end). I believe-f rawvideo -
might work instead ofimage2pipe
also. -
Synexis over 4 yearsNote that
vframes
is obsolete in (at least) version 4.2.2. Per manual "[vframes
] is an obsolete alias for '-frames:v
', which you should use instead." -
Smeterlink almost 4 yearsWorks perfect and fast.
-
Kyryl Zotov about 3 yearsMy video is 15 seconds 15.000 Millis But when I try to get the 15.000 it gives me error but if I try to get 14.800 its ok. What can be the problem with latest frame?