In this post I will explain how I made some timelapse videos using the Raspberry Pi camera module. This is a two step process which involves getting the camera module to take a series of stills over a period of time and then combining them into an MP4 video file.
We can use the “raspistill” utility to take a set of time lapsed photos as described in my Taking Hi-Res Photos With The Pi Camera Module article. Make sure you have installed the camera and updated your operating system.
Step 1 – Taking the time-lapsed photos
This command will take a photo every 60 seconds (60000 milliseconds) for 2 hours (7200000 milliseconds) resulting in a sequence of 120 images.
raspistill -o myimage_%04d.jpg -tl 60000 -t 7200000
The “%04d” will result in a four digit number appearing in each filename.
myimage_0001.jpg myimage_0002.jpg ... myimage_0119.jpg myimage_0120.jpg
Step 2 – Combine images into MP4 video
Once you’ve got your image sequence you will need a method to stitch them together. I decided to use “avconv”. You can install this useful library with the following command :
sudo apt-get -y install libav-tools
To construct the video file from your image sequence you use the command shown below. Although it appears on multiple lines for readability it should be entered as a single line on the command line :
avconv -r 10 -i myimage_%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 timelapse.mp4
The video will be the full resolution of the default image size (2592×1944).
To crop the images and create a more standard 1280×720 resolution video you can use the following command :
avconv -r 10 -i timelapse_%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 timelapse.mp4
The “vf” option defines a video filter. In this case two filters which crop the incoming image to 2592×1458 and then scale them to 1280×720.
The “r” option tells avconv to create a video with a frames per second of 10. It appears twice to prevent avconv dropping frames that it thinks are similar.
The “crf” option tells avconv to aim for a quality level of “20″ which is a good starting point. Lowers values are better but will increase the file size.
The “-g” option sets the GOP value. The YouTube Advanced Encoding Settings page recommends that the GOP should be set to half the frame rate so this is set to 15.
The conversion process is very slow on the Pi compared to doing the same thing on a desktop PC. For long sequences with hundreds of frames I would recommend downloading an appropriate version of Libav on your desktop or laptop and build your MP4 files much faster!
use ffmpeg
When using ffmpeg to compress a video, I recommend using the libx264 codec, from experience it has given me excellent quality for small video sizes. I have noticed that different versions of ffmpeg will produce different output file sizes, so your mileage may vary.
To take a list of images that are padded with zeros (pic0001.png, pic0002.png…. etc) use the following command:
ffmpeg -r 60 -f image2 -s 1920×1080 -i pic%04d.png -vcodec libx264 -crf 15 -vpre normal test.mp4
where the %04d means that zeros will be padded until the length of the string is 4 i.e 0001…0020…0030…2000 and so on. If no padding is needed use something similar to pic%d.png.
- -r is the framerate
- -crf is the quality, lower means better quality, 15-20 is usually good
- -s is the resolution
- -vpre is the quality setting, better quality takes longer to encode, some alternatives are: default, normal, hq, max
the file will be output (in this case) to: test.mp4
Note: the -vpre command only works if the corresponding setting file is available
Finer Bitrate control (to control size and quality)
- -b 4M
you can use the -b flag to specify the target bitrate, in this case it is 4 megabits per second
Testato:
ffmpeg -r 5 -i images_%04d.jpg -vcodec libx264 -crf 10 -vpre hq test.mp4