python opencv帧合成视频
import cv2
import os
from datetime import datetime
# print(cv2.__version__)
def frames_to_video(save_path, frames_path):
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
fps = 25
video_width, video_height = 1920, 1080
videoWriter = cv2.VideoWriter(save_path, fourcc, fps, (video_width, video_height))
# videoWriter = cv2.VideoWriter(save_path, -1, fps, (video_width, video_height))
imgs = glob.glob(frames_path + "/*.jpg")
# print(imgs)
# frames_num = len(imgs)
for i in range(2851,2894):
if os.path.isfile("%s/%d.jpg"%(frames_path, i)):
# print(i)
frame = cv2.imread("%s/%d.jpg"%(frames_path, i))
# print(frame.shape)
videoWriter.write(frame)
videoWriter.release()
return
if __name__ == '__main__':
frames_to_video("jian_1_25.mp4", 'jian_1_25_frames')
之前几次用的时候好像因为生成视频中图片的大小和原来图片大小不对应。所以老是生成失败。