This guide has been written using Mplayer version 1.0rc1 with a headless machine (without a monitor and/or video card); things could be different with other versions or configurations.
Mplayer is not only a versatile movie’s player but also a very powerful encoder. Webcam can be captured with it and the output is completely customisable: from a jpeg for every frame, to a divx video, or even an event driven capturer…the limit is just your imagination!
First, the source code for the latest version of mplayer/mencoder can be downloaded at mplayer headquarters’
site. In order to use it, an essential set of codecs must be obtained from the same
page. To install them just type
# mkdir /usr/local/lib/codecs/
# cd /usr/local/lib/codecs
# tar xvfj /full/path/essential*.tar.bz2
and there you are. To compile and install mplayer you must do a little more:
$ ./configure
$ make
# make install
(“#” means you need to be root to run the command). The odds are you will get mplayer compiled without any problem.
Remember that if you plan to run mplayer in slave mode so that you can grab any screenshot, libpng has to be installed (before configure is executed) or it won’t work.
Well, mplayer is now installed in your system I suppose. If a webcam is correctly installed and v4l is working then you should be able to do many things:
capture 15 screenshots per second in jpeg format:
$ mplayer -tv driver=v4l:fps=15 -vo jpeg tv://
capture 30 screenshots per second with a custom width & height (the default one is probably too small) :
$ mplayer -tv driver=v4l:fps=15:height=288:width=352 -vo jpeg tv://
(if you set an illegal width and/or height you should probably obatin pink or green garbage)
Capture and encode in a resized mpeg4 video named movie.avi:
$ mencoder -tv driver=v4l:fps=15:height=288:width=352 -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=640:480 -o movie.avi tv://
Capture a frame in a png picture only when you want it:
$ mkfifo /tmp/test.fifo
$ mplayer -slave -quiet -input file=/tmp/test.fifo -vf screenshot -tv driver=v4l tv:// &
Then when you want to capture just type:
$ echo "screenshot 0" >/tmp/test.fifo
Alternatively you can also start a continuos capture by typing:
$ echo "screenshot 1" >/tmp/test.fifo
(type it again to stop capture)
A practical example: an html page containing a refreshing captured picture
I am assuming you already have Apache2 running and that it’s pointing to your filesystem directory /var/www (that is if you put text.html in /var/www/ everyone can see it in http://yoursite.example/text.html).
Write this code in webcam.sh
#!/bin/bash
[ -p /tmp/my.fifo ] && rm /tmp/my.fifo
mkfifo my.fifo
mplayer -slave -quiet -input file=/tmp/my.fifo -vf screenshot,scale=640:480 -tv driver=v4l:width=352:height=288 tv:// &
while true
do
echo "screenshot 0" >/tmp/my.fifo
sleep 10
name=`ls -tr shot*.png | tail -n1`
mv $name /var/www/lastshot.png
sleep 50
done
The delay of 60 seconds is splitted in two parts because we don’t know when the image will be ready to be moved. This solution is not much elegant but it should work for every configuration. Alternatively, with ImageMagick installed you could use something like:
$ identify shot*.png >/dev/null 2>&1; while [ $? -ne 0 ]; do identify shot*.png >/dev/null 2>&1; done
The above command waits until a valid png picture is present in the current directory (man identify to get more info).
Now you need only to create the following page in /var/www/:
<html>
<head>
<meta http-equiv="refresh" content="60" />
<title>Webcam shot every minute!</title>
</head>
<body bgcolor="black" text="white">
<img src="lastshot.png" />
</body>
</html >
Finished! To make it working just type:
$ nohup sh webcam.sh &
and visit http://yoursite.example/thepageyouhavejustcreated.html
Hope this helps!