使用Python中的ffmpeg即时将mp3转换为wav [英] Convert mp3 to wav on the fly using ffmpeg in Python
问题描述
我正在使用ffmpeg将Python中的MP3文件转换成wav文件。
I'm trying to convert a mp3 file on the fly in Python to wav file using ffmpeg.
我使用子进程调用它,我如何得到它的输出把它当作wav在飞行中没有保存它作为文件(或播放它,而它的转换)然后播放它?
I call it using subprocess, how can I get it's output to play it as wav on the fly wthout saving it as the file (or playing it while its converting) and then playing it?
这是我迄今为止所做的:
This is what I have so far:
我正在使用aplay举个例子。
I'm using aplay just for a example.
FileLocation = "/home/file.mp3"
subprocess.call(["ffmpeg", "-i", FileLocation etc etc "newfail.wav"])
os.system("aplay ... ") #play it on the fly here
据我所知,如果我把 - 作为文件名,它会输出到stdout,但是我不知道如何读取stdout。
As far as I understand, if I put "-" as file name, it will output it instead to stdout, but I don't know how to read stdout...
推荐答案
模拟 source arg1 arg2 | sink
没有shell的shell命令:
To emulate source arg1 arg2 | sink
shell command without the shell:
from subprocess import Popen, PIPE
source = Popen(['source', 'arg1', 'arg2'], stdout=PIPE)
sink = Popen(['sink'], stdin=source.stdout)
source.stdout.close()
source.wait()
sink.wait()
这篇关于使用Python中的ffmpeg即时将mp3转换为wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!