Easy Time-Lapse Video with Ubuntu
I hate snow. For me, it represents a lot of manual labor for the sole benefit of being able to get out of my driveway. I found myself into about three hours of shoveling the day after an unusual October nor'easter, looking out at my back yard, and reveling in the fact that the predicted warm temperatures for the week ahead will make this 14 inches of snow all go away. Then it hit me. This would make a great time-lapse video; watch it melt over the next eight hours. I have never done time-lapse video before, but I figured I could do it with just a little bit of research and the free software available in Ubuntu. But time was of the essence, and I had to learn quick. Here's how I did it.
First, I needed software to capture video. I had previously used Cheese Webcam Booth and I knew it worked well. I installed it via Ubuntu Software Center and verified that it worked with my webcam, which it did right out of the box. Now, I needed to get my webcam in place for capturing video. I strung together two USB extensions and set up the webcam just outside my home office.
I started up Cheese in video mode, but quickly found that it would take a ton of disk space to capture video over eight hours. After about five minutes I already had about a gigabyte of video. I realized that I needed to capture snapshots over time, which had I put more thought into it at the outset, I should have known that was the way to go.
Cheese has a "burst mode", where it will take a number of snapshots at regular intervals (like an old fashioned photo booth, hence "webcam booth", I suppose). In the "Preferences" dialog, you can specify the number of snapshots to take and the interval between each. I figured I would set it to go for 10 hours, take a snapshot each minute. This would be 600 photos with 60 seconds between each.
I later found that the maximum setting for number of photos in Cheese's burst mode is 100. I was up to 95 shots once I figured this out, so I realized I had to stop and restart it every hour-and-a-half or so. No problem, because I wanted to monitor its progress anyway.
Now that I was capturing photos, I need to find out how to combine them into a video. A quick Google search let me to mencoder, a command-line movie encoder. I found the following command line for combining my JPEG's into a movie file:
mencoder "mf://*.jpg" -mf fps=10 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
This worked nicely, creating a movie file called "test.avi", with just one little problem. The "*.jpg" in the command line feeds files to the program in alphabetical order. I looked at Cheese's file naming convention and quickly realized that alphabetical order is not the same as timestamp order. The first file name was "2011-10-30-105055_1.jpg", which was the date and time at which I launched burst mode, followed by a sequence number. Only the sequence number changes from one snapshot to the next. So, snapshot 2 comes after snapshot 19, and 3 comes after 29, and so on.
This is nothing that a little bit of piping magic can't fix. I can easily use "ls -t" to list files in timestamp order, most recent first. However, I need my files ordered oldest first, and each file name must be preceeded with "mf://". Enter tac to reverse the order of files and awk to modify each file name. My modified command line looks like this:
mencoder $(ls -t *.jpg | tac | awk '{ print "mf://" $1; }') -mf fps=4 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800
List all files in timestamp order, most recent first, pipe through tac to reverse the order, the pipe through awk to tack on "mf://" before each file name, and now I'm feeding the JPEG's to mencoder in the correct order.
So, with all that, after 9-10 hours and a little babysitting with Cheese, I have a time-lapse video. Post it to YouTube and Facebook, and it's there for the world to see. You can see the finished product at http://youtu.be/cKBKlfdPLpA.
For me, the best part of a big snowstorm is watching the snow melt away. And condensing that process into under thirty seconds of video makes it all the better.