Creating Timelapse Videos With The Raspberry Pi Camera

Timelapse ProcessIn this post I will explain how I made some timelapse videos using the Raspberry Pi camera module. This is a two step process which involves getting the camera module to take a series of stills over a period of time and then combining them into an MP4 video file.

We can use the “raspistill” utility to take a set of time lapsed photos as described in my Taking Hi-Res Photos With The Pi Camera Module article. Make sure you have installed the camera and updated your operating system.

Step 1 – Taking the time-lapsed photos

This command will take a photo every 60 seconds (60000 milliseconds) for 2 hours (7200000 milliseconds) resulting in a sequence of 120 images.

raspistill -o myimage_%04d.jpg -tl 60000 -t 7200000

The “%04d” will result in a four digit number appearing in each filename.

myimage_0001.jpg
myimage_0002.jpg
...
myimage_0119.jpg
myimage_0120.jpg

Step 2 – Combine images into MP4 video

Once you’ve got your image sequence you will need a method to stitch them together. I decided to use “avconv”. You can install this useful library with the following command :

sudo apt-get -y install libav-tools

To construct the video file from your image sequence you use the command shown below. Although it appears on multiple lines for readability it should be entered as a single line on the command line :

avconv -r 10 -i myimage_%04d.jpg 
       -r 10 -vcodec libx264 -crf 20 -g 15
       timelapse.mp4

The video will be the full resolution of the default image size (2592×1944).

To crop the images and create a more standard 1280×720 resolution video you can use the following command :

avconv -r 10 -i timelapse_%04d.jpg
       -r 10 -vcodec libx264 -crf 20 -g 15
       -vf crop=2592:1458,scale=1280:720
       timelapse.mp4

The “vf” option defines a video filter. In this case two filters which crop the incoming image to 2592×1458 and then scale them to 1280×720.

The “r” option tells avconv to create a video with a frames per second of 10. It appears twice to prevent avconv dropping frames that it thinks are similar.

The “crf” option tells avconv to aim for a quality level of “20” which is a good starting point. Lowers values are better but will increase the file size.

The “-g” option sets the GOP value. The YouTube Advanced Encoding Settings page recommends that the GOP should be set to half the frame rate so this is set to 15.

The conversion process is very slow on the Pi compared to doing the same thing on a desktop PC. For long sequences with hundreds of frames I would recommend downloading an appropriate version of Libav on your desktop or laptop and build your MP4 files much faster!

Results

Here is a quick-n-dirty timelapse video I took out the window of my man-cave. It consists of 600 frames which were taken at a rate of 1 per minute for 10 hours. Nothing much happened in those 10 hours but it proved the Pi could do the job and that avconv could create the MP4. I got a PC to do the hard work!

When I’ve got the time I am going to make some more interesting timelapse videos. But that is for another day. Hopefully with better weather …

This entry was posted in Camera Module and tagged , , , , . Bookmark the permalink.



27 Responses to Creating Timelapse Videos With The Raspberry Pi Camera

  1. Nice work. I’ve set a PiCam to shoot a time-lapse of our garden once every ten minutes for a week. It’s got 2 days left to run. It won’t be spectacular, but it’s all fun. I’ve seen some good ones in town centre type environments.

  2. David says:

    You were not kidding about the Pi taking a ton of time to take the .jpg and construct a video file. I echo the sentiment that taking the .jpg to .mp4 is best done on a different machine than the Pi. Sure you can do it on the Pi, but it takes a long long time! Nice write-up, thanks for sharing!
    D

  3. Teknynja says:

    I just did something very similar on a desktop Linux system, using a Python script to capture images from multiple cameras – but instead of creating a bunch of individual jpeg files, I appended them together as they were captured to create a crude mjpeg file that can also be converted to mp4 using avconv. As a bonus, the Python script also adds a timestamp to each captured image. The script could be easily converted to use the raspistill tool instead of network cameras, if anyone is interested.
    http://www.teknynja.com/2013/05/time-lapse-video-capture-from-network.html

  4. Steviep says:

    not to worry found them, the only problem I seem to have is the first 5 jpgs are fine but after that the file size is 0

    any ideas as to why ?

    thank you

    Steviep

  5. angelo accolla says:

    raspberry+webcam low cost , using motion
    http://youtu.be/Oe5luKlMuh8

  6. eric says:

    Very cool project. Tried this out today. Lots of potential for future projects using the timelapse setting.

  7. Ole says:

    I’m having problems running this command. Can you help me?

    pi@raspberrypi /home $ raspistill -o myimage_%04d.jpg -tl 2000 -t 10000
    mmal: main: Error opening output file: myimage_0001.jpg~
    No output file will be generated

    mmal: main: Error opening output file: myimage_0002.jpg~
    No output file will be generated

    mmal: main: Error opening output file: myimage_0003.jpg~
    No output file will be generated

    mmal: main: Error opening output file: myimage_0004.jpg~
    No output file will be generated

    • Matt says:

      You must have write permissions to the directory where you are creating the image.jpg file. If you log in as “Pi” and run the command straight away you should be ok.

  8. Johan says:

    How can i make it to autostart when i start my pi so i have no need for lan.?

    //Johan

  9. Isaac says:

    How can I view the mp4 video, I can’t seem to get mine to work.
    Any help will be appreciated

  10. Graham Easton says:

    Hi
    RE Pi timelapse
    I have the images on the sd card in a file in my home directory but I want to edit them in a video editing package on a PC. I understand that I can export the file containing the images to the PC but having now spent two days looking around I am still can not find how as the file does not show up on the SD card.
    Any guidance would be appreciated

    Many thanks in advance
    Graham

  11. Tim says:

    I have write permissions. Typed in the code as above. Get error message.
    At least one output file must be specified . How is this done?
    Thanks!!!

  12. LE PONNER Sébastien says:

    Un grand merci a son auteur pour ce tutorial…. Tout est clair et précis malgré la barrière de la langue…. Cela m’a été d’une aide précieuse…. Encore merci!!!!!

  13. Nashie says:

    Hey,

    I am having trouble running the cmd – Get error “No Such file or directory” every time.
    All my files are names:

    00022207.jpg
    00032212.jpg
    00042217.jpg
    00052222.jpg
    etc…

    I am trying the following code:

    avconv -r 10 -i %8d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 timelapse.mp4

    Error:
    avconv version 0.8.10-6:0.8.10-1+rpi1, Copyright (c) 2000-2013 the Libav developers
    built on Mar 22 2014 02:13:15 with gcc 4.6.3
    %8d.jpg: No such file or directory

    Do I need to have a name like “photo_” at the start of each file? I thought %8d would have taken the first 8 digets…

    Any help is appreciated.

    Cheers,
    Nashie

  14. Nashie says:

    Hi Matt,

    Sadly, still got issues. any other tricks?

    Here is what I did:

    pi@raspberrypi ~/timelapse_201404261512 $ ls
    image_00011512.jpg image_00111654.jpg image_00211835.jpg image_00312016.jpg image_00412157.jpg image_00512338.jpg image_00610210.jpg image_00710351.jpg image_00810532.jpg image_00910713.jpg
    image_00021523.jpg image_00121704.jpg image_00221845.jpg image_00322026.jpg image_00422207.jpg image_00522348.jpg image_00620220.jpg image_00720401.jpg image_00820542.jpg image_00920723.jpg
    pi@raspberrypi ~/timelapse_201404261512 $ avconv -r 10 -i image_%08d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 timelapse.mp4
    avconv version 0.8.10-6:0.8.10-1+rpi1, Copyright (c) 2000-2013 the Libav developers
    built on Mar 22 2014 02:13:15 with gcc 4.6.3
    image_%08d.jpg: No such file or directory

    pi@raspberrypi ~/timelapse_201404261512 $

  15. Fred says:

    The timelapse works OK but the final image remains on the monitor. Unable to construct the video file because the image is there. Rebooting does not show any images on thw SD card.
    How do I get rid of the final image?

Leave a Reply

Your email address will not be published. Required fields are marked *

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=""> <strike> <strong>