Cristian Gutierrez
Cristian Gutierrez

Reputation: 51

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. 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?

Upvotes: 4

Views: 9697

Answers (2)

Julian Gerhard
Julian Gerhard

Reputation: 116

I think this is a duplicate posting of this post 4 years ago

I did some research and found out, that WASAPI still works and seems to be your only option. Here's the Gitlab Link for the extended PyAudio lib. I found no solution for any Linux OS that works "out of the box". If it is really important, try to use any VirtualMachine with Windows on it.

As mentioned earlier the question is why do you want to achieve this and if there might be a more "elegant" way.

Upvotes: 1

lesthaeghet
lesthaeghet

Reputation: 116

The audio subsystem on Linux works around the concepts of sources and sinks. 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:

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:

voice recording using 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:

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.

Upvotes: 3

Related Questions