使用python将图片合成视频
1. 使用依赖:
python版本: 3.7.2
使用到的库:opencv
(安装命令:pip install opencv-python)
2. 代码:
import cv2
if __name__ == '__main__':
# 保存视频的FPS,可以适当调整, 帧率过低,视频会有卡顿
fps = 20
photo_size = (2560, 1440)
# 可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# video: 要保存的视频地址
video = 'test.avi'
videoWriter = cv2.VideoWriter(video, fourcc, fps, photo_size)
for i in range(1, 21):
# image: 图片地址
image = "image_" + str(i) + ".jpg"
frame = cv2.imread(image)
videoWriter.write(frame)
videoWriter.release()