Like many during lockdown I got a side hobby to keep me occupied – growing vegetables hydroponically
And like many side hobbies, this one has ‘grown’ my interest in this alternative means of cultivating vegetables as well as taking a financial hit on my wallet. As I like tinkering with IoT and raspberry PI’s taking a timelapse video of the tomatoes growing in water seemed like a fun thing to try out.
Creating timelapse videos is really easy, it’s basically taking an image at a set time interval, then stitching all the images together into a video.
I put to work a rPI Zero W (wireless) with the official camera module into the growing tent. I used the following commands saved as a script (timelapse.sh) in a directory on the PI
#!/bin/bash raspistill -t 56443000 -rot 90 -w 1920 -h 1080 -tl 300000 -dt -o %d.jpg
- -t = how long in milliseconds to run the timelapse. 56443000 equals the time my grow lights stay on for – 16 hours in this case.
- -tl = in milliseconds the time between taking a photo.
- -dt = gives each image filename the current date / time.
I could have used sequential numbering for filenames but the issue with that is I stop taking images when the grow lights turn off. When the script starts back up again it resets the counter. The only problem with -dt is that it doesn’t pad the months with zeros so I have to run the images through a batch renamer later on to make sure the images are sequential in a directory.
I setup the script with a process manager called PM2 which manages the running of scripts. It’s very useful as it will autostart scripts after a reboot occurs. This is important as I can then switch off the pi when the lights go off, and automatically restart it when they come on again and the camera will start taking images again as PM2 will restart the script.
Once I had run the script for a day I had around 200 images to work with.
Getting date / time overlay on the images
This next part took some work. I looked at using ffmpeg to draw text on the images but couldn’t work out how to feed ffmpeg each images’ date time.
As I was saving each image with date/time in the filename, I had a look to see if I could use that to stamp the date/time over each image frame. I came across a post where someone had managed to do this using a little python script. The guy was timelapsing a house demolition.
Overlay EXIF date / time on timelapse images
I tried his script to save each image frame using raspistill with date/time in the filename but it didn’t work for some reason. I then had a thought that each image saves EXIF metadata which includes the file creation time, this would be perfect for using to timestamp each image.
A little bit of modification to the house demolition code and I have a working script to extract the EXIF value of ‘DateTimeOriginal’ to then timestamp each image.
Creating the timelapse video from images
Once we have all the images with their timestamp, I could then run the images through memcoder/ffmpeg
ls *.jpg > stills.txt mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o timelapse.avi -mf type=jpeg:fps=24 mf://@stills.txt ffmpeg -i timelapse.avi timelapse.mp4