Python中调用OpenCV接口保存摄像头或视频中的图像帧到视频文件

Python中调用OpenCV接口保存摄像头或视频中的图像帧到视频文件

import cv2

cap = cv2.VideoCapture(0)   # 打开摄像头

size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, size)


while(1):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

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

效果图:

猜你喜欢

转载自blog.csdn.net/mago2015/article/details/85332497