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 http://media.libsyn.com/media/djsteveboy/the_swing_set.mp3 > 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 http://192.168.5.4:1000/axis-cgi/jpg/image.cgi > 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 ]
do

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

mkdir $DIR
links -source http://192.168.5.4:1000/axis-cgi/jpg/image.cgi > $DIR/$FILE

sleep 2

done


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!

1 comment: