How to Create Time-lapse Videos with Raspberry Pi and USB WebcamHow to build a Raspberry Pi Time-lapse camera to capture photos over an extended period of time and merge them into a timelapse video.
Create Time-lapse videos using a Raspberry Pi to take a picture from a webcam at regular intervals, such as every minute, then use an application to stitch the pictures together into a video. Times flies. This guide assumes you already have a working Raspberry Pi configured and ready to go. Please see the article on building a NextCloud server to find out how to install and configure a Raspberry Pi.
To complete this Raspberry Pi Time-lapse project yourself you will need:
- Raspberry Pi running Raspbian (or another OS), configured, running and accessible on the network or directly connected to KVM
- Internet Connection
- USB Webcam
- Optional: Powered USB Hub. Depending on the Raspberry Pi model and webcam, the Pi may not be able to supply enough power to the webcam. A powered USB hub should solve this.
There are two ways of using a Raspberry Pi as a time-lapse recorder. We can either use the built-in Raspistill
application if you are running Raspbian, or we can use the much more powerful >code>motion application which works on all variants of Linux.
Using Raspistill inbuilt Time-Lapse Mode
The raspistill application has a built-in time-lapse mode, using the --timelapse (or -tl) command line switch.
The command takes a few options, but the one we are interested in is --timelapse followed by the time between shots, in milliseconds, and the -t parameter which is the maximum length to record the timelapse for.
Let's say we want to take a shot every 30 seconds for the next hour. The interval for --timelapse would be 30 seconds * 1000 = 30000, and the maximum length given to the -t parameter would be 60 minutes * 60 seconds * 1000 = 3600000. The next parameter is the output destination image name, which we can use as a sequential number in the filename.
The command then looks like this:
raspistill -t 3600000 -tl 30000 -o image%04d.jpg
The %04d in the output filename indicates the point in the filename where you want a frame count number to appear. It will generate image1.jpg, image2.jpg, image3.jpg and so on.
You can read more in the Raspistill documentation.
Using Motion to Capture Raspberry Pi Time-lapse Videos
Motion is a much more widely used application for using webcams in Linux. It can record at full frames per second, and capture stills and time-lapse videos. It is widely available in many distribution repositories so compatibility shouldn't be an issue.
To install motion, type in the command to start the installation.
sudo apt-get update && sudo apt-get upgrade sudo apt-get install motion
Once that is complete we can make sure that the camera is correctly detected and the driver has been installed. We can do this with the lsusb
command. Simply type it in and hit enter.
lsusb
You should see the name of your camera listed. If it is NOT there, then there is some problem with your camera or the camera is not supported in motion.
techman@raspberrypi:~$ lsusb Bus 005 Device 001: ID 0000:0000 Bus 004 Device 001: ID 0000:0000 Bus 003 Device 001: ID 0000:0000 <strong>Bus 002 Device 002: ID 045e:00f7 Microsoft Corp.</strong> Bus 002 Device 001: ID 0000:0000 Bus 001 Device 004: ID 046d:c03f Logitech, Inc. UltraX Optical Mouse Bus 001 Device 001: ID 0000:0000
If all is well we should be able to go ahead and configure motion.
sudo pico /etc/motion/motion.conf
You need to set a few of the options in the config file to these values.
- Make sure
daemon
is ON. -
Set
framerate
anywhere in between 1000 to 1500. -
Stream_quality
should be 100. -
Change
Stream_localhost
to OFF. -
Change
webcontrol_localhost
to OFF. -
Set
quality
to 100. -
Set
width
&height
to 1280 & 720. -
Set
Snapshot_interval
equals to 10. ( every 10 seconds 1 photo will be taken ) -
Set
target_dir
as the path that you have copied earlier. -
Press
ctrl + x
to exit. Type y to save and enter to conform.
Now, edit the daemon to start the service.
sudo nano /etc/default/motion
- Set
start_motion_daemon
to yes. - Save and exit.
To start capturing photos simply type the command and hit enter.
sudo service motion restart sudo motion
Stitching Multiple Images into a Raspberry Pi Time-lapse Video
When you have all the images you need we can then join the images to make a video. Since this process can be quite CPU intensive I found it easier to transfer the images to my desktop Linux machine. The commands are the same, you can do this on your Raspberry Pi as well, it'll just take a lot longer.
We are going to use ffmpeg to convert the images to video. FFmpeg is a cross-platform solution for streaming audio and video as well as recording and conversion. It's also useful to convert multimedia files between various formats.
We must first add the developer's PPA repository then we can install the software. Just type in these commands and let the Raspberry Pi do the work.
sudo add-apt-repository ppa:jonathonf/ffmpeg-3 sudo apt-get update sudo apt-get install ffmpeg libav-tools x264 x265
Next, chdir into the folder containing all the captured frames. We will issue one single command which will take these frames and convert them into a video which you can upload to Facebook or YouTube.
ffmpeg -r 30 -pattern_type glob -i "*.jpg" -c:v libx264 -pix_fmt yuv420p -movflags +faststart timelapse.mp4
This will output an x264 encoded MP4.
- -r 30 is the framerate, 30 fps
- -pattern_type glob is for creating a video from filenames matching the glob pattern
- -i "*.jpg" is the glob pattern, in this case picking up all jpegs,
- -c:v libx264 specifies the libx264 codec
- -pix_fmt sets the pixel format, in this case, the YUV colour encoding system
Depending on the number of frames you captured and the specification of the machine running it, this process will take several hours or even days to complete.
If you need to resize the frames to shrink the video you can do that easily with ImageMagick.
mogrify -resize 1920x1080 *.jpg
This will resize and overwrite the source image, so be sure to work on copies, not the originals.
Raspberry Pi Time-lapse Sample Video
The final results are sent to an MP4 video which you can then share on the internet or keep for yourself. Here is an example video I took over the winter of snow falling during the day.
If this tutorial was useful to you, why not share your Raspberry Pi Time-lapse videos with us in the comments below?