The Daily Peacehaven Timelapse Video

A couple of posts ago I wrote about the ‘WeatherPi’ Raspberry Pi that collects the latest weather readings every 10 minutes and make a decision whether or not to lower the storm shutters in front of our house.

I also added that the service was publishing minute-by-minute photographs of the sea view. This post explores that feature in more detail as well as introduce the YouTube channel where you can watch daily timelapse videos created of the images!\

Let’s start at the beginning: Upstairs in a front window of our house is a Raspberry Pi B+ called ‘SeaviewCam’ with its official Raspbery Pi Camera ‘Raspicam’ connected.

Every single minute between 3am and 10:59pm local time, SeaviewCam runs a cron job that performs these tasks:

  • Executes the official image capture program raspistill which takes a single, high-resolution photo of the sea view, saving the image as a JPEG file.
  • Takes the captured JPEG file and uses the convert program from imagemagick to embed the location and date/time-stamp text at the bottom of the image, saving to a new file.
  • If the new file is bigger than 1MB in size, uploads the resulting file to lansley.com’s content-delivery-network (CDN) hosted by DigitalOcean for publication (otherwise it is discarded).
  • In addition, upload two extra files – a smaller version of the same image and a further using filename with a date/time stamp.

The reason for the 1MB minimum threshold is that files smaller than that only show a black night view with no detail.

Here is the actual bash script that performs this work every minute:

#!/bin/bash
raspistill -w 2592 -h 1458 -awb sun -drc low -o /var/www/html/original.jpg
DATENOW=$(date +"%Y-%m-%d %H:%M")
FILEDATENOW=$(date +"%Y-%m-%d-%H-%M")
convert /var/www/html/original.jpg -brightness-contrast 0x20 -gravity south -fill white -pointsize 30 -annotate +0+50 "Peacehaven, East Sussex, UK (50.791386 N,-0.007877 E)   $DATENOW" /var/www/html/live.jpg
FILESIZE=$(stat -c%s "/var/www/html/live.jpg")
if [ "$FILESIZE" -gt 1000000 ];
then
	convert /var/www/html/live.jpg -resize 800x400\> /var/www/html/live-small.jpg
	s3cmd -c /home/pi/cdn-lansley-com put --acl-public --guess-mime-type /var/www/html/live.jpg "s3://cdn-lansley-com/seaviewcam/live.jpg"
	s3cmd -c /home/pi/cdn-lansley-com put --acl-public --guess-mime-type /var/www/html/live-small.jpg "s3://cdn-lansley-com/seaviewcam/live-small.jpg"
	s3cmd -c /home/pi/cdn-lansley-com cp "s3://cdn-lansley-com/seaviewcam/live.jpg" "s3://cdn-lansley-com/seaviewcam/$FILEDATENOW.jpg"
	s3cmd -c /home/pi/cdn-lansley-com setacl  "s3://cdn-lansley-com/seaviewcam/$FILEDATENOW.jpg" --acl-public
fi

At five minutes past midnight, a second cron job runs that stitches all “yesterday’s” images together in chronological order into a video file running at 15 frames per second which means that playback will be at the rate of 15 minutes per second. To do this, the video creating program ffmpeg is used. Here is the script that performs this task:

#!/bin/bash
FILEDATE=$(date -d "yesterday 13:00" +"%Y-%m-%d")
echo Image Timelapse Creator v1.0
echo "Downloading images for $FILEDATE ..."
s3cmd -c /home/nick/cdn-lansley-com get "s3://cdn-lansley-com/seaviewcam/$FILEDATE*.jpg" /home/nick/seaviewcam/
echo Creating a list of images to convert to video...
ls /home/nick/seaviewcam/*.jpg  | sort -V | xargs -I {} echo "file '{}'" > /home/nick/scripts/timelapseimagelist.txt
echo Running conversion...
ffmpeg -r 15  -f concat -safe 0 -i /home/nick/scripts/timelapseimagelist.txt -c:v libx264 -crf 22 -pix_fmt yuvj420p   /home/nick/timelapses/liveview.mp4
echo Creating small version of video...
ffmpeg -i /home/nick/timelapses/liveview.mp4 -crf 22 -vf scale=1280:720  /home/nick/timelapses/liveview720p.mp4
echo Uploading results...
s3cmd -c /home/nick/cdn-lansley-com put --acl-public --guess-mime-type /home/nick/timelapses/liveview.mp4 "s3://cdn-lansley-com/seaviewcam/timelapse/$FILEDATE.mp4"
s3cmd -c /home/nick/cdn-lansley-com put --acl-public --guess-mime-type /home/nick/timelapses/liveview720p.mp4 "s3://cdn-lansley-com/seaviewcam/timelapse/latest.720p.mp4"
echo Tidying up...
rm /home/nick/seaviewcam/*
rm /home/nick/timelapses/*.mp4
rm /home/nick/scripts/timelapseimagelist.txt
echo Completed!

The code works by:

  • Downloading all “yesterday’s” images by creating a date value based on “yesterday 13:00”.
  • Creating a sorted list of the image filenames and save to a text file suitable for input by ffmpeg (each filename has to be on its own line preceded with the text “file “).
  • Running ffmpeg with parameters:
  • -r 15 (frame rate will be 15 frames per second)
  • -c:v libx264 (use the X.264 video library)
  • crf 22 (constant quality setting of ’22)
  • -pix_fmt yuvj420p (recommended colour range format)
  • Creating a second, lower resolution version of the video in HD 720p format for public use.
  • Uploading to the lansley.com CDN
  • Tidying up afterwards!

You are welcome to try out the code by performing the image downloads yourself and recreate out the video. (Don’t worry, DigitalOcean are generous with their Spaces CDN download capacity, and I can always cut the link if there is a sudden and steep increase in downloads out of the usual pattern!).

Leave a Comment

Your email address will not be published. Required fields are marked *