问题描述
是否可以使用python(而不是任何外部软件)来播放mp3文件(如麦克风输入)?
例如,我有一个mp3文件,并带有python脚本,它将通过我的麦克风播放,因此语音室中的其他人可以听到它.正如我所说的只是一个例子.
For example, I have a mp3 file and with a python script it would play it through my mic so other in a voice room would hear it. As I say it is just an example.
当然,我已经做了一些研究.我发现我可以使用软件来创建虚拟设备,并做一些事来获得结果.但是我的意思是,是否可以不安装软件而使用某种python脚本呢?
Of course, I have done some research. I found out that I can use a software to create a virtual device and do few things to have the result. But my point is if it is possible without installing software but with some kind of python script?
推荐答案
如果您的意思是如何使用Python播放MP3,这是一个广泛的问题.
If you meant how to play MP3 using Python, well, this is a broad question.
有没有可能没有任何依赖性,是的,但这是不值得的.好吧,播放未压缩的音频是,但是MP3好吧,我将在下面说明.
Is it possible, without any dependencies, yes it is, but it is not worth it. Well, playing uncompressed audio is, but MP3, well, I'll explain below.
要在不安装pyaudio或pygame或类似工具的情况下播放来自Python的原始音频数据,您首先必须知道运行脚本的平台.
To play raw audio data from Python without installing pyaudio or pygame or similar, you first have to know the platform on which your script will be run.
然后实现一组不错的功能,用于选择音频设备,设置采样率,比特率,单声道/立体声...之类的属性,将流馈送到声卡并停止播放.
Then implement a nice set of functions for choosing an audio device, setting up properties like sample rate, bit rate, mono/stereo..., feeding the stream to audio card and stopping the playback.
这并不难,但是要做到这一点,您必须在Windows上使用ctypes,在Mac和Linux上使用PyObjC是特例,因为它支持许多音频系统(可能使用套接字连接到PulseAudio或通过管道传输到诸如aplay/paplay/mpeg123 ...或利用gstreamer.)
It is not hard, but to do it you have to use ctypes on Windows, PyObjC on Mac and Linux is special case as it supports many audio systems (probably use sockets to connect to PulseAudio or pipe to some process like aplay/paplay/mpeg123... or exploit gstreamer.).
但是,当您拥有漂亮的库并带有用于访问和使用音频设备的简单接口时,为什么还要进行所有这些操作以避免依赖关系.
But why go through all this just to avoid dependencies, when you have nice libraries out there with simple interfaces to access and use audio devices.
PyAudio很棒.
PyAudio is great one.
好吧,这是您的关注点.
Well, that is your concern.
但是,从纯Python实时播放不带外部库的MP3并不是完全不可能,但是很难实现,据我所知,甚至没有人尝试过.
But, playing MP3 without external libraries, in real time, from pure Python, well, it's not exactly impossible, but it is very hard to achieve, and as far as I know nobody even tried doing it.
有纯Python MP3解码器实现,但它比实时音频播放所需的速度慢10倍.可以针对几乎全速对其进行优化,但是没有人对此感兴趣.
There is pure Python MP3 decoder implementation, but it is 10 times slower than necessary for real-time audio playback. It can be optimized for nearly full speed, but nobody is interested in doing so.
它具有教育意义,主要用于不需要实时速度的情况.
It has mostly educational value and it is used in cases where you do not need real-time speed.
这是您应该做的:
安装pygame并使用它直接播放MP3
Install pygame and use it to play MP3 directly
或:
安装PyAudio和一些解码Mp3的库,在pypi.python.org上有很多它们,并使用它解码MP3并将输出馈送到PyAudio.
Install PyAudio and some library that decodes Mp3, there are quite a few of them on pypi.python.org, and use it to decode the MP3 and feed the output to PyAudio.
还有更多可能性,包括pymedia,但我认为这些是最简单的解决方案.
There are some more possibilities, including pymedia, but I consider these the easiest solutions.
好的,正如我们澄清的那样,您真正需要的是答案.
Okay, as we clarified what is really you need here is the answer.
我也将保留第一个答案,因为您也需要该部分.
I will leave first answer intact as you need that part too.
现在,您想将音频播放到记录流中,以便任何记录音频输入的应用程序都会记录您正在播放的内容.
Now, you want to play audio to the recording stream, so that any application recording the audio input records the stuff that you are playing.
在Windows上,这称为立体声混音,可以在音量控制"的音频输入下找到.
On Windows, this is called stereo mix and can be found in Volume Control, under audio input.
您选择立体声混音作为默认输入.现在,当您打开一个录音应用程序时,它没有选择自己的输入通道,而是使用选定的输入通道(例如Skype),它将记录所有来自扬声器和麦克风/线路输入的声音.
You choose stereo mix as your default input. Now, when you open an recording app which doesn't select itsown input channel, but uses the selected one (e.g. Skype) , it will record all coming out of your speakers and coming into your mic/line in.
我不确定100%是否在所有Windows上都显示此选项,或者它是否是您所拥有的声卡的功能.
I am not 100% sure whether this option will appear on all Windows or it is a feature of an audio card you have.
我很肯定Creative和Realtek声卡支持它.
I am positive that Creative and Realtek audio cards supports it.
所以,对此进行研究.
要从Python中选择该选项,必须使用ctypes连接到winmm.dll并调用适当的函数.我不知道是哪一个,有什么争论.
To select that option from Python, you have to connect to winmm.dll using ctypes and call the appropriate function. I do not know which one and with what arguments.
如果在音量控制中不存在此选项,则只能安装虚拟声卡为您进行环回.
If this option is not present in volume control, there is nothing for it but to install a virtual audio card to do the loopback for you.
可能有这样的软件作为库打包在一起,以便您可以从Python或其他任何方式使用它.
There might be such a software that comes packaged in as library so that you can use it from Python or whatever.
在Linux上,使用Pulseaudio应该很容易.我不知道如何,但是我知道您可以做到,重定向流等.在那里有教程.
On Linux this should be easy using Pulseaudio. I do not know how, but I know that you can do it, redirect the streams etc. There is tutorial out there somewhere.
然后,您可以从Python调用该命令,将其设置为该命令并重置为正常状态.
Then you can call that command from Python, to set to this and reset back to normal.
在Mac上,我真的不知道,但是应该可以.
On Mac, well, I really have no idea, but it should be possible.
如果您只想在录制流中播放MP3,而在扬声器上根本不播放扬声器,那么在Windows上,如果没有环回音频设备,您将无法做到这一点.
If you want your MP3 to be played only to the recording stream, and not on your speakers at all, well on Windows, you will not be able to do that without a loopback audio device.
我确信在Linux上您将能够做到,而在Mac上应该可以,但是问问如何实现.
On Linux, I am sure you will be able to do it, and on Mac it should be possible, but how is the Q.
我目前没有时间去浏览库等,以便为您提供一些有用的代码,因此您必须自己动手做.但我希望我的指导会为您提供帮助.
I currently have no time to sniff around libraries etc. to provide you with some useful code, so you will have to do it yourself. But I hope my directions will help you.
这篇关于使用python通过麦克风播放mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!