How to extract a fixed number of frames with ffmpeg?
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
Admin
Updated on June 14, 2022Comments
-
Admin almost 2 years
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!
-
Admin about 8 yearsIt did fix the problem, but the extracted frames were terribly damaged. Is there something wrong with the parameters?
-
Admin about 8 yearsProblem fixed. I still calculated the ideal output fps for each video, but round it and set -frames 50 this time. Thanks for your help!
-
Gyan about 8 yearsMy commands don't specify or require "ideal output fps" and default parameters for PNG should "terribly damage" the output. What command did you use?