H264 videos do not have all features

Hi, I am trying to read specific frames from a h264 video file in Python. For webm this works, but for h264, several feature do not work.

for fileIn in [
    vidObj = cv2.VideoCapture(fileIn)
    print('Framerate:', int(vidObj.get(cv2.CAP_PROP_FPS)))
    print('Frame count:', int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT)))
    vidObj.set(cv2.CAP_PROP_POS_FRAMES, 1000)
    print('Position:', int(vidObj.get(cv2.CAP_PROP_POS_FRAMES)))


Framerate: 29
Frame count: 5345
Position: 1000

Framerate: 28
Frame count: -221617133663316
Position: 0

As you can see, the framecount is negative, and I cannot set CAP_PROP_FRAME_COUNT. Hence, I am forced to read through all earlier frames, but there are many of them in my application (not this simplified example).

Any tips/tricks?

Place your raw video files (h264) in containers (avi, mp4 etc.)

A raw video file such as h264 has no header infomation allowing you to seek to a specific point withing the file (frame) or detailing how many frames are available, it is simply the raw encoded video data with all the information required to decode it.

This post was flagged by the community and is temporarily hidden.

Is your response from chatgpt?

I’m asking because your answer is very general and doesn’t help when the a raw video file that has no meta data, additionaly

is a bit of a suspect addition.

1 Like

Thanks for bringing this to my attention. Although that specific post didn’t contain obvious falsehoods, other posts by that user did. The "<lang>Copy code" strings showed up in several posts.

I would expect any (properly containered) video to at least report a correct video duration, which should be accurate… but I think I remember that OpenCV doesn’t expose such a property, although it does expose a frame count, which is merely calculated from duration and self-reported frame rate.

True but the user’s question was with reference to a raw .h264 file which unless I am mistaken doesn’t have any meta information regarding number of frames etc. It 100% can’t if its streamed from a camera but maybe it can in other circumstances when its length as apposed to start coded prefixed?