Recording Lectures on Ubuntu using ffmpeg, sox and xdotool

For the 3 lecture courses I taught at SIIT in 2010 I recorded the audio of each lecture. The procedure was quite easy: connect the Record Output of the lecture room amplifier into my laptop and record using Audacity. Then I would save the entire recording in FLAC format, as well as make a 32kb/s MP3 available for students on my course website. I also used a simple script to record the timing of each slide, with the intention of using it to created an audio slideshow on the website allowing students to replay the lecture. However, I never finished the code for the web-based slideshow (although I've now made videos of the lectures using dvd-slideshow). This year I've simplified the recording of the audio and slide timing, as well as recorded a screencast. Below are the steps I use.

Recording the Lecture

I have a single script that records the audio, screencast and timing. First, the audio is recording using sox, producing a FLAC file using mono, 44100Hz and 16-bit encoding.
$ rec -q -r 44100 -b 16 -c 1 audio.flac
The screencast is recorded using ffmpeg, producing a MPEG (H.264) file with 1024x768 frames, 2 frames per second. With my laptop during a lecture I extend my desktop to the second video output (i.e. the projector). On my laptop screen resolution, which is 1024x600, I display a clock and other stuff for myself. This means what is displayed on the projector (and what I want to record) is offset of 1024x0 pixels (since my laptop screen has a width of 1024 pixels). Hence the +1024,0 offset option below.
$ ffmpeg -v 0 -f x11grab -r 2 -s 1024x768 -i :0.0+1024,0 -sameq \
-vcodec libx264 -vpre lossless_ultrafast -threads 0 screen.m4v
I record the timing of the presentation (i.e. the time spent on each slide) by using a Bash shell script to move between slides in the presentation. Using the script, pressing the f key moves forward a slide, and b back. Each time a key is pressed the time (since the start of the presentation) is recorded, as well as the slide number. How do I use a shell script to progress the presentation? I use xdotool to emulate the clicking of the left mouse button on the PDF presentation (and right-click for back). The complete script, including starting audio and screen recording, is here. When I start the lecture, I run the script and simply press f, b and a few other keys to give the presentation.

Distributing the Lecture

To make the lecture available to students I convert into both WebM (Firefox/Chrome) and H.264 (IE/Safari) for viewing via the HTML5 video tag. For WebM, ffmpeg is used directly:
$ ffmpeg -i screen.m4v -vcodec libvpx -r 2 -s 640x480 -i audio.flac \
-acodec libvorbis -ab 32000 -ac 1 video.webm
(Ogg Theora is another alternative I considered. Same as above command except video codec is theora). For H.264, the audio needs to be converted from FLAC to AAC. My current install of ffmpeg doesn't support AAC encoding, so instead I use FAAC directly:
$ flac -d audio.flac 
$ faac -b 32 audio.wav
$ ffmpeg -i screen.m4v -vcodec copy -i audio.aac -acodec copy video.mp4
I've used Handbrake to reduce the resolution of the MP4 file to 640x480 for web display. Finally, I post the video files on website using the video tag, e.g.:
<video controls>
   <source src='video.webm' type='video/webm; codecs="vp8.0, vorbis"'>
   <source src='video.m4v' type='video/mp4; codecs="avc1.4D401E, mp4a.40.2"'>
One last thing - make sure the web server understands the above MIME formats. On my Apache webserver I adding the following lines to the /etc/apache2/httpd.conf file:
AddType video/ogg .ogv
AddType video/mp4 .mp4 .m4v
AddType video/webm .webm
AddType audio/mp3 .mp3