python使用ffmpeg合并两个MP4视频

python使用ffmpeg合并两个MP4视频

目的:将视频1.mp4和2.mp4合并为3.mp4

import subprocess
import cv2
import time
import threading
import os

class DEMO(object):
    def __init__(self):
        pass

    def merge_video(self):
        output_mp4 = "./3.mp4"
        mp4_list =  ['./1.mp4', './2.mp4'] #这个地方也可以改成多个视频文件
        txt = ("file \'{}\'\n"*len(mp4_list)).format(*mp4_list)
        with open('video.txt','w') as f:    #设置文件对象
            f.write(txt) 
        print("txt: ", txt)
        cmd = "cd ./;ffmpeg -f concat -y -safe 0 -i %s -c copy %s" % ("video.txt", output_mp4)
        print(cmd)
        with open("stdout.txt","wb") as out, open("stderr.txt","wb") as err:
            proc1 = subprocess.Popen(cmd, shell=True, stdout=out, stderr=err, bufsize=-1)
            proc1.wait()
            proc1.kill()  

    def start_write_thread(self):
        thread = threading.Thread(target=self.write_thread, args=())
        thread.daemon = True
        thread.start()
        return thread
    def write_thread(self):
        self.merge_video()

if __name__ =="__main__":
    d = DEMO()
    t = d.start_write_thread()
    while t.is_alive():
        pass

参考链接:
https://blog.csdn.net/MemoryD/article/details/108952463

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值