pantz.org banner
Recording sound from your web browser using Linux
Posted on 11-27-2012 18:25:59 UTC | Updated on 12-05-2012 01:19:22 UTC
Section: /software/alsa/ | Permanent Link

My goal here is to record any audio coming from my browser. I looked a bit and could not find a Firefox add-on or Chrome extension that could do this. Some looked like they might, but they looked shady and gave me an icky feeling. There was one called Freecorder, but it was all up in Microsoft's business. This being Linux I assumed there had to be a better way and there was.

For the impatient (quick version)

  1. sudo apt-get install flac gnome-media pavucontrol lame
  2. Start programs PulseAudio Volume Control (pavucontrol) and Sound Recorder (gnome-sound-recorder).
  3. Start you audio. Go to Sound Recorder. Click the record button (red circle).
  4. Switch to PulseAudio Volume Control. In "Record Stream from" select your audio device.
  5. When ready switch back to Sound Recorder and click the stop button. Then click the save button to save the recording.

See below for more detailed information on this process and about encoding files after recording.

What you need

  1. You need an Ubuntu distribution or another Linux distro that has the applications below. I'm using Xubuntu 12.04 for this demo.
  2. Internet access to install the packages.
  3. A working sound system with PulseAudio. Most Ubuntu distros already use this for the sound system.
  4. The following packages installed: sudo apt-get install flac gnome-media pavucontrol lame. This will install the packages on a system with apt-get (Ubuntu/Debian).

Getting things started for recording

First thing first is open your browser and find some type of audio and start playing it. Keep it playing while we do the rest of this. Next, in your Application menu find the "PulseAudio Volume Control" program and start it. Mine was in the Multimedia menu. Then find something called "Sound Recorder". That was in the Multimedia menu as well. If you can't find them you can try to start them from the command line with pavucontrol & and gnome-sound-recorder &. If they are installed this will start and background both programs.

Starting the recording

Make sure your sound is still playing and you can hear it in your speakers. Go to the Sound Recorder program and make sure "Recording from input:" is set to "Master". Then in the "Record as:" menu select how you want to save your audio. If you want the best sound select "CD quality, Lossless". This will record audio as a flac file. The worst quality is at the bottom of the menu and it is "Voice Lossy". That is mono 22k and sounds like crap, but is much smaller. This would be good for just talking. Now, click the big red circle to start the recording. This will begin writing a file to the /tmp dir.

Switch to the PulseAudio Volume Control" window. Click on the "Recording" tab. You should see an area called "Record Stream from:". Click the drop down box next to this and select your audio device you want to record. Usually this says something like "Monitor of ..." where ... is the name of your Linux alsa audio device. You can look at the "Playback:" tab to see your playback device name. Try many different ones until you hit one that starts the bar bouncing a little below the selection box. This bouncing bar (level) shows you are receiving sound. You might need to fiddle with the "Show:" drop down at the bottom if this bar does now show up. Try the 3 different settings and see if that helps.

Finishing the recording

Now that the recording is going you should check the bottom of the Sound Recorder and make sure the level bar is bouncing or is filled with color (you have it up to high). If that is the case you can just let it go as long as you want. Once your ready to stop your recording switch back to the Sound Recorder window and click the red square button. We have not yet saved the file so click the icon for the disk with the arrow. It will ask you where to save your file and what to call it. Give it a name and click the "Save" button. Depending on how big the file is this might take some time. It will even look like the window is locked up. Just give it a good amount of time to save the file.

Converting recordings to mp3

This step is optional, but most people will want to do this if you did not select mp3 "Record as" option in Sound Recorder. That setting is a low 128bit recording so depending on your sound source you might want to record in a lossless codec like flac. I suggest recording in flac if you have the disk space, because you can always downgrade the mp3 encode from there if you want. My example will use FLAC files recorded by Sound Recorder.

# Decode to wav format. Decoded file will have same filename with .wav extension
flac -d recording.flac
# Encode decoded .wav file to 192k mp3 
lame --preset cbr 192 recording.wav recording.mp3

Suggestions/Notes

Recording from the command line

After doing all of the above with GUI's I thought the geeker people might want to know how to do this from the command line. Since the desktop Ubuntu system is using PulseAudio for the sound server we can tap into that using the same method that Sound Recorder does by using PulseAudio recorder program (parec). The problem is finding the monitor device name from the PulseAudio server. Luckily someone has already done the work for us. Thanks to www.outflux.net we have a script that will find the device and even start the recording for us. See the script below. I modified it so you can give it a stop time and it will cut off your recording in X amount of seconds. Without the time given it just records until you hit Ctrl-C. I have also modified it to use lame and encode mp3's on the fly. It will use the filename extension to detect either .wav or .mp3 and encode accordingly.

#!/bin/bash
# Get pulseaudio monitor sink monitor device then pipe it to 
# sox to record wav, lame to encode to mp3, or flac to encode flac
FILENAME="$1"
STOPTIME="$2"
# Encoding options for lame and flac.
LAMEOPTIONS="--preset cbr 192 -s 44.1" 
FLACOPTIONS="--force-raw-format --endian=little --channels=2 --sample-rate=44100 --sign=signed --bps=16 -f"

if [ -z "$FILENAME" ]; then
    echo -e "
    Usage: $0 /path/to/output.wav or output.mp3 or output.flac
    Usage: $0 /path/to/output.wav or output.mp3 or output.flac stopinseconds" >&2
    exit 1
fi

# Get sink monitor:
MONITOR=$(pactl list | egrep -A2 '^(\*\*\* )?Source #' | \
    grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1)
echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null

# Record it raw, and pipe to lame for an mp3
echo "Recording to $FILENAME ..."

if [[ $FILENAME =~ .mp3$ ]]; then
  if [ -z $STOPTIME ]; then
    parec -d $MONITOR | lame $LAMEOPTIONS -r - $FILENAME 
  else
    echo -e "\nStopping in $STOPTIME seconds"
    parec -d $MONITOR | lame $LAMEOPTIONS -r - $FILENAME 2>&1 &
    SPID=$!
    sleep $STOPTIME
    kill -9 $SPID
  fi
fi 

# Note: wav has a limit of about 6.5hrs using 44k 16bit. 
if [[ $FILENAME =~ .wav$ ]]; then
  if [ -z $STOPTIME ]; then
    parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$FILENAME"
  else
    echo -e "\nStopping in $STOPTIME seconds"
    parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$FILENAME" trim 0 $STOPTIME
  fi
fi

if [[ $FILENAME =~ .flac$ ]]; then
  if [ -z $STOPTIME ]; then
    parec -d "$MONITOR" | flac - $FLACOPTIONS -o $FILENAME
  else
    echo -e "\nStopping in $STOPTIME seconds"
    parec -d $MONITOR | flac - $FLACOPTIONS -o $FILENAME 2>&1 &
    SPID=$!
    sleep $STOPTIME
    kill -9 $SPID
  fi
fi 

Reddit!

Related stories


RSS Feed RSS feed logo

About


3com

3ware

alsa

alsactl

alsamixer

amd

android

apache

areca

arm

ati

auditd

awk

badblocks

bash

bind

bios

bonnie

cable

carp

cat5

cdrom

cellphone

centos

chart

chrome

chromebook

cifs

cisco

cloudera

comcast

commands

comodo

compiz-fusion

corsair

cpufreq

cpufrequtils

cpuspeed

cron

crontab

crossover

cu

cups

cvs

database

dbus

dd

dd_rescue

ddclient

debian

decimal

dhclient

dhcp

diagnostic

diskexplorer

disks

dkim

dns

dos

dovecot

drac

dsniff

dvdauthor

e-mail

echo

editor

emerald

ethernet

expect

ext3

ext4

fat32

fedora

fetchmail

fiber

filesystems

firefox

firewall

flac

flexlm

floppy

flowtools

fonts

format

freebsd

ftp

gdm

gmail

gnome

google

greasemonkey

greylisting

growisofs

grub

hacking

hadoop

harddrive

hba

hex

hfsc

html

html5

http

https

hulu

idl

ie

ilo

intel

ios

iperf

ipmi

iptables

ipv6

irix

javascript

kde

kernel

kickstart

kmail

kprinter

krecord

kubuntu

kvm

lame

ldap

linux

logfile

lp

lpq

lpr

maradns

matlab

memory

mencoder

mhdd

mkinitrd

mkisofs

moinmoin

motherboard

mouse

movemail

mplayer

multitail

mutt

myodbc

mysql

mythtv

nagios

nameserver

netflix

netflow

nginx

nic

ntfs

ntp

nvidia

odbc

openbsd

openntpd

openoffice

openssh

openssl

openvpn

opteron

parted

partimage

patch

perl

pf

pfflowd

pfsync

photorec

php

pop3

pop3s

ports

postfix

power

procmail

proftpd

proxy

pulseaudio

putty

pxe

python

qemu

r-studio

raid

recovery

redhat

router

rpc

rsync

ruby

saltstack

samba

schedule

screen

scsi

seagate

seatools

sed

sendmail

sgi

shell

siw

smtp

snort

solaris

soundcard

sox

spam

spamd

spf

sql

sqlite

squid

srs

ssh

ssh.com

ssl

su

subnet

subversion

sudo

sun

supermicro

switches

symbols

syslinux

syslog

systemrescuecd

t1

tcpip

tcpwrappers

telnet

terminal

testdisk

tftp

thttpd

thunderbird

timezone

ting

tls

tools

tr

trac

tuning

tunnel

ubuntu

unbound

vi

vpn

wget

wiki

windows

windowsxp

wireless

wpa_supplicant

x

xauth

xfree86

xfs

xinearama

xmms

youtube

zdump

zeromq

zic

zlib