Table of contents
ClassVideoWriter()
cv2.VideoWriter(filename, int fourcc, double fps, frameSize, int is_color=1)
filename: path to generate video
fourcc: Encoder, you can set the parameter to -1 when saving the video for the first time, and then run the program to see the available encoding formats.
fps: video stream frame rate
frameSize: video resolution
is_color: 1 (color frame), 0 (grayscale frame), default is 1
示例:
width = 640
height = 360
fps = 30
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
#也可以写成 fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(filename='../media/test7.mp4', fourcc=fourcc, fps=30, frameSize=(width, height))
VideoWriter.write()
Function: Write frames to video file
VideoWriter.release()
Function: Release resources
Video storage example
import cv2
# 获取摄像头并打开
camera = cv2.VideoCapture(0)
# 创建窗口
cv2.namedWindow('camera',cv2.WINDOW_AUTOSIZE)
# 创建文件为写多媒体文件
width = int(camera.get(3))
height = int(camera.get(4))
fps = camera.get(5)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
vw = cv2.VideoWriter('D:\深度学习\计算机视觉学习资源/写多媒体.mp4', fourcc, fps, (width,height),1)
# 判断摄像头开启则读取视频
while camera.isOpened():
# 读取视频帧
retval, image = camera.read()
if retval:
# 窗口显示视频帧
cv2.imshow('camera', image)
# 将数据写入多媒体文件
vw.write(image)
# 等待键盘按键q,停止读取视频
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
#释放资源
camera.release()
cv2.destroyAllWindows()
vw.release()