小案例:
把某文件夹中的images合成视频序列,
其中images命名为:1.jpg 2.jpg … 10.jpg 11.jpg … n.jpg
import os
import time
import cv2
import numpy as np
def merge_video():
path = r'F:\src' # 图片序列所在目录,文件名:0.jpg 1.jpg ...
dst_path = r'F:\dst\result.mp4' # 生成的视频路径
filelist = os.listdir(path)
filepref = [os.path.splitext(f)[0] for f in filelist]
filepref.sort(key = int) # 按数字文件名排序
filelist = [f + '.jpg' for f in filepref]
width = 480
height = 270
fps = 30
col_cnt = 2 #显示视频时的图片列数(倍数)
vw = cv2.VideoWriter(dst_path, cv2.VideoWriter_fourcc(*'DIVX'), fps, (width * col_cnt, height))
for file in filelist:
if file.endswith('.jpg'):
file = os.path.join(path, file)
img = cv2.imread(file)
img = np.hstack((img, img)) # 如果并排两列显示
vw.write(img)
vw.release()
if __name__ == "__main__":
merge_video()
print('end')