How can I record audio output from command line in linux?

I want to record the output of audio to an mp3 file. What tools and commands can I use in linux ubutu Terminal?


Solution 1:

First you need to extract the name of output device:

To do this, you can install the following modules:

sudo apt-get install pulseaudio-utils lame mpg123

And run:

pacmd list-sinks | grep -e 'name:' -e 'index' -e 'Speakers'

The output could be like this:

index: 1
    name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
        analog-output-speaker: Speakers (priority 10000, latency offset 0 usec, available: unknown)
index: 23
    name: <alsa_output.pci-0000_00_03.0.hdmi-surround71>

After you found the name, you can run the following command to record the output to an mp3 file:

parec -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor | lame -r -V0 - out.mp3

Or using sox you can do the following, however I found the first solution more robust:

sox -t pulseaudio alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -t mp3 test.mp3 

However, if you want to automatically start and stop recording you can run:

parec -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor | sox -t raw -b 16 -e signed -c 2 -r 44100 - test.ogg silence 1 0.1 3% 1 3.0 3%

It begins recording when a sounds sent to the speaker and stops if nothing is received after 3 seconds. for more information about sox refer to sox man page in linux