Python中调用OpenCV接口保存摄像头或视频中的图像帧到视频文件
Python中调用OpenCV接口保存摄像头或视频中的图像帧到视频文件
import cv2
cap = cv2.VideoCapture(0) # 打开摄像头
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, size)
while(1):
ret, frame = cap.read()
if ret==True:
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
效果图: