Friday, 16 April 2010

Quick and Dirty Webcam monitoring

Just thought I'd mention a very quick and dirty way to get 'webcam monitoring' going. I've done this with axis webcams, but it should work with any webcam that allows you to fetch a jpeg image from a url.

This will, of course, be working under linux. It's only using the standard 'Borne' shell and a program called 'links' so it should work under FreeBSD and Mac OSX too.

The command-line webbrowser, 'links' has an option called -'-source'. This has the effect that a webpage or item fetched using http with links is written in raw format to stdout. So

links -source > TheSwingSet.mp3

Will download the example .mp3 file.

Most axis webcams allow you to download an image from http://<webcamip>/axis-cgi/jpg/image.cgi, so we can use links to Grab an image thusly:

links -source > snapshot.jpg

Of course, what's best is to get images every couple of seconds and put them into a date/timestamped file. Here's the script I've been using, it creates directories for each hour of a day, and puts timestamped .jpg files in them.

#! /bin/sh

cd /home/Public/AxisWebcam

while [ 1 = 1 ]

DIR=`date "+%d_%m_%Y_Hour-%H"`
FILE=`date "+%H_%M_%S.jpg"`

mkdir $DIR
links -source > $DIR/$FILE

sleep 2


One issue is that links doesn't have any way (that I know of) of expressing a username/password to access a website. Fortunately, Axis cameras have a setup option under 'Setup->Users' called 'Enable anonymous viewer login'. This allows access to the image without having to supply a username and password.

If you're going to leave this script running, you may want to periodically run a command like:

find -mtime +7 -exec rm -f {} \;

This command will delete any files older than a week.

And that's it!