import cv2 # 0代表的是电脑上的默认摄像头 cap = cv2.VideoCapture(0) # 创建VideoWriter对象,第二个参数是帧率,第三个参数是视频的宽度和高度,第四个参数是输出视频的格式 out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480)) while(cap.isOpened()): ret, frame = cap.read() # ret是一个布尔值,表示是否成功读取一帧。frame是读取的帧 if ret == True: out.write(frame) # 将帧写入输出视频文件 cv2.imshow('frame', frame) # 显示帧 # 如果按下'q'键,就退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() # 释放摄像头资源 out.release() # 释放VideoWriter对象资源 cv2.destroyAllWindows() # 关闭所有OpenCV窗口
#####
标签:python,frame,cap,cv2,opencv,mp4,摄像头,VideoWriter From: https://www.cnblogs.com/herd/p/17929121.html