Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

Time lapse video from Raspberry PI camera

RPI camera time lapse featured image
0
(0)

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

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 new time-lapse video.

Before digging in technical commands, a breaf introduction in what time lapse video is.

Time Lapse Technique

Looking 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 of still frames, extracting them from a video stream or capturing these frames from a camera with defined intervals. Still images, consolidaded on a smaller interva so that they can appear to human eye as a continuous flow, gives illusion that time is running faster on video.

In following picture this process is described with an example, where from a 15 fps (frame per second) we take 1 image every 5 and compress extracted images in a new video. Images that whould be showed after 5 seconds (from frame 76) will appear in 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 needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing 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 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 terminal, use following command:

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

We also need ffmpeg:

sudo apt install ffmpeg

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

sudo raspi-config

Terminal will show 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 fist option (Camera) and press ENTER. In next screen move selectio from “No” to “Yes”:

raspi-config interface options camera pi3 model A+

Press ENTER and confirm also in following screen.

raspi-config interface options camera enabled pi3 model A+

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

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

raspi-config reboot pi3 model A+

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

Capturing images

Create a folder where to store images and enter it:

mkdir timelapse
cd timelapse

With 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, following parameters are used:

  • -t 30000: this option will make command lasting for a total of 30 seconds
  • -tl 2000: this option produces a capture (frame) every 2 seconds
  • -o image%04d.jpg: every frame captures 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 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 list of images that ffmpeg will put together creating final video. This will be created with 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 current folder whose name starts with “image” and ends with “.jpg”. Wildcard “*” will consider good any character and any digits amount between those 2 strings. Each file name will be appended to a new stills.txt within a “file ‘……'” prefix. Output will be like following one:

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 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, make possile getting 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 favoutire media player (supporting mp4 format, like VLC).

As you can see, we already used -s option to reduce computing power required 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 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 nRaspberry PI, notebook, desktop PC)

Following ones are 2 more options tat 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 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 vith 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

Remaining steps follow commands provided in this tutorial. Resulting video can be showed in following:

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

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?

Leave a Reply

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

I accept the Privacy Policy