How To Capture Time-Lapse Photography With Your Raspberry Pi and DSLR or USB Webcam
Watch the clouds move or a seedling grow. Time-lapse photography can really give you a sense of how the world works on a macroscopic level, beyond anything conceivable to the normal human experience of time. Or other words: it can turn watching paint dry into something quite interesting. Unfortunately, professional time-lapse equipment – whilst adding the ability to rotate or move your camera – like thecan cost up to $1000. Today, we’ll make a basic time-lapse controller with no movement – for DSLR or a USB webcam – with just a Raspberry Pi.
Be sure to check out all our other great Raspberry Pi projects too.
As usual, all our work with the Pi will be done over an SSH command line, and we can grab the files using SFTP with the same credentials if you’ve stored them on Pi instead of the camera. If you’re not sure what the IP address of your Pi is, log on to your router and view the list of currently connected devices – it should be reporting itself correctly.
Begin by installing some essential software:
sudo apt-get install gphoto2 sudo apt-get install imagemagick
Since I’ve used automatic ISO values and AV mode, the setup is relatively simple and we can capture the series using just the command line – the utility includes built-in time lapse parameters. Here’s the most basic command you can use to test the capabilities:
which will transfer the file to your Pi, or
to keep the image on the camera. At 8 MB a pop, it’s possibly best to keep them on the camera and perform the movie stitching after the fact from a PC or Mac.
Important: By default, images are saved to SD-RAM, not the memory card in the camera. Meaning you will lose them all (I speak from experience). To ensure they actually save, we need to first set the capture target:
gphoto2 --get-config /main/settings/capturetarget
Look at the list it outputs and set the target as follows, replacing 1 with whichever is shown as your card.
gphoto2 --set-config /main/settings/capturetarget=1
To capture in time-lapse mode, append -F and -I to specify the total number of frames you want to capture, and the time interval between each one. In this example, capturing once every 30 seconds for a total of 1440 frames, which when made into a 24 FPS movie will equal about a minute. I don’t think my camera’s battery will last this long anyway, but I’ll see how many I get.
gphoto2 --capture-image -F 1440 -I 30
One quirk to bear in mind; after the command completes, the camera may turn off and becomes unresponsive – you will need to manually cycle the power switch (at least, on my model it did). This shouldn’t be a problem out in the field when in use, but for testing purposes it gets quite annoying. If you know a fix for this, let me know in the comments, please!
Making a Movie
Transfer the files to your desktop computer for processing. On a Mac, I’ve found the easiest is to use the command line, but you’ll need XCode, Homebrew, and MPlayer installed first. To do this, first download Xcode from the App Store (it’s free), then from the Preferences -> Downloads tab, enable Command Line Tools.
Then run (one at a time):
ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go) brew doctor brew install mplayer
You should now have the necessary encoding components installed. Run the following commands to first create a list of the captured .jpg files, then feed that list into mencoder, a utility used to create videos. Change the FPS value if you’d prefer the movie go faster.
cd <directory with your time-lapse photos> ls *.jpg > list.txt mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o timelapse.avi -mf type=jpeg:fps=24 mf://@list.txt
Here’s what I ended up with, using 24 FPS from 330 frames shot every minute from morning to mid-afternoon. The weather is Britain really is that bad, from sunny to overcast in 5 minute intervals throughout the day. Sigh.
To encode a movie in Windows, or if you prefer using a GUI, download Avidemux, and follow the video tutorial below. Make sure your photos are numbered sequentially.
Using a USB Webcam
Of course, not everyone has a DSLR, but rather than buying an expensive official camera module for the Raspberry Pi, you can also perform time-lapse photography using an off-the-shelf USB webcam – in my case, a PS3 Eye which is usually quite a hassle to get working on PC or Mac, but works out of the box on the Pi. You may need to use a powered USB hub, and camera support is sketchy.
First, check if your webcam has been detected correctly.
ls -l /dev/video*
Hopefully, you’ll see /dev/video0 listed like this:
Then we can go ahead with installing the webcam capture software, and take a test shot.
sudo apt-get install fswebcam
fswebcam -d /dev/video0 -r 640x480 test.jpeg
Delete the file with:
Take a few test shots to enable you to position the camera appropriately, then we’ll get on with the time-lapse script.
mkdir timelapse nano runtimelapse
Paste in the following:
#!/bin/bash # Timelapse controller for USB webcam DIR=/home/pi/timelapse x=1 while [ $x -le 1440 ]; do filename=$(date -u +"%d%m%Y_%H%M-%S").jpg fswebcam -d /dev/video0 -r 640x480 $DIR/$filename x=$(( $x + 1 )) sleep 10; done;
For testing purposes, I’ve set the wait time between shots as 10 seconds. When you actually start capturing, you might want to change that to every minute instead. Change the following line to determine how many frames to capture, in the example it’s set as 1440:
while [ $x -le 1440 ]; do
To make the script executable, use:
chmod 755 runtimelapse
Then run it using:
Making a Movie
Still on the SSH command line, we’ll try stitching the movie directly on the Pi – if you’ve taken thousands of shots and your webcam is higher resolution, you may find this is appallingly slow, in which case scroll up to the movie instructions for DSLR, and transfer the files to your PC using SFTP.
cd timelapse ls *.jpg > list.txt sudo apt-get install mencoder mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o timelapse.avi -mf type=jpeg:fps=24 mf://@list.txt
If it all seems well but you get a 0 KB file output, it means there’s not enough space on your devices temp folder. I solved this with a clean installation and expanding the root system again – it seems BTSync had messed up some temp files.
Here’s the finished scene from a USB webcam, from late afternoon to dark.
This was far easier than I thought it would be – and I wouldn’t be surprised to find someone in the comments detailing an even easier method! Give it a go for a fascinating view of life that’s otherwise difficult to comprehend, and share a link to your creations in the comments or other time lapse videos you’ve found particularly inspiring.