This guide assumes you already have a working Raspberry Pi configured ready to go. Please see the article on building a NextCloud server to find out how to install and configure a Raspberry Pi.
In order to complete this 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 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 time-lapse videos
Motion is a much more widely used application for using webcams in Linux. It can record at full frames per second, 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.
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
framerateanywhere in between 1000 to 1500.
Stream_qualityshould be 100.
heightto 1280 & 720.
Snapshot_intervalequals to 10. ( every 10 seconds 1 photo will be taken )
target_diras the path that you have copied earlier.
ctrl + xto exit. Type y to save and enter to conform.
Now, to edit the daemon to start the service.
sudo nano /etc/default/motion
- 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 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 developers 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 color 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.
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 time-lapse videos with us in the comments below.