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 MILLISECONDS = 1000 cap.set(cv2.CAP_PROP_POS_MSEC, float(start_time * MILLISECONDS))
Now I try and check where it is with:
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): MILLISECONDS = 1000 frame_pos = 0 cap = cv2.VideoCapture(path) cap.set(cv2.CAP_PROP_POS_MSEC, float(start_time * MILLISECONDS)) frames =  try: while cap.isOpened() and cap.get(cv2.CAP_PROP_POS_MSEC) <= float(end_time * MILLISECONDS): ret, frame = cap.read() if not ret: break # OpenCV default capturing is on BGR format, # if RGB is needed then un-comment this line: # frame = frame[:, :, [2, 1, 0]] frames.append(frame) 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) finally: cap.release() 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 = cap.read() actually reads a frame when I’m executing it in the debugger just before stepping to the
So what I’m doing wrong here?
The video file is
.avi & 60fps.