How I use ffmpeg in Linux to record from Macbook Pro iSight

I have an older Macbook Pro (version 5,5) and I recently got screencasting working again after about a year in which nothing worked. There are two steps to getting a usable video output. First I needed to get audio recording working properly, then I needed to get the video to grab without dropping all the frames. Once I got it working I wrote it into a tiny little script that I’ve pasted below.

First came audio recording issues. Though much trial and error and cargo-cult parameter setting, I discovered the right incantations to make. First, the sound module needs to be loaded with the mbp55 setting. In /etc/modprobe.d/snd-hda-intel.conf I have:

options snd-hda-intel model=mbp55 power_save=1

Next, in alsamixer, I had to make sure that recording wasn’t muted. Fire up alsamixer, press f4, right-arrow over to the “capture” value and press the spacebar. I always thought that controls were muted or unmuted by hitting “M”, but in this case, you need to press “space” in order to toggle recording. What you should see is a red “CAPTURE” written underneath the “Capture” input slider, as shown in the screen capture below.

Make sure the middle slider has a red "CAPTURE" under it

To test if recording works, I used arecord -d 10 -N out.wav and made some noises for 10 seconds. It worked, so on to the next step.

To capture video I used an older command line I had for ffmpeg, but it didn’t work for me at first. What I originally had was a slightly modified version of what is in the ffmpeg documentation

ffmpeg -f alsa -ac 2 -i hw:0,0 -f video4linux2 -i /dev/video0 /tmp/out.mkv

The problem with this is that it dropped most of the frames. In a few seconds of recording, I would see maybe one or two frames. The sound recording was fine, but the ffmpeg output just blathered on about dropped frame counts.

I poked around in the docs and found a solution that works. In a nutshell, I specify the frame rate and screen size based on the characteristics of the iSight camera. I also specify using the x264 library with the ultrafast preset, in order to encode with a decent library and as fast as possible.

The final script I use is as follows:

#!/bin/sh
# simple command to capture video from iSight and audio from ALSA

OUTPUT="${1:?[usage] ffmpeg_camgrab.sh OUTPUTFILE}"
ffmpeg -f alsa -ac 2  -i hw:0,0 \
    -f v4l2 -r 25 -s 640x480 -i /dev/video0  \
    -vcodec libx264 \
    -preset ultrafast \
    -threads 2 \
    -y "${OUTPUT}"

I use it like so:

./ffmpeg_camgrab.sh out.mkv

and I marvel at how the video is out of sync with the audio, and wonder how to fix that. But at least I have video and audio to process, so that’s something.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s