Capturing webcam video with linux

Written by NickWal

You would like to record the video stream from your USB webcam. Make sure you have the correct kernel drivers installed and then use ffmpeg to capture the video from the video for linux interface.

Linux Drivers for your Webcam

Before capturing video you must make sure your webcam as been recognized by Linux, many modern Linux distributions will load the drivers you need by default, but in case yours doesn’t, you can take a look at my other article “Taking snapshots from a webcam under linux” for some notes on linux kernel drivers.

You can check if your webcam has been recoginised by linux after plugging it in look for files called /dev/video0 or /dev/video1 (if you have more than one) in the file system. Alternatively, using the ‘dmesg‘ command from the bash command line shell you should be able to see initialization messages from the webcam and its drivers

Just a note on situations when you have more than one webcam attached to your computer. There is a bandwidth limit that you may hit with USB. I found that when I had two webcams connected to an external hub which linked into just one USB slot, when I tried to open the video stream from the second camera, with the first one already open I received a confusing error “No space left on device“. I was able to cure this by connecting the two webcams to different slots in the back of the computer and not using the hub.

Grab the video with ffmpeg

Firstly make sure you have ffmpeg and its dependencies correctly installed. Just how you install a packges varies widely for each distribution, so consult your documentation on how to install a package. For example, to install ffmpeg under Gentoo use (from the command line):

emerge ffmpeg

To record video from your web cam, you can open a bash command shell and use the following command.

ffmpeg -an -f video4linux -s 320x240 -r 15 -i /dev/v4l/video0 -vcodec mpeg4 -vtag DIVX /tmp/out.avi

This will save video using mpeg4 encoding (which is the same as DivX) and at the default bandwidth. The default bandwidth may not provide sufficient quality, so you can increase the bandwidth and hence the quality of the encoding using the -b option like this:

ffmpeg -an -f video4linux -s 320x240 -b 600k -r 15 -i /dev/v4l/video0 -vcodec mpeg4 -vtag DIVX /tmp/out.avi