简体   繁体   English

如何在Python中录制系统音频? (Linux)的

[英]How do I record system audio in Python? (Linux)

I've tinkered with PyAudio and I've only been able to record from the microphone. 我已经修改了PyAudio,但只能通过麦克风进行录音。 I want to be able to record what's played through the speakers. 我希望能够记录通过扬声器播放的内容。 Is there a way to do this with PyAudio or should I be using something else? 有没有办法用PyAudio做到这一点,或者我应该使用其他东西吗?

The audio subsystem on Linux works around the concepts of sources and sinks. Linux上的音频子系统围绕源和接收器的概念工作。 A source is a place where sound can some into the audio subsystem (microphone, app playing sound, etc), and a sink is a place where it can leave (speakers). 源是声音可以进入音频子系统(麦克风,应用程序播放声音等)的地方,而水槽是可以离开声音的地方(扬声器)。 You need to find the audio source that acts as a loopback monitor of the speaker sink. 您需要找到充当扬声器接收器回送监视器的音频源。 See b-ak's answer to the following post: 请参阅b-ak对以下帖子的回答:

https://askubuntu.com/questions/229352/how-to-record-output-to-speakers https://askubuntu.com/questions/229352/how-to-record-output-to-speakers

It explains the commands you can use from the command line to aid in identification of the appropriate identifier of this audio source. 它说明了可从命令行使用的命令,以帮助识别此音频源的适当标识符。 Then, you will need to follow Roland Smith's answer to the following post to load this into pyaudio: 然后,您需要按照Roland Smith对以下帖子的回答将其加载到pyaudio中:

voice recording using pyaudio 使用pyaudio录音

Here, it is explained that you can instantiate a pyaudio object and get a list of device ids at an interactive python prompt like this: 在这里,说明了可以实例化pyaudio对象并在交互式python提示符下获取设备ID列表,如下所示:

import pyaudio
p = pyaudio.PyAudio()
[p.get_device_info_by_index(i) for i in range(p.get_device_count())]

Find the appropriate device and then pass the input_device_index parameter to p.open with the appropriate device id when opening the stream. 查找合适的设备,然后在打开流时使用适当的设备ID将input_device_index参数传递给p.open

I think this is a duplicate posting of this post 4 years ago 我认为这是该帖子4年前的重复发帖

I did some research and found out, that WASAPI still works and seems to be your only option. 我进行了一些研究,发现WASAPI仍然有效,并且似乎是您唯一的选择。 Here's the Gitlab Link for the extended PyAudio lib . 这是扩展的PyAudio lib的Gitlab链接。 I found no solution for any Linux OS that works "out of the box". 我找不到任何“开箱即用”的Linux操作系统的解决方案。 If it is really important, try to use any VirtualMachine with Windows on it. 如果真的很重要,请尝试在Windows上使用任何VirtualMachine。

As mentioned earlier the question is why do you want to achieve this and if there might be a more "elegant" way. 如前所述,问题是为什么要实现这一目标,以及是否可能有一种更为“优雅”的方式。

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

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