Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a fixed number of frames with ffmpeg?

I am trying to extract a fixed number of frames uniformly from a bunch of videos(say 50 frames from each video, 10,000 videos in total).

Since the duration varies, I calculated the ideal output fps for each video and take it as a parameter for ffmpeg extraction, but failed to get the required number of frames.

Does anyone know how to extract a fixed number of frames with ffmpeg, or other tools? Thanks!

like image 433
W. Han Avatar asked Mar 10 '16 09:03

W. Han


1 Answers

You could use the thumbnail filter. It picks one representative frame from every set of n frames (default is 100)

So, if your source video is 10 minutes long and at 25 fps, then it has 15000 frames in total. So, to select 50 frames, you would use

ffmpeg -i input.mp4 -vf thumbnail=300,setpts=N/TB -r 1 -vframes 50 inputframes%03d.png

This selects one representative frame from each set of 300 frames, so 50 frames from 15000. Despite the name, thumbnail simply selects frames, it does not downsize them for thumbnail use. The setpts and r are set in conjunction to avoid duplicate or dropped frames. The vframes is set so no more than 50 images are output.

If you need to select strictly every nth frame, use

ffmpeg -i input.mp4 -vf select='not(mod(n\,300))',setpts=N/TB -r 1 -vframes 50 inputframes%03d.png
like image 90
Gyan Avatar answered Oct 21 '22 08:10

Gyan