Best codecs for video and how to encode

Build the ultimate media network
Transcoding a video between formats takes a lot of CPU power

Our mobile devices are faster, the way we watch videos on our TVs has changed, and the way we buy and store content is completely different from 20 years ago. We're in the middle of a home media revolution, where shelves of DVDs are being pushed into NAS boxes and discreetly hidden away.

We're only in the infancy of this revolution, though. That means we've yet to find a definitive method, and there are as many good solutions as there are pitfalls.

Codecs are the best example. If you've had anything to do with digitised video over the last 10 years, you'll know that video performance is dominated by the black art of manipulating these encoding and decoding programs. Finding the right one is a balancing act between the amount of processing that goes into creating the video file and the amount required to decode it.

Too much on the input and you'll be waiting until next Wednesday for your file. Too little and the new generation of HD decoders plugged into your TV won't have enough grunt to reconstruct the video stream at 60fps.

Codecs call for careful consideration. That's why, for high definition, it's often better to use a codec designed specifically for the job rather than one from the '90s shoe-horned into a higher bit-rate and resolution than it was ever intended for.

The same is true for any other disparate playback devices. You get the best results from a movie encoded specifically for your usage scenario, whether that's mobile access across the 3G network or playback on a wireless tablet.

We're going to help you solve these challenges using a mix of hand-on tweaks, free software and plenty of acronyms, taking you from 1990s DVD diva to 21st century streaming media mogul. You'll find everything you need to become an expert over the next six pages.

The best codecs

Best codec for high definition

H.264 & MKV


Before we start, we need to first make a distinction between the codec used to store the video data and the container used to encapsulate everything else, because the two are often confused.

The most popular container for high definition content, for example, is called Matroska, as denoted by its MKV file extension. Matroska isn't a codec in itself, because it doesn't define how to encode and decode the video data - it simply stores the bytes from a codec in such a way that MKV-compatible applications know where to find everything for playback. That could be the video, audio and subtitle data (if present), all of which can be encoded using different MKV-compatible formats and codecs.

That's why you always have a choice of which video codec to use with a container. With MKV files, for example, the choice is usually between H.264, MPEG-4 and VP3, the latter of which is based on Theora. All three use similar technology, but the first two are far better than the third.

For high definition, we'd recommend H.264. This is often represented by x264, which is the free software implementation of the codec.

Bit-rates and resolution are important too. 1080p source material has 2.25 times more pixels than 720p, and higher rates can be harder to decode on your playback hardware. As a rough approximation, we recommend generating a file with a size within the 8-12GB region for a typical two-hour HD movie. To maximise quality, aim for a 12GB+ file with a bitrate of 10+ Mbps.

As for the choice of audio codec, this is down to your AV setup's capabilities. Our preference is not to touch the audio at all and use the 'pass-through' option in your encoding software. DTS and AC3 audio streams can be passed within MKV, and this will embed an exact copy of the audio tracks, as found on your original media, within the MKV file, which should play back on your equipment the same way it would with the original.

Best codec for streaming



H.264 has become one of the most common codecs for high-quality streaming across the internet thanks to portals like YouTube and Vimeo. It's therefore no surprise that it's good at providing a high quality, homogeneous and predictable video stream across a limited bandwidth.

It's a great choice for streaming too, if you encode your video and drop it into a MOV or MP4 file, but we've found that its closely related alternative, MPEG-4, offers a similar pedigree and is usually a better choice at low bitrates over limited bandwidth.

The relationship between MPEG-4 and H.264 is complicated, as they're both parts of a wider MPEG-4 specification, but it's also a codec that's closely related to the data on a DVD that's streamed as digital television. That means it can produce better and more robust results under bandwidth and processor limitations.

There are as many container formats for MPEG-4 as there are for H.264. They include stalwarts like AVI, MOV and the raw transport stream (TS), which you will often have found dumped from your DVB hardware, as well as newer variants like MP4 and MKV. Which you choose will depend on the compatibility of your playback device, but encoding them shouldn't be too difficult.

The only reliable free tool is the open source FFMPEG utility, which you can use to create MPEG-4 compatible files, although there are plenty of expensive commercial options available that may stick closer to the original specification.

When it comes to encoding, the main limiting factor is available bandwidth rather than playback hardware. You may want to stream video across a wireless-N network, for example, and while its specification may boast a transfer speed of 108Mbps, the results are seldom as fast as promised.

Thanks to the vagaries of distance, interference, other users and hardware, wireless 802.11n is seldom fast enough for high definition video, and anything with a lower bandwidth is going to require a compromise. The same is probably true of your broadband connection. You might want to stream videos across the internet from your NAS, for instance, but this operation will be limited by your broadband upload speeds, which are often far less than 2Mbps.

This means you need to find a compromise between resolution, bandwidth and quality that hits your bandwidth sweet spot, and unlike the limitless world of high definition, you'll also need to compress the audio. The codec you choose will depend on the playback hardware, but the most common options is MP3 encoded through Lame.

Best codec for iOS

H.264 and MOV


Even Apple's portable devices are constrained by bandwidth and hardware. That means you need to make as many cuts as you can without sacrificing quality. If you're encoding video for playback on an iPad, for example, it makes good sense to scale your original material to 1,024 x 768 before encoding.

This is good practice for any encoding job where you know the end platform is the only place your files are likely to be played. You should also consider whether you plan to output the video from the device to a bigger screen. For example, you can connect an iPad to a TV and get 720p output, which may affect your choice of resolution. Apple's mobile devices also combine with the software to provide excellent video acceleration, getting the best possible video quality and battery life from your device.

In order to capitalise on these advantages, your video files need to adhere to Apple's strict codec discipline, which at least makes the job of choosing a codec easier. For best results on your iOS device, you need to choose an MOV container using the H.264 codec. 29.97fps (NTSC) is the best framerate to choose, which you can enter as 30,000/1,001 if you're using FFMPEG, and audio should use the AAC codec with a bitrate of around 160kbps.

Best codec for Android

H.264 or AAC-LC


Android devices don't have the same degree of lock-in as their Apple counterparts, which means you're free to install a media player like VLC that can handle many kinds of video file. There's no standard hardware configuration, so playback performance and capability are specific to each device.

Many will accelerate Flash, or even DivX files, because hardware acceleration is more general and not limited to a single codec, but there are still some Google-endorsed standards based on H.264 and MPEG-4. Google's documentation recommends H.264 with a bitrate of 500kbps, and AAC-LC at 128kbps for audio.

Resolution should be the same as the destination device, and you can use an MP4, 3GP or even a raw TS as a container. Google now has its own container and codec combination in WebM. This is also worth a try, because the VP8 codec it uses is closely related to H.264 and is likely to benefit from acceleration now that Google owns Motorola's smartphone division.

The law

Here in the UK, when you format-shift your CD and DVD collection from the discs you own to another device, you're currently breaking the law. That's because, in legal terms, only the owner of the copyrighted material can permit its duplication.

This is also a law that's been openly flouted since the 1980s, when we all started taping singles from the top 40 countdown on a Sunday evening. Music and video players, from Microsoft's Zune to Apple's iPod, have been allowed to flourish despite this obvious flaw in UK law.

This is unlike the US, which has a fair-use caveat that allows personal copying if you're moving the media to a different device for personal use, but things in the UK could be about to change.

The Hargreaves report on intellectual property, a preview of which was published in May [PDF], recommends that the government amends the rules on format shifting to allow for a stance similar to that of the US.

In August, the cabinet declared its full support for those recommendations, hopefully paving the way for a change in the law. Until then, you can't legally copy a DVD or CD that you own and stream it to another device. Consider yourself warned.

Handling audio

We've spent a lot of space discussing video encoding, but in some ways, audio can be even more important. When you're streaming a movie, video data can be scaled to fit the end resolution and format regardless of the input format, but audio isn't as flexible.

If you only include the surround channels, and your playback device supports only stereo, you won't typically hear anything. Some players can downmix a surround stream, but not many, and the ones that can are usually PC-based.

The solution is to encode the stereo and surround tracks if you want to keep the surround data, or just the stereo mix if you don't. Tools like Handbrake will let you downmix a surround stream if that's the only one available, or choose the stream you want to encode from the Track dropdown menu.

If you want to re-encode a surround stream, you can usually choose between AC3 and DTS, depending on which codecs are installed, and lower their bitrates. This will preserve the multi-channel aspect of the audio, but you will need to make sure your playback device is connected digitally to an amplifier that can decode it.