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

  7 Responses to “How To : Use Motion To Generate Timelapse Videos”

  1. […] Use motion to generate timelapse videos. Read more over at The Rantings and Ravings of a Madman. […]

  2. Hi There
    I have just tried this – the output video plays fine in Linux (Ubuntu Movie Player) but playing in any other player like VLC the shape of the video is wrong (tall and skinny) and the colour is all green. Any ideas? Thanks!

  3. hi, I’m looking into using this type of technique to offload the avi generation from my raspberry pi
    As I have little xp with these image processing tools this is very helpful.
    Just a thought – do you know whether mencoder pre-sorts the files in some way to get the correct ordering for the frame sequence, or is it just down to the filesystem returning them in an assumed order?

  4. I used the same parameters as you had in your mencoder command line and it works great and I was able to played the movie file in VLC.

    Thanks for posting this, you made it very simple to take the individual jpg files that motion generates in numeric sequence order and convert them into a time-lapse movie file.
    I search Google for other methods, suggesting ffmpeg and others but yours is the simplest and easiest method.

    Here is the command line I used on my system – it converted 6,280 (1.3Gb) files in just a few minutes):
    # mencoder mf:///home/martin/junk1/motion/*.jpg -mf w=320:h=240:fps=25:type=jpg -ovc copy -oac copy -o motion_time_lapse.avi

    My motion file names were sequenced,
    935-20180124140332-08.jpg
    935-20180124140332-09.jpg
    612-20180121075550-01.jpg
    612-20180121075550-02.jpg
    612-20180121075550-03.jpg

    Thanks alot.

    BTW, I tried running it using higher values w=1280and h=960 but the VLC player the shape of the video was tall and skinny and the colour was all green. I didn’t take the time to troubleshoot this issue.
    The same issue the other guy encountered.

    I tried to match the settings in my motion.conf file.

    # My motion.conf file settings:
    # Image width (pixels). Valid range: Camera dependent, default: 352
    width 1280

    # Image height (pixels). Valid range: Camera dependent, default: 288
    height 960

Leave a Reply to SirLagz Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.