Time Lapse Video using gphoto2 and ffmpeg
An interesting little project I’ve been working on is time lapse photography. I picked up a used Canon Powershot A520 pretty cheap, and set up a laptop with Ubuntu to communicate with the camera. I’m still working on the best angle to minimize the power lines out front, but I’ve got a good start going.
What you’ll need:
- A Linux machine (a laptop really helps)
- gphoto2 >= 2.4.5 (note that you can upgrade jaunty’s gphoto2 with the karmic packages to get this version)
- A camera that supports remote capture
- An AC power outlet near were you want to take your photos (and an AC adapter for your camera unless you have really awesome batteries).
- jpeg2yuv and ffmpeg (with libx264 support)
- Something relatively interesting to take pictures of
This is what I ended up with:
So here’s what I did:
- Connect the USB cable to the camera
- Run the following command (in a while loop in case it crashes):
while true ; do gphoto2 --capture-and-download -I 30 done - Wait about 8 hours or so
- If you’re impatient like me, you can nfs mount the laptop after about 45 frames (about 20 minutes) and get a preview.
- You can rsync the laptop’s nfs mounted directory locally so you don’t have to copy the files over (most likely) wireless every time you want to encode the latest version
- Collect all of your images and make sure that each frame is numbered sequentially.
- Create an MPEG with jpeg2yuv by piping the output to ffmpeg:
starframenum=XXXX # put the number of the first image in the sequence here jpeg2yuv -b $startframenum \ -v 0 \ -j the/path/to/your/images/IMG_%04d.JPG \ -f 15 \ -I p | ffmpeg -threads 2 -y -i - \ -vcodec libx264 \ -b 2500k \ -acodec libfaac -ab 48k -ar 48000 -ac 2 -s 1024x768 -f mp4 \ outputfile.mp4
In the options above, the important ones are ffmpeg “-f” which is the framerate. You can change this to speed up and or slow down your movie. The “-s” option is the size. Keep in mind that the width and height of your images needs to be a multiple of 16 (ie, 640×480, 1024×768, 1920×1152, etc). Note that 1080 is not divisble by 16. 1280×720 will work for widescreen (16:9) hi-def though. Lastly, the “-b” option is the video encoding bitrate. Increase it for better quality and decrease it for smaller output movie files.