python-OpenCv 使用videoWrite保存视频

函数原型:

cv.videoWriter(video_name, file_format, fps, isColor)

参数说明:

video_name 视频名称
file_format 文件格式
fps 帧率
isColor 输出格式,等于0时输出灰度视频,不等于0时输出彩色视频

代码示例

import cv2 as cv

# 调用摄像头
videoCapture = cv.VideoCapture(0)

# 设置帧率
fps = 30

# 获取窗口大小
size = (int(videoCapture.get(cv.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT)))

# 调用VideoWrite()函数
videoWrite = cv.VideoWriter('MySaveVideo.avi', cv.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)

# 先获取一帧,用来判断是否成功调用摄像头
success, frame = videoCapture.read()

# 通过设置帧数来设置时间,减一是因为上面已经获取过一帧了
numFrameRemainling = fps * 5 - 1

# 通过循环保存帧
while success and numFrameRemainling > 0:
    videoWrite.write(frame)
    success, frame = videoCapture.read()
    numFrameRemainling -= 1

# 释放摄像头
videoCapture.release()

 

你可能感兴趣的:(openCV,python)