python opencv写视频——cv2.VideoWriter()

python opencv写视频——cv2.VideoWriter()

函数原型 cv2.VideoWriter()

VideoWriter(filename, fourcc, fps, frameSize[, isColor]) -> 

参数说明:

  • 第一个参数是要保存的文件的路径
  • fourcc 指定编码器
  • fps 要保存的视频的帧率
  • frameSize 要保存的文件的画面尺寸
  • isColor 指示是黑白画面还是彩色的画面

示例代码1

import numpy as np
import cv2
 
cap = cv2.VideoCapture(0)
 
fourcc = cv2.VideoWriter_fourcc(*'XVID')
 
out = cv2.VideoWriter('testwrite.avi',fourcc, 20.0, (1920,1080),True)
 
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
 
        cv2.imshow('frame',frame)
        out.write(frame)
 
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    else:
        break
 
cap.release()
out.release()
cv2.destroyAllWindows()

示例代码2(不全,项目中用到的,功能多一些)


# 写视频是否使能
writeVideo_flag = True
# writeVideo_flag = False
#写视频相关操作
if writeVideo_flag:
    # Define the codec and create VideoWriter object
    w1 = 1280  # 返回视频的宽
    h1 = 720  # 返回视频的高
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')  # avi格式
    # fourcc = cv2.VideoWriter_fourcc(*'mp4v')#MP4格式
    # out = cv2.VideoWriter('./output/'+args["input"][43:57]+ "_" + args["class"] + '_output.avi', fourcc, 15, (w, h))
    out = cv2.VideoWriter('./vedio_out/' + "mangdaojiance_0225" + '_output.avi', fourcc, 30, (w1, h1))

project_outpath = 'vedio_out/0716.mp4'
# project_video_clip = VideoFileClip("0716.mp4")
#project_video_clip = VideoFileClip("vedio_input/0719_test1.mp4")
Frame_jiange = 1  # 每2帧取一幅图片
NumFrame = 0
video_capture = cv2.VideoCapture('vedio_input/0719_test1.mp4')
#video_capture = cv2.VideoCapture(1)
#video_capture = cv2.VideoCapture(0)
# 其中0表示打开内置摄像头,1表示打开外接摄像头
if video_capture.isOpened():  # VideoCaputre对象是否成功打开
    print('打开摄像头或者视频成功')
    readVideo_flag = True
else:
    print('打开摄像头或者视频失败')
    readVideo_flag = False
while readVideo_flag:

    ret, frame00 = video_capture.read()  # frame shape 640*480*3

    if ret != True:
        break
    NumFrame = NumFrame + 1
    if NumFrame % Frame_jiange == 0:  # 取余每间隔 Frame_jiange 帧处理一次
        project_video_out_clip=processing(frame00, object_points, img_points, M, Minv, left_line, right_line)
        if writeVideo_flag:
            #save a frame
            out.write(project_video_out_clip)

参考:
https://blog.csdn.net/weixin_36670529/article/details/100977537

你可能感兴趣的:(opencv,图像识别与处理)