cap.set(cv2.CAP_PROP_POS_MSEC) changes to unreachable timestamp?

I’m trying to read frames from a certain timestamp to a certain timestamp, problem is that just after setting it up to start at 800 milliseconds for example:

start_time = 0.8
end_time = 4
cap.set(cv2.CAP_PROP_POS_MSEC, float(start_time * MILLISECONDS)) 

Now I try and check where it is with:


Which returns:


and according to the docs, that flag stands for

Current position of the video file in milliseconds.

Which is kind of confusing because my entire video is 4 seconds long i.e. 4000 milliseconds, the entire code snippet is:

def load_video(path, start_time, end_time, skip_frames=False):
    frame_pos = 0

    cap = cv2.VideoCapture(path)
    cap.set(cv2.CAP_PROP_POS_MSEC, float(start_time * MILLISECONDS))
    frames = []
        while cap.isOpened() and cap.get(cv2.CAP_PROP_POS_MSEC) <= float(end_time * MILLISECONDS):
            ret, frame =
            if not ret:
            # OpenCV default capturing is on BGR format,
            # if RGB is needed then un-comment this line:
            # frame = frame[:, :, [2, 1, 0]]
            if skip_frames:
                # If we want to take every 5th frame (where there is a an action documented).
                frame_pos += 12
                cap.set(cv2.CAP_PROP_POS_FRAMES, frame_pos)

    return np.array(frames)

The point is to read from a certain timestamp to a certain timestamp, but the 2nd condition in the while statement obviously isn’t satisfied since 13346.680013346679 > 4000 from the get go, and most strangely ret, frame = actually reads a frame when I’m executing it in the debugger just before stepping to the while check.

So what I’m doing wrong here?


The video file is .avi & 60fps.

related: python - OpenCV's cap.set(cv2.CAP_PROP_POS_MSEC) changes to unreachable timestamp? - Stack Overflow