Basic Raspberry PI time-lapse with gphoto2

posted on 2013-02-09

I have attached a powered USB hub and a one terabyte USB drive to my Raspberry PI, so I decided to try out a simple time-lapse project. Let's start with the result:

To get up and running, here are the steps. First install gphoto2 (sudo apt-get install gphoto2 or sudo pacman -Ss gphoto2), connect your camera via USB, move to a directory with enough storage space and run:

gphoto2 --capture-image-and-download --interval 5

You can also leave the pictures on the camera memory card by using --capture-image instead of --capture-image-and-download. Then combine the images using ffmpeg.

Because the images I took where not according to the ffmpeg image2 naming convention (start with 0 and count up), the first example I found on the internet did not work. I then tried using the cat *.jpg approach mentioned here. However, that did not work as image2pipe kept screaming about the image resolution changing and the encoding being wrong. Maybe it was because of the 6016 by 4000 resolution of the files, I'm not sure. I decided to symlink the images to suite the simple %d.jpg naming the image2 module would understand:

let i=0
for f in DSC*.JPG; do ln -s "$f" $i.jpg; let i+=1; done

This will create a symlink for every JPG file. The symlinks make it easy for ffmpeg to find the files. You can see the mapping using ls -alhv in the directory.

$ ls -alhv |head
total 6.8G
drwxr-xr-x 2 bram bram  36K Feb  9 20:06 .
drwxr-xr-x 8 bram bram  12K Feb  9 17:35 ..
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 0.jpg -> DSC_0425.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 1.jpg -> DSC_0426.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 2.jpg -> DSC_0427.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 3.jpg -> DSC_0428.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 4.jpg -> DSC_0429.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 5.jpg -> DSC_0430.JPG
lrwxrwxrwx 1 bram bram   12 Feb  9 20:05 6.jpg -> DSC_0431.JPG

Then for the big finally, create an WebM movie of the separate images:

ffmpeg -f image2 -r 10 -i %d.jpg -r 25 -s 3008x2000 -qmin 15 -qmax 42 out.webm

The command break-down

  • -f image2 allows you to give a list of images using a format pattern.
  • -r 10 make sure the input images are considered as coming in at 10 frames per second. This determines the speed of the movie.
  • -i %d.jpg makes the image2 input look for 0.jpg followed by 1.jpg etc.
  • -r 25 sets the output rate at 25 frames per second.
  • -s 3008x2000 scales the output size to 3008 by 2000 pixels. To calculate different options, try my downscaling calculator.
  • -qmin 15 -qmax 42 control the quality of the WebM output. The blog where I got them from was right to say that they keep the output movie from looking terrible.

Things I have learned

  • Make sure the picture is interesting or beautiful. Otherwise, taking a lot of them and pasting them into a movie won't make it any better.
  • I should have chosen either doing the whole day (sunrise to sunset) or take a whole lot more photo's to make for a more fluent video.
  • Do some calculations on how long you want the video to be, and then decide how many frames you need to create a fluent movie. I should aim for 25 frames a second.
  • Even though I focused the camera and turned off the auto focus feature, some pictures where of different color then the rest. You don't really notice it, but I think that for the best effect I would have to fixate all lighting settings (ISO etc) as well.