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.
See below for more detailed information on this process and about encoding files after 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.
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.
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.
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
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