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.
mjpg-streamer.tar.gz
file, using the following command:pi@raspberrypi ~ $ tar xvf mjpg-streamer.tar.gz
The tar
command can both create and extract archives, so we supply three flags 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 file 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"
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.
[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.
The following pages demonstrate the different methods of obtaining image data from your webcam:
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 <img src="http://[IP address]:8080/?action=snapshot"/>
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.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 chapter, so we'll go through the installation here.
Let's install VLC and open up the webcam stream:
vlc-2.0.5-win32.exe
, at the time of writing).http://[IP address]:8080/?action=stream
), and click on the Play button."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:
vlc-2.0.5.dmg
(at the time of writing), should be fine for most Macs.Applications
folder.Applications
folder.http://[IP address]:8080/?action=stream
) and click on the Open button./Applications/Utilities
) and typing in the command line we learned from the Listening in on conversations from a distance section in Chapter 2, Audio Antics: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:
vlc
or mplayer
package.vlc http://[IP address]:8080/?action=stream
mplayer "http://[IP address]:8080/?action=stream&stream.mjpg"
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 file 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 file 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:
cmd
in the Run or Search fields. Then type in the following command to start recording the video stream to a file called myvideo.avi
, located on the desktop:C:> "C:Program Files (x86)VideoLANVLCvlc.exe" http://[IP address]:8080/?action=stream --sout="#standard{mux=avi,dst=%UserProfile%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.
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.We're going to launch VLC from the command line, to record our video:
/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 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.
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.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.
18.117.146.155