OpenCV实现摄像头录像

《OpenCV系列教程》
项目位置:OpenCV-Sample
代码位置:7-CameraRecording.py

import cv2

cameraCapture = cv2.VideoCapture(0)

fps = cameraCapture.get(cv2.CAP_PROP_FPS)

size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWriter = cv2.VideoWriter('./out/record.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'),
                              fps, size)
success, frame = cameraCapture.read()
numFrame = 5 * fps -1

while success and numFrame > 0:
        videoWriter.write(frame)
        success, frame = cameraCapture.read()
        numFrame -= 1

cameraCapture.release()
  • cv2.VideoWriter_fourcc(‘I’, ‘4’, ‘2’, ‘0’):该选项是一个未压缩的YUV颜色编码,是4:2:0色度子采样。这种编码兼容性很好,但文件较大,文件扩展名.avi。
  • cv2.VideoWriter_fource(‘P’, ‘I’, ‘M’, ‘1’) :该选项是MPEG-1编码类型,文件扩展名为.avi。
  • cv2.VideoWriter_fource(‘X’, ‘V’, ‘I’, ‘D’) :给选项是MPEG-4编码类型,如果希望得到的视频大小为平均值,推荐这个选项,文件扩展名.avi。
  • cv2.VideoWriter_fource(‘T’, ‘H’, ‘E’, ‘O’) : 该选项是Ogg Vorbis,文件扩展名应为.ogv。
  • cv2.VideoWriter_fource(‘F’, ‘L’, ‘V’, ‘1’) : 该选项是一个Flash视频,文件扩展名.flv。

你可能感兴趣的:(OpenCV)