~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Record Output Audio via Terminal

Published on

Updated on

2 minute reading time

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

This post is specific to PulseAudio on Linux.

I know of GUI based solutions like PulseAudio Volume Control that lets you set up monitor devices. But, what if you want to do this through the terminal?

Luckily, b-ak on AskUbuntu gave an elegant answer to this question!

First make sure you have pulseaudio-utils installed,

sudo apt install pulseaudio-utils

Next we need to search for the speaker we wish to monitor

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

It will output something similar to this:

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

From here note the name in <> of the speaker you wish to monitor. For example for my output above, I wish to monitor alsa_output.pci-0000_00_1b.0.analog-stereo.

Next we will use the parec command to record the raw audio stream from the PulseAudio server.

parec --device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor | encoder_command

Notice the addition of .monitor at the end of the device.

lame

For the encoder_command, b-ak used lame.

lame -r -V0 - out.mp3

This command takes in a raw pcm -r and enables variable bit rates for the highest quality -V0. From there it encodes it and puts it in out.mp3.

Now lame actually makes a couple assumptions about your raw pcm if you didn’t specify additional arguments:

  • The Raw PCM is formatted in signed 16-bit little endian samples
  • The Raw PCM has 2 channels

If you’re assumptions don’t meet the above, then you will need to add additional arguments.

ffmpeg

We can replace lame with the more featureful ffmpeg if we take note of the same assumptions above.

ffmpeg -f s16le \
       -ac 2 \
       -i pipe:0 \
       -b:a 0 \
       out.mp3

Where we can replace the .mp3 with whatever file extension ffmpeg supports.

Now to show the entire command

parec --device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor | \
  ffmpeg -f s16le \
         -ac 2 \
         -i pipe:0 \
         -b:a 0 \
         out.mp3
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :