Updated on November 23, 2017
Security Camera Motion Detection with Linux Motion
Security cameras are great for keeping physical evidence on strange events in and around your house. They have picked up thieves stealing Christmas packages, house break-ins, and various other thefts. In this article I’ll teach you how to capture videos from your security cameras using Linux Motion.
Many of the security camera manufacturers release some software that allows you to view the cameras with your phone, or save the videos to an off-camera location, such as your computer. For example, Logitech has the Alert Commander system, which only runs in Windows. It won’t even install with Wine, as much as I’ve tried.
Luckily there’s an application called Motion which can monitor your video cameras, whether it’s network-enabled, USB, or coaxial. This application monitors the cameras for motion and can store videos or images to a hard drive. There are many configuration values that you can tweak, and the system also runs a web server for viewing the cameras and editing the configuration.
While your configuration might differ slightly, I’m going to post my configuration for the Logitech Alert security camera system with 3 connected network-enabled cameras. The Motion software will save videos to a 3TB hard drive attached to my computer.
Install motion using apt or your package manager.
$ sudo apt-get install motion
You can also run motion on daemon mode as a system service. Instead of following the steps to create user directories and configuration files, essentially you would perform similar tasks in the /etc/motion directory, modifying the /etc/motion/motion.conf file.
I’ve included the instructions below with a user-based configuration because I prefer this setup for now. The downside is that I must log into the computer before the motion service will start.
Create a user directory for motion
$ mkdir ~/.motion
Copy the sample configuration to the user directory.
$ cp /etc/motion/motion.conf ~/.motion/motion.conf
You can now manually edit this file (gedit ~/.motion/motion.conf) or continue to the next section to use my sample.
Customizing Motion Configuration
Since I have 3 network-enabled cameras, I needed to use the threaded mode. This mode is really simple to use, and only requires the initial motion.conf file, plus 3 threadX.conf files.
Feel free to copy these files to your ~/.motion directory and change the configuration values listed below. Each thread file represents one camera. The network URL is contained within the file.
Find the following variables and change the values to your liking
logfile – Location to store the log (example value places the log file within the security camera folder).
width – Change to the maximum width of your camera
height – Change to the maximum height of your camera
netcam_url – Assuming you do not use threads, set this to your camera’s network URL. Example: rtsp://192.168.1.4/HighResolutionVideo
locate_motion_mode – My example configuration enabled motion boxes to be drawn around the interesting parts of the video. I find this helpful.
webcontrol_authentication – The username and password for the web server.
target_dir – Change to your Security Camera folder. Please note that this should be a very large hard drive (1 to 3+TB)
thread – Add as many thread files as you have cameras and change the configuration within each file accordingly. I’ve included 3 files above for my sample cameras.
Run motion on Startup
In Ubuntu, open the Dash menu and type “Startup Applications”. You will be presented with a window that looks similar to the image below. Here you can add the command for motion which will start it whenever you log in.
GUI Configuration Using motion Website
The motion application includes a web server that runs on port 8080. Each camera also gets its own port so that you can view the camera from your computer. With additional configuration you can allow the cameras to be viewed from other computers or from outside the network, but I will not cover that in this post.
Once motion is running, you can browse to http://localhost:8080. The default username and password using my example file is admin:password. I highly recommend changing that as soon as you log in.
This link allows you to configure the main system and each camera independently. The main configuration will apply to all cameras unless they overwrite a value.
Reducing False Positives
Motion is a decent application, however it can be hit-and-miss with the sensitivity settings. If the motion you’re watching is very far away then you need to make the system sensitive enough to detect it, however if it’s too sensitive then it will record videos that are too short and offer no value. I will often see 1 second videos with no detectable motion due to the settings I use, but I use the settings because I need to detect small changes far away.
To reduce false positives I’ve developed a simple cleanup script that runs using a cron job every 5 minutes.
smallVideoFileCleanup.sh – Download this file to the ~/.motion directory and edit the SECURITY_CAM_FOLDER variable to match your folder location:
Once you have the file edited and placed in the ~/.motion directory, you can create a cron job to execute the task every 5 minutes. Please note that the $ represents a terminal command. Do not include the $ when using the terminal.
$ crontab -e
Paste the following line of code, with the correct folder path:
*/5 * * * * /home/myuser/.motion/smallVideoFileCleanup.sh
The cleanup script will find any video file that is less than 600KB in size, representing only a few seconds of video, and will delete them. It will also find any image that may be saved in the same folder and delete them if they are 0KB in size. I’ve found that a bug in the motion service will write empty images as text files.
The End Result
The folder structure that is created will be similar to this, if you used the configuration provided.
I know you want to see examples now, so here are a couple of video examples.