ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

Discrepancy in number of frames numpy array

Open jbyuki opened this issue 3 years ago • 3 comments

Using ffprobe:

probe = ffmpeg.probe(fn)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
print(int(video_stream['nb_frames']))

I get 230 frames.

By reading it as a numpy array:

stream = ffmpeg.input(fn)
out = ffmpeg.output(stream, 'pipe:', format='rawvideo', pix_fmt='rgb24')
out, _ = out.run(capture_stdout=True)
frames = np.frombuffer(out, np.uint8)
frames = frames.reshape([-1, h, w, 3]) 
print(frames.shape[0])

I have 231 frames. The ffprobe number of frames is correct I believe. Where would the extra frame come from?

EDIT: After inspecting the resulting frames, the first frame and second frame are identical, so it's enough to trim it with frames = frames[1:,:,:,:]

jbyuki avatar Jun 09 '22 23:06 jbyuki

Hello!

I do face the same issue except I have 3 identical frames at the beginning instead of 2 for a video of 3603 frames according to ffprobe.

thomascarreau avatar Jun 10 '22 13:06 thomascarreau

Interesting so this is not consistent. Thanks for the info, I will fix my codebase and dig maybe further to check the error from.

In the meanwhile, an adaptation of the above fix would be frames[-nb_frames:,:,:,:] where nb_frames is fetched using ffprobe.

jbyuki avatar Jun 10 '22 15:06 jbyuki

Will try to do it as well. I'll keep you updated if I find anything.

Note that for another video I get 51936 frames with ffmpeg into NumPy array and 51945 with ffprobe. Thus we do not necessarely have duplicated frames...

thomascarreau avatar Jun 10 '22 16:06 thomascarreau