Posts: 59
Joined: Sun Mar 04, 2012 7:39 pm
Contact: Website

Complete Pi Camera Timelapse Python Script

Wed Jul 17, 2019 8:09 am

I've attached a timelapse camera Python script for anyone who wants it. It uses the Pi camera module and it's been working great for a few weeks now even with me restarting the Pi at random.

The Python script is run on startup using systemd (there are loads of guides on how to do this) and this means I can check the log easily whenever I need to. The images are captured into a folder named after the current date and it calls FFMPEG at the end of each day to compile the captured images into a video for that day and then deletes the images. I've got the images being stored on a USB flash drive (although the path can be easily changed) and the whole folder is shared with SAMBA so I can access the files remotely to copy/delete them. It's easy to change the capture delay and video frame-rate on the fly as they are read from files at the beginning/end of each day.

I'm using an RTC module to ensure that it doesn't get broken by power cuts etc as the date will be reset. I modified it to have a larger battery pack than the tiny one it came with as space is not an issue. It won't work as flawlessly without an RTC module if it is possible it will restart, but you can buy one of those for a few pounds that fits right on to the GPIO pins and takes about a minute to configure.

There are a LOT of try...except blocks as I wanted to cover anything that may go wrong - it will be left to it's own devices for weeks at a time. I only want to capture between certain time periods, so that is hard-coded in - but it would be easy to change that or even have it read from files at the beginning of the day as it does with the capture delay. There are no comments, but there are a lot of error/descriptive print statements that can be read from the systemd log.

WARNING: if you want to see pictures it is UGLY! It's made from bits of wood we found in the skip and the hardware was knocked together in under two hours (including two coats of paint). It has a second pi that provides a livestream with a Logitech USB webcam and mjpgstreamer. There is an old TalkTalk router in the box that I attached an external antenna to that allows access over wifi. The forum wont' let me attach the images at a decent size so you can see the images in my tweet about it here.

UPDATE: new version of the program with support for more constants that can be loaded from files and better use of functions. Also, it won't overwrite images now if you restart it half-way through recording.

UPDATE 2: fixed bug in the improved version that stopped it encoding videos and also added a little program to encode the videos if you have folders of images that need encoding and then deleting.

UPDATE 3: added a feature to read from a file which week days to record on (as the demolition/building I'm currently recording doesn't happen on the weekends (Monday = 0).
(3.01 KiB) Downloaded 234 times

Return to “Graphics, sound and multimedia”