如何合并两个视频部分并使用Python获取可播放的视频文件?

时间:2013-06-27 07:33:39

标签: subprocess

在这里,我实际上想要合并两个字符串out1& out2(包含第一个和第二个30秒长的视频数据)并将其写入文件。这样我就可以获得1分钟长的可播放视频文件。但我得到的只是第一个30秒的视频。我该如何编辑这段代码来实现呢?请帮我。非常感谢。

import subprocess,os

ffmpeg_command1 = ["ffmpeg", "-i", "PATH/connect.webm", "-vcodec", "copy", "-ss", "00:00:00", "-t", "00:00:30","-f", "webm", "pipe:1"]

p1 = subprocess.Popen(ffmpeg_command1,stdout=subprocess.PIPE)

out1, err = p1.communicate()

ffmpeg_command2 = ["ffmpeg", "-i", "PATH/connect.webm","-vcodec", "copy", "-ss", "00:00:31", "-t", "00:00:30","-f", "webm", "pipe:1"]

p2 = subprocess.Popen(ffmpeg_command2,stdout=subprocess.PIPE)

out2, err1 = p2.communicate()

string = out1 + out2

fname = "PATH/final.webm"

fp = open(fname,'wb')

fp.write(string)

fp.close()

请帮帮我。我来了。

2 个答案:

答案 0 :(得分:1)

如果你想用ffmpeg连接两个视频,它就是这样的:

ffmpeg -vcodec copy -isync -i \
"concat:file1.mp4|file2.mp4|...|fileN.mp4" \
outputfile.mp4

答案 1 :(得分:0)

#coding=utf-8
import os

#Function to create a file list in the folder
import os
s = os.sep
path = r"F:\folder_mp4_files\temp"
def create_file_list(path):
    return_list = []
    for filenames in os.walk(path):
        for file_list in filenames:
            for file_name in file_list:
                if file_name.endswith((".mp4")):
                    return_list.append(path+s+file_name)
    return return_list

alist = create_file_list(path)
tsString = '|'.join([i.replace('.mp4','.ts') for i in alist])
print(tsString)
# mp4 converts to ts
for i in alist:
    noExtension = i.replace('.mp4','')
    # batch processing
    os.system("ffmpeg -i %s -vcodec copy -acodec copy -vbsf h264_mp4toannexb %s.ts" % (i,noExtension))
# Remove used mp4 files
for i in alist:
    os.remove(i)
os.system("""ffmpeg -i concat:"{0}" -acodec copy -vcodec copy -absf aac_adtstoasc {1}""".format(tsString, alist[0]))
# Remove used ts files
for i in alist:
    os.remove(i.replace('.mp4','.ts'))