Wednesday, June 9, 2010

Editing H.264 in Ubuntu

It took weeks of searching through marginally informative posts to get enough information together to edit the HD video captured by the 5D Mark II in Ubuntu 10.04, but I'm happy to say that I have at least three solutions at this point with which I'm very pleased. HD video captured with the 5D Mark II is stored using the H.264 video codec and raw 16-bit PCM audio, all of which is wrapped in a QuickTime container. Having never gotten very serious about video before, at least two weeks were dedicated to constructing that one sentence.

The first approach to dealing with this absolutely stunning video is to use the Pitivi video editor, which is included with the Lucid Lynx release of Ubuntu. The problem with using Pitivi is that the probability that rendering will fail increases with increasing project size (at least as of version 0.13.4). I was unable, for example, to render projects that included more than about eight minutes of video. This is a known issue and I expect a fix soon. The other problem is that it's missing some basic features like transitions, but the interface is so simple and intuitive, you'll be able to pick it up in a few minutes. For a version number like 0.13.4, you can hardly complain and I'd use Pitivi immediately for anything under about 5 minutes, pulling in images created in GIMP for titles, credits, and etc.

I had the most success rendering with the default settings (Ogg container, Vorbis audio encoding, Theora video encoding), but neither Facebook nor YouTube were happy with these. Both Facebook and YouTube were happy, however, with renders to an MP4 container with XViD (xvidenc) video and AAC (faac) audio encoding, but I really had to do a lot of work to make these available in Ubuntu because neither encoder is provided with the distribution version. If you want to use Ubuntu for this type of editing, you're going to have to bite the bullet and compile some things yourself. Visit the Ubuntu forums for more information. For some reason, I have yet to get x264enc to work for anything, which is why I'm still using XViD.

The link to the forum post above, in fact, brings me to solution number two, ffmpeg. I'm fully convinced that this command-line utility would do everything I ever needed to do with video if I understood it well enough. Until then, here are a few simple recipes I've put together for working with video from the 5D Mark II. If you want to use Cinelerra to edit the video, use ffmpeg to convert it to a more palatable form as follows:

ffmpeg -i X -vcodec mpeg4 -b 3000k -s hd720 -acodec libfaac -ab 256k -ar 44100 Y.mov

where X is the input (e.g. MVI_0001.MOV) and Y is the output (e.g. MVI_0001.MOV again in this case). After this conversion, Cinelerra will play both the audio and video tracks and the -s hd720 is only required if you'd like to reduce the size of the video before using it in Cinelerra. Cinelerra is, in fact, my third solution, but it requires ffmpeg as a preprocessing step.

If you want some compression before uploading to Facebook or YouTube, use the following:

ffmpeg -i X -vcodec copy -qscale 2 -s hd720 -acodec libfaac -ab 128k -ar 44100 Y.mp4

And if you want to trim a segment out of the video first, use the following:

ffmpeg -i X -vcodec copy  -acodec copy  -ss HH:MM:SS -t HH:MM:SS Y

where the first time (-ss) is the start time and the second time (-t) is the duration of the video segment you'd like to keep. I oscillate between specification of the bitrate (-b) and the quality (-qscale). You only need one or the other and I think specifying the quality is probably the easiest unless you're dealing with a hard bitrate requirement.

So there it is. Hop on Ubuntu, follow the ffmpeg directions in the link above, use the handful of recipes provided, and check out Pitivi and Cinelerra for more advanced editing. The best places to look for help are the link above and man ffmpeg. Good luck!