Friday, October 09, 2015

Raspberry Pi Timelapse

I'm fascinated by timelapse photography and I'm also fascinated with single board computers. Luckily the Raspberry Pi makes it easy to mix these two interests. I use a Raspberry Pi model B and the camera module to take a bunch of still photos and then turn that into a video with software.

The video below is over 3 hours looking out my basement window. It faces north so you get lots of great shadows running across the house across the street. Unfortunately my setup was a bit unstable and it moved down a bit to show the window frame. This is just a test so it's not super important. I just wanted to make sure I had the command right and that everything worked.


My setup is the raspberry pi running raspian with the camera module connected and enabled in the configuration.

Keep in mind it's just a test so try to ignore the kleenex box.

The command to capture the images is:

raspistill -vf -hf -w 1280 -h 720 -o time_%04d.jpg -tl 18000 -t 1080000
-vf is vertical flip since my camera is upside down
-hf - horizontal flip since the image is mirrored
-w - width. The native resolution is quite high so I just capture at 720p resolution
-h - height
-o Output file name and location. %04d tells it to use a 4 digit incremental number to name the files
-tl - duration between images. If this is low, say under 2 seconds you may want to use -bm for burst mode. This keeps the camera active and you'll have fewer dropped frames
-t - total capture time. These are both in milliseconds so in the above example I was taking an image every 18 seconds for 3 hours which gives me 600 images and at 10 frames/second that is a 1 minute video.

If you have a monitor connected you may want to use the '-n' switch that prevents a preview from being displayed.

So how did I decide on my shot interval? Well I use an online calculator. I know that 10fps is good enough and I picked 1 minute as the final length so a little math or the online calculator will tell me to take an image ever 'x' seconds. You could go up to 24fps and take more images but 10 seems to work pretty well.

So now that you've got the images how do you make it into a movie? If you have access to them you can use quicktime pro quite easily or Adobe premier but I'm poor and don't have either of those. If you want you can do the encoding right on the raspberry pi with libav. It takes a while but it does work. Here is the command:

avconv -r 10 -i myimage_%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 timelapse.mp4
I stole these settings from Raspberry-Spy and they seem to work. This is also where I found that 10fps was good enough.  The catch with avconv is the files need to be consecutive. Sometimes raspistill will drop a frame so you'll be missing that file and that will cause avconv to die. You can fix this with a quick bash script.

a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a")
mv -- "$i" "$new"
let a=a+1
done

This will take all the jpegs in a folder and rename them in consecutive order.

You'll need to install libav on Raspian if you want to do it this way. Apt-get install libav-tools should do it. If you do the conversion on another machine Ubuntu has libav installed by default I believe.

An easier way to do it for free is to use Windows Movie Maker. It's a free download from Microsoft and it's a little more user friendly if you like GUIs. Install and fire up Movie Maker and then add your photos. You're basically making a slide show but luckily you can set very short durations.


 You'll have to do some math to determine the duration you need. Here are a couple to get you started.

10fps = 0.1
25fps = 0.04 - can't do exactly 24fps

After you've got that set go to the file menu and choose to save your video. If you pick Android medium that'll give you a 1280x720 mp4 video.

One last thing I'll touch on, what if you don't have the camera module? Well you can use a USB webcam. I can tell you from experience that it doesn't work as well. I'm not sure if it's lack of horsepower on the pi side or what but I had to use very low resolutions to get it to work properly. The upside, motion, the software I used can do timelapse directly to a video output. That sure streamlined the expereience.



So that's the crash course in raspberry pi timelapse. I hope you enjoyed it and hopefully found it useful.

No comments: