使用来自How does QueryFrame work?的代码示例我注意到,如果程序运行到视频结尾,程序会花很多时间退出。我想在最后一帧快速退出,并且我确认如果我不尝试播放视频的结尾会更快,但有一些细节不能制作对我有意义。这是我的代码:
import cv2
# create a window
winname = "myWindow"
win = cv2.namedWindow(winname, cv2.CV_WINDOW_AUTOSIZE)
# load video file
invideo = cv2.VideoCapture("video.mts")
frames = invideo.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
print "frame count:", frames
# interval between frame in ms.
fps = invideo.get(cv2.cv.CV_CAP_PROP_FPS)
interval = int(1000.0 / fps)
# play video
while invideo.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) < frames:
print "Showing frame number:", invideo.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
(ret, im) = invideo.read()
if not ret:
break
cv2.imshow(winname, im)
if cv2.waitKey(interval) == 27: # ASCII 27 is the ESC key
break
del invideo
cv2.destroyWindow(winname)
唯一的一点是返回的帧数是744,而最后一个播放的帧数是371(从0开始计数,因此372帧)。我认为这是因为视频是隔行扫描的,我想我需要考虑到这一点,然后将interval
除以2,将frames
除以2.但问题是,我怎么知道我需要去做这个?似乎没有财产来检查这个: