Zoneminder rotating Webcam images.

Note: I’ve made an entire new system running from a database that can accommodate multiple rotating outputs, because I liked seeing the webcams so much on my tv along with my own cctv. I leave this here as an idea script. btw, there was an update for 3 monitors based on the below that used this very script, but I’m going to jump straight to the database one as it’s much better.

I had the idea to include some public webcams onto my zoneminder monitor, I’m already running xlib_shm so was considering find a way within that code to pickup some jpegs and display them in one area. However this seemed overly complicated, it seemed a much simpler idea to create a new monitor in zoneminder that picks up from a local file and just add that monitor onto the xlib_shm line in the xinitrc file so it displays on bootup.

So the steps.
1) create a new directory /var/cache/zoneminder/webcams
2) create a text doc to hold the url’s /var/cache/zoneminder/webcams/urls.txt for example “http://www.ceredigion.gov.uk/english/visiting/aber_webcam/img/994.jpg” without “
3) create a text doc ~/webcams.sh with the following :
#!/bin/bash
DIR=/var/cache/zoneminder/webcams
PULLINTERVAL=300
ROTATEINTERVAL=13
CHECKINTERVAL=300
NEXTPULL=0
NEXTROTATE=0
NEXTXLIBCHECK=0
CURRENTIMAGE=1
echo Directory : $DIR
echo Pull Interval : $PULLINTERVAL
echo Rotate Interval : $ROTATEINTERVAL
cd $DIR
geturls()
{
FILENO=0
for i in `cat $DIR/urls.txt`
do
   FILENO=$[$FILENO+1]
   wget -q $i -O $FILENO.jpg
done
NEXTPULL=$[$DATE+$PULLINTERVAL]
}
rotateimage()
{
convert $CURRENTIMAGE.jpg -resize 640x480! rotate.jpg
if [ "$CURRENTIMAGE" -eq "$FILENO" ]; then
  CURRENTIMAGE=1
else
  CURRENTIMAGE=$[$CURRENTIMAGE+1]
fi
NEXTROTATE=$[$DATE+$ROTATEINTERVAL]
}
checkxlib()
{
PGREPXLIB=`pgrep xlib_shm`
 if [ -z $PGREPXLIB ]; then
   startx &
 fi
NEXTXLIBCHECK=$[$DATE+$CHECKINTERVAL]
}
while true; do
DATE=`date +%s`
   if [ "$DATE" -gt "$NEXTPULL" ]; then
   geturls
   fi
   if [ "$DATE" -gt "$NEXTROTATE" ]; then
   rotateimage
   fi
   if [ "$DATE" -gt "$NEXTXLIBCHECK" ]; then
   checkxlib
   fi
sleep 2
done
4) chmod +x ~/webcams.sh
5) Now add a new monitor to Zoneminder pulling from a local file /var/cache/zoneminder/webcams/rotate.jpg with dimensions 640×480.
6) After testing that this script runs while I’m connected via putty, I add the following to /etc/init.d/zoneminder under the startx & ‘/root/webcams.sh &’. That way when the computer starts up and everything is displayed the webcams script is also started and away it rotates.
7) Add the following to /etc/init.d/zoneminder under the stop section where I added kill -9 xlib_shm ‘killall -9 webcams.sh’. Now the script is stopped if zoneminder is stopped.
One thing to note is that restarting zoneminder via the interface is not the same as running /etc/init.d/zoneminder restart. So the script and does not get reset when restarting via the interface.

Leave a Reply

Your email address will not be published. Required fields are marked *