opencv record video to save

        Using opencv to save video requires the use of the cv2.VideoWriter object.

VideoWriter(filename, fourcc, fps, frameSize[, isColor])
  • filename: indicates the save path
  • fourcc: used to specify the encoder
  • fps: saved frame rate
  • frame: the frame size of the saved video
  • isColor: Whether the screen color is color.

        where fourcc is the cv2.VideoWriter_fourcc object. We specify the encoding through this object.

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

        The above statement can return a fourcc object encoded in MP4. Please find other codes by yourself


record video code

import cv2

cap = cv2.VideoCapture(0)
# 设置编码类型为mp4
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# 得到摄像头拍摄的视频的宽和高
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 创建对象,用于视频的写出
videoWrite = cv2.VideoWriter(r'../videos/test.mp4', fourcc, 30, (width,height))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    # 将图片写入视频
    videoWrite.write(frame)
    cv2.imshow('frame', frame)

    if cv2.waitKey(33) & 0xFF == ord('q'):
        break
# 刷新,释放资源
videoWrite.release()
cap.release()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/123927674