程式語言 - OpenCV - Python - Record Video



record_video.py

import cv2

cap1 = cv2.VideoCapture(0)

fps = 30
width = int(cap1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap1.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out1 = cv2.VideoWriter('cam0.avi', fourcc, fps, (width, height))

while True:
    ret1, frame1 = cap1.read()
    if not ret1:
        break

    out1.write(frame1)
    cv2.imshow("cam0", frame1)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap1.release()
out1.release()
cv2.destroyAllWindows()