我如何解析字符串作为subprocess.Popen的输入?

时间:2013-06-26 10:27:51

标签: python ffmpeg subprocess stdin

这里我尝试将第一个和第二个30秒长的视频文件从“path / connect.flv”剪切到文件output1.flv和output2.flv。有用。我能够连接这两个文件,形成一个60sec长的新文件“final.flv”。所以这有效,我得到输出output1.flv [30sec],output2.flv [30sec]和final.flv [1min]。

这是python代码:

import subprocess

ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30", "/home/xincoz/test/output1.flv"]

ffmpeg_command2 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:30", "-t", "00:00:30", "/home/xincoz/test/output2.flv"]

ffmpeg_command3 = ["mencoder", "-forceidx", "-ovc", "copy", "-oac", "pcm", "-o", "/home/xincoz/test/final.flv", "/home/xincoz/test/output1.flv", "/home/xincoz/test/output2.flv"]

subprocess.call(ffmpeg_command1)

subprocess.call(ffmpeg_command2)

subprocess.Popen(ffmpeg_command3)

但我真正想要的是连接两个字符串out1和out2并将这两个字符串连接到一个文件而不是连接“/home/xincoz/test/output1.flv”和“/home/xincoz/test/output2.flv ”。那么如何解析字符串out1和out2作为mencoder的输入?请编辑我的代码以获得结果。

import subprocess,os

ffmpeg_command = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30","-f", "flv", "pipe:1"]

p = subprocess.Popen(ffmpeg_command,stdout=subprocess.PIPE)

out1, err = p.communicate()

ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:30", "-t", "00:00:30","-f", "flv", "pipe:1"]

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

out2, err1 = p1.communicate()

ffmpeg_command2 = ["mencoder", "-forceidx", "-ovc", "copy", "-oac", "pcm", "-o", "/home/xincoz/test/final.flv", out1, out2 ]

p2=subprocess.Popen(ffmpeg_command2)

请帮帮我。非常感谢。

1 个答案:

答案 0 :(得分:0)

您还没有说过您的代码是如何失败的,但看起来您正在尝试将MEncoder二进制字符串作为命令行参数。 http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-selecting-input.html表示您可以在命令行中提供文件名,而不是文件的内容。

所以回答你的问题:

with open('merged.flv', 'wb') as f:
    f.write(out1 + out2)

ffmpeg_command2 = ["mencoder", "your", "other", "args", "merged.flv"]