How-To: Extract images from a video file using FFmpeg
Extracting all frames from a video file is easily achieved with FFmpeg.
Here’s a simple command line that will create 25 PNG images from every second of footage in the input DV file. The images will be saved in the current directory.
ffmpeg -i input.dv -r 25 -f image2 images%05d.png
The newly created files will all start with the word “images” and be numbered consecutively, including five pre-appended zeros. e.g. images000001.png.
From a video that was 104 seconds long, for a random example, this command would create 2600 PNG files! Quite messy in the current directory, so instead use this command to save the files in a sub-directory called extracted_images:
ffmpeg -i input.dv -r 25 -f image2 extracted_images/images%05d.png
Moving on, let’s say you just wanted 25 frames from the first 1 second, then this line will work:
ffmpeg -i input.dv -r 25 -t 00:00:01 -f image2 images%05d.png
The -t flag in FFmpeg specifies the length of time to transcode. This can either be in whole seconds or hh:mm:ss format.
Making things a little more complex we can create images from all frames, beginning at the tenth second, and continuing for 5 seconds, with this line:
ffmpeg -i input.dv -r 25 -ss 00:00:10 -t 00:00:05 -f image2 images%05d.png
The -ss flag is used to denote start position, again in whole seconds or hh:mm:ss format.
Maybe extracting an image from every single frame in a video, resulting in a large number of output files, is not what you need. Here’s how to create a single indicative poster frame, of the video clip, from the first second of footage:
ffmpeg -i input.dv -r 1 -t 00:00:01 -f image2 images%05d.png
Notice that the -r flag is now set to 1.
If you want the poster frame from a different part of the clip, then specify which second to take it from using the -ss tag, in conjunction with the line above.
Lastly, if you wanted to create a thumbnail story board, showing action throughout the entire length of the video clip, you’ll need to specify the output image dimensions. Use the following line:
ffmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
My original file was 720×576, so the image dimensions are a whole division of this.
Thank you for this explanation!
May be you can help me with the following problem:
Given the .flv I need to extract 10 images from the begining to the end of the video. Each time it would be a different clip with it’s length. The first image will be from 1st second, the 2nd from (length/10)*2, then (length/10)*3 etc……and the 10th picture is from the last second.
Can I do it in one command, without checking the length of the file and 10 times calling?
Thank you in advance!
Yulia, maybe you could try this command:
ffmpeg -i input.dv -r 1 -f image2 images%05d.png
That should extract 1 image from every second of the video. So, if your video is 10 seconds long, it will extract 10 images.
Is this what you mean?
Hi,
)
first: thanks for sharing your tips.
I’m also trying to extract images from a movie (I guess as yulia would like too).
I have some movies who are only few seconds long and some others who may be more than an hour.
For every movies I would like to extract 3 images (one at the start, one at the middle and finally one at the end).
Until yet, I did check the length of the movie, divide it by 3 and use something like that (where [SS] is the time in seconds of the picture to extract):
ffmpeg -i input.dv -vframes 1 -ss [SS] -f image2 images%d.png
It works, but… if the movie is more than 30 minutes (for example, could be more longer) the process will take a lot of time (and by the way I need then 3 commands, not really nice…).
I also have also try something like:
ffmpeg -i input.dv -r [R] -f image2 images%d.png
Where [R] is a really small float value obtained by the number of images I would like divided by the length of the movie in seconds. But here the problem is that I can not use [R] smaller than 0.1.
Any better idea?
Thanks again (and in advance
You explained very well how to extract images from a video file using FFmpeg, this post proved to be very useful for me as it helped me to do that. I am so glad that I have bookmarked this website because I see that it is full of various and attractive information about everything.
For fast seek at video file, ffmpeg comand line parametrs -ss vust go defore -i .