CCTV With a Raspberry Pi

If you're running Arch Linux on a Raspberry Pi with an official camera, and you'd like to use this humble rig as a CCTV camera, here's one way to do it. First, make a simple script to take photos,

date=`date "+%Y-%m-%d-%H-%M"`
/opt/vc/bin/raspicam -o /mnt/usbstick/camera/$date.jpg

Next, make another script to delete the old ones,

find /mnt/usbstick/camera -mtime +5 -daystart -print0 | xargs -0 rm -f

The third and final script,, is used to convert the photos into daily videos:

date=`date —iso-8601 —date='yesterday'`
ffmpeg -f image2 -pattern_type glob -i "/mnt/usbstick/camera/$date-??-??.jpg" -target dvd /mnt/usbstick/video/$date.mpg

Lastly, add these scripts to the crontab using crontab -e:

* * * * * /home/zoeb/
0 1 * * * /home/zoeb/
0 2 * * * /home/zoeb/

I've set mine to take a new photo every minute, delete five day old photos every day at 1AM, and make a new video out of yesterday's photos every day at 2AM. These daily videos end up being just under a minute long each.

Try out the scripts manually first, to troubleshoot any issues quickly. If you don't yet have FFmpeg, you can install it (as the root user) using Pacman:

pacman -S ffmpeg

Obviously, you should change the names of directories as appropriate to your particular setup. You can also use programs such as scp and rsync to automatically copy the videos to a remote location.

If you'd like to take photos more frequently, then remove from the crontab, and change it to something like this:

while true
	date=`date "+%Y-%m-%d-%H-%M-%S"`
	/opt/vc/bin/raspicam -o /mnt/usbstick/camera/$date.jpg
	sleep 10

You can then run it continuously in the background by running screen, running the script using ./, then typing in ^A — control plus a lower case A — then d to detach the virtual terminal screen. (You can always reattach the screen later on using screen -r, then stop the script using ^C — control plus a lower case C.) Remember to update to include an extra set of question marks!

All in all, this isn't too shabby for a £50 CCTV camera, especially considering it doubles up as a fully fledged computer.