72
43
How can I convert a video file to a sequence of images, for example one frame every N seconds. Can mplayer or ffmpeg do this? I have used MPlayer to grab screenshots manually but I would like to automate this for a long video.
72
43
How can I convert a video file to a sequence of images, for example one frame every N seconds. Can mplayer or ffmpeg do this? I have used MPlayer to grab screenshots manually but I would like to automate this for a long video.
28
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 -endpos
option. For example, for a 147 second video:
mplayer -vo jpeg -sstep 5 -endpos 145 file.avi
Could you at least mention on which operating system are you using this command? – Tomáš Zato - Reinstate Monica – 2015-04-23T00:06:10.233
It was 5 years ago, but it was probably the latest Ubuntu Desktop at the time, Ubuntu 10.4 . – Liam – 2015-04-23T22:27:25.117
this sounds like an mplayer bug. nice workaround. – quack quixote – 2010-04-29T17:04:49.277
How can I choose steps shorter than 1s. It does only accept integers. – fcpenha – 2016-03-02T22:52:27.977
Nowadays, mpv --vo=image --sstep=5 movie.mkv
works great without the aforementioned bug (mpv is a fork of mplayer) using mpv 0.27.0 on Debian.
I appreciate how on Super User this answer is 2nd even though it's the accepted answer. On SO and SO Meta the accepted answer is showed first – Nathan – 2018-06-11T20:41:38.563
73
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.
What should I put into -r
if I want to export every frame? If I know the video's FPS, I guess I input that, but what if I don't? – Tomáš Zato - Reinstate Monica – 2015-04-23T00:10:04.153
5Don't use the -r flag at all. Then FFMPEG will use the input file's frame rate. – stib – 2015-04-23T00:42:23.770
2Note that you'll really want to use jpegs
for this, unless you're sure png
's are what you want. In my case, jpeg
s were about 50kb each, and png
s were about 2MB. – mlissner – 2015-10-28T02:14:47.860
5the advantage of pngs is lossless compression, so if you're using the image sequence as an intermediate and don't mind the extra size then that's when you might use png. – stib – 2016-05-11T07:43:57.320
It is worth to add that -q:v 1
is a very useful option here as in https://superuser.com/questions/318845/improve-quality-of-ffmpeg-created-jpgs
This should probably be its own question, but @stib is that why most of the images I find on Google are .jpgs? Because they're smaller? – Nathan – 2018-06-11T21:07:13.363
Not only its own question, but probably on a different site… But whatevs: to oversimplify, jpegs work better for photographs (or stills from video), where better means smaller file for less perceptible quality loss, pngs are lossless, and deal with text and design elements much better. See this definitive answer: https://stackoverflow.com/questions/2336522/png-vs-gif-vs-jpeg-vs-svg-when-best-to-use
– stib – 2018-06-12T00:50:54.533Tip: make sure the -r 1
goes AFTER the input file, otherwise it doesn't have the intended effect. – Simon East – 2019-09-23T09:10:22.130
Good point Simon. – stib – 2019-09-24T10:58:35.130
@stib for -r
> 1 how are those frames distributed? I'm trying to extract 5 frames from every second of video which are evenly distributed across that second. – Filip Allberg – 2020-02-24T19:40:04.043
11
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
This seems to do what I want, but with one problem. It will process the file from the start on each iteration, getting exponentially slow. – Liam – 2010-04-27T14:42:45.140
@Liam - Does it help if you leave the offset time alone (set it at 1) and use the -ss position
flag to seek further into the file instead? -ss Seek to given time position in seconds. hh:mm:ss[.xxx] syntax is also supported
– Nifle – 2010-04-29T07:53:31.767
1
@liam: put the -ss
before the -i
so that the seek happens before the input video gets decoded. this should speed it up. source
2There's a better, much faster way to do it with ffmpeg. See my answer below. – stib – 2014-03-15T09:47:53.227
stib's solution is the best IMO. – Griffin – 2014-06-02T07:48:23.320
8
If you are after a kind of contact sheet and if you are working with a Unix-like OS, you can use this elaborate script, called to the point Video Contact Sheet *NIX, short vcs.
In the background it also uses ffmpeg
(by default) or mplayer
, hence can handle a lot of video formats. It automates the process of capturing still images from the movie and compose these to an image with some header and footer. You can choose e.g. how many captures you want or alternatively the time differences between them.
For a interval of 10 minutes, the invocation is like that:
vcs -i 10m input.avi
Check the full list of commandline options for some other tweaks.
Here is an example contact sheet, taken from the homepage:
7
With VLC 1.1.0 and above, you can use the scene video filter:
vlc C:\video\to\process.mp4 --rate=1 --video-filter=scene --vout=dummy --start-time=10 --stop-time=11 --scene-format=png --scene-ratio=24 --scene-prefix=snap --scene-path=C:\path\for\snapshots\ vlc://quit
The above saves 1 out of every 24 frames (--scene-ratio=24), starting at 00:00:10 and ending at 00:00:11.
Just tested and confirmed this works with VLC 2.0.3 on a fresh Windows 8 installation (I have no additional video plugins or software).
Full documentation: http://wiki.videolan.org/How_to_create_thumbnails
2
VirtualDub can do this for you
File
-> Export
-> Image sequence
Will this save every frame as an image? – Liam – 2010-04-27T15:32:43.260
yes, every frame. – Shevek – 2010-04-27T16:25:52.227
I need to skip most frames, outputting all frames will use up too much storage space. I need just one frame every few seconds. – Liam – 2010-04-28T09:56:32.803
1
You could also try this
from the VLC command line...
vlc "C:\YOUR\file\path\file.avi" -V image --image-out-prefix=capname --image-out-ratio=60
file.avi is the video you want to capture from, capname is the prefix of the saved images, you might want to play around with the ratio (60 means that 1 out of 60 images is captured) You can add more commands, for example --image-out-format jpeg will save your caps as jpegs instead of of pngs, --snapshot-path lets you choose where to save your caps.
I got this message: The command line options couldn't be loaded, check that they are valid. – Liam – 2010-04-28T09:45:51.860
On Ubuntu, I got this message: unknown option or missing mandatory argument `--image-out-prefix=capname' – Liam – 2010-04-28T09:54:54.010
1
You can skip frames in VirtualDub. Just use "Decimate By" option located at Video -> Frame Rate menu. For example if you set "Decimate By 100" then use File -> Export -> Image sequence, it will save only every 100th frame.
See this related question about choosing the quality of images: https://stackoverflow.com/questions/10225403/how-can-i-extract-a-good-quality-jpeg-image-from-an-h264-video-file-with-ffmpeg
– Pierz – 2018-05-01T14:02:28.337