Hi,
I’m trying to read a video (60 fps) within a custom range [t_{start} - t_{end}] and since not every timestamp is documented in my data, I need to extract frames every .2 seconds.
For example:
0 is t_{start} and 3.2 is t_{end}, so extract the frames at [t_0, t_{0.2}, ... , t_{3.2}]
I’m fairly new to OpenCV
so this is what I had in mind:
after initializing cap
with cv2.VideoCapture
, i’ll use the CAP_PROP_POS_MSEC
flag to initialize my starting time:
cap.set(cv2.CAP_PROP_POS_MSEC, start_time * milliseconds)
Now I didn’t find any flag that corresponds to my end_time
so the only “fix” I could think of was something like:
while cap.isOpened() and cap.get(cv2.CAP_PROP_POS_MSEC) <= end_time * milliseconds:
ret, frame = cap.read()
...
...
cap.release()
break
And for extracting every 0.2 seconds which is every f_{20} frame the final logic will look like this:
FRAME_POS = 0
frames = []
while cap.isOpened() and cap.get(cv2.CAP_PROP_POS_MSEC) <= end_time * milliseconds:
ret, frame = cap.read()
if ret:
frames.append(frame)
FRAME_POS += 20
cap.set(cv2.CAP_PROP_POS_FRAMES, FRAME_POS)
else:
cap.release()
break
If this implementation is correct, is there anyway to optimize it with built-in methods/flags?