This is quick how-to explaining how to store videos from an IP camera into a remote storage service, each time motion is detected in the camera – in this case we will be using Dropbox.
The default setup provides limited video quality but in my case this is fine since the cameras themselves keep a HD version of the recordings. I just wanted this to work as a remote backup in case any of the cameras are vandalised.
What do you need?
The same setup can be completed with similar components (e.g. you could use a USB camera instead). For illustrative purposes, these are the things I’ve used:
- Hardware:
- IP camera.
- Raspberry Pi.
- SD Card.
- Software:
- Any Debian-based distribution installed on the Raspberry Pi.
- Motion.
- Dropbox uploader.
- 3rd party services:
- Dropbox account
This guides assumes that the following setup is already completed:
- Your IP camera has already been assigned a valid IP and/or rtsp address.
- Your Raspberry Pi is up and running with either SSH access or console access through a video interface.
Installing and configuring Motion
Motion is a pretty interesting software that is able to process streams of video from different devices and allows triggering actions based on the motion detection, storing video on specific locations and streaming it.
First thing first, we will ensure the Operating System is updated:
sudo apt-get update && sudo apt-get upgrade -y
We are then ready to install Motion:
sudo apt-get install motion
Now it’s time to tweak the Motion configuration, to add the IP camera address, configure Motion to be started as a service and define the directory where the videos will be stored.
Let’s edit the /etc/motion/motion.confto modify the following settings.
Set the camera RTSP address:
netcam_url rtsp://username:password@camera_IP
Set the target directory where the videos will be stored:
target_dir /path/to/video/recordings
Enable Motion to be run as a daemon, insteado of running it manually:
daemon on
Allow camera stream to be used from other hosts (by default is restricted to localhost):
stream_localhost off
Allow web UI to be used from other hosts (by default is resitricted to localhost):
webcontrol_localhost off
Now we will edit the /etc/default/motion file to enable Motion to be run as a service by changing the following property:
start_motion_daemon=yes
You can now start the Motion service:
sudo service motion start
You should now be able to access the Motion web UI at http://raspberry_pi_address:8080
Installing and Configuring Dropbox Uploader
There isn’t an official Dropbox client that runs on the Raspberry hardware, but there is pretty decent alternative called Dropbox Uploader that allows uploading, downloading, deleting and listing Dropbox files. The only thing that does not seem to be supported is synchronisation.
Clone Dropbox Uploader into your preferred path by doing:
sudo apt-get install git
git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
Run Dropbox Uploader for the first time and you will be asked for your Dropbox API key. The script itself will give you instructions to obtain the key:
./dropbox_uploader.sh
Schedule automated upload of the videos to Dropbox and clean up
We will add 2 Cron jobs which will:
- Upload the vídeos to Dropbox every minute.
- Delete videos that are older than 7 days from the Raspberry Pi (credits to heiko)
Type:
sudo crontab -e
And add the following lines:
* * * * /path/to/dropbox_uploader/dropbox_uploader.sh upload /path/to/your/videos/* / >> /var/log/dropbox-uploader/dropbox-uploader.log 0 0 * * * /usr/bin/find /path/to/your/videos/* -name "*.*" -type f -mtime +7 -exec rm -f {} \;
I’m not scheduling the automated deletion from Dropbox since I want to check them manually first. Using the default video quality means that motion-detected videos of 20 seconds use only around 300 KB.