保存视频

1、创建一个VideoWriter的对象,我们应该确定一个输出文件的名字。

2、接下来指定FourCC编码。这是一个4字节码,用来确定视频的编码格式,

以MJPG为例,cv2.cv.FOURCC(*'MJPG')

3、播放频率和帧的大小也都需要确定。

4、最后一个是isColor标签,如果是True,每一帧就是彩色图,否则就是灰度图。 

import numpy as np
import cv2

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)#后面这个参数不加会出问题。。

# 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()

 

posted @ 2020-02-06 21:42  Tomorrow1126  阅读(230)  评论(0编辑  收藏  举报