Recording Linux desktop with PulseAudio
Tuesday, October 19th, 2010
For a while now I've been trying to record my Ubuntu desktop with various levels of success. I've been using recordmydesktop to do my recording. Recordmydesktop is a simple commandline tool that outputs an .ogv (Ogg Video) file which is a container format that uses the Theora video codec and the Vorbis audio codec. Most modern video/audio applications on closed-source desktops such as Windows and MacOS-X won't be able to read those, but we convert them to other formats using a range of open tools. We'll get to that later.
Recording the desktop
Recording the desktop is fairly easy. We can either record the entire desktop:
recordmydesktop -o /home/todsah/out.ogv
or we can record a single window. For this we need the window id, which you can get with a commandline tool called xwininfo:
$ xwininfo xwininfo: Please select the window about which you would like information by clicking the mouse in that window.
Now we click on the window, and xwininfo will give us the window ID:
xwininfo: Window id: 0x9c00003 "[root@eek]/home/fboender$"
Now we can tell recordmydesktop that we want to record just that window by giving it the window ID:
recordmydesktop --windowid 0x9c00003 -o /home/todsah/out.ogv
Once you're done recording, simply press ctrl-c in the recordmydesktop terminal, and it will start encoding your recording to out.ogv.
Sound issues with PulseAudio
Okay, so far so good, except for the sound, which is simply missing. We can see that recordmydesktop did record some kind of sound by looking at the output of the encoding:
Done. Written 1967932 bytes (1845110 of which were video data and 122822 audio data)
So why isn't there any sound in the video? The problem is PulseAudio (again and again and again). PulseAudio acts as a layer between all the applications, all the different sound architectures (ALSA, OSS, etc) and between the hardware. In my case, the problem was that recordmydesktop was recording from the incorrect device. Of course the default Sound Preference of Ubuntu 10.04 didn't list any Recording devices, which is why it took me so long to find out what the problem was. To fix this, install the PulseAudio Volume Controller:
# aptitude install pavucontrol
You will now get a PulseAudio Volume Controller in the menu:
Start it up, and select the 'Recording' tab:
Now, make sure you've already started recording your desktop, or the application that's trying to record sound will NOT show up in this list! You should also start playing a song or generate some other audio so it's easier to spot when things are working. As you can see, it shows per application where that application is recording sound from. Select the source it will record from by clicking on the button that says 'Monitor of Internal Audio Analog Stereo'. A list will appear with options. Try each option until the volume bar below the application comes to life. I have not yet experimented with microphones, but I wouldn't be surprised if this was also the place to select that as a recording source.
recordmydesktop will now probably record sound also. You may have to tell it which channel to record sound from. This command at least worked for me:
recordmydesktop -o /home/todsah/out.ogv --fps 15 --channels 1 --freq 22050 --device pulse --v_quality 63 --s_quality 10 --workdir /tmp
You can also configure 'pulse' in the GTK front-end for recordmydesktop by going to Advanced → Sound → Device. Enter 'pulse' there.
Converting to other formats
Converting to other formats is, frankly, a real pain in the ass. There are basically three applications which you can use:
The main problem with encoding is codecs. A movie contains video and audio, and both of those are basically stuffed into a single file. Such a file is called a container. Examples of containers are Ogg, AVI and mp4. The video and audio files in these containers are called streams. The streams are almost always encoded in order to compress them or to allow for additional information to be embedded in the streams. Encoding these streams is done with codecs. And this is where we get in trouble. You see, most codecs are patented, trademarked, copyright and generally completely unsuitable for Free Software usages. This is why mencoder, ffmpeg and vlc usually have trouble with them.
Any of the above programs may not support the input container/codecs (Ogg container, Theora video, Vorbis audio) or may not support the output container/codecs such as the TS/Avi/Ogg/MP4/ASF containers which contain the Theora, Mpeg-4, Mpeg-2, WMV, etc video codecs and the AAC, MP3, etc audio codecs. VLC can usually play just about everything (because it provides many illegal decoding codecs), but it has trouble encoding in many format.
To my great regret, it's up to you to find out which combinatation of containers and codecs works for you. You can check the Medibuntu website for instructions on how to add illegal codecs to your Ubuntu installation.
For me, it seems mencoder works the best. For instance, to convert the out.ogv file to an AVI file:
mencoder -idx out.ogv -ovc lavc -oac mp3lame -o output.avi