擅长:python、mysql、java
<p>使用ffmpeg,您可以使用<code>-</code>作为输入/输出文件名,指示它应该从stdin读取数据/写入stdout。</p>
<p>然后可以使用<code>Popen</code>的<code>stdin</code>/<code>stdout</code>参数来读/写数据。</p>
<p>例如:</p>
<pre><code>from subprocess import Popen, PIPE
with open("test.avi", "rb") as infile:
p=Popen(["ffmpeg", "-i", "-", "-f", "matroska", "-vcodec", "mpeg4",
"-acodec", "aac", "-strict", "experimental", "-"],
stdin=infile, stdout=PIPE)
while True:
data = p.stdout.read(1024)
if len(data) == 0:
break
# do something with data...
print(data)
print p.wait() # should have finisted anyway
</code></pre>
<p>相反,您可以为<code>stdin</code>提供一个文件,也可以使用<code>PIPE</code>并直接写入进程输入流(<code>p.stdin</code>)。或者你只需要使用wavfile。。。</p>
<p>注意,您必须显式地指定输出格式和编解码器,因为ffmpeg不能像通常那样从文件扩展名猜测它们。<br/>
它只适用于不需要可查看的输出流的muxer,但是flac应该可以工作。。。</p>