Simple Time Lapse Video With a Raspberry Pi

Here is a video that we created for my daughter’s science fair project. It was created by taking a snapshot every 5 minutes.

And this video was created using the same 5 minute snapshot but the video acts more like a slideshow with a frame very second.

Both of these types of time lapse videos are easily created using open source software and the Raspberry Pi or any Linux based computer. Here are the steps I used:

Getting the Camera Up and Running

This is assuming you are using a simple usb webcam. If you plug the camera into a USB port and run:

lsusb

you should see some output indicating that your camera was detected.

Next up is installing ffmpeg. The stock ffmpeg that comes with the package manager is good enough for our needs.

sudo apt-get install ffmpeg

Your camera may require you to install an additinal package. I have a Logitech Quickcam and in order to use it I had to install guvcview.

sudo apt-get install guvcview

Image Capture

Since we will be scheduling the snapshot, it is best to put the image capture command in a file.  In this case it is named webcamSnapshot.sh.  Here are the contents of that file.

#!/bin/bash
/usr/local/bin/ffmpeg -s 640x480 -f video4linux2 -i /dev/video0 \
-vframes 1 -qmin 1 -qmax 1\
-vf "drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf: text='Raspberry Pi Time Lapse': fontsize=14:fontcolor=white@1.0: box=1: boxcolor=black@0.5: x=7: y=440, drawtext=fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf: text='%{localtime} EST (GMT -6)': fontsize=14:fontcolor=white@1.0: box=1: boxcolor=black@0.5: x=(w-250): y=440" -strftime 1 "/home/pi/timelapse/%Y-%m-%d_%H-%M-%S.jpg"

This is basically one l-o-n-g command calling ffmpeg with the following options:
* Size: 640×480
* Format: video4linux2
* Input device: /dev/video0
* Video Frames: 1
* Quality of 1 (best possible from source)
* Place two blocks of text using the DejaVu font with a white foreground and black background with 50% opacity.
* Output file with a dynamically created name based on the time it was taken. This is output format is optional but makes it easier to see when it was taken at a glance.

Change the permissions to allow it to be executed.

chmod 755 webcamSnapshot.sh

You can test this out by running it at the command line. It should capture one image with a file name based upon the current time.

./webcamSnapshot.sh

Scheduling the Capture

Task scheduling on the Raspberry Pi is done through a daemon called cron. It is always checking if it is time to run something and if it meets the criteria it kicks it off. To create a new scheduled task in cron, issue the following command:

crontab -e

This will open up a Nano editor. scroll down to an empty line and create a new entry to execute the script we just wrote.

*/5 * * * * /home/pi/timelapse/timelapse.sh

Cron uses a column-based syntax where the first column is minutes, the second is hours, then days, etc, etc. In this case, */5 will execute every minute that is divisible by 5. The remaining asterisks simply mean that it will execute regardless of what hour or day it is.

The following script will take the photos created for the current day and move them into their own folder and create a time lapse video for the day:

#!/bin/bash

SNAPSHOTDIR="/home/pi/timelapse"
WORKINGDIR=`date +"%Y-%m-%d"`
SNAPSHOTSTART="2016"

#Make new directory for today's files
if [ ! -d "$WORKINGDIR" ]; then
mkdir $WORKINGDIR
fi

# Copy today's files over to new directory
i=1
for file in `find $SNAPSHOTDIR/$SNAPSHOTSTART*.jpg -mtime -1 | sort -k 1 -n` 
do
	j=$(printf "%s/%04d.jpg" $WORKINGDIR $i)
        cp "$file" "$j"
        (( i++ ))
done

#  Make timelapse video

/usr/bin/ffmpeg  -i $WORKINGDIR/%04d.jpg -r 10 /home/pi/timelapse/$WORKINGDIR/$WORKINGDIR.mp4

If you want to slow down the rate, ffmpeg added a nice “framerate” flag. For example, if you wanted to show a picture for 3 seconds (like a slide show) change the ffmpeg command to the following:

/usr/bin/ffmpeg -framerate 1/3 -i %024d.jpg -vb 20M -r 30 /home/pi/timelapse/$WORKINGDIR/$WORKINGDIR.mp4

This will cycle through the images every 3 seconds but still maintain the 30 fps of a normal video.

Leave a Reply

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