编写背景:

由于线上用户反馈媒体添加页加载时间很长,猜测是由于本地视频内存过大引起,于是编写此脚本以便快速生成内存很大的视频

代码如下:

# coding=utf-8
from moviepy.editor import *
from natsort import natsorted
import os
 
L = []
 
 
def ConnectVideo():
    # 访问Video文件夹
    filePath = "/Users/ceshi/Downloads/Video/"
    for root, dirs, files in os.walk(filePath):
        # 按文件名排序
        files = natsorted(files)
        # 遍历所有文件
        for file in files:
            # 如果后缀名为 .mp4
            if os.path.splitext(file)[1] == '.mp4':
                # 拼接成完整路径
                filePath = os.path.join(root, file)
                # 载入视频
                video = VideoFileClip(filePath)
                # 添加到数组
                L.append(video)
 
    # 拼接视频
    final_clip = concatenate_videoclips(L)
    # 生成目标视频文件
    targetFile = "/Users/ceshi/Downloads/Video/target.mp4"
    final_clip.to_videofile(targetFile, fps=30, remove_temp=False)
 
 
ConnectVideo()