Skip navigation.
Home

Dr. Apple TV or: How I Learned to Stop Worrying and Love Spending CPU Cycles on Transcoding

Apple TVI have an AppleTV that is now playing movies from a variety of sources. This is how I am able to take some of the more popular movie containers and convert (transcode) them for use with the AppleTV using OSX.

Update (05/02/2010): added MKV processing as well as my Best Practices if you will in the form of Tips.

Goals

I wanted all of my movies to have:

  • Video compatible with ATV (obviously).
  • A Dolby Surround (Pro-Logic II) audio track always.
  • A Dolby Digital 5.1 (AC-3) audio track when possible.
  • Soft subtitles. These are subtitles that can be disabled. Think closed captioning.
  • Movie ratings. (e.g. PG, G, R, etc)
  • Poster/Cover Art.

Toolkit

I build and run everything on Mac OSX Leopard (10.5). Here are the essential packages that I had to install to make everything meet my ATV goals given a variety of movie sources:

  • FAAD: An AAC decoder. You can build from source or use MacPorts instead: sudo port install faad2. I used MacPorts.
  • Aften: An AC-3 audio encoder. You must install cmake before you'll be able to compile. Via MacPorts: sudo port install cmake
  • MP4Box: Necessary for working with the different parts (streams) of an MP4 container. This one is a little harder to find a Mac version for, but I found a pre-compiled universal binary that works great. Or you could just build it from source, instructions near the end of this article.
  • Handbrake: Transcoder for ATV compatible video.
  • Subler: for subtitles, ratings and artwork.
  • MacTheRipper (MTR): A good program for copying your non-protected DVDs to your hard drive.
  • IMP Awards: movie poster artwork
  • MediaInfo: a tool that will tell you all of the characteristics of your media. (e.g. frames per second, audio streams, etc)
  • libdca: a DTS decoder. Via MacPorts: sudo port install libdca
  • mkvdts2ac3: a script which converts DTS tracks to AC-3 in MKV container files.
  • mkvtools: utilities to work with MKV containers. Via MacPorts: "sudo port install mkvtoolnix"
  • MKVtools UI: a GUI version of the mkvtools. It's nagware but super easy to use.
  • YAMB: this is really just a front-end to MP4Box and only works in Windows or WINE. I run this in WINE to unpack containers that MP4Box on Mac can't read very well. This could also run in a virtual machine (Parallels, etc) but works perfectly fine in WINE.

The only thing that you will have to pay for in the toolkit above is MTR. The rest are free and open source.

Non copy-protected DVD

Let's say you have an unprotected DVD that you would like to send to ATV. Here's the workflow to get it from a disc to ATV:

  1. MTR: Copy DVD to harddrive. Some people call this ripping a DVD. You can also skip this step, if you like, and have Handbrake rip/encode at the same time. I personally like to get the media off of the disc though. (5 minutes)
  2. Handbrake: source is ripped DVD folder. Use AppleTV preset. (3 hours)
    1. Make sure that the audio tab lays down two tracks.
      1. AC3 Source, AAC (CoreAudio) Codec, Dolby Pro Logic II Mixdown
      2. AC3 Source, AC3 Passthru Codec and Mixdown.
    2. Check subtitles tab. If it's available, pick closed-caption (text). If you pick any of the others and "Burned In" is selected, you will always see the subtitles (they won't be soft subtitles). If you don't have a cc track, you can get subtitles as .srt files across the web.
  3. Subler: Open m4v created by Handbrake. Add ratings, release date, and artwork. Also make sure to fix the language of the audio tracks, they may be Unknown. (5 minutes)
  4. iTunes: Drag m4v into iTunes. iTunes will sync with ATV. (5 minutes)

Total Time/Movie: ~3.25 hours on MacBook Pro Core Duo.

MP4 file

Let's say you have an MP4 file that has HD video in it and AAC 6-channel audio. It's possible that the ATV will be able to play this file without modification, but the video will likely be choppy as you're approaching the limits that the ATV can handle. In addition, your home theater receiver can't decode the AAC encoded audio as discrete channels, so you end up with Pro-Logic sound. This is not ideal and doesn't fit our stated goals. Here's the workflow to fix everything up:

  1. Convert AAC multi channel to Dolby Digital 5.1. Script is below this list. (5 minutes)
  2. Handbrake: source is ripped DVD folder. Use AppleTV preset. (3 hours)
    1. Make sure that the audio tab lays down two tracks.
      1. AC3 Source, AAC (CoreAudio) Codec, Dolby Pro Logic II Mixdown
      2. AC3 Source, AC3 Passthru Codec and Mixdown.
    2. I don't do sub-titles in Handbrake but you probably could.
  3. Subler: Open m4v created by Handbrake. Add ratings, release date, and artwork. Also make sure to fix the language of the audio tracks, they may be Unknown. Add any .srt subtitles as well. (5 minutes)
  4. iTunes: Drag m4v into iTunes. iTunes will sync with ATV. (5 minutes)

Total Time/Movie: ~3.5 hours on MacBook Pro Core Duo.

If you attempt to skip step #1, you'll end up with some really bad audio in the finished product. I wrote a little script to help me do this conversion..

Script for converting AAC 6-channel audio to Dolby Digital 5.1

convert_aac_ac3.sh:


faad -w "$MOVIE" | aften -readtoeof 1 - "$MOVIE".ac3
MP4Box -rem 2 "$MOVIE"
MP4Box -fps 23.976 -add "$MOVIE".ac3 "$MOVIE"
rm "$MOVIE".ac3

Usage:

./convert_aac_ac3.sh Movie\ File\ Name.mp4

If you want to do a whole bunch of mp4s in one shot:

find . -name "*.mp4" -exec ./convert_aac_ac3.sh {} \

Notes:

  • This script assumes the AAC audio is on track 2. You could get rid of the delete line if you like (line #3) but then you'll have to check the audio tab inside Handbrake to make sure you have setup your AC-3 source tracks.
  • You don't need to mess with channel mapping with Aften (e.g. -chmap 7). FAAD outputs a WAV stream that is WAVE_FORMAT_EXTENSIBLE and Aften reads WAVE_FORMAT_EXTENSIBLE, so you're all good. If you mess with it, you'll find that the audio tracks are all coming out of the wrong speakers.
  • The frames per second are defined as 23.976. If your fps is different than the source fps (in this case the MP4) the audio will be all messed up (like double the length and super slo-mo). You can get the source fps by either looking in the Activity Window in Handbrake (e.g. scan: 10 previews, 1280x688, 23.976 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1) or by using MediaInfo.

Script for converting DTS audio to Dolby Digital 5.1

Use mkvdts2ac3.sh. This works great and has a bunch of command line options to do what you want. Make sure you have the other utilities installed though

Building MP4Box

Get the latest stable release, and then do the usual ./configure, make and make install.

For Mac OS users there is an extra step that you have to do if you want to be able to build and install the GPAC utilities without headaches, and it involves the editing of a source file in the package.

So, after you have downloaded the sources, open the file src/utils/os_net.c, and find the line that reads:

#ifndef __DARWIN__
right AFTER it, add the following:

typedef unsigned long u_long;
save the file, and you can now configure, build and install the GPAC utilities (MP4Box) on Mac OS without any problem.

Hat Tip: Diego

Tips

  • To send a file to the AppleTV you'll need to get everything setup properly for Handbrake. This means that you want an MKV, MP4 or M4V that has the video in pretty much any format and the audio in AC-3 format. Handbrake doesn't support multichannel AAC or DTS very well.
  • Sometimes the container files (MP4, MKV, M4V, AVI, etc) are a little messed up. This means that you need to unpack the audio track, convert it, then re-package the streams (tracks in a container) into a new container. Then take that new container and push it through Handbrake.
  • To unpack the audio stream from some containers, I usually have to resort to YAMB. Then take the AAC file and run it through my AAC --> AC-3 script. FAAD will read the AAC file directly without any container. So instead of using the MP4 as an input, just use the extracted AAC file instead.
  • To re-assemble an M4V from an existing MP4 or the extracted steams, I usually use Subler! In the File menu you can simply do a New and you'll have a blank container. Drag the old MP4 on it and you can choose which streams you want to add. Drag an AAC or AC3 file onto the window and it adds them with ease.
  • I usually extract the subtitle files out of MKV files into an SRT prior to processing the file through Handbrake. I use MKVTools GUI to do this since, it is stupid easy to use. I then just add the external SRT via the subtitles tab.