May 212013
 

I recently had an email from one of my readers enquiring about making timelapse videos with the Raspberry Pi.

Since I already had a webcam connected to one of my Pis, I set about making it take timelapse shots.
There are a myriad of ways to accomplish this, but I decided to use motion as it took the least configuration to get it to do what I wanted it to do.

Getting motion was as easy as apt-get install motion on my Raspbian powered Pi.
I also needed mencoder to encode the resulting images into a video. Mencoder is also found in the repositories, so a quick apt-get later and I also had mencoder installed.

In order to get timelapse shots, I had to setup motion to take pictures at intervals.
On line 295 in the default motion.conf, there is this following line

snapshot_interval 0

Change that to the number of seconds between each snapshot.

snapshot_interval 5

I’m also allowing remote access to the webcam so that I can check in on it.
On lines 413 and 429, change the webcam_localhost and control_localhost to off.

After changing those, I restarted the motion daemon for the changes to take effect.
Once motion is started, you will start seeing files in /tmp/motion (or wherever you decided to save the files)
The ones ending in -snapshot.jpg are the ones that we will be using for the timelapse movie.
If you have motion detection activated, you will see other files in the directory but we can ignore those.

To create the movie, we are going to use mencoder. This part could also be done with ffmpeg, which I may cover in a later post.
I wrote up a small script to run the mencoder command –

#!/bin/bash
mencoder mf:///tmp/motion/*-snapshot.jpg -mf w=320:h=240:fps=25:type=jpg -ovc copy -oac copy -o output.avi

The resolution (-mf w=320:h=240) should match the resolution setup in motion.
When the script is run, it will take all files in /tmp/motion/ that end in -snapshot.jpg and make a movie out of them. The output file is set by the -o switch, in this case I’ve used output.avi.
Setting this script to run once a minute for example, will keep the timelapse video up-to-date to the last minute.

Once the file has been created, you’ll be able to view the file on another computer very easily.
The only issue with this timelapse movie, is that it will keep getting longer and longer and longer, as there is nothing cleaning up the old files.

So what we’ll do, is add an extra line into the script to remove any files older than a certain time, and that way we can control the length of the timelapse movie.
If we wanted to make the timelapse movie 10 minutes only, we’d add the following line before the mencoder line

find /tmp/motion -name "*.jpg" -type f -mmin +10 -delete

Resulting in this

#!/bin/bash
find /tmp/motion -name "*.jpg" -type f -mmin +10 -delete
mencoder mf:///tmp/motion/*-snapshot.jpg -mf w=320:h=240:fps=25:type=jpg -ovc copy -oac copy -o output.avi

That way, whenever the script is run, we’ll have a 10 minute long timelapse video !

Share