我写了一些代码来读取视频,并将每个帧作为图像写入目录。代码执行预期的功能,但仅在写入最后一帧后约40秒终止。 [python 2.7; cv2版本2.4.8]
import cv2
import os
def video_to_frames(video, path_output_dir):
# extract frames from a video and save to directory as 'x.png' where
# x is the frame index
vidcap = cv2.VideoCapture(video)
count = 0
while vidcap.isOpened():
success, image = vidcap.read()
if success:
cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
count += 1
else:
break
cv2.destroyAllWindows()
vidcap.release()
video_to_frames('../somepath/myvid.mp4', '../somepath/out')
输出正确,但我发现自己手动终止进程而不是等待vidcap.read()返回False。
有没有更快的方法退出VideoCapture.isOpened()循环?