1
votes

Im am working on processing a video and one of the steps that I need to perform is extract a specific frame out of the video.

cap = cv2.VideoCapture(videoFile)
for frame in range(startFrame, endFrame): 
    cap.set(cv2.CAP_PROP_POS_FRAMES, frame)       
    print frame
    position = cap.get(cv2.CAP_PROP_POS_FRAMES)
    print position

Ideally, frame and position should be equal. This is initially true. However after 2 or 3 loops the position becomes -2.04963823041e+15. I am very confused by this. I am running this on an amazonaws server with anaconda and OpenCV 3.

1
What is the frame when you're getting that position? Frame is an int correct? How are you grabbing startFrame and endFrame?alkasm
The frames that I am getting are 1 through 50 from a 247 frame video. startFrame and enFrame are just constant that I set like 1 through 50. Yes the frame is an int.David Brenes
I am getting the irregular position at frame 6David Brenes

1 Answers

0
votes

This is old question but I'll leave my observations since it was on the top of results when I was searching for hints.

The reason I encountered this problem was probably different than what it was for the OP, but in my case I was experiencing weird behavior when OPENCV_FFMPEG_CAPTURE_OPTIONS environmental variable was set to video_codec;h264_cuvid. I was using GPU acceleration to decode frames and was not aware this would cause issues when rewinding to arbitrary position in the file.

Interestingly rewind would work for arbitrarily low frame number, for example when dealing with 7000 frames file it would work to rewind to frame ~2000, but would not reliably work when rewinding to frame above 4000.

As soon as I removed the environmental variable rewind works reliably for any frame number. And it's taking less time to rewind now than when GPU acceleration was turned on...