After making changes to motion.conf, type the command:
sudo service motion reload
so that it will now attempt to write any new files to the /var/www/images folder. Now, you can easily access the files created by the Motion service and display them on the web just like any other typical web page.
Although the path has been changed in motion.conf, the images folder hasn't been created yet. So, make it now. The folder will be located within the www or html folder. If it sounds like we're repeating ourselves here, because it means you have been paying attention and are aware that the Apache root web folder can be in one of two paths:
By default, the www directory will be owned by the root user and root group. You will want to make some changes; such as all files will be owned by pi and the group will be www-data. To change this use:
chown -R pi:www-data www
For more information about users and the chown command, check out our guide on how user accounts, rights and ownership work in Linux.
So, what we are up against now is to make this images folder writable by the Motion service. As of right now, the other files have adequate ownership and permissions, but, the images folder does not. Well, let's change that right now.
The code snippet below has three commands. The first command will add the user motion to the www-data group. In case you are wondering, www-data is an existing user and group for the Apache server. The second command gives the images folder permissions to the Motion user and www-data group, and the final command makes the folder writable so that the images and SWF files can magically appear in the images folder:
usermod -a -G www-data motion
chown motion:www-data images
chmod 777 images
It's within the www folder where you can create a file called shadowbox.php that will be used to display content on the web. This file has all the code you need to display a thumbnail from each recorded SWF video, the video itself and the first JPEG image of motion.
The coding process to display the content goes like this: The images directory gets scanned and an array of files is created and sorted in descending order. These files are multiple JPEG files and a single SWF file for each event.
All files have a name that starts with the event, followed by the date and finally followed by the sequence. The date sequence is year, month, day, hour, minutes and seconds. After that, Motion just adds the sequence starting from 01 only for the JPEG files.
In order to keeps things simple and uncluttered, the coding for the shadowbox.php file will only display a single image and a single SWF file for each event. This simple script also uses shadowbox to create popups of the first JPEG image and flash SWF file for each event.
Now, you can see all the latest detected motion, down to the first recorded by Motion. This file gives all of the results and here is where you may want to customise the output.
If you want to keep these web pages password protected with Apache, you can open up the file /etc/apache2/sitesavailable/default and make some minor changes.
If you look for the line , you can add three simple lines to it. The code sample is shown below:
AuthName "Private Documentation Repository"
After that, you navigate to the /var/www folder and create a blank file called .htpasswd, and create the username and password with the simple command displayed below. You will be prompted for a password twice. You simply add it followed by Enter:
sudo htpasswd /var/www/.htpasswd add_username_here
Since the files can pile up pretty quickly and your disk space can readily disappear, you may want to create a purging system or back them up to another drive. Some backup plans are discussed next.
One tip for determining your backup plan is to watch how much space you routinely tend to use and develop a plan based on those conditions. Simple. For example, if you go through 1GB a week and you have a 8GB card you may want to TAR the images folders, SCP the file to a remote server and remove all files that are more than one-week old.
Since the files contain the year, month and date, it's a rather easy process to delete the ones that have expired. The file called purge.php is a cleanup file that is included in the motion.zip file you downloaded earlier in this tutorial.
This file removes every file that's more than a couple of days old. I will explain the code in a little more detail in a moment. First off, the images folder is scanned and all of the files become an array. That array of files then iterates through a foreach loop. A few built-in PHP functions, such as strstr(), preg_replace(), substr_replace(), substr(), date() and unlink() are used to translate all the file names into actual date timestamps that can be used for comparison.
Once a timestamp is made from the filename, it goes through a simple if() statement and is compared against a time that is set to two days ago from the current time. This part is really easy to change since you just need to change the number 2 to your desired amount of days in the past.
Once this criteria is met, the file is deleted with the unlink() function. Since this system is only using files without a database, it's rather elementary to move all of these files to your backup location, and since this is copying and moving files, two methods come to mind. One is using a package such as rsync and the other is a simple method of compressing the desired files and folders with ZIP or TAR and shipping them to their new destination with SCP.
A simple example of SCP is shown below:
scp -P 22 /var/www/images.tar email@example.com:/home/pi/images.tar
So there we have it. You've just created your own video surveillance and motion recording system that has several options to suit your needs or that you can customise. Although we've made a rough skeleton and files for you to monitor your video, record files and make backups, you can take this farther if you want. Some simple suggestions would be are to add a responsive template to both the video.php and shadowbox.php files, and polish up the content with a little CSS magic.
On top of that, you could set up webcams at other sources and have them viewable by the public or friends, depending upon what you want to achieve. Have fun!
- Enjoyed this article? Expand your knowledge of Linux, get more from your code, and discover the latest open source developments inside Linux Format. Read our sampler today and take advantage of the offer inside.
Sign up to receive daily breaking news, reviews, opinion, analysis, deals and more from the world of tech.