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