87

how can i convert a video to images using ffmpeg? Example am having a video with total duration 60 seconds. I want images between different set of duration like between 2-6 seconds, then between 15-24 seconds and so on. Is that possible using ffmpeg?

hack
  • 1,403
  • 2
  • 14
  • 20
  • 1
    I'll comment with something relevang to all of the answers. You probably don't want to output in png. Output in jpg. If you want good quality, add `-qscale:v 2` parameter. – Íhor Mé Aug 18 '20 at 20:48

4 Answers4

124

Official ffmpeg documentation on this: Create a thumbnail image every X seconds of the video

Output one image every second:

ffmpeg -i input.mp4 -vf fps=1 out%d.png

Output one image every minute:

ffmpeg -i test.mp4 -vf fps=1/60 thumb%04d.png

Output one image every 10 minutes:

ffmpeg -i test.mp4 -vf fps=1/600 thumb%04d.png
ei2
  • 3
  • 2
  • 5
Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • Thanks. Can we specify duration for which we need images? Like i need images between 2-6 seconds and 13-17 seconds of a 60 sec video, through a single command. – hack Oct 17 '16 at 15:02
  • it is possible to seek input video to some starting position, but I'm not sure that ffmpeg has a filter that can accept ranges. Maybe you'll need to make 2 ffmpeg calls (for 2-6 range and 13-17 range). – Vitaliy Fedorchenko Oct 17 '16 at 15:35
80

You can use the select filter for a set of custom ranges:

ffmpeg -i in.mp4 -vf select='between(t,2,6)+between(t,15,24)' -vsync 0 out%d.png
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 1
    Hi. How can i specify the frame rate by which it should extract images? example, i want to extrate 5 frames/second between 2 to 6. – hack Jan 13 '17 at 06:15
  • 4
    Use `-vf fps=5,select='between(t,2,6)+between(t,15,24)'` – Gyan Jan 13 '17 at 06:29
  • Thanks for quick response! This will extract images 'between' mentioned time (2-6). How can i get images 'From' 2-6? – hack Jan 13 '17 at 07:37
  • 1
    Also, can i mention multiple duration set with 'ss' filter? `ffmpeg -ss 10 -i video.mp4 -to 12 -copyts -vf fps=1 v2f/out%d.png` this would create images from 10 seconds to 12 seconds. Can i give multiple duration set here? – hack Jan 13 '17 at 07:47
  • I don't understand what you mean by `from`. Only one `ss` can be applied. – Gyan Jan 13 '17 at 07:57
  • Is it like `between(t,2,6)` will extract images between time interval 2-6 (2 and 6 excluding)? – hack Jan 13 '17 at 08:00
  • I just tested. It is inclusive of 2 and 6 :). One final doubt, will the input file be decoded till it reaches the mentioned time position?Or will it be parsed ,just like in specifying `ss` before input – hack Jan 13 '17 at 08:10
  • Nope, decoded till it reaches first qualifying timecode. – Gyan Jan 13 '17 at 08:24
  • Why `-vsrync 0`? I read the docs but still don't understand: https://ffmpeg.org/ffmpeg.html -- It seems the 0 option prevents any frames being dropped due to timestamp issues. – Matt Kleinsmith Feb 16 '18 at 10:52
  • 2
    The image2 muxer, which is used to output image sequences, defaults to CFR. So, when given frames with timestamp differences greater than 1/fps, ffmpeg will duplicate frames to keep CFR. Will happen here between selection of t=6 frame and t=15 frame `vsync 0` prevents that. – Gyan Feb 16 '18 at 11:02
  • Output numbers with fixed length : out_%04d.jpg (change "4") – P.O.W. Jun 07 '21 at 13:25
3

Another way is to use ffmpeg library for python, particularly useful if you don't want to add ffmpeg to your pc environment. Start by installing ffmpeg on conda with:conda install ffmpeg Then you can write a script as bellow:

import ffmpeg
input_file_name = 'input_video.mp4'
(ffmpeg
 .input(input_file_name )
 .filter('fps', fps=10, round = 'up')
 .output("%s-%%04d.jpg"%(input_file_name[:-4]), **{'qscale:v': 3})
 .run())
Nahom Aymere
  • 104
  • 3
2

In addition to the select filter in Gyan's answer (which I use with eq rather than between), I came across another filter in the manual: thumbnail

Select the most representative frame in a given sequence of consecutive frames.

The filter accepts the following options:

  • n: Set the frames batch size to analyze; in a set of n frames, the filter will pick one of them, and then handle the next batch of n frames until the end. Default is 100.

Since the filter keeps track of the whole frames sequence, a bigger n value will result in a higher memory usage, so a high value is not recommended.

Examples

  • Extract one picture each 50 frames:

    thumbnail=50
    
  • Complete example of a thumbnail creation with ffmpeg:

    ffmpeg -i in.avi -vf thumbnail,scale=300:200 -frames:v 1 out.png
    
Louis Maddox
  • 5,226
  • 5
  • 36
  • 66