KatPadi's Point

Time-Lapse Video Generator Using Raspberry Pi

So, I’ve been tinkering with my Raspberry Pi for the past couple of days. This weekend, I started with my time-lapse video generator using Raspberry Pi and its camera module. Taking snapshots is easy because RPi has a built-in raspistill command:

raspistill -hf -vf -x -o melody.jpg

There are many options which you can find here.

When I connected the camera to its slot, the camera is positioned upside down just like the image below:

Pi with NoIR Camera

So, horizontal and vertical flips are both needed to rotate the image 180 degrees. Adding both -hf and -vf flags help me capture correctly.

Adding the -x flag gets EXIF data, which has date and time (and many other things), written to the image. Which brings me to the next step. I needed this for embedding timestamps on the still images because why not.

Putting timestamp

To be able to annotate onto the images, ImageMagick must be installed.

sudo apt-get install imagemagick

I created a basic script for annotating:

convert $file -gravity NorthEast -pointsize 70 -fill white  -stroke black \
       -annotate +100+100  %[exif:DateTimeOriginal] \
       -gravity SouthWest \
       -pointsize 40 -stroke gray -annotate +100+100 'by @katpadi using RPi NoIR Cam' \
       "annotated-${file}"

The script basically just writes a text containing timestamp from the EXIF data of image on the upper right side of the image (Check out: gravity NorthEast!). Then, I also put a static text on the lower right side of the image because I’m vain.

The actual time-lapsing

Before I learned that there’s a default time-lapse option for RPi, I made a shell script that runs every 5 minutes using cron. (stupid, sorry)

Anyway, the built-in -tl command works like this:

raspistill -o myimage_%06d.jpg -tl 120000 -t 18000000

This command will take a photo every 2 minutes (120000 milliseconds) for 5 hours (18000000 milliseconds) resulting in a sequence of 150 images. The “%06d” appends 6-digit number on the image filename.

Then run it in the background.

Converting still images to video

If you’re doing this once only, you can just use iMovie. But that’s boring.

Of course I want the compiling and uploading automated. (I haven’t finished doing the “uploading” part yet)

For the collecting and converting still images to video part, I needed to install mencoder.

sudo apt-get install mencoder

The command for collecting the still images and actually converting them to a video is this:

#!/bin/sh

# Collect the jpg's
ls *.jpg stills.txt
# Create the video (mencoder)
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vibrate=8000000 -vf scale=1920:1080 -o timelapse.avi -mf type=jpeg:fps=24 mf://@stills.txt

# TODO: Upload to YouTube?

My plan is to run this script every time my time-lapse thingy ends.

The endgame

This was my first try.

I did not position my camera in a perfect place so I’m sorry for the quality. The actual quality of the photos are good (5mp) but I compressed it to be able to upload faster. Sorry!

Anyway, here’s how it looked like in the background:

Raspberry Pi with NoIR camera ugly Set upYikes.

 

2 comments for “Time-Lapse Video Generator Using Raspberry Pi

  1. June 30, 2015 at 7:35 am

    Galing naman!

  2. coolio
    August 10, 2015 at 2:52 am

    Awesome!

Leave a Reply

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