Ask Your Question

Revision history [back]

Why does video.set(..FRAME_POS,<index>) index not align with frame number?

I have a video that is 2:12 sec long according to QuickTime on MacOS (10.14 Mojave).

I have the following code:

    import cv2

    vid = cv2.VideoCapture("video.mov")
    length = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))  # = 3953
    fps    = int(vid.get(cv2.CAP_PROP_FPS))          # = 29

    def frame_set(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, index)
        success, img = vid.read()
        return img

    def frame_walk(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, 0)
        for i in range(index):
            vid.read()
        success, img = vid.read()
        return img

    sum(abs(frame_set(0) - frame_walk(0)))   # = 0
    sum(abs(frame_set(29) - frame_walk(29))) # = 0
    sum(abs(frame_set(30) - frame_walk(30))) # = <big number>

    frame_set(3953 - 128)  # = <image>
    frame_set(3953 - 127)  # = None
    frame_set(3952)        # = None
    frame_walk(3953 - 127) # = <image>
    frame_walk(3952)       # = <image>

Clearly a misalignment between the "frame index" method starts as soon as "1 second" has elapsed in the video. What ends up happening is that valid frame indices do not return images for the index method.

Am I doing something wrong here?

I tried reporting a bug, but that link is dead so I came here.

Why does video.set(..FRAME_POS,<index>) index not align with frame number?

I have a video that is 2:12 sec long according to QuickTime on MacOS (10.14 Mojave).

I have the following code:

    import cv2

    vid = cv2.VideoCapture("video.mov")
    length = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))  # = 3953
    fps    = int(vid.get(cv2.CAP_PROP_FPS))          # = 29

    def frame_set(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, index)
        success, img = vid.read()
        return img

    def frame_walk(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, 0)
        for i in range(index):
            vid.read()
        success, img = vid.read()
        return img

    sum(abs(frame_set(0) - frame_walk(0)))   # = 0
    sum(abs(frame_set(29) - frame_walk(29))) # = 0
    sum(abs(frame_set(30) - frame_walk(30))) # = <big number>

    frame_set(3953 - 128)  # = <image>
    frame_set(3953 - 127)  # = None
    frame_set(3952)        # = None
    frame_walk(3953 - 127) # = <image>
    frame_walk(3952)       # = <image>

Clearly a misalignment between the "frame index" method starts as soon as "1 second" has elapsed in the video. What ends up happening is that valid frame indices do not return images for the index method."set" method, even though they should (as displayed by the more cumbersome "walk" method).

Am I doing something wrong here?

I tried reporting a bug, but that link is dead so I came here.

Why does video.set(..FRAME_POS,<index>) index not align with frame number?

I have a video that is 2:12 sec long according to QuickTime on MacOS (10.14 Mojave).

I have the following code:

    import cv2

    vid = cv2.VideoCapture("video.mov")
    length = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))  # = 3953
    fps    = int(vid.get(cv2.CAP_PROP_FPS))          # = 29

    def frame_set(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, index)
        success, img = vid.read()
        return img

    def frame_walk(index):
        success = vid.set(cv2.CAP_PROP_POS_FRAMES, 0)
        for i in range(index):
            vid.read()
        success, img = vid.read()
        return img

    sum(abs(frame_set(0) - frame_walk(0)))   # = 0
    sum(abs(frame_set(29) - frame_walk(29))) # = 0
    sum(abs(frame_set(30) - frame_walk(30))) # = <big number>
number>  <---- PROBLEM, mismatch

    frame_set(3953 - 128)  # = <image>
    frame_set(3953 - 127)  # = None
None    <---- PROBLEM, should be valid image
    frame_set(3952)        # = None
None    <---- PROBLEM, should be valid image
    frame_walk(3953 - 127) # = <image>
<image> <---- correct answer
    frame_walk(3952)       # = <image>
<image> <---- correct answer

Clearly a misalignment between the "frame index" method starts as soon as "1 second" has elapsed in the video. What ends up happening The OpenCV ".set" method is that valid frame indices do not return images for the "set" method, even though they should (as displayed by not actually setting to the correct frame. However the more cumbersome "walk" method).method works just fine.

Am I doing something wrong here?

I tried reporting This appears to be a bug, but that link is dead so I came here.bug in the OpenCV codebase, because the video length divided by the fps provides a 2 minute 16 second video, when QuickTime correctly reports a 2 minute 12 second video. That difference accounts for the last 127 frames being dropped from the ".set" method.