Security Monitoring with IP Cameras
Security Monitoring with IP Cameras
The market offers many choices when it comes to IP cameras. Most people who purchase these devices typically use the bundled software or hardware. This can be convenient but has it limitations. Many of the manufacturers do not support other brands of devices. In this post I’ll be covering how to gather imaging and video directly from IP cameras without the need for the vendors software or hardware solution.
The camera I’ll be demonstrating in this example is a Wansview 720p wireless IP camera.
These devices are a value for the quality imaging they offer. I initially found this site that defined the methods to use to scrape imaging from the cameras, https://www.ispyconnect.com/man.aspx?n=Wansview. However, the URLs they offered did not work for this model.
IP Camera Scraping
After some more digging I found this site, http://marc.merlins.org/perso/linuxha/post_2013-11-10_Reviewing-IP-Webcams-for-Linux-and-Zoneminder_Dlink-DCS900_-Ubnt-Aircam_-Foscam-FI8904W-FI8910W_-FFI9820W_-FI9821W_-Wansview-NCB541W_-and-Zavio-F3210.html. Marc goes into detail about this topic and offered URLs that I was able to validate with the Wansview IP cameras. These URLs worked for me.
Captures a still image
http://<Username>:<Password>@<IP of Camera>/mjpeg/snap.cgi?chn=0
Streams live video
http://<Username>:<Password>@<IP of Camera>/mjpeg/stream.cgi?chn=0
The protocol used is insecure and the URL contains username and password information. I would suggest not placing this on any public facing network. For my test purposes, this will be fine.
This site provided a script for scraping and saving images from IP cameras, https://alvinalexander.com/linux-unix/wget-command-shell-script-example-download-url. I made some changes to the code for my purposes. I run this script on a CRON every 5 minutes and it creates image files with a timestamp in the name.
#!/bin/sh # alvinalexander.com # a shell script used to download a specific url. # this is executed from a crontab entry every day. DIR=/home/local/Videos # wget output file FILE1=IPCam1_`date +"%Y%m%d%H%M"`.jpg FILE2=IPCam2_`date +"%Y%m%d%H%M"`.jpg FILE3=IPCam3_`date +"%Y%m%d%H%M"`.jpg # wget download url URL1=http://<Username>:<Password>@<IP of Camera 1>/mjpeg/snap.cgi?chn=0 URL2=http://<Username>:<Password>@<IP of Camera 2>/mjpeg/snap.cgi?chn=0 URL3=http://<Username>:<Password>@<IP of Camera 3>/mjpeg/snap.cgi?chn=0 cd $DIR wget $URL1 -O $FILE1 wget $URL2 -O $FILE2 wget $URL3 -O $FILE3 exit
Streaming to Video Files
The interval for the scrape probably isn’t suitable for use as a security monitor. So then I found another site that provided a way to save the stream to a video file using VLC in the command line, https://wiki.videolan.org/Documentation:Streaming_HowTo/Receive_and_Save_a_Stream/
cvlc http://<Username>:<Password>@<IP of Camera 1>/mjpeg/stream.cgi?chn=0 --sout=file/ogg:IPCam1.avi
Now the quality was well suited for security monitoring, however the files created are large. This needed to be considered since I planned to use a Raspberry Pi.
Security Monitoring IP Camera Footage
To get around this I changed the earlier script to run in a loop. Since the images captured would be more frequent, the timestamp in the file name needed to include seconds. Another problem was housekeeping. If this process were left to run without any intervention, the images it scraped would eventually use up all available storage. To fix this, I added a function that removes files that have aged more than 2 weeks. To be sure I don’t delete something unintentional, the path is entered in function. Also, in the event that a camera goes offline, I have set timeouts and retry limits on my scrape. This prevents a hangup waiting for a response from a camera no longer online.
#!/bin/sh # alvinalexander.com # a shell script used to download a specific url. # this is executed from a crontab entry every day. while : do # wget output file FILE1=/home/local/Videos/Security/IPCam1_`date +"%Y%m%d%H%M%S"`.jpg FILE2=/home/local/Videos/Security/IPCam2_`date +"%Y%m%d%H%M%S"`.jpg FILE3=/home/local/Videos/Security/IPCam3_`date +"%Y%m%d%H%M%S"`.jpg # wget download url URL1=http://:@/mjpeg/snap.cgi?chn=0 URL2=http://:@/mjpeg/snap.cgi?chn=0 URL3=http://:@/mjpeg/snap.cgi?chn=0 wget -T 2 -t 1 $URL1 -O $FILE1 wget -T 2 -t 1 $URL2 -O $FILE2 wget -T 2 -t 1 $URL3 -O $FILE3 find /home/local/Videos/Security/ -mtime +15 -delete sleep 3 done exit
This will create a series of image files from my cameras that are taken every 3 seconds. I can review 2 weeks of footage. Since they are named with the timestamp, finding what I need is easier. Since the housekeeping function deletes old stuff, I don’t have to worry about disk space. This will help keep the process running as expected.
Of course there is plenty more to cover, but this should be a good start. I hope you have enjoyed this post and if you do consider purchasing the Wansview camera, please click my link above.