Streaming a web cam in Ubuntu

It’s pretty easy to get a webcam to stream in Ubuntu, or indeed any Debian distribution. In this example I’m using Ubuntu 10.04 (Karmic) and a Microsoft Lifecam VX-1000 web cam.

First get root access with the usual command.

$ sudo bash
[sudo] password for user: *********

Next open up a terminal window and install webcam-server as follows:

$ apt-get install webcam-server

Now go to the /dev directory and list the contents. From your home directory type:

$ cd ..
$ cd ..
$ cd /dev
$ ls

You’ll now see a list of files that will look similar to this:

input            ram11               tty32  vcsa4
kmsg             ram12               tty33  vcsa5
log              ram13               tty34  vcsa6
loop0            ram14               tty35  vcsa7
loop1            ram15               tty36  tty7   vcsa8
loop2            ram2                tty37  tty8   zero
loop3            ram3                tty38  tty9
loop4            ram4                tty39  ttyS0

Now plug in your webcam into one of the USB ports, wait a couple of seconds then type the same command again:

$ ls

You should see another file has appeared that starts with video. Mine’s shown up as video0 as it’s the first video device connected to the system. If you have an integrated webcam, subsequent webcam handles will be incremented, i.e. video1, video2 etc..

Now to start the webcam-server daemon, type the following:

$ webcam-server -d /dev/video0

If it’s working, you’ll just see the cursor continue to blink. To confirm that the daemon is running, go to http://localhost:8888. You should see a JPEG image with a timestamp, showing what your webcam can see.

If not, check the terminal for errors.

libv4lconvert: Error decompressing JPEG: fill_nbits error: need 6 more bits

If you see the above message or similar, you’ll need to try and run your webcam in compatibility mode, as follows:

$ LD_PRELOAD=/usr/lib/libv4l/  webcam-server -d /dev/video0

Now check http://localhost:8888 again, and all things being well, you’ll see the JPEG snapshot. (if not, check the Ubuntu forums to check that your device is supported)

Now as that’s not strictly speaking a stream, there’s one last thing to do.

$ cp /usr/share/doc/webcam-server/applet/* /var/www/

This will copy some files, including the Java streaming applet, to your web accessible directory, /var/www. Now if you go to http://localhost/webcam.html, you’ll see an applet load, followed by a live stream from your webcam.

Note: The useful thing about this, is that you can run as many daemons as your system can handle, so you can plug in a number of webcams to the same system and with some additional webcam-server switches, you can assign each camera to a different port.