Skip to content

Tips: To extract frames using ffmpeg

suntong edited this page Nov 5, 2018 · 2 revisions

https://stackoverflow.com/questions/10957412/fastest-way-to-extract-frames-using-ffmpeg

To extract frames using ffmpeg, here's a quick comparison of two different ways to extract one frame per minute from a video 38m07s long:

time ffmpeg -i input.mp4 -filter:v fps=fps=1/60 ffmpeg_%0d.bmp
1m36.029s

This takes long because ffmpeg parses the entire video file to get the desired frames.

time for i in {0..39} ; do ffmpeg -accurate_seek -ss `echo $i*60.0 | bc` -i input.mp4   -frames:v 1 period_down_$i.bmp ; done
0m4.689s

This is about 20 times faster. We use fast seeking to go to the desired time index and extract a frame, then call ffmpeg several times for every time index. Note that -accurate_seek is the default , and make sure you add -ss before the input video -i option.

Note that

  • it's better to use -filter:v -fps=fps=... instead of -r as the latter may be inaccurate. Although the ticket is marked as fixed, I still did experience some issues, so better play it safe.
  • If bc is not available as it is not a native Ubuntu package, one can use bash instead: let "i = $i * 60".