I try to get the first, the middle, and the last frame from the video but it can only get the first and the middle one.
This is my code:
import cv2
import math
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
# Get number of frame of the video
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)
# Define a tuple of frame need to get
frame = (0, math.ceil(length/2), length - 1)
# Get the frame
for f in frame:
vidcap.set(1, f)
_, image = vidcap.read()
cv2.imwrite("/home/data/output/frame%d.jpg" % f, image)
It does save the last frame image but it doesn't have any data.
Is there anything wrong with my code?
Update:
According to this post, it seems like cv2.CAP_PROP_FRAME_COUNT
can count wrong the number of frames.
So is there a better way to get the number of frames without reading through the entire frame?