opencv-python之保存视频

实现步骤:

1.创建一个VideoWrite的对象

2.确定输出文件名

3.指定FourCC编码

4.播放频率和帧的大小

5.最后是isColor标签True为彩色。

FourCC是一个4字节码,用来确定视频的编码格式。
1.In Fedora : DIVX , XVID , MJPG , X264 , WMV1 , WMV2
XVID是最好的,MJPG是高尺寸视频,X264得到小尺寸视频
2.In Windows : DIVX
3.In OSX :不知道用什么好

设置FourCC格式时,原文里采用了cv2.VideoWriter_fourcc()这个函数,若运行程序的时候显示这个函数不存在,可以改用了cv2.cv.CV_FOURCC这个函数。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

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

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/qq_15256443/article/details/85160750