Opencv video input, after processing each frame, combined video export (python)

Recently, there is a requirement in the project. It is necessary to input a video, mark it, and then export it in the form of video, which is hereby recorded.

1. Incoming video

#传入视频
video_path=r"具体路径"
video_capture = cv2.VideoCapture(video_path)

#打开摄像头获取
video_capture = cv2.VideoCapture(0)

2. Save the video.
Note that this paragraph should be placed in front of the loop

    fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 指定视频视频编解码器格式
    fps = video_capture.get(cv2.CAP_PROP_FPS) #帧率
    size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) #自动获取视频大小
    out = cv2.VideoWriter('output.avi', fourcc, fps, size)  #opencv好像只能导出avi格式

3. Add to the video by processing each frame.
Note that this paragraph should be placed in the loop

out.write(im0)  # 存储帧图像——依次存入每一帧获取的图像 (im0为每帧frame)
#显示预览效果
cv2.namedWindow('image',0)
cv2.resizeWindow("image", 544, 960)  #设置预览窗口大小
cv2.imshow("image", im0)

Reference materials:
https://blog.csdn.net/weixin_44604887/article/details/104660290

Guess you like

Origin blog.csdn.net/qq1198768105/article/details/114176476