简体   繁体   English

在Python中使用ffmpeg即时将mp3转换为wav

[英]Convert mp3 to wav on the fly using ffmpeg in Python

I'm trying to convert a mp3 file on the fly in Python to wav file using ffmpeg. 我正在尝试使用ffmpeg将Python中的mp3文件即时转换为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? 我使用子进程调用它,如何在不将其另存为文件(或在转换时播放)的情况下即时将其作为wav播放来输出呢?

This is what I have so far: 这是我到目前为止的内容:

I'm using aplay just for a example. 我仅以aplay为例。

FileLocation = "/home/file.mp3"

subprocess.call(["ffmpeg", "-i", FileLocation etc etc "newfail.wav"])
os.system("aplay ... ") #play it on the fly here

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... 据我了解,如果我将“-”作为文件名,它将输出它到stdout,但是我不知道如何读取stdout ...

To emulate source arg1 arg2 | sink 模拟source arg1 arg2 | sink source arg1 arg2 | sink shell command without the shell: 没有外壳的接收source arg1 arg2 | sink 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM