opencv读取摄像头采集视频并保存

import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('lianzheng.avi', fourcc, 20.0, (640, 480))
while True:
    ret, frame = cap.read()
    print(frame.shape)
    out.write(frame)
    cv2.imshow('frame', frame)

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

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

(640, 480)
一定要注意这个参数,很多人都是因为这个参数不对才导致保存的视频打不开
可以先看一下frame.shape
打印出来如果是(480,640)
那参数就是(640,480)
根据自己情况自己定

猜你喜欢

转载自blog.csdn.net/wuxulong123/article/details/116976500