Using a Tenvis IP webcam to make a internet webcam

How to make a webcam and basic site with timelapse mp4 creation using an ip camera and a virtual machine.

Background

Cron runs a script every 5 mins to grab a frame from the snapshot URL on the webcam. This is archived and processed according to the time of day to make a daily snapshot overview and as the basis of the mp4 creation.

root@com-test-webcam01:~# crontab -l
# m h  dom mon dow   command

# grab weathercam pics every 10 mins
*/5 * * * *   /var/www/scripts/sitecam.sh
root@com-test-webcam01:~#

This is located in /var/www/scripts/sitecam.sh for no better reason than it is in the apache serving directory:-

root@com-test-webcam01:~# cat /var/www/scripts/sitecam.sh
#!/bin/bash

#written Andrew Stringer, 19/07/2015 onwards
#Modified AJS 01/10/2016 for FMTS building site camera.
#intended to be run once every 5 minutes from crontab.

# grab weathercam pics every 10 mins
#*/5 * * * *   /var/www/scripts/sitecam.sh

OUTPUT='/tmp/SITECAM.log'

echo "Starting at:- " `date` > ${OUTPUT}

SITECAM='10.10.100.239'
PICPATH='/var/www/html/cam'
TODPATH='/var/www/html/timeofday'
ARCHIVE='/var/www/html/archive'
TIMELAPSE='/var/www/html/timelapse'
FRAMERATE=2


#seconds past epoch
FILESEQ=`date +%s`

#delete jpg picture files older than 24h, but not mpg files ( change -ls to -delete)

echo "Deleting old files" >> ${OUTPUT}
for UPLOADFILE in `find ${PICPATH}  -mmin +$((60*24))  -type f`;
        do mv ${UPLOADFILE} ${ARCHIVE}
done


#Capture pic from webcam
cd ${PICPATH}
wget -q -P ${PICPATH} http://${SITECAM}/snapshot.cgi?user=snapshot\&pwd=snapshot -O ${PICPATH}/currentsnapshot.jpg 2>&1 >> ${OUTPUT}

/usr/bin/convert -pointsize 20 -fill magenta -draw "text 270,460 \"`date`\"" ${PICPATH}/currentsnapshot.jpg ${PICPATH}/SITECAM-${FILESEQ}.jpg 2>&1 >> ${OUTPUT}

chown www-data:www-data ${PICPATH}/currentsnapshot.jpg
chown www-data:www-data ${PICPATH}/SITECAM-${FILESEQ}.jpg


#copy capture to time of day, midnight, 6am, 12 noon, 6pm,
#Don't call these "SITECAM-anything" so mp4 encode won't see them.
TIMEOFDAY=`date +%H%M`
case ${TIMEOFDAY} in
    0600)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TODPATH}/timeofday-0600.jpg
        chown www-data:www-data ${TODPATH}/timeofday-0600.jpg
        ;;
    0900)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TODPATH}/timeofday-0900.jpg
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}
        chown www-data:www-data ${TODPATH}/timeofday-0900.jpg
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1000)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1100)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1200)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TODPATH}/timeofday-1200.jpg
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TODPATH}/timeofday-1200.jpg
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1300)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1400)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1500)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TODPATH}/timeofday-1500.jpg
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TODPATH}/timeofday-1500.jpg
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1600)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1700)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        ;;
    1800)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TIMELAPSE}/
        chown www-data:www-data ${TIMELAPSE}/SITECAM-${FILESEQ}.jpg
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${TODPATH}/timeofday-1800.jpg
        chown www-data:www-data ${TODPATH}/timeofday-1800.jpg
        ;;
    0000)
        cp ${PICPATH}/SITECAM-${FILESEQ}.jpg ${PICPATH}/timeofday/timeofday-0000.jpg
        chown www-data:www-data ${PICPATH}/timeofday/timeofday-0000.jpg
        cat ${TIMELAPSE}/SITECAM-*.jpg | /usr/bin/ffmpeg -loglevel panic -f image2pipe -r $FRAMERATE -vcodec mjpeg -i - -vcodec libx264 ${TIMELAPSE}/timelapse-tmp.mp4 2>&1 >> ${OUTP
UT}
        mv ${TIMELAPSE}/timelapse-tmp.mp4 ${TIMELAPSE}/timelapse.mp4
        chown www-data:www-data ${TIMELAPSE}/timelapse.mp4
esac




cat ${PICPATH}/SITECAM-*.jpg | /usr/bin/ffmpeg -loglevel panic -f image2pipe -r $FRAMERATE -vcodec mjpeg -i - -vcodec libx264 ${PICPATH}/last24h-tmp.mp4 2>&1 >> ${OUTPUT}

#mv tmp file
mv ${PICPATH}/last24h-tmp.mp4 ${PICPATH}/last24h.mp4

chown www-data:www-data ${PICPATH}/last24h.mp4

echo "Finished at:- " `date` >> ${OUTPUT}
exit 0

root@com-test-webcam01:~# 

Packages to install

This uses convert which is part of libav on Ubuntu, and ffmpeg to create the actual mp4 files. It is probably inefficient for large sized time lapse files as the whole mp4 is recreated each time new frames are added to the video file.


rb/webcamip.txt · Last modified: 06/10/2017 14:44 (external edit)