参考资料:
python opencv写视频——cv2.VideoWriter()_cv2.cv.videowriter(_翟羽嚄的博客-CSDN博客
import cv2 as cv
import numpy as np#1. 打开原始视频
video_in = cv.VideoCapture("../SampleVideos/Unity2D.mp4")
video_width = int(video_in.get(cv.CAP_PROP_FRAME_WIDTH))
video_height = int(video_in.get(cv.CAP_PROP_FRAME_HEIGHT))
video_fps = int(video_in.get(cv.CAP_PROP_FPS))
print("Original Video Resolution: (", video_width, ",", video_height, ") FPS:", video_fps)#2. 创建VideoWriter对象
# cv.VideoWriter(filename, fourcc, fps, frameSize[, isColor])
# filename: 要保存的文件的路径
# fourcc: 指定编码器
# fps: 要保存的视频的帧率
# frameSize: 要保存的文件的画面尺寸
# isColor: 指示是黑白画面还是彩色的画面
#参考资料:https://blog.csdn.net/mao_hui_fei/article/details/107573021
video_out = cv.VideoWriter("out.avi", cv.VideoWriter_fourcc(*'XVID'), video_fps, (800,600), True)#循环处理每一帧图像
while True:ret,frame = video_in.read()if ret == False:break;frame = cv.resize(frame, (800, 600))video_out.write(frame)cv.imshow('TestVideoOut', frame)key = cv.waitKey(10)if key == 27:break;#释放资源
video_in.release()
video_out.release()