7

I want to record the output of audio to an mp3 file. What tools and commands can I use in the Ubuntu Linux terminal?

3

4 Answers 4

5

Other answers suggested the following tools: sox, arecord, and parec. Additionally, PipeWire users may use pw-cat, although the other tools work with PipeWire also. They can all be invoked simply:

arecord

arecord --format=cd file.wav

It saves in .wav by default. If you don't specify a decent format, the default audio quality is poor. One feature I like about this program is its nice vumeter graphical output. It also supports dynamic output filenaming (via hour, minute, day...). Unfortunately, it can't output to ogg or mp3.


sox

rec file.ext

This program doesn't currently work with zsh completions. It displays a meter in its graphical output, but it displays other information also. It supports mp3 and ogg.


pacat

parecord file.ext

It supports mp3 and ogg. But it can't display a meter in its graphical output.


pw-cat

pw-record file.ext

It's pacat for PipeWire. It supports custom quality settings for ogg and mp3, whereas pacat doesn't. It doesn't have a metered graphical output either.

To record from a different audio device, a USB microphone for example, you can use one of the methods listed here to first get the name or nickname of your recording device. For example:

pw-cli list-objects | grep node.nick

Assuming this has yielded "USB Audio Device", we then use that nickname with the --target option:

pw-record --target "USB Audio Device" file.ext
0
4

First you need to extract the name of your output device:

To do this, you can install the following packets:

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 sound is sent to the speaker and stops if nothing is received after 3 seconds. For more information about sox, refer to its man page for Linux.

1
  • 1
    Great info! As an aside, pacmd list-sinks no longer works on newer systems that use pipewire-pulseaudio backend. But this does. pactl list|grep monitor
    – hellork
    Feb 9 at 6:31
2

Kudos to @Ahmad. This worked for me:

sox -e signed-integer -L -b16 -c2 -r44100 -t raw "|parec -d alsa_output.pci-0000_0c_00.4.iec958-stereo.monitor" -t mp3 -c2 -C 320.01 stream.mp3 silence 1 0.1 3% 1 3.0 3%

where input stream parameters can be seen with pacmd list-sinks:

name: <alsa_output.pci-0000_0c_00.4.iec958-stereo>
sample spec: s16le 2ch 44100Hz
properties:
        alsa.resolution_bits = "16"
0
2

You can use arecord for this.

Get your available devices with arecord -l. This will produce something like this:

card 0: PCH [HDA Intel PCH], device 0: CX20590 Analog [CX20590 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Then, to record using the USB microphone (card 1, subdevice 0) use, for example:

arecord --device="hw:1,0" --format=S16_LE --rate=48 rec_$(date "+%F_%H-%M-%S").wav

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.