Time lapse video from Raspberry PI camera

4.5
(2)

Last Updated on 13th April 2024 by peppe8o

In this tutorial, I’m going to show you how to create a time-lapse video with a Raspberry PI board and Camera. We’ll use Raspicam commands to get images and ffmpeg library to create a new time-lapse video.

One of the most common Raspberry PI accessories is the Raspberry PI Camera. Besides common usages (like videoconferencing), RPI gives a lot of advanced functions. One of the most intriguing is getting a time-lapse video from a Raspberry PI camera

Before digging into technical commands, a brief introduction to what a time-lapse video is.

Time Lapse Technique

Looking at a list of images projected on a screen, human eyes can’t distinguish interruption between an image (frame) and following one with a switch rate higher than 60 Frames Per Second (fps). On the other side, getting an illusion of continuous motion requires at least 10 fps (even if the optimal fps is supposed to 25). This uses the so-called Persistence of Vision.

Time Lapse technique uses only some still frames, extracting them from a video stream or capturing these frames from a camera with defined intervals. Still images, consolidated in smaller intervals so that they can appear to the human eye as a continuous flow, give the illusion that time is running faster on video.

In the following picture, this process is described with an example, where from 15 fps (frame per second) we take 1 image every 5 and compress extracted images in a new video. Images that would be shown after 5 seconds (from frame 76) will appear in a new video file after 1 second:

time lapse explaination diagram

What We Need

Raspberry model A+

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon Raspberry PI Camera box

Step-by-Step Procedure

Start connecting your Camera module to Raspberry PI.

Prepare Operating System

I’m going to use only the terminal command, so you can install Raspberry PI OS Lite (for a fast, headless OS) or Raspberry PI OS Desktop (in this case, using its internal terminal).

Once installed, make your OS up to date. From the terminal, use the following command:

sudo apt update -y && sudo apt upgrade -y

We also need FFmpeg:

sudo apt install ffmpeg

Enable the Raspberry PI Camera interface from the raspi-config tool. From terminal:

sudo raspi-config

Terminal will show the following page:

raspi-config home pi3 model A+

Go to option 3 (Interface Option) and press ENTER:

raspi-config interface options pi3 model A+

Select first option (Camera) and press ENTER. In the next screen move selection from “No” to “Yes”:

raspi-config interface options camera pi3 model A+

Press ENTER and confirm also in the following screen.

raspi-config interface options camera enabled pi3 model A+

You will go back to raspi-config home. Move to the finish button and press ENTER.

This operation will require a reboot. Confirm in the next screen and wait for reboot:

raspi-config reboot pi3 model A+

Once your Raspberry PI is rebooted, connect again to the terminal.

Capturing images

Create a folder where to store images and enter it:

mkdir timelapse
cd timelapse

With the following command we’ll capture images with defined intervals and save them with increasing naming:

raspistill -t 60000 -tl 2000 -o image%04d.jpg

In raspistill command, the following parameters are used:

  • -t 30000: this option will make the command last for a total of 30 seconds
  • -tl 2000: this option produces a capture (frame) every 2 seconds
  • -o image%04d.jpg: every frame captured is saved as jpg file with names like image0001.jpg, image0002.jpg, image0003.jpg and so on. “%04d” indicates to use 4 digits with leading zeroes padding missing digits.

This will generate a list of jpg files inside the current folder as expected:

pi@raspberrypi:~/timelapse $ ls
image0000.jpg  image0007.jpg  image0014.jpg  image0021.jpg  image0028.jpg
image0001.jpg  image0008.jpg  image0015.jpg  image0022.jpg  image0029.jpg
image0002.jpg  image0009.jpg  image0016.jpg  image0023.jpg  image0030.jpg
image0003.jpg  image0010.jpg  image0017.jpg  image0024.jpg  image0031.jpg
image0004.jpg  image0011.jpg  image0018.jpg  image0025.jpg
image0005.jpg  image0012.jpg  image0019.jpg  image0026.jpg
image0006.jpg  image0013.jpg  image0020.jpg  image0027.jpg

We are going now to create a text file including a list of images that FFmpeg will put together to create the final video. This will be created with the following terminal command:

rm -f stills.txt; for f in image*.jpg; do echo "file '$f'" >> stills.txt; done

This is a short, one-line, bash script. It removes stills.txt file if exists, then a common “for” loop reading list of files in the current folder whose name starts with “image” and ends with “.jpg”. Wildcard “*” will consider good any character and any digit amount between those 2 strings. Each file name will be appended to a new stills.txt within a “file ‘……'” prefix. The output will be like the following:

pi@raspberrypi:~/timelapse $ cat stills.txt 
file 'image0000.jpg'
file 'image0001.jpg'
file 'image0002.jpg'
file 'image0003.jpg'
file 'image0004.jpg'
file 'image0005.jpg'
file 'image0006.jpg'
file 'image0007.jpg'
file 'image0008.jpg'
file 'image0009.jpg'
file 'image0010.jpg'
file 'image0011.jpg'
file 'image0012.jpg'
file 'image0013.jpg'
....

Creating Time Lapse Video

Output (time-lapsed) video can be created from jpg images with the following command:

ffmpeg -f concat -i stills.txt -s 1280x768 outVideo.mp4

Where:

  • -f concat: uses concatenation demuxer
  • -i stills.txt: -i option indicates input media. Referring a txt file, makes it possible to get input media from a file content
  • -s 1280×768: sets output resolution
  • outVideo.mp4: sets output file name

After this command you will find a new file named outVideo.mp4 which you can play in your favourite media player (supporting mp4 format, like VLC).

As you can see, we already used -s option to reduce the required computing power and make it compatible with a low hardware board like Raspberry PI. Ffmpeg requires a lot of RAM to run video manipulation. For this reason, a Raspberry PI A+ could not manage long duration time-lapse videos at full resolution. When RAM problems occur, you will receive from the terminal a “killed” notification and “journalctl -xe | grep ffmpeg” will return an Out of Memory error. In these cases, you have a few options:

  • Change RPI swap file size
  • reduce computing resources required by reducing output file quality (by tuning output frame rate, resolution, presets)
  • move concatenation step on higher RAM computer (higher Raspberry PI, notebook, desktop PC)

Following ones are 2 more options that you can use with ffmpeg:

  • -r: sets framerate (default value, if not specified “-r 30”). It can be used both for input media as for output media
  • -preset: sets preset (default value, if not specified “-preset medium”). A preset is a collection of options that will provide a certain encoding speed to compression ratio. Available values are [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo]

Final Gift, Adding a Textbox on Time Lapse Video

A nice final feature for our video is adding a text box on it. Using again ffmpeg and tricks coming from Stackoverflow, we get a Text box with the following terminal command:

ffmpeg -i outVideo.mp4 -vf drawtext="text='peppe8o.com': fontcolor=white: fontsize=24: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)*4/5" -codec:a copy finOUT.mp4

Final Result Example

As final example for results, I’ve prepared a video with a time lapse captured from 45 minutes, with 1 second between each image and restricting width and height to 1280×768 from jpg files:

raspistill -t 2700000 -tl 2000 -w 1280 -h 768 -o image%04d.jpg

The remaining steps follow commands provided in this tutorial. The resulting video can be shown in the following:

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?