Set up your Mac to encode video for you

by Will Head on May 18, 2010

Will Head

Will Head

With an open-source video encoder, you can say goodbye to the time-consuming process of producing multiple versions of a final video edit.

There’s never a single best format for video – it all depends on where the footage will end up and what it’s being used for. A piece that you output for DVD isn’t suitable for watching on an iPod, and a clip optimised for HD TV will be too big for Internet streaming. Ultimately, this means that sometimes, you’re going to have to produce more than one version of a final edit, which is a time-consuming process. While there’s no way of cutting down the actual encode time, you can make producing multiple versions more efficient – so you can just set your machine going and let it get on with creating all the copies you need.

The idea is to create a single high-quality encode and then produce all the necessary versions from that, so you’re only exporting a single file from your editing application – all further encodes will be performed independently.

In order to do this, we’re going to use FFmpeg, which is an open-source video converter. It can handle more than 100 different codecs, making it incredibly flexible when it comes to outputting various different formats. The downside, however, is that it’s not exactly easy to set up. Installation takes a fair bit of command line tinkering, but you only have to do it once to get a working version.

FFmpeg is distributed in source code form, which means you’ll need to compile it into an executable program on your Mac to get it to work. The first step in this process means stopping by Apple’s developer site and picking up a copy of its Xcode developer tools, which installs all the necessary components to compile your own programs.

If you’re running Snow Leopard, simply point your browser at  developer.apple.com/technology/xcode.html and click the Download Now button next to where it says: Xcode for Mac-only Development. You’ll need to register for an Apple Developer Connection (ADC) account if you don’t already have one, but it’s free. After you’ve logged in, download the latest Xcode version – which was 3.2.1 at the time of writing.

If you’re still on Leopard, you’ll need to go to connect.apple.com instead. Log in with your ADC account, click on Downloads, then Developer Tools on the right-hand side. Scroll down to Xcode 3.1.4 Developer Tools and download that version instead.

Launch the installer, choose the default option, and then sit back and let it get on with the process – it takes a while to get everything set up.

The next step involves dipping into the Terminal application and entering commands manually. It’s not particularly hard, but you do need to follow the instructions carefully, as you will be altering system files on your Mac. It’s well worth backing up at this point, just in case.

Create a temporary directory in Finder to store all the files, while you’re compiling them – somewhere like Users/Shared/Temp on your Mac hard drive. Start Terminal and then type:

cd /Users/Shared/Temp/

This puts you in the same temporary directory you created using Finder.

Before setting up FFmpeg, you need to install some extra files to provide support for formats like MP3, AAC and H.264 encoding. First up is Lame, which provides MP3 support. In the same Terminal window, you’ll need to type:

mkdir lame

This creates a directory to store the Lame installation files in.

Next, go to sourceforge.net/projects/lame/files and download the latest version of Lame – it’ll be under the Newest Files label, which at the time of writing was lame-398-2.tar.gz – and save it into the lame folder you created (/Users/Shared/Temp/lame).

Back in the Terminal type:

cd lame

To extract the files from the archive enter:

tar zxvf lame-398-2.tar.gz

In Finder, you should now see a folder called lame-398-2 inside /Users/Shared/Temp/lame. In Terminal type:

cd lame-398-2

The final stage is to configure and install Lame. To do that, enter the following three commands, pressing return after each one:

./configure

make

sudo make install

The last command will prompt you for your password. After you enter each command, you’ll see a lot of mostly indecipherable text scroll up the screen. Unless you see an error when it’s finished, it’s safe to ignore this. To check that Lame installed successfully, in Terminal type:

lame

You should see:

Lame 32bits version 3.98.2 (http://www.mp3dev.org/)

usage: lame [options] <infile> [outfile]

With Lame successfully installed, you now need to add AAC support using two packages – FAAD to decode AAC and FAAC to encode AAC. To start with FAAD, go back to the Terminal and type:

cd /Users/Shared/Temp/

mkdir faad

Next, download the FAAD source files from sourceforge.net/projects/faac/files. Again, it will be under the Newest Files heading, and at the time of writing faad2-2.7.tar.gz was the latest. Save the file to your /Users/Shared/Temp/faad directory.

Over in Terminal type:

cd faad

tar zxvf faad2-2.7.tar.gz

cd faad2-2.7

./configure

make

sudo make install

Again, you’ll see plenty of text scroll up the screen and provided no errors are reported, you should be able to type the following into Terminal to confirm FAAD was installed successfully:

faad

FAAC is next, which is the same process. Start by typing the following in Terminal:

cd /Users/Shared/Temp/

mkdir faac

Now we need to grab the FAAC package. It’s on the same page as FAAD (sourceforge.net/projects/faac/files), but under Newest Files it’s faac-1.28.tar.gz. Save the file to your /Users/Shared/Temp/faac directory and in Terminal type:

cd faac

tar zxvf faac-1.28.tar.gz

cd faac-1.28

./configure

make

sudo make install

To check that FAAC installed correctly, in the Terminal type:

faac

The next stage is to add support for H.264 encoding using the x264 library. In Terminal, you’ll need to type:

cd /Users/Shared/Temp/

mkdir x264

Get the latest version of x264 by going to videolan.org/pub/videolan/x264/snapshots and scrolling to the bottom. In this case, download x264-snapshot-20100225-2245.tar.bz2 to /Users/Shared/Temp/x264, then in the Terminal, type:

cd x264

tar xvf x264-snapshot-20100225-2245.tar.bz2

cd x264-snapshot-20100225-2245

./configure –enable-shared –disable-asm

make

sudo make install

To check everything installed successfully, type the following into Terminal:

x264

It gives the following error, but at least you’ll know that it’s installed:

x264 [error]: No input file. Run x264 –help for a list of options.

Now all the supporting files have been installed, you’re now ready to set up FFmpeg itself. Enter the following:

cd /Users/Shared/Temp/

mkdir ffmpeg

To get the latest version of FFmpeg, you’ll need to use a command called svn to download it directly. To do this type:

cd ffmpeg

svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg-src

Next, you need to configure FFmpeg to include all the supporting files that you downloaded earlier.

First type:

cd ffmpeg-src

Then enter the following command, all as one line:

./configure –enable-gpl –enable-postproc –enable-swscale –enable-avfilter –enable-avfilter-lavf –enable-libmp3lame –enable-libfaad –enable-libfaac –enable-libx264 –enable-shared –enable-pthreads –enable-nonfree –arch=x86_64

Once that has completed, type the following to actually install it:

make

sudo make install

Once it’s finished churning out text, FFmpeg should finally be ready to use. Enter the following in Terminal:

ffmpeg

If everything went to plan, you should see:

FFmpeg version SVN-r22075, Copyright (c) 2000-2010 the FFmpeg developers

built on Feb 26 2010 14:38:01 with gcc 4.0.1 (Apple Inc. build 5493)

configuration: –enable-gpl –enable-postproc –enable-swscale –enable-avfilter –enable-avfilter-lavf –enable-libmp3lame –enable-libfaad –enable-libfaac –enable-libx264 –enable-shared –enable-pthreads –enable-nonfree –arch=x86_64

Now FFmpeg is up and running, you can start using it. It offers a lot of control over the output it creates, but it’s also happy to fill in the blanks if you just give it minimal instruction. For example, if you wanted to turn a 1080p H.264 encoded file into one suitable for burning to DVD, you need only enter:

ffmpeg -i testfile.mp4 -target pal-dvd testfile.mpg

The -i argument tells it that testfile.mp4 – the 1080p H.264 file – is the input, -target pal-dvd means you want a DVD compatible file, and testfile.mpg tells it to save it as an Mpeg.

If you wanted a bit more control, for example to produce a 720p H.264 file that’s small enough for Internet use, then the following would work:

ffmpeg -i testfile.mp4 -acodec libfaac -ab 128k -vcodec libx264 -vpre hq -b 4096k -s hd720 testfile-720-4k.mp4

This results in a 1280 x 720 resolution H.264 file with a video bitrate of 4096kbits/sec and AAC audio at 128kbits/sec. If you also wanted an iPod compatible version as well, then this would do the trick:

ffmpeg -i testfile.mp4 -acodec libfaac -ab 128k -vcodec libx264 -vpre hq -vpre ipod640 -b 1024k -s 640×360 -aspect 16:9 -f ipod testfile-ipod-1k.mp4

FFmpeg offers a bewildering array of options for each setting, so it does take some time and tweaking with test files to get the exact result you want. However, once that’s done, you can then re-use the settings again and again easily. As it’s command line based, you can even write a simple script that takes your input file and then outputs a 720p version for online, a Pal DVD copy and an iPod compatible video as well – all one after another without any further interaction on your part.

You do need to invest quite a lot of time to getting FFmpeg setup and working to your specification, but once that’s done, it’s a great timesaver and an invaluable tool to have at your disposal.

For more breaking news and reviews, subscribe to MacUser magazine. We'll give you three issues for £1

Previous post:

Next post:

>