Webcam Project with Time-lapse
From Initq
I had two Internet cameras that i wanted to use for real time access so i can see what is going on outside my apartment. The webcams I had were Internet and self contained OS ready cams. The web cams had a web server running on it and the only interaction you can have with the web can was through their http interface. This would cause security and limited access issue if i were to open that to the public. I started looking at other ways so i decided to pull the still images from the web cam and make my own interface to show still images and eventually show a time-lapse for a video effect that everyone can enjoy without compromising any security.
Contents |
Get info from web cams
I did not know how the web cam was saving images because there was no other way to get to the cam apart from using http. I could have tried, viewcode on my browser and gone through that but i decided to use wget. Here is what i did:
[root@initq junk]# wget -r http://leech:deleted_pass@192.168.1.21/ --2010-03-28 10:18:32-- http://leech:*password*@192.168.1.21/ Connecting to 192.168.1.21:80... connected. HTTP request sent, awaiting response... 401 Authorization Required Connecting to 192.168.1.21:80... connected. HTTP request sent, awaiting response... 200 OK Length: 456 [text/html] Saving to: `192.168.1.21/index.html' 100%[==========================================================================>] 456 --.-K/s in 0s 2010-03-28 10:18:33 (15.0 MB/s) - `192.168.1.21/index.html' saved [456/456] <..deleted..> 2010-03-28 10:18:44 (111 KB/s) - `192.168.1.21/Home.jpg' saved [1329/1329] --2010-03-28 10:18:44-- http://leech:*password*@192.168.1.21/xplug.class Connecting to 192.168.1.21:80... connected. HTTP request sent, awaiting response... 200 OK Length: 13810 (13K) [application/octet-stream] Saving to: `192.168.1.21/xplug.class' 100%[==========================================================================>] 13,810 --.-K/s in 0.04s 2010-03-28 10:18:44 (308 KB/s) - `192.168.1.21/xplug.class' saved [13810/13810] FINISHED --2010-03-28 10:18:44-- Downloaded: 15 files, 100K in 0.6s (174 KB/s) [root@initq junk]# ls 192.168.1.21/ [root@initq junk]# cd 192.168.1.21/ [root@initq 192.168.1.21]# ls Activex.jpg Config.jpg Home.jpg Java.jpg Title.htm Aview.htm Device.jpg IMAGE.JPG?cidx=2010328101835662 Jview.htm Top.htm Centerma.jpg Home.htm index.html Logo.jpg xplug.class
Now i knew that the file i needed was IMAGE.JPG. So i had my wget statement.
- WEBCAM1=`/usr/bin/wget http://leech:hidden_pass@192.168.1.21/IMAGE.JPG -O IMAGE1.JPG`
Saving Image to Directories
Now that i knew where the image was, the next step was to save it in the proper place so i can get to it. The image had to have a nice time stamp all the way down to seconds.
YMD=`date "+%Y%m%d"` HOUR="hour_`date "+%H"`" MINUTE=`date "+%M%S"`
I wanted to create nice folders to keep all images separate. So i did:
if [ ! -d $YMD/$HOUR ]; then /bin/mkdir -p $YMD/$HOUR fi #Get Cam1 file from web cam. $WEBCAM1 if [ ! -a $WEBCAM_DIR/$CAM1_FILE$YMD.gif ] then touch $WEBCAM_DIRr/$CAM1_FILE.gif fi /bin/mv $WEBCAM_DIR/IMAGE1.JPG $WEBCAM_DIR/$YMD/$HOUR/cam1_$MINUTE.jpg
Now we have our images in the proper directories so we cron the job to run every minute.
webcam.sh
#!/bin/sh # $Id: lexiana.webcam,v 1.1 20100322 # Run this script using the following crontab entry on lexiana.com # * * * * * cd /var/www/html/webcam; /qasket/backup/scripts/webcam.sh > /dev/null 2>&1 # create folder qasket.com WEBCAM_DIR=/var/www/html/webcam YMD=`date "+%Y%m%d"` HOUR="hour_`date "+%H"`" MINUTE=`date "+%M%S"` WEBCAM1=`/usr/bin/wget http://leech:_deleted_pass@192.168.1.21/IMAGE.JPG -O IMAGE1.JPG` WEBCAM2=`/usr/bin/wget http://leech:_deleted_pass@192.168.1.22/IMAGE.JPG -O IMAGE2.JPG` if [ ! -d $YMD/$HOUR ]; then /bin/mkdir -p $YMD/$HOUR fi #Get Cam1 file from web cam. $WEBCAM1 /bin/mv $WEBCAM_DIR/IMAGE1.JPG $WEBCAM_DIR/$YMD/$HOUR/cam1_$MINUTE.jpg #Get Cam2 file from web cam. $WEBCAM2 /bin/mv $WEBCAM_DIR/IMAGE2.JPG $WEBCAM_DIR/$YMD/$HOUR/cam2_$MINUTE.jpg
webcam.sh cron job
* * * * * cd /var/www/html/webcam; /qasket/backup/scripts/webcam.sh > /dev/null 2>&1
Making Time-Lapse
#!/bin/sh # $Id: lexiana.fullday,v 1.1 20100327 # Run this script using the following crontab entry on lexiana.com # 01 2 * * * /usr/local/sbin/fullday_cam # create folder qasket.com WEBCAM_DIR=/var/www/html/webcam TEMP_FOLDER=/var/www/html/webcam/temp TEMP_FOLDER2=/var/www/html/webcam/temp2 CAM1_FILE=cam1_fullday_ YESTERDAY_YMD=`date --date "yesterday" "+%Y%m%d"` YMD=`date "+%Y%m%d"` HOUR="hour_`date "+%H"`" MINUTE=`date "+%M%S"` MKDIR=/bin/mkdir COPY=/bin/cp REMOVE=/bin/rm CONVERT=/usr/bin/convert JOIN=/usr/bin/gifsicle if [ ! -a $TEMP_FOLDER ] then $MKDIR $TEMP_FOLDER fi if [ ! -a $TEMP_FOLDER2 ] then $MKDIR $TEMP_FOLDER2 fi cd $WEBCAM_DIR/$YESTERDAY_YMD for i in {0..23} do directory=`printf hour_%02d $i` echo $directory $COPY $WEBCAM_DIR/$YESTERDAY_YMD/$directory/cam1*.jpg $TEMP_FOLDER $CONVERT $TEMP_FOLDER/*.jpg $TEMP_FOLDER2/$directory.gif $REMOVE $TEMP_FOLDER/* done $JOIN --merge --loop --disposal=previous $TEMP_FOLDER2/*.gif > $WEBCAM_DIR/cam1day_$YESTERDAY_YMD.gif $REMOVE $TEMP_FOLDER2/* for i in {0..23} do directory=`printf hour_%02d $i` echo $directory $COPY $WEBCAM_DIR/$YESTERDAY_YMD/$directory/cam2*.jpg $TEMP_FOLDER $CONVERT $TEMP_FOLDER/*.jpg $TEMP_FOLDER2/$directory.gif $REMOVE $TEMP_FOLDER/* done $JOIN --merge --loop --disposal=previous $TEMP_FOLDER2/*.gif > $WEBCAM_DIR/cam2day_$YESTERDAY_YMD.gif $REMOVE $TEMP_FOLDER2/* $REMOVE -rf $TEMP_FOLDER2 $TEMP_FOLDER find $WEBCAM_DIR/2010* -mtime +10 -exec rm -rf {} \; find $WEBCAM_DIR/cam* -mtime +10 -exec rm -rf {} \;
Finished Project
Finished project is located at: