import cv2
fourcc = cv2.VideoWriter_fourcc(*‘DIVX’)
vw = cv2.VideoWriter(“C:\Users\yangxj\Desktop\MachineLearning\temp\1234.avi”, fourcc, 30, (1280, 720))
cv2.namedWindow(“video”, cv2.WINDOW_NORMAL)
cv2.resizeWindow(“video”, 640, 480)
cap = cv2.VideoCapture(0)
while cap.isOpened():
# 从摄像头读取视频帧
ret, frame = cap.read()
if ret == True:
# 将视频帧在窗口中显示
cv2.imshow('video', frame)
# 重新交窗口设置为批定大小
cv2.resizeWindow("video", 640, 480)
# 写数据到多媒体文件
vw.write(frame)
# 等待按键事件,如果是q就退出
key = cv2.waitKey(1) # 1ms一次,采集时用
if (key & 0xFF == ord('q')):
break
else:
break
cap.release()
vw.release()
cv2.destroyAllWindows()