53

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
  • 923
  • 1
  • 8
  • 17
  • 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

3 Answers3

57

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
Amit Agarwal
  • 5
  • 1
  • 4
Vitaliy Fedorchenko
  • 6,675
  • 2
  • 33
  • 33
  • 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
  • 3
    `fps=1/600` specifies one image every TEN minutes. For one image every minute use `fps=1/60` – MFisherKDX Aug 30 '18 at 15:35
56

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
  • 63,018
  • 7
  • 100
  • 141
  • 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
  • 2
    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
1

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
  • 111
  • 3