cv2调用摄像头并保存视频

cv2调用摄像头并保存视频

    本篇博客主要介绍通过python中的cv2模块调用摄像头,并将读取的视频内容保存为视频文件。

import cv2

cap = cv2.VideoCapture(0)
width = 640
ret = cap.set(3, width)
height = 480
ret = cap.set(4, height)

fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('out.avi', fourcc, 20.0, (width, height))

while cap.isOpened():
    ret, frame = cap.read()
    if ret is True:
        frame = cv2.resize(frame, (640, 480))

        out.write(frame)

        cv2.imshow('frame', frame)

    else:
        break

    key = cv2.waitKey(1)
    if key == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/github_39611196/article/details/80951712