13 min read
(For more resources related to this topic, see here.)
Setting up your camera
Go ahead, plug in your webcam and boot up the Pi; we’ll take a closer look at what makes it tick.
If you experimented with the dwc_otg.speed parameter to improve the audio quality during the previous article, you should change it back now by changing its value from 1 to 0, as chances are that your webcam will perform worse or will not perform at all, because of the reduced speed of the USB ports..
Meet the USB Video Class drivers and Video4Linux
Just as the Advanced Linux Sound Architecture (ALSA) system provides kernel drivers and a programming framework for your audio gadgets, there are two important components involved in getting your webcam to work under Linux:
- The Linux USB Video Class (UVC) drivers provide the low-level functions for your webcam, which are in accordance with a specifcation followed by most webcams produced today.
- Video4Linux (V4L) is a video capture framework used by applications that record video from webcams, TV tuners, and other video-producing devices. There’s an updated version of V4L called V4L2, which we’ll want to use whenever possible.
Let’s see what we can find out about the detection of your webcam, using the following command:
[email protected] ~ $ dmesg
The dmesg command is used to get a list of all the kernel information messages that of messages, is a notice from uvcvideo.
Kernel messages indicating a found webcam
In the previous screenshot, a Logitech C110 webcam was detected and registered with the uvcvideo module. Note the cryptic sequence of characters, 046d:0829, next to the model name. This is the device ID of the webcam, and can be a big help if you need to search for information related to your specifc model.
Finding out your webcam’s capabilities
Before we start grabbing videos with our webcam, it’s very important that we find out exactly what it is capable of in terms of video formats and resolutions. To help us with this, we’ll add the uvcdynctrl utility to our arsenal, using the following command:
[email protected] ~ $ sudo apt-get install uvcdynctrl
Let’s start with the most important part—the list of supported frame formats. To see this list, type in the following command:
[email protected] ~ $ uvcdynctrl -f
List of frame formats supported by our webcam
According to the output of this particular webcam, there are two main pixel formats that are supported. The first format, called YUYV or YUV 4:2:2, is a raw, uncompressed video format; while the second format, called MJPG or MJPEG, provides a video stream of compressed JPEG images.
Below each pixel format, we find the supported frame sizes and frame rates for each size. The frame size, or image resolution, will determine the amount of detail visible in the video. Three common resolutions for webcams are 320 x 240, 640 x 480 (also called VGA), and 1024 x 768 (also called XGA).
The frame rate is measured in Frames Per Second (FPS) and will determine how “fuid” the video will appear. Only two different frame rates, 15 and 30 FPS, are available for each frame size on this particular webcam.
Now that you know a bit more about your webcam, if you happen to be the unlucky owner of a camera that doesn’t support the MJPEG pixel format, you can still go along, but don’t expect more than a slideshow of images of 320 x 240 from your webcam. Video processing is one of the most CPU-intensive activities you can do with the Pi, so you need your webcam to help in this matter by compressing the frames first.
Capturing your target on film
All right, let’s see what your sneaky glass eye can do!
We’ll be using an excellent piece of software called MJPG-streamer for all our webcam capturing needs. Unfortunately, it’s not available as an easy-to-install package for Raspbian, so we will have to download and build this software ourselves.
Often when we compile software from source code, the application we’re building will want to make use of code libraries and development headers. Our MJPG-streamer application, for example, would like to include functionality for dealing with JPEG images and Video4Linux devices.
- Install the libraries and headers for JPEG and V4L by typing in the following command:
[email protected] ~ $ sudo apt-get install libjpeg8-dev libv4l-dev
- Next, we’re going to download the MJPG-streamer source code using the following command:
[email protected] ~ $ wget http: // mjpg-streamer.svn.sourceforge.net/
viewvc/mjpg-streamer/mjpg-streamer/?view=tar -O mjpg-streamer.tar.gz
The wget utility is an extraordinarily handy web download tool with many uses. Here we use it to grab a compressed TAR archive from a source code repository, and we supply the extra -O mjpg-streamer.tar.gz to give the downloaded tarball a proper filename.
- Now we need to extract our mjpg-streamer.tar.gz article, using the following command:
[email protected] ~ $ tar xvf mjpg-streamer.tar.gz
The tar command can both create and extract archives, so we supply three fags here: x for extract, v for verbose (so that we can see where the files are being extracted to), and f to tell tar to use the article we specify as input, instead of reading from the standard input.
- Once you’ve extracted it, enter the directory containing the sources:
[email protected] ~ $ cd mjpg-streamer
- Now type in the following command to build MJPG-streamer with support for V4L2 devices:
[email protected] ~/mjpg-streamer $ make USE_LIBV4L2=true
- Once the build process has finished, we need to install the resulting binaries and other application data somewhere more permanent, using the following command:
[email protected] ~/mjpg-streamer $ sudo make DESTDIR=/usr install
- You can now exit the directory containing the sources and delete it, as we won’t need it anymore:
[email protected] ~/mjpg-streamer $ cd .. && rm -r mjpg-streamer
- Let’s fre up our newly-built MJPG-streamer! Type in the following command, but adjust the values for resolution and frame rate to a moderate setting that you know (from the previous section) that your webcam will be able to handle:
[email protected] ~ $ mjpg_streamer -i "input_uvc.so -r 640x480 -f
30" -o "output_http.so -w /usr/www"
MJPG-streamer starting up
You may have received a few error messages saying Inappropriate ioctl for device; these can be safely ignored. Other than that, you might have noticed the LED on your webcam (if it has one) light up as MJPG-streamer is now serving your webcam feed over the HTTP protocol on port 8080. Press Ctrl + C at any time to quit MJPG-streamer.
- To tune into the feed, open up a web browser (preferably Chrome or Firefox) on a computer connected to the same network as the Pi and enter the following line into the address field of your browser, but change [IP address] to the IP address of your Pi. That is, the address in your browser should look like this: http://[IP address]:8080.
You should now be looking at the MJPG-streamer demo pages, containing a snapshot from your webcam.
MJPG-streamer demo pages in Chrome
The following pages demonstrate the different methods of obtaining image data from your webcam:
- The Static page shows the simplest way of obtaining a single snapshot frame from your webcam. The examples use the URL http://[IP address]:8080/?action=snapshot to grab a single frame. Just refresh your browser window to obtain a new snapshot. You could easily embed this image into your website or blog by using the HTML tag, but you’d have to make the IP address of your Pi reachable on the Internet for anyone outside your local network to see it.
- The Stream page shows the best way of obtaining a video stream from your webcam. This technique relies on your browser’s native support for decoding MJPEG streams and should work fne in most browsers except for Internet Explorer. The direct URL for the stream is
- The Java page tries to load a Java applet called Cambozola, which can be used as a stream viewer. If you haven’t got the Java browser plugin already installed, you’ll probably want to steer clear of this page. While the Cambozola viewer certainly has some neat features, the security risks associated with the plugin outweigh the benefits of the viewer.
- The VideoLAN page contains shortcuts and instructions to open up the webcam video stream in the VLC media player. We will get to know VLC quite well during this article; leave it alone for now.
- The Control page provides a convenient interface for tweaking the picture settings of your webcam. The page should pop up in its own browser window so that you can view the webcam stream live, side-by-side, as you change the controls.
Viewing your webcam in VLC media player
You might be perfectly content with your current webcam setup and viewing the stream in your browser; for those of you who prefer to watch all videos inside your favorite media player, this section is for you. Also note that we’ll be using VLC for other purposes further in this article, so we’ll go through the installation here.
Viewing in Windows
Let’s install VLC and open up the webcam stream:
- Visit http://www.videolan.org/vlc/download-windows.html and download the latest version of the VLC installer package(vlc-2.0.5-win32.exe, at the time of writing).
- Install VLC media player using the installer.
- Launch VLC using the shortcut on the desktop or from the Start menu.
- From the Media drop-down menu, select Open Network Stream….
- Enter the direct stream URL we learned from the MJPG-streamer demo pages (http://[IP address]:8080/?action=stream), and click on the Play button.
- (Optional) You can add live audio monitoring from the webcam by opening up a command prompt window and typing in the following command:
"C:Program Files (x86)PuTTYplink" [email protected][IP address] -pw
[password] sox -t alsa plughw:1 -t sox - | "C:Program Files
(x86)sox-14-4-1sox" -q -t sox - -d
Viewing in Mac OS X
Let’s install VLC and open up the webcam stream:
- Visit http://www.videolan.org/vlc/download-macosx.html and download the latest version of the VLC dmg package for your Mac model. The one at the top, vlc-2.0.5.dmg (at the time of writing), should be fne for most Macs.
- Double-click on the VLC disk image and drag the VLC icon to the Applications folder.
- Launch VLC from the Applications folder.
- From the File drop-down menu, select Open Network….
- Enter the direct stream URL we learned from the MJPG-streamer demo pages (http://[IP address]:8080/?action=stream) and click on the Open button.
- (Optional) You can add live audio monitoring from the webcam by opening up a Terminal window (located in /Applications/Utilities ) and typing in the following command:
ssh [email protected][IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox
Viewing on Linux
Let’s install VLC or MPlayer and open up the webcam stream:
- Use your distribution’s package manager to add the vlc or mplayer package.
- For VLC, either use the GUI to Open a Network Stream or launch it from the command line with vlc http://[IP address]:8080/?action=stream
- For MPlayer, you need to tag on an MJPG article extension to the stream, using the following command: mplayer “http://[IP address]:8080/?action= stream&stream.mjpg”
- (Optional) You can add live audio monitoring from the webcam by opening up a Terminal and typing in the following command:
ssh [email protected][IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox - -d
Recording the video stream
The best way to save a video clip from the stream is to record it with VLC, and save it into an AVI article container. With this method, we get to keep the MJPEG compression while retaining the frame rate information.
Unfortunately, you won’t be able to record the webcam video with sound. There’s no way to automatically synchronize audio with the MJPEG stream. The only way to produce a video article with sound would be to grab video and audio streams separately and edit them together manually in a video editing application such as VirtualDub.
Recording in Windows
We’re going to launch VLC from the command line to record our video:
- Open up a command prompt window from the Start menu by clicking on the shortcut or by typing in cmd in the Run or Search fields. Then type in the following command to start recording the video stream to a article called myvideo.avi, located on the desktop:
C:> "C:Program Files (x86)VideoLANVLCvlc.exe" http://[IP
As we’ve mentioned before, if your particular Windows version doesn’t have a C:Program Files (x86) folder, just erase the (x86) part from the path, on the command line.
- It may seem like nothing much is happening, but there should now be a growing myvideo.avi recording on your desktop. To confirm that VLC is indeed recording, we can select Media Information from the Tools drop-down menu and then select the Statistics tab. Simply close VLC to stop the recording.
Recording in Mac OS X
We’re going to launch VLC from the command line, to record our video:
- Open up a Terminal window (located in /Applications/Utilities) and type in the following command to start recording the video stream to a file called myvideo.avi, located on the desktop:
$ /Applications/VLC.app/Contents/MacOS/VLC http://[IP
Replace [username] with the name of the account you used to log in to your Mac, or remove the directory path to write the video to the current directory.
- It may seem like nothing much is happening, but there should now be a growing myvideo.avi recording on your desktop. To confirm that VLC is indeed recording, we can select Media Information from the Window drop-down menu and then select the Statistics tab. Simply close VLC to stop the recording.
Recording in Linux
We’re going to launch VLC from the command line to record our video:
Open up a Terminal window and type in the following command to start recording the video stream to a file called myvideo.avi, located on the desktop:
$ vlc http://[IP address]:8080/?action=stream
Replace [username] with your login name, or remove the directory path to write the video to the current directory.