How to extract a fixed number of frames with ffmpe

2019-04-16 02:00发布

问题:

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!

回答1:

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