How Can I Record the Audio Output Using Sox?

I want to record the output of Google Text to speech to a mp3 file. I need a command that I can use in Terminal. I read sox can record audio, however don't know how to set up it to record the output audio of my computer. I also prefer to automatically stop recording if the audio output was silent for at least 3 seconds... Any other tool for this purpose is acceptable.

1 Answer

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:

sox -v 5 -t pulseaudio alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -t mp3 test.mp3 silence 1 0.1 3% 1 3.0 3%

Or using parec:

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest