如何录制视频并将其从网络摄像头保存?

问题描述

我有一个脚本可以使用Python查看和保存网络摄像头素材。但是,它可以让我观看视频,但保存后会保存一个空白的output.avi文件,我不知道为什么有人可以提供帮助;

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(1)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
rows = (int(vc.get(3)))
cols = (int(vc.get(4)))
out = cv2.VideoWriter("output.avi",fourcc,20.0,(rows,cols),False)
        
if vc.isOpened(): # try to get the first frame
    rval,frame = vc.read()
else:
    rval = False


while rval:
    
    rval,frame = vc.read()
    cv2.imshow("preview",frame)
    out.write(frame)
    key = cv2.waitKey(20)

    if key == 27: # exit on ESC
        vc.release()
        out.release()
        break



cv2.destroyAllWindows()

解决方法

请不要将isColor设置为false。

您的脚本与out = cv2.VideoWriter("output.avi",fourcc,20.0,(rows,cols))一起为我工作