How to extract one frame of a video every N seconds to an image?

It's very simple with ffmpeg, and it can output one frame every N seconds without extra scripting. To export as an image sequence just use myimage_%04d.png or similar as the output. The %0xd bit is converted to a zero-padded integer x digits long - the example I gave gets output as

  • myimage_0000.png,

  • myimage_0001.png,

  • myimage_0002.png

    etc..

You can use lots of still image formats, png, jpeg, tga, whatever (see ffmpeg -formats for a full list).

Ok so now we know how to export the movie as a sequence of images, but say we don't want to export every single frame?

The trick is to simply change the frame rate of the output to whatever we want using the -r n option where n is the number of frames per second. 1 frame per second would be -r 1, one frame every four seconds would be -r 0.25, one frame every ten seconds would be -r 0.1 and so on.

So to put it all together, this is how it would look to save one frame of input.mov every four seconds to output_0000.png, output_0001.png etc.:

ffmpeg -i input.mov -r 0.25 output_%04d.png

Note that the -r 0.25 option goes after the -i input.mov part, because it's controlling the frame rate of the output. If you put it before the input it would treat the input file as if it had the specified frame rate.

Change the %xd to however many digits you need, e.g. if the command would create more than 10,000 frames change the %04d to %05d. This also works for input files that are image sequence. Read more here.

Windows users: On the command line use %

example: ffmpeg -i inputFile.mp4 -r 1 outputFile_%02d.png

In CMD and BAT Scripts use %%

example: ffmpeg -i inputFile.mp4 -r 1 outputFile %%02d.png

So double %% in scripts, and single % on the interactive command line. Getting it wrong in either situation will generate an error.


mplayer -vo jpeg -sstep 5 file.avi

will save a frame as a jpeg file every 5 seconds.

However, it will not stop at the end of the file, it will continue producing copies of the last frame. To avoid this, find the duration of the video in seconds, using another player, or with mplayer:

mplayer -vo null -ao null -frames 0 -identify file.avi

and look for a line like "ID_LENGTH=147.00".

Subtract 2 from the length, and use this value for the -endposoption. For example, for a 147 second video:

mplayer -vo jpeg -sstep 5 -endpos 145 file.avi

With ffmpeg, you can do the following:

ffmpeg -ss 4 -i input.avi -s 320x240 -frames:v 1 output.jpg

This command generates a 320×240 sized JPG thumbnail at the 4th second in the video. Put this in a script that changes the time and file name and you're done.

More info: Create a thumbnail image every X seconds of the video