OpenCV's VideoCapture is broken and hasn't been fixed for the last 5 years: opencv/opencv#9053
This is a PyAV based replacement. Unlike other implementations it can seek at any time.
How to use:
reader = VideoReader('video.mp4')
reader.seek(reader.total_frames - 100) # frame number
while True:
frame = reader.read()
if not frame:
break
# frame is an ndarray - do something with it
print(f'frame {reader.position}: {frame}')
reader.close()
I see I see, thank you the code works quite well, after I used ffprobe to check the keyframe intervals in my video, I realised the interval was roughly around 10s which is around 250 frames for fps=25, that's why the 100 frames of seek didn't work for me, but it worked after I set it to 300 frames:)