Make a time-lapse video

By Phil King. Posted

One of the easiest, and yet most impressive, uses for a Raspberry Pi camera is to film a time-lapse video created from a series of still images.

Sometimes used for interludes in film and TV dramas, to indicate the passing of time between scenes, a time-lapse video is an impressive and versatile effect that appears to speed up time. Classic examples include clouds passing across the skyline, city traffic moving (a lot faster than in reality), star trails, a sunset or sunrise, and plants blooming/growing.

With a Raspberry Pi Camera Module or High Quality Camera, you can easily create effective time-lapses. You just need to set everything up correctly and make sure the camera doesn’t move during the filming period.

Step 1: Connect the camera

You will need to connect the Camera Module or High Quality Camera before switching on your Raspberry Pi, otherwise it won’t be detected. If the ribbon cable isn’t already connected to the camera’s CSI connector, pull the plastic tab out and insert the cable (with the covered blue side facing the plastic tab), then push the tab back in. On the Raspberry Pi, insert the other end of the cable into its Camera port in similar fashion. Note that on a Raspberry Pi Zero, the Camera port is on the edge of the board and is narrower, so requires a special ribbon cable/adapter. Make sure both cable ends are snug and secure – a poor connection can stop the camera working.

Make sure that the  camera is connected  to the correct port

Step 2: Remote access

You can connect the Raspberry Pi to a monitor to check the camera is working – by entering the libcamera-hello terminal command for a five-second camera preview.

When filming a subject, however, you may well want to position your camera away from a monitor – in which case, you will need to access the Raspberry Pi remotely via SSH (Secure Shell) to control it. You can set up SSH in the Raspberry Pi Configuration menu’s Interfaces tab, or in the raspi-config tool from the command line.

Alternatively, you can enable SSH when writing the Raspberry Pi OS image to a microSD card in the advanced options of the Raspberry Pi Imager tool (click the cog icon after choosing an OS), as well as altering the host name and/or setting up the Wi-Fi connection to your router so the Raspberry Pi automatically connects to it on first boot.

If you’re only going to be using the Raspberry Pi remotely, you may want to just install the Lite version of Raspberry Pi OS, which lacks a desktop and therefore takes up less room on the card and is quicker to boot.

You can SSH into the Raspberry Pi from the terminal/PowerShell of another computer using the Raspberry Pi’s IP address:

<b>ssh [username]@[IP address]</b>

You can find the IP address of your Raspberry Pi by entering hostname -I at the command line – or, if you don’t have it connected to a monitor, by checking the devices list on your wireless router web page to find it.

Alternatively, you can use the Raspberry Pi’s host name instead of an IP address. It’s raspberrypi.local by default, but can be set to another name in Imager’s advanced options.

<b>ssh [username]@raspberrypi.local</b>

Either way, you’ll be prompted to accept the connection by typing ‘yes’ (the first time), then asked to enter the Raspberry Pi’s password. You’ll then see the usual command line prompt for it.

Summer has finally come!

Step 3: Position your camera

The main thing to ensure when filming a time-lapse video is that your camera doesn’t move, otherwise it’ll ruin the effect. So it’s best to place it in one of the many Camera Module mounts, which can be bought cheaply – or, if using an HQ Camera, you can screw it onto a standard camera tripod.

An alternative for filming exterior scenes is to stick the camera to a window using a special mount with suction cups, such as the ZeroView which can also hold a Raspberry Pi Zero W on the rear.

At this point, you may want to take another test shot to check the focus and light level for the scene. The V1 and V2 Camera Modules have fixed focus, while the V3 has the option of continuous autofocus. The exposure mode is automatic by default, although you can use the --ev parameter to make it lighter/darker or use one of several exposure scenes, e.g. night, with the --ex parameter. Note that conditions may change during the shoot, however.

The HQ Camera’s focus and aperture can be adjusted manually by rotating parts of the attached C- or CS-mount lens.

Step 4: Take a test shot

The latest versions of Raspberry Pi OS have libcamera-apps pre-installed. While these replace the previous raspistill and raspivid commands, they work almost the same way with a similar range of options. To take a test still image with the camera, enter:

<b>libcamera-still -o test.jpg</b>

You’ll see a stream of information in the terminal as the shot is taken. If you’re using a desktop version of Raspberry Pi OS and are connected to a monitor (or are accessing the desktop remotely via VNC), you can view the photo in the Image Viewer desktop application. If not, you can copy it to your local computer using SCP (Secure Copy):

<b>scp [username]@[Pi's IP or hostname]:test.jpg [/local/path/to/save]</b>

Enter the Raspberry Pi’s password to proceed. Check that the test shot looks OK before proceeding with filming the time-lapse. Otherwise you may want to reposition the camera or alter some of libcamera’s settings – see the documentation for more info.

We love watching clouds move in fast-forward

Step 5: Let’s do the time-lapse

You can start shooting a time-lapse with a single libcamera-still command with the --timelapse option and a few parameters. For example:

<b>libcamera-still -t 30000 --timelapse 2000 -o image%04d.jpg</b>

Here, the number after -t is the total duration in milliseconds, in this case, 30 seconds. The number after --timelapse is the interval between images in milliseconds, in this case, 2 seconds. Finally, we use %04d to add a four-digit number to the file name, after ‘image’, starting with ‘0000’.

By varying the timings, you can take much longer time-lapses with more or less frequent image captures. For slower scenes, such as clouds and star trails, use a longer interval between captures; for faster ones, such as traffic or people, a shorter interval is better to capture the movement without it appearing too disjointed. If you’re shooting a long duration via SSH, you may want to use the nohup prefix before the command. Short for ‘no hangup’, this will ensure it continues working even if the SSH connection is lost.

Step 6: Compile the video

Once you have your sequence of stills, it’s time to compile them into a time-lapse video. For this, a good option is the ffmpeg command-line tool. It should be pre-installed in the latest version of Raspberry Pi OS; if not, install it with:

<b>sudo apt install ffmpeg</b>

Compile the time-lapse video from your images with a command like this:

<b>ffmpeg -r 10 -f image2 -pattern_type glob -i 'image*.jpg' -s 1280x720 -vcodec libx264 timelapse.mp4</b>

Here, the number after -r is the frame rate, in this case, 10 fps – try varying it for different results. The next part tells it to pattern-match your set of image files using the * wildcard. The numbers following -s (scale) determine the video resolution. The final part uses the x264 encoder to save the video as an MP4 file.

Depending on the Raspberry Pi model and the number of images, this may take a while to complete. A Raspberry Pi 4 is pretty quick, but other models are slower (a Raspberry Pi 3 will encode around two frames per second). So you may instead want to transfer the image files to a more powerful computer via SCP:

<b>scp [username]@[Pi's IP or hostname]:image\*.jpg [/local/path/to/save]</b>

…and then use ffmpeg on that to compile the video much faster. Now you know the basics, have fun experimenting with different subjects and interval/frame rates to create impressive time-lapse videos.

From HackSpace magazine store

Subscribe

Subscribe to our newsletter