(For more resources related to this topic, see here.)
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..
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:
Let's see what we can find out about the detection of your webcam, using the following command:
pi@raspberrypi ~ $ 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.
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:
pi@raspberrypi ~ $ 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:
pi@raspberrypi ~ $ 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.
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.
pi@raspberrypi ~ $ sudo apt-get install libjpeg8-dev libv4l-dev
pi@raspberrypi ~ $ 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.
pi@raspberrypi ~ $ 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.
pi@raspberrypi ~ $ cd mjpg-streamer
pi@raspberrypi ~/mjpg-streamer $ make USE_LIBV4L2=true
pi@raspberrypi ~/mjpg-streamer $ sudo make DESTDIR=/usr install
pi@raspberrypi ~/mjpg-streamer $ cd .. && rm -r mjpg-streamer
pi@raspberrypi ~ $ 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.
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:
http://[IP address]:8080/?action=stream.
http://[IP address]:8080/javascript_simple.html
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.
Let's install VLC and open up the webcam stream:
"C:Program Files (x86)PuTTYplink" pi@[IP address] -pw
[password] sox -t alsa plughw:1 -t sox - | "C:Program Files
(x86)sox-14-4-1sox" -q -t sox - -d
Let's install VLC and open up the webcam stream:
ssh pi@[IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox
- -d
Let's install VLC or MPlayer and open up the webcam stream:
ssh pi@[IP address] sox -t alsa plughw:1 -t sox - | sox -q -t sox - -d
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.
We're going to launch VLC from the command line to record our video:
C:> "C:Program Files (x86)VideoLANVLCvlc.exe" http://[IP
address]:8080/?action=stream --sout="#standard{mux=avi,dst=%UserPr
ofile%Desktopmyvideo.avi,access=file}"
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.
We're going to launch VLC from the command line, to record our video:
$ /Applications/VLC.app/Contents/MacOS/VLC http://[IP
address]:8080/?action=stream --sout='#standard{mux=avi,dst=/Users/
[username]/Desktop/myvideo.avi,access=file}'
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.
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
--sout='#standard{mux=avi,dst=/home/[username]/Desktop/myvideo.
avi,access=file}'
Replace [username] with your login name, or remove the directory path to write the video to the current directory.