Best codecs for video and how to encode

11th Dec 2011 | 09:00

Best codecs for video and how to encode

Plus streaming explained and how to build a media PC

Best codecs for video

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

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

MPEG-4

MPEG-4

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

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

OGG

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.

How to encode your movies

Encode your movies

Encode movies

Provided you're legally entitled to do so, getting your data off an optical disc and onto your streaming server or portable device isn't difficult. There are two stages.

The first decodes the disc and grabs the raw data from it, so you'll need the hardware required by your media. This step might be redundant if the source of your material is a legitimate unlocked download, or perhaps a recording from a digital television receiver or PC-based DVB card.

The second stage takes this raw data and runs it through the number crunching routines that generate the final file. Success on the first part depends on the protection used by the source disc. If there's none, then you'll be able to copy and encode your movies in a single step. If there's encryption, then you'll first need to remove this from the data before you can begin the transcoding stage.

Selling software that side-steps this protection is legally dubious, especially in the UK, but there are free tools available that will do the job, leaving you with either a copy of the DVD on your hard disk, or a new ISO burned to disk.

The best tool we've found for transcoding is called Handbrake. This is an open source application that harnesses the power of several free codecs, including FFMPEG, x264 and libtheora, and turns your video data into a file you can easily stream or move to a portable device. You don't even need to worry about the intricacies of codec configuration.

Handbrake includes a list of profiles you can use to quickly select the end device to load the best values into the codecs. These can be previewed and altered before you commit yourself to the final process.

Encoding a film, especially in high definition, can easily take several hours. But the length of time is dependent on the power of your system, so this might be a good time to upgrade that aging Core2Duo you're sitting at.

How to encode your movies with Handbrake

1. Select a source

HB step 1

We installed the the nightly build of Handbrake because it often has cutting edge features and speed improvement, but the official release is more stable. When you run it, you need to select the source location for your movie. After clicking in the 'Source' icon, select the optical drive, folder or file you want to convert.

2. Choose a profile

HB step 2

Once Handbrake has scanned for chapter information, select a preset for the destination format and choose a title from the source. If your presets don't include Android devices, choose 'Reset' from the Options menu. Handbrake defaults to the longest title, which is probably what you need unless you're after the outtakes.

3. Tweak presets

HB step 3

Adjust the video and audio encoding options to suit your own requirements (see the main text for some hints), and use the 'Picture' page to change the resolution, if required. You can use the 'Preview' window to generate a 10-second clip with your settings, then click 'Start' to generate the final file.

How to build the best media PC

CPU

Transcoding a video from one format to another takes a lot of CPU power, so a powerful machine will save you hours of waiting and, eventually, your sanity. But before you hand over your credit card details to your favourite retailer, you should first consider whether an upgrade is really necessary and how often you're likely to want to encode a movie.

Initially you'll want to transfer as much of your current collection as you still watch to your digital library. That's a big hurdle, but also a finite one. It might not be worth investing in the best hardware if it's only going to save you a couple of weeks of pain.

Your super video transcoding PC might become a costly white elephant whose resources you're unlikely to test again after an initial splurge. If you only buy a few movies a month, there's no real need to upgrade your hardware. Anything from the last five years will handle even a high definition movie overnight. That said, if you want an excuse to upgrade your machine, a big encoding job is the perfect opportunity.

Processor overheads

As with any upgrade, the best place to start is with the CPU. Most of the codecs we've mentioned use the free x264 library, which is widely considered to be one of the best available, regardless of price. It will use as much processing power as you throw at it, so the CPU question is easily answered with 'the best you can afford'.

If money is no limit, that means something sporting Intel's Sandy Bridge architecture, with its requirement for a LGA1155 CPU slot and memory accelerating potential of up to quad-speed DDR3-1333. We'd recommend the Intel Core i7 2600k, which can now be had for around £240. It has four cores and it's fast.

If you're looking for something a little cheaper, or a CPU that might not require a complete architecture overhaul, AMD's six-core Phenom II X6 Thuban represents great value for money for that amount of processing power, as it can now be bought for around £120.

Video encoding is also hungry for memory, so fit as much as either your motherboard or wallet can take. The more you add, the less your system will have to read either your slow optical drive, or the hard disk.

Unfortunately, graphics cards don't change the CPU equation, despite the promise of GPU-accelerated movie encoding. Modern graphics cards, and the APIs their vendors have built to access their raw number crunching abilities, have failed to offer the advantages that seemed imminent in 2008.

The current consensus on the encoding software that's able to shoe-horn a GPU into encoding duties is that the quality of the output just isn't good enough. As a result, CPU grinding is still the best route for the serious media collector.

The only good news is that this could save you a few pounds, because the latest Sandy Bridge Intel CPU's offer SoC - systems on a chip - and these include graphics. They might not be good enough for the latest games, but they're definitely good enough to show you what you're doing while you fine-tune your Handbrake codec parameters, and for playback.

It might also be worth investing in a screen capable of 1,920 x 1,080 if you want to check the encoding quality of your high definition material without moving it to your television.

Finally, for the hardware, don't forget to add an optical drive that can read your media. SATA II Blu-Ray players can be bought for as little as £50.

With your hardware sorted out, you just need to choose your operating system. There's nothing wrong with whatever version of Windows you've already got, but you will need the latest updates to version 7 for the modern chip designs and drivers, and a 64-bit installation is essential if you've got more than 4GB of RAM at your disposal.

Storage issues

Storage

There's no getting around the fact that movies take up a lot of space, which means your storage provision is just as important as the rest of your hardware. Capacity is probably the most important feature to consider, because you may be storing your entire collection on the same machine.

However, you don't need to store all your films on an internal drive. A portable remote drive attached to a PogoPlug is a great solution, as external drives are readily available with 2TB of space, for example, for about £60. Failing that, look for a NAS that offers streaming built-in.

For the internal drive, speed becomes a more important factor. The faster the drive, the more quickly your super-fast CPU will be able to crunch through the data. The fastest drives you can get are solid state, and while they're relatively limited in terms of storage capacity, as long as you're not planning to store the resulting files on the same drive, you won't encouter any problems. You can now get 120GB of SSD storage with a SATA II interface and a 3.5-inch form factor for £140.

Media playback hardware

The hardware you use to access your newly complied media collection is utterly dependent on the device it's being connected to. Our recommended storage solution is a Pogoplug, which is capable of streaming media using a wide variety of protocols. If you're playing your movies on an Android or iOS device, you can download a free app that can access your data through the cloud and stream content directly from your storage device, for example.

Unless you're happy connecting your laptop directly to your TV, you'll need a hardware streaming client. The premium version of Pogoplug, like many other streaming servers, can send video data using the UPnP protocol, and dozens of devices will be able to transform this into high definition video.

You might already have a suitable device sitting under your TV, because two of the most readily accessible devices are Microsoft's Xbox 360 and Sony's PlayStation 3 games consoles. If you do need a separate unit, we recommend investing in one of the latest Popcorn Hour boxes, which are quiet, simple to use and capable of playing virtually any codec you throw at them.

Local streaming and movies on the go

Local streaming

local streaming

Now you've got your content encoded and ready to go, learn how to stream it across your local network to any device Networking is the most important part of your setup to get right and can easily become the weakest link in the chain, making all those codec refinements you've just spent an hour on redundant.

There are two principle aspects of networking that affect video streaming. The first is bandwidth, because streaming high definition video is probably the most bandwidth intensive task you're likely to perform, short of running a Tor exit node.

The second is reliability. This is important because even if you have the bandwidth, you also need to ensure that packets are transferred not just quickly, but with enough consistency to ensure your player never goes hungry. It's not like web browsing or downloading game updates - video needs to arrive at your playback device as a predictable and constant stream of blocks.

This provision of data starts at the PC or NAS that holds the videos. Data access needs to be quick enough, which isn't always the case with older units or cheaper NAS devices. You can easily work out how much bandwidth you need because the bitrate value for video encoding is often the same metric used for network speed. If your total bitrate exceeds that of the available bandwidth, you're likely to run into problems.

This competition for bandwidth can cause problems with wireless networks in particular. Any 802.11n wireless gateway will have enough theoretical bandwidth, but not if slower devices are connected at the same time, or if several people are streaming video at once. That's what makes them fail.

The obvious answer is to have two networks - one for video streaming and the other for everything else. Both networks could even be wireless, because several modern wireless routers, like the FritzBox 7390, can now run two frequency bands side by side, effectively offering the advantage of two networks without the hardware overhead. This lets you set your family up on one, for example, and your video network on the other.

As the hub of your network, the router is vitally important. You might want to consider using a standalone switch rather than an ADSL/wireless/router combination, because these are less likely to overheat in use and let you siphon off your video network from your standard connections.

For the ultimate in bandwidth provision, we recommend using a powerline network for your video data and a wireless router for everything else. Ethernet through a powerline network is slightly more costly than wireless, as you'll need a plug capable of AV speeds for both the router, your NAS and each playback client. But if you've already spent a fortune on the equipment that can make the most of HD content, as well as the movies to play on it, the extra £100-150 for those three units is worth it.

They're also plug and play, and after a few button presses, you'll have a network that won't destroy the suspense in the gas station scene from No Country for Old Men just because someone else is microwaving noodles.

The final network consideration is the limit on your upload bandwidth. For domestic ADSL installations, this is likely to be the most limiting factor in streaming your movies to your devices while you're out and about. Typical upload speeds are in the region of 500-800Kbps, for example, which is enough for a low resolution, medium quality film.

But even this number can vary, depending on your distance from the exchange. Users of an ISP that support Annex M, like O2 and Be, or BT's new Infinity broadband will have between 2 and 10Mbps, which are slightly more flexible. But it does mean you'll have to re-encode high definition material for both your television at home and for your portable devices, if you want to stream movies to your phone, tablet or laptop while you're stuck waiting for delayed aircraft at Heathow's Terminal 5.

Movies on the go

pogo 1

Getting content off your LAN and onto the internet can be tricky. You could run your own server, forwarding ports manually and ensuring the setup is secure, or you could use cloud storage like Dropbox, but both are convoluted.

The best option we've found is to use PogoPlug. This is a software and a hardware solution, and the best description of its facilities is that it provides a DropBox replacement where you supply the storage. With the desktop software installed on your PC, for instance, you can share specific files and folders with your online account. As long as your PC is on, you can access those files though a suite of mobile apps, desktop tools and web portals without any further configuration.

If you don't want to leave your PC on, you can buy a hardware PogoPlug unit that will connect to a USB hard drive to provide always-on access.

For movie lovers, the Android and iPhone apps will stream your movies from any PogoPlug source, depending on the video format, and you can use the web browser interface for any other device like a laptop. This works brilliantly at home, where fast wireless provides a seamless stream of data to your palm.

But the PogoPlug software can also stream video outside of your LAN. For a one off $29 payment, you can stream data from your LAN through the internet to any other Pogo-compatible device without any further configuration. This is ideal if you don't have the patience for messing with firewalls or setting up your own servers, and PogoPlugs web interface makes managing your content easy.

Just install the desktop client, select the folders you want to share, and install the client apps on your chosen device. As soon as you've created an account, registered the premium upgrade and synced your collection, you'll be able to stream movies and browse your image collection immediately.

UPnP servers

The PogoPlug is a great plug-and-play solution, but there are many alternatives that may not be quite so convenient but might offer greater flexibility.

Even Microsoft's default media player can act as a UPnP server, automatically streaming your content to other UPnP clients on your network. But another good option is a modern NAS from the likes of Qnap and Synology.

The latest firmware on these boxes include UPnP, iTunes and web streaming without any extra effort, and both manufacturers also offer iPhone and Android helper applications that can be used to view content and upload data to your NAS box while you're out and about. If you need terabytes of storage, a NAS box is the best option.

Alternatively, you can run a server on a PC. This has the disadvantage of always needing to be on to be accessible, but it is cheap. The free TVersity server, for instance, can stream almost any format to any UPnP client and is also very good at converting between formats on the fly. This is great if you've got an MOV file that won't play on a PlayStation 3, for example, because TVersity will make the changes itself.

Stream movies to your mobile with PogoPlug

1. Install the software

pogo 1

A PogoPlug source can be either a desktop application for Windows and OS X, or a hardware device connected to some storage. The desktop option is free if you'll limit your streaming to your LAN, and confi guration is easy. Register an account at PogoPlug.com, install the software and select the folders you want to share.

2. Add the content

pogo 2

You can now add the files you want to have access to. If you've shared your Windows 'My Videos' folder, for example, move your transcoded movie fi les into this folder and log into the web interface. Within a few moments, you should see your content overview updated to include the new fi les you've just added.

3. Watch a movie

pogo 3

Now your content is accessible from your desktop and registered at PogoPlug, you can use any access route to play back your collection. On an iOS device, for example, log in with the same account credentials and navigate to the file. You can stream immediately or download to watch later in another application.

--------------------------------------------------------------------------------------------------

First published in PC Plus Issue 314. Read PC Plus on PC, Mac and iPad

Liked this? Then check out 16 best TV streaming services

Sign up for TechRadar's free Week in Tech newsletter
Get the top stories of the week, plus the most popular reviews delivered straight to your inbox. Sign up at http://www.techradar.com/register

Follow TechRadar on Twitter * Find us on Facebook * Add us on Google+

tutorial home networking networking media PCs
Share this Article
Google+

Most Popular

Edition: UK
TopView classic version