Recording audio streams in Linux with avconv

So you came here because you want to record Web radio streams, Soundcloud or that YouTube song you can't find anywhere else.

You have obviously Linux running. You already found out that Wondershare Audiorecorder won't run in Wine and, anyway you don't want to emulate Windows-based Software, ever. You are a Linux native and want to learn about the powers Linux offers in the realms of audio recording. In essence, you came to the right place.

lnxmnt

avconv

All you need is the open source application avconv, the successor of ffmpeg, a project that split itself due to internal quarrels, or whatever.

Learn about the powers Linux offers in the realms of audio recording.

In this tutorial I will show you how to setup web stream recording on Linux Mint or on any other Ubuntu/Debian-based distribution. The solution won't recognise pauses nor will it fetch titles, but it will record in OGG or even AAC, which are among the best audio codecs known to audiophiles.

Install avconv

Chances are avconv is already installed on your system. If not, install it by issuing the following commands in your terminal:

sudo apt-get update
sudo apt-get install libav-tools

Record via USB Audio interface

Identifier for Pulseaudio

To find out how to refer to your audio interface using pulseaudio, do a

pactl list sources | grep Name: Name: alsa_output.usb-Focusrite_Scarlett_2i2_USB-00-USB.analog-stereo.monitor Name: alsa_input.usb-Focusrite_Scarlett_2i2_USB-00-USB.analog-stereo

Grab the one where it says monitor and substitute it in the following section accordingly.

Ogg-Vorbis

Now that you have avconv installed and found out your usb interface name we can start recording and encoding. We will try to capture every sound that your computer makes, so try to let only the Web stream or the specific audio source running that you want to record.

First we start by using a very high quality setting with the Ogg Vorbis Codec. Navigate to the folder where you want to have the file saved and issue:

avconv -f alsa -ac 2 -ar 48000 -f pulse \ -i alsa_output.usb-Focusrite_Scarlett_2i2_USB-00-USB.analog-stereo.monitor \ -acodec libvorbis -aq 6 test.ogg

Avconv switches

ogg

-ac Channels -ar Audio Rate -acodec Audio-Codec -aq VBR Quality Level

mp3

For mp3 encoding substitute

-acodec libvorbis with -acodec libmp3lame and -aq 6 with a mp3 specific value like -aq 0. The aq switch defines the variable bitrate. It is not recommended to use constant bitrates (CBR) with lossy compression formats like mp3 or ogg-vorbis, but you can do it if you know what you are doing with -ab 192k.

Troubleshooting

Unknown input format: 'pulse'

If you run into bug 1086500 and get a

Unknown input format: 'pulse'

you can approach this problem by building avconv manually and compile it with the enable-libpulse switch:

git clone git://git.libav.org/libav.git avconv
cd avconv sudo ./configure --prefix="/pathTo/avconv/" --enable-gpl --enable-libpulse --enable-libvorbis sudo make && sudo make install

Empty File or low Bitrate

In case your file recorded has no sound in it and bitrate is low, chances are you need to setup your pulse audio setup (Pulse Audio is the default in Ubuntu 12.01, Mint Linux 15 and ElementaryOS Luna and later versions)

To configure your sound in any Ubuntu-based Operating System you need to install pavucontrol with

sudo apt-get install pavucontrol

Once installed, you need to fiddle around with the settings in your control panel and in pavucontrol. The following settings have a good chance of working on any system:

System Control Panel / Audio

Output Tab
- Select Analog Output Soundcard_Name

Pavucontrol

Playback Tab
- Unmute ALSA plug-in container and set to: Soundcard_Name Analog Stereo

Record Tab
- Set libav to Monitor of Soundcard_Name Analog Stereo and unmute libav

Output Hardware Tab
- Soundcard_Name Analog Stereo, Port: Analog Output, Unmute
- Internal Audio Analog Stereo, Port: Analog Output, Unmute

Input Hardware Tab
- Monitor of Soundcard_Name Analog Stereo: Unmute
- Monitor of Internal Analog Stereo: Unmute

Configuration
- Internal Audio, Profil: Analog Stereo Duplex
- Soundcard_Name, Profil: Analog Stereo Duplex

The Duplex setting insures that you will be able to listen to what you are recording. So basically you could set this to another setting but you will not be able to listen while you record.

Restarting Pulse Audio

pulseaudio -k
pulseaudio --start

Bitrates

Below you will find a table showing the variable bitrates that are offered by the ogg and mp3 compression formats. For Ogg the value q5 is considered high quality and for MP3 v1 and higher is considered to be high quality.

I tend to use q6 with ogg just to be safe, which should be transparent in case the source material is the original or lossless. If you encode from a lossy source like a lower quality stream, you should probably go for a even higher setting, keeping in mind the larger file you are going to end up with.

OGG-Vorbis nominal VBR bitrates and quality levels

Switch   VBR target
(kbit/s)
VBR range
(kbit/s)
Channel
Coupling
Noise
Normalization
-q -2 ~32 ~32 – ~64 point/lossless yes
-q -1 ~48 ~48 – ~64 point/lossless yes
-q 0 ~64 ~64–~80 point/lossless yes
-q 1 ~80 ~80 – ~96 point/lossless yes
-q 2 ~96 ~96 – ~112 point/lossless yes
-q 3 ~112 ~112 – ~128 point/lossless yes
-q 4 ~128 ~128 – ~160 point/lossless no
-q 5 ~160 ~160 – ~192 point/lossless no
-q 6 ~192 ~192 – ~224 lossless no
-q 7 ~224 ~224 – ~256 lossless no
-q 8 ~256 ~256 – ~320 lossless no
-q 9 ~320 ~320 – ~500 lossless no
-q 10 ~500 ~500 – ~1000 lossless no

MP3 Variable Bitrate (VBR) levels

Switch   Kbit/s   Bitrate range kbit/s  
-b 320 320 320 CBR
-V 0 245 220...260
-V 1 225 190...250
-V 2 190 170...210
-V 3 175 150...195
-V 4 165 140...185
-V 5 130 120...150
-V 6 115 100...130
-V 7 100 80...120
-V 8 85 70...105
-V 9 65 45...85

Converting to looseless FLAC

in case you don't fancy making any sonic compromises, I suggest you convert to flac, e.g.:

ffmpeg -i title.wav -c:a  flac title.flac

That's about it what you need to know to get going in the realm of audio stream recording on Linux.

I hope you found this tutorial useful and link to my blog as sign of your appreciation for my research. If you have any feedback, please don't hesitate letting us know in the comments.