使用python+ffmpeg合并多个视频为1个

# encoding=utf-8
import os
import itertools


def main():
    #使用前,要先配置好ffmpeg的环境变量,并删除videos_path中txt文件夹下的所有文件
    ffmpeg_path = "D:\\FFmpeg\\bin\\ffmpeg"
    videos_path = "C:\\Users\\Yan\\Desktop\\videos"
    concat_list_path = videos_path + "\\txt\\"

    # 定义一个数组
    L = []

    # 访问 videos 文件夹 (假设视频都放在这里面)
    for root, dirs, files in os.walk(videos_path):
        # 按文件名排序
        files.sort()
        # 遍历所有文件
        for file in files:
        # 如果后缀名为 .mp4
            if os.path.splitext(file)[1] == '.mp4':
            # 拼接成完整路径
                filePath = os.path.join(root, file)
                # 添加到数组
                L.append(filePath)

    # 把要合并的视频文件写入txt中
    concat_file_path = concat_list_path + "concat_list.txt"
    print(concat_file_path)
    total = len(L)
    for i in range(0,total):
        print(L[i])
        with open(concat_file_path, 'a', encoding='utf-8') as f:
            video_file = "file " + "'" + L[i] + "'" + "\n"
            f.write(video_file)

    result_file_path = videos_path + "\\" + "merge.mp4"
    print(result_file_path)
    # 拼接好运行ffmpeg的命令行语句
    cmd = ffmpeg_path + " -f concat -safe 0 -i " + concat_file_path + " -c copy " + result_file_path
    print(cmd)
    # 调用cmd命令行执行ffmpeg拼接视频
    os.popen(cmd)


if __name__ == '__main__':
    main()


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值