The Raspberry Pi Camera Module: Part 3 - Making Time-Lapse Videos

In Part 1 we set up your Raspberry Pi camera module with your Pi to enable us to start taking shots. In Part 2 we went through some simple commands to take standard "out of the box" photos and videos with the camera module.
Here in Part 3 I'll show you how to make a clever time-lapse video using the camera module to take the pictures, and a Windows PC to compile them into a video. I'm still figuring out how to get the Pi to complete all of these tasks without the need for a PC, but I've come up against some bugs so will leave that for another day.
Let's get started...

My time-lapse position


A time-lapse video is simply a large number of photos taken from the same physical position over a period of time, stitched together to make a video. This then gives the appearance of time progressing much faster than usual, and works well on things that take a long time in real life, such as sunset/sunrise, tides and retirement!
What we want to achieve here is a photo taken by the Pi every so often (say 5 seconds) over a period of time (say 2 hours) and then put these together into video file format (Avi in this example).

Setup
Ideally you will have followed the first two parts in this series, which means you'll have your camera module hooked up and the necessary applications installed on your Pi. If not, go back now and make sure you have.
This part is actually quite simple in terms of the Pi, as we're already set up and ready to go. We just need to alter the code we use.

Location
Now I've been talking about doing a beach sunset time-lapse for a while, but I haven't taken delivery of my power bank yet (to power the Pi away from home) so for now i'm just going to use the window of my apartment to capture sundown after work on a Friday evening. I've taped my Pi Pod case to my window rail using a bit of masking tape...classy!

The wife loved this...


When doing yours, try and choose a scene that will change gradually over time, or that has a lot of movement. Sunsets, long journeys, busy roads etc are perfect for this.

Want a Pi Pod case? Get one on Amazon.


Code
The code we use for a time-lapse shot is very similar to the command we used in Part 2 for taking regular pictures, just with a few bits added. I'll show you what I used for a 2 hour duration, and you can change what you like to suit your needs. I think a bit of trial and error is needed here to work out what different settings will produce
I used:

 raspistill -o timelapse%04d.jpeg -tl 5000 -t 7200000  


Let's break it down so you can see which bits you can change:

  • raspistill - this is the command to say to the Pi "we're going to take a picture"
  • -o - this tells the Pi to save the output, rather than just giving us a preview and discarding
  • timelapse%04d.jpeg - this sets the output file names (because we're going to take a lot of shots). 'timelapse' is the first part, then the '%04d' is telling the Pi to save file names with a 4 figure number at the end. '.jpeg' is the image file type we will use.
  • -tl 5000 - this is saying to the Pi "take a shot every 5000 milliseconds (every 5 seconds for us non-robots)
  • -t 7200000 - this is telling the Pi to do all of this over and over for 7200000 milliseconds (2 hours to you and me) and then stop.


So as you can see, there are a lot of numbers and names you can play with.
You could change that 5000 to 2500 to take a shot every 2.5 seconds, good if you want a smoother video with more detail. The 7200000 could be changed to 3600000 to run for just 1 hour, or whatever you want. The filename could also be changed.
Got it? Great! Let's move on...

Lights...Camera...Action!
Fire up your Pi, jump into Terminal and pump that command in. The red LED on your camera module should be lit when it's busy. Terminal won't do anything, it will simply sit with a clear space under the command until it has completed.

This LED lights when the camera is in use


I set a timer for the same amount of time, to give me a heads up when it should be finishing my artistic masterpiece.
Once complete, use the following Terminal command to list the contents of the folder you added the images to.

 ls -al  


You should see a list of your images (starting with timelapse 0001) appear, alongside other files in that folder:
All good? Let's continue...

Images Into Video
So you've got a load of images now (I had 1400-odd), all in name order (timelapse0001, timelapse0002 etc). So, it's time to stitch these together and make a video.
There are a couple of methods floating around that get the Pi to make the video, however one only uses the slow processor (takes ages) and the other...I can't get to work just yet. What can I say, i'm 'Average Man'...
So for this tutorial we'll be using a Windows PC to compile our shots. You need to get your files over to a Windows machine - I used Filezilla, and there's plenty of tutorials around so I won't cover that here.
1. First, download VirtualDub (free):
VirtualDub [SourceForge]
2. Open up VirtualDub


3. Go to File > Open Video File - select the first image from your timelapse. Make sure "Automatically load linked segments" is checked - this will grab all the other images for you, clever huh?


4. The first image will now show on screen, all big and nasty. It's now time to set the framerate.
Go to Video > Frame Rate - I set mine to about 24fps, but have a play with different values and see what you prefer.


5. This step is optional, but probably worthwhile as the default Raspberry Pi camera shot is an impressive 2592x1944, and for things like YouTube, you don't need all that.
Go to Video > Filters - click 'Add' and then find and click on 'Resize'. Press 'OK' and a new box will show giving you options to resize your video.


Use the absolute setting if you know what size you're looking for - I entered 1280 in the first box which automatically gave me 960 in the second box. 1280x960... that'll do.
Click 'OK' and it's set.
6. The final bit of fiddling is compression. We're saving this video as an Avi file, so we need to add compression manually. You'll need the xvid codecs for this part, most of you will have them already - download from here if not.
Simply click on Video > Compression and select the Xvid MPEG-4 Codec and click 'OK'.


7. We're ready to make the video. You can preview your video, or walk on the wild side and just go for it.
When you're ready, click on File > Save as AVI - you'll be asked where to save it, and what to call it, and then the conversion will commence. It shouldn't take too long for a minute or 2 video (hardware dependant). A handy progress box shows to keep you informed.

Done!
If that all went as planned, you'll get a fully working Avi file at the end of it. Here's mine, made using the settings in this tutorial:


I hope that helped get you started on your photographic journey. I'm certainly going to have some fun with this, and will no doubt post up time-lapse videos of car journeys, ice hockey games, rotting food and the like. I think I'll play with the settings a bit more to improve the output. Feel free to post links to your Raspberry Pi timelapse videos below in the comments section.

Continue reading here: Make Your Raspberry Pi Tweet Its Temperature

Was this article helpful?

0 0

Readers' Questions

  • guy
    How to take photos and videos with raspberry pi camera module?
    1 year ago
  • To take photos and videos with the Raspberry Pi Camera Module, you will need to install the Raspberry Pi Camera software, which can be found in the Raspbian OS repository. Once installed, you can use the "raspistill" and "raspivid" command-line tools to capture photos and videos respectively. You can also use various third-party programs, such as Python or C/C++, to interact with the camera module.