Systems status: [ OK ]
  • [ OK ]proxy
  • [ OK ]mailserver
  • [ OK ]sunrise
  • [ OK ]

Capturing every Zanzibar sunrise

A script is fetching sunrise time, triggering a camera in Zanzibar and uploading a timelapse to a server every day

Capturing every Zanzibar sunrise

TLDR: A script is fetching sunrise time, triggering a camera, uploading a timelapse to a server every day and embedding it into a website for public viewing.

See it live here! https://www.whitesandvillas.com/

Looking at the horizon in pixels

Although not portrayed in Metal Gear Solid 1, Zanzibar has some of the most beautiful sunrise I’ve ever seen. Why not setup a camera to capture them forever in a bucket of data and share them around you say? Yes.

6.24am, Paje, Zanzibar

What you’ll need:

  • RTSP enabled Camera (I used a Unifi G3 Bullet)
  • A small Linux server (Hetzner has great prices, I used a CX11– 2GB Ram @ EUR ˜4/month)
  • An Amazon S3 Bucket
  • (optional) A firewall to whitelist IPs and forward ports to the camera.

Skills required:

  • SSH
  • Bash
  • AWS S3 / IAM
  • Will to learn ffmpeg basics
  • Time: about 4 hours.

The goal is to capture the sunrise every morning from your RTSP-powered camera and saving it into a file on a server to display on a website.

General approach

  1. Install a camera high-up, looking east
  2. Determine sunrise time
  3. Capture the sunrise in a video
  4. Serve the video publicly
  5. Schedule it daily
  6. Embed it in a website

Installing the camera

While on a trip to Zanzibar, I had an old G3 Bullet camera laying around.

I tested it before installing it high up — it was still working.

ain’t nobody got time for clean cabling

Found a great spot next to a floodlight with easy access to electricity. Thank you Zanzibar Kite Paradise!

After it was installed, I was able to access it from my local network to activate RTSP streaming.

Activating RTSP for direct streaming

Next up, I had to create a local Firewall rule to allow TCP connections to the camera and forward incoming request for port 7441 to it.

Testing the stream in VLC from a distant computer worked! Now anybody with the correct URL can stream it continously (at the expense of african bandwith, which can be quite expensive. I’ll make another article about I went around this with Restreamer).

Watch it live here: https://www.zanzibarkiteparadise.com/webcam

Now let’s add some automation to capture the sunrise daily into a video file.

Rent a server!

We don’t want to run anything of our machine, do we? Better rent a small linux server. I used HETZNER which had exactly what I needed: a shared vCPU, 2 GB Ram and 20GB of disk space for the small price of EUR ˜4/month. It is located in Europe.

Determining the sunrise time

Since sunrise times change daily, I needed a way to dynamically determine the correct time each day.

I used the public Sunrise-Sunset API to fetch the sunrise time.

A simple bash script (initiate_sunrise.sh) will fetch the day’s optimal time to trigger the automation (ffmpeg_capture_script.sh).

#!/bin/bash
# Filename: initiate_sunrise.sh

# Location for Zanzibar
LAT="-6.1357"
LNG="39.3621"

# Fetch sunrise time for Zanzibar from the API
RESPONSE=$(curl -s "https://api.sunrisesunset.io/json?lat=$LAT&lng=$LNG")
SUNRISE=$(echo $RESPONSE | jq -r '.results.sunrise')

## SUNRISE returns = 06:29

# Starts the video capturing script at sunrise
echo "$HOME/ffmpeg_capture_script.sh" | at $SUNRISE

Note: My server is in Europe but the sunrise api is returning data in EAT (UTC+3), so a small correction was needed to adjust the sunrise time to CET.

Video capturing script

Who has time to watch a 20min sunrise video? Instead, let’s make a 20 second timelapse.

Using a bash script, we will use ffmpeg to read the camera stream and screenshot it every 5s for 20min.

#!/bin/bash
# Filename: ffmpeg_capture_script.sh

# Cam RTSP URL
CAM_URL="<public RTSP URL>"

# Capture images for 20 minutes
ffmpeg -t 00:20:00 -i $CAM_URL -vf fps=1/5 "$SAVE_PATH/img_%03d.jpg"

Bingo! I was in possession of a bunch of images. Now let’s create a timelapse.

one image every 5 seconds, for 20min

ffmpeg can stitch pictures together to create a video. I will spare you the parameter explanation but there is a great explanation on this article by Rahul Sekhar.

# Create A 20-second video at 30 fps requires 600 frames (30 fps * 20 s)
ffmpeg -framerate 30 -pattern_type glob -i "/*.jpg" -c:v libx264 -pix_fmt yuv420p -r 30 -vframes 600 "/timelapse.mp4"

Et voila! This script outputs a timelapse.mp4 file.

Time to upload it somewhere public, so that a website can embed it.

Serve the video

What better place to host the video file than an AWS S3 Bucket? It has a CLI which makes it very easy to setup.

#!/bin/bash

# Upload to S3
aws s3 cp "timelapse.mp4" "s3://<BUCKET_NAME>/timelapse.mp4"

To make sure the server had the correct AWS rights to upload the file to the S3 Bucket, I followed this article.

I can now access the video from it’s public URL in the browser! Now let’s make sure the script runs daily.

Schedule a daily script

Using cron on the server, I can automatically run the script every day

# crontab -e

0 1 * * * $HOME/initiate_sunrise.sh

This command runs the script at 1:00 AM every day of the month, every month.

Et Voila!

Next steps

  • Make a “sunrise” frame with a raspberry pi, showing today’s sunrise in a beautiful and fashionable way.
  • Make a reverse-timelapse: a 1 hour slow video of the sunrise that will serve as an Aerial background, similar to Apple’s Sonoma moving backgrounds.
Sunsets will look great on a Macbook screensaver

And this is how I’m capturing all Zanzibar sunrises in an S3 bucket.

Thank you for reading!