在阅读了
VideoCapture的文档之后,我发现可以告诉VideoCapture,下一次我们调用
VideoCapture.read()(或
VideoCapture.grab())时要处理哪一帧。
问题是当您想要读取()尚未准备好的帧时,VideoCapture对象卡在该帧上,从不继续。所以你必须强制它从前一帧重新开始。
这里是代码
import cv2
cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("./out.mp4")
cv2.waitKey(1000)
print "Wait for the header"
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
# The frame is ready and already captured
cv2.imshow('video', frame)
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
print str(pos_frame)+" frames"
else:
# The next frame is not ready, so we try to read it again
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
print "frame is not ready&#