嘿,所以我开始玩 OpenCV,但我无法将我的网络摄像头输出保存到文件中。这就是我所拥有的。这运行良好,启动网络摄像头并创建“output.avi”问题是 output.avi 很小(414 字节),每次运行程序时的字节完全相同。我猜问题出在fourcc编码上,但我无法找到适合我的情况。我在 Mac OS X 上运行。如果您需要更多信息,请告诉我。
import numpy as np
import cv2
path = ('/full/path/Directory/output.avi')
cap = cv2.VideoCapture(0)
cap.set(1, 20.0) #Match fps
cap.set(3,640) #Match width
cap.set(4,480) #Match height
fourcc = cv2.cv.CV_FOURCC(*'XVID')
video_writer = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
while(cap.isOpened()):
#read the frame
ret, frame = cap.read()
if ret==True:
#show the frame
cv2.imshow('frame',frame)
#Write the frame
video_writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()