Ask Your Question

Revision history [back]

VideoCapture.read() count doesn't match CAP_PROP_FRAME_COUNT or output by FFMPEG

When I check my video frame count using CAP_PROP_FRAME_COUNT, it says there are 300 frames. Similarly, when I use FFMPEG to output the frames of the video, I also get 300 frames. However, when I use a regular VideoCapture read loop similar to the code below, I always get 296 frames. I'm guessing this has something to do with the video format being H264 and the way the data is compressed in such a video. But some of the data I'm using is given with the 300 frame count and my code relies on the OpenCV output which is giving me 296 frames. This discrepancy makes matching the two sets of data a headache. Can anyone explain why there is the difference in frame count/output and how to properly get the two sets of data to match up? In particular, is there just some specific setting I need to look into to get the read() loop to hand back the same 300 frames that FFMPEG would output? Thanks.

Code:

ret, frame = self.read()
frame_count = self.video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
while True:
    if ret:
        # Do stuff with the frame here.
    else:
        break
    ret, frame = self.read()