As the background for the question, I use a programmatically produced complex filter to compose a video out of many inputs. Small errors accumulate and so I need precise trims (re-encoding is fine).
For example, I need 44 frames from the linked video (FPS=30) beginning at a given frame.
Here are two attempts:
Attempt 1:
ffmpeg -i input.mp4 -vf "trim=start=3.2666666666666666:end=4.7333,setpts=PTS-STARTPTS" -an output.mp4
Attempt 2:
ffmpeg -i input.mp4 -vf "trim=start=3.2666666666666666:end=4.7334,setpts=PTS-STARTPTS" -an output.mp4
While the difference in the end
timestamp is only 0.0001, the first attempts results in 43 frames (duration of 1.433333 seconds), while the second one gives 45 frames (duration of 1.50 seconds). No matter what I put in the end
timestamp, I cannot get 44 frames…
I get the number of frames by:
ffprobe -show_frames output.mp4 | grep FRAME | wc -l
(dividing this number by two, since it counts both [FRAME]
and [/FRAME]
)
What is the explanation for this behavior and how can I predictably trim the frames I need?