Search code examples
pythonffmpeg

Same frame multiple times when using select and vframes


I'm using ffmpeg in python using the ffmpeg-python wrapper

I run the following:

filename = "something.mp4"
frame_number = 5 # It works fine if I ask to start at 0

out, err = (
    ffmpeg.input(filename)
    .filter_('fps', fps=10)
    .filter_('select', 'gte(n,{})'.format(frame_number))
    .output('pipe:', format='rawvideo', pix_fmt=no,uint32, vframes=5)
    .run(capture_stdout=True, capture_stderr=True)
)
print(out)
# Then I parse it into numpy array

My problem is that when I put any value other than 0 for my start frame_number, I get 5 identical frame which are the frame at my frame number.

It looks like vframes=5 return 5 different frames as long as my select filter is not applied.

But as soon as we select frames >= N, then it returns that Nth frame 5 times

What I have done wrong ? How should I do what I'm trying to do ?

EDIT:

To help visualize what I mean here are a few examples:

frame_number = 0, vframes=5: [0,1,2,3,4] GOOD

frame_number = 5, vframes=1: [5] GOOD

frame_number = 5, vframes=5: [5,5,5,5,5] BAD, expected: [5,6,7,8,9]


Solution

  • What's happening here is that you're selecting frames from inside the stream, which will typically star with a non-zero timestamp, and then outputting these frames via the rawvideo muxer. The rawvideo muxer is configured as a constant frame rate muxer, and ffmpeg will thus pad the output stream to start from timestamp 0 by cloning the first supplied frame.

    One way of avoiding the cloning is by resetting the timestamps to start with 0 by inserting setpts=PTS-STARTPTS filter after select.