Encode mp4 files containing both AAC and Dolby Digital 5.1 audio

This is a short howto on creating a .mp4 file that plays on as Dolby Digital 5.1 through the popular media players such as AppleTV and XboxOne. The .mp4 file contains H.264 video, both AAC and AC-3 stereo audio and subtitles.

Configure the Apple TV to pass-thru Dolby Digital 5.1 to the receiver.

To output the best quality audio, the Apple TV should be configured as

  • Settings » Audio and Video » Surround Sound = auto

For music that would be 2-channel PCM, and for movies and TV Shows that is Dolby Digital 5.1 when available (otherwise it plays the AAC stereo track).

File format

Apple TV media files containing Dolby Digital Professional AC-3 must conform to a specific track layout. Two Sound Tracks are used and both must be provided: [src]

  • AAC Encoded Stereo – The standard Stereo audio track.
  • AC-3 Encoded Surround – The Dolby Digital Professional AC-3 Surround audio track.

The AAC Stereo audio track must appear first in the media file and must be enabled by default. The Dolby Digital Professional AC-3 Surround track must appear after the AAC Stereo track and must be disabled by default. It is good practice to encode the audio for both tracks at the same sample rate, 48kHz is normal. Note that QuickTime only supports multiple audio tracks when the file extension is .m4v instead of .mp4.

QuickTime Pro Movie Properties

Create the H.264 Video stream

Assuming you have an AviSynth video source, you can encode a H.264 video stream for AppleTV 3 (and up) using x264:

avs4x264mod.exe --pass 1 --slow-firstpass --bitrate 12000--sar 1:1 ^
--preset slow --tune film --level 4.0 --profile high --ref 4 --stats video.stats ^
--colormatrix bt709 --colorprim bt709 --transfer bt709 ^
--output NUL video.avs

avs4x264mod.exe --pass 2 --bitrate 12000 --sar 1:1 ^
--preset slow --tune film --level 4.0 --profile high --ref 4 --stats video.stats ^
--colormatrix bt709 --colorprim bt709 --transfer bt709 ^
--output video.264 video.avs

Create the AC-3 Audio stream

Given a 5.1 PCM audio stream, we use sox-14.4.1 and aften-svn-r762 to create a Dolby Digital 5.1 AC-3 stream.

# add audio delay (if needed, in this example it is just 0.000 sec)
sox.exe audio.wav tmp.wav pad 0.000 stats 2>NUL:

# use dynamic range compression (or not)
aften.exe -v 0 -dnorm 31 -dynrng 1 -b 448 tmp.wav audio.ac3
# aften.exe -v 0 -b 448 tmp.wav audio.ac3

Create the AAC Audio stream

Once more, assuming you have a 5.1 PCM audio stream. This time, use sox-14.4.1 and neroAacEnc to create a stereo AAC stream.

# down mix to stereo and add audio delay (if needed, in this example it is just 0.000 sec)
sox.exe audio.wav tmp.wav pad 0.000 channels 2 stats 2>NUL:

# convert to AAC
neroAacEnc.exe -lc -br 192000 -if tmp.wav -of audio.aac

Create SubRip subtitles

Assuming you have SubRip subtitle file, you’re all set. On the other hand, if you have an Adobe Encore subtitles (.txt), you can convert it using SubtitleWorkshop and iconv.

# UTF-8 .txt to ANSI .srt (assuming 24 frames per second video)
SubtitleWorkshop.exe "/CONVERT(subtitles.txt/subtitles.utf8/SubRip/24/24)"
iconv.exe -f UTF-8 -t MS-ANSI subtitles.utf8 >subtitles.srt

Package them in a MP4 container

Using MP4Box-0.5.1-dev and

# assuming 24 fps and Dutch language
MP4Box.exe -new -add "video.264:lang=eng:fps=23.976023976:name=H.264 video" ^
  -add "audio.aac:name=AAC stereo" ^
  -add "audio.ac3:name=AC3 DD 5.1" ^
  -add "subtitles.srt:hdlr=sbtl:lang=nld" -ipod ^

While we’re at it, let’s set some meta data for iTunes and Google Photos using mp4v2 and my mp4date.

set META=-type tvshow -show "show name" -season 1 -episode 1 ^
  -network "your name" -song "video name" -year 2017 ^
  -artist "artist name" -album "album name" -comment "your comments" ^
  -genre "genre name" -encodedby "your name" ^
  mp4tags.exe %META% -hdvideo 2 tmp.mp4

# Google Photos uses this date to order videos
mp4date --create 2017-07-12T01:00 --file tmp.mp4

Using FFmpeg, set the AC-3 as disabled. Re-encode AC-3 to prevent warning. Set missing handler_name.

ffmpeg.exe -y -i tmp.mp4 -map 0:0 -map 0:1 -map 0:2 -map 0:3? ^
  -codec:v copy ^
  -codec:a:0 copy ^
  -codec:a:1 ac3 ^
  -codec:s copy ^
  -disposition:a -default -disposition:a:0 default ^
  -metadata:s:a language=eng -metadata:s:s handler_name="Dutch subtitles" ^

Add to iTunes, and play using your AppleTV 3 (or up).

Embedded software developer
Passionately curious and stubbornly persistent. Enjoys to inspire and consult with others to exchange the poetry of logical ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.