Wiki Index All Recent Edit Bottom

Converting MKV

1.   Introduction
2.   PS3 MPEG-4 Limits
3.   Installing the tools
4.   Splitting the .MKV
5.   Getting .MKV info
6.   Extract the Video
6.1   Check Video Profile
7.   Convert the Audio
7.1   Down mixing to Stero is simple and compatible?
7.2   Understanding Channel Arrangements for 5.1 Audio Formats
7.3   Using mplayer with NeroAacEnc or faac to make AAC 5.1
8.   Creating the MPEG-4

Introduction

This is based on the following blog post adding to effort of my own to correctly map channel assignments for AAC 5.1

Like the blog post above, I also want to keep re-encoding to a minimum.

If you just want some scripts to automate the process visit my the Code section of my site for more details.

If you want to understand the process then read on :-)

PS3 MPEG-4 Limits

During the process of figuring out the conversion of MKV to MP4 I have discovered there are some limits to what the PS3 will play.

Installing the tools

This install the tools for GPAC, mkvtoolnix, mpeg4ip and ffmpeg.

 sudo aptitude install a52dec faac ffmpeg gpac hexedit mkvtoolnix-gui mpeg4ip-utils mpeg4ip-server 

I also use the Nero AAC encoder as it produces better results than faac, for more details see the URL below...

 wget http://ftp6.nero.com/tools/NeroDigitalAudio.zip
 wget ftp://ftp6.nero.com/tutorials/nerodigital/audio_encoder/NeroDigitalAudio_tut_eng.pdf
 unzip NeroDigitalAudio.zip
 chmod 755 linux/*
 sudo mv linux/* /usr/local/bin/

Splitting the .MKV

If the .mkv file is large than 4GB we need to split it. This is because, that although MP4Box support splitting MPEG-4 files, the PS3 will only play the first part and any subsequent parts refuse to play.

Therefore if the .mkv file is greater than 4GB we will split it then and convert those parts.

 mkvmerge -o movie_split.mkv --split 4G movie.mkv

Getting .MKV info

In order to extract the video, audio and/or subtitles from the .mkv we first need to know what is in the container.

 mkvmerge -i movie.mkv

Should return something like the following...

 File ’movie.mkv’: container: Matroska
 Track ID 1: video (V_MPEG4/ISO/AVC)
 Track ID 2: audio (A_AC3)

Check the video is encoded in the proper format, look for "V_MPEG4/ISO/AVC" and make a note of which tracks hold the video and audio you are after. Now we need to get the framerate using the following...

 mkvinfo movie.mkv | grep fps

...which should return something like...

 |  + Default duration: 41.708ms (23.976 fps for a video track)

Extract the Video

Use a command similar to the one below, simply replacing the video and audio track numbers are required.

 mkvextract tracks movie.mkv 1:movie.h264

Check Video Profile

Some video comes in profile 5.1 which the PS3 doesn't support, the PS3 only supports profile 4.1. So first check the profile level...

 file movie.h264

The output will look something like this.

 movie.h264: JVT NAL sequence, H.264 video @ L 51

If the number after the @ is 41 you have 4.1 profile which is great and you can skip ahead to the Convert the Audio section.

If you have 51 after the @ then you need to change the profile level which is easy to do and takes less than a second using the following bit of Python.

 python -c "f=open('movie.h264','r+b'); f.seek(7); f.write('\x29'); f.close()" 

You can open movie.h264 in hex editor, such as 'hexedit' and manually patch the file.

 hexedit movie.h264

Look for the following on the first line:

 67 4D 40 33

Which we change to:

 67 4D 40 29

NOTE! Different number combinations than the above do exist, the good news is that the 33 remains the same and is the one that you want to change to 29. What you're looking for in the hex editor, is the 2nd distinct column grouping of numbers. The 33 will be on the top line and is the right most number in that 2nd grouping.

After changing the profile level check it is now 4.1

 file movie.h264

Which should return something like...

 movie.h264: JVT NAL sequence, H.264 video @ L 41

Convert the Audio

Whilst is it technically possible to include Dolby Digital and DTS audio in an MPEG-4 container, there are not currently any MPEG-4 container tools (other than Handbrake) which support that configuration and I have yet to get my PS3 to see, let alone play, an AC3 audio track embedded in an MPEG-4 container. Therefore, we do need to re-encode the AC3/DTS to AAC.

Down mixing to Stero is simple and compatible?

We can convert AC3/DTS in the .mkv to either Stereo or 5.1 multi channel AAC, but there are some considerations when choosing 5.1 AAC.

So, a lot of good reasons to not bother with AAC 5.1 and just down mix to stereo. If you need Stereo then use the method below.

ffmpeg : Downmix to Stereo AAC

This method works well for down mixing 5.1 audio to stereo and it works well with both AC3 and DTS. 'ffmpeg' has multi channel options but they garble DTS and have no channel re-mapping capability.

 ffmpeg -i movie.mkv -vn -acodec mpeg4aac -ac 2 -ar 48000 -ab 160k movie_2ch.aac

Understanding Channel Arrangements for 5.1 Audio Formats

Now, I could have stopped with the simple down mix method above, but that just doesn't sit well with me because I want multi channel damn it! ;-)

Wouldn't it be great if all the 5.1 audio formats had the same channel arrangement? Fact, is they are nearly all different.

Channel Arrangement for Multi Channel 5.1 Audio Formats

FormatChan 0Chan 1Chan 2Chan 3Chan 4Chan 5
5.1 WAVFLFRFCLFESLSR
5.1 AC3FLFCFRSLSRLFE
5.1 DTSFCFLFRSLSRLFE
5.1 AACFCFLFRSLSRLFE
5.1 AIFFFLSLFCFRSRLFE

Using mplayer with NeroAacEnc or faac to make AAC 5.1

This is the only method I have found which can create 5.1 Multi-Channel AAC with the correct channel assignments. That last point needs driving home, with the correct channel assignments. I have found numerous methods which claim they convert AC3 5.1 or DTS 5.1 to AAC 5.1, and certainly the resultant AAC files do have 6 channels. It is just a shame the channels are in the wrong places which sounds extremely funky when you playback through a 5.1 capable home cinema sound system. Grrr.

The problem is this, the .wav that 'mplayer' outputs will have the channel arrangement of the source audio (i.e AC3 or DTS) and 'NeroAacEnc' and 'faac' expect different channel arrangements (different from each other and different from AC3 and DTS) for their input.

After some extensive testing I have figured out several channel mappings to correctly created AAC 5.1 from AC3 5.1, DTS 5.1 and WAV 5.1.

NOTE! some of following has been validated and some hasn't. For example, AC3 5.1 to AAC 5.1 is confirmed working when using 'NeroAacEnc' and I'm fairly sure the DTS 5.1 to AAC 5.1 using 'NeroAacEnc' works too. So, apart from possible typos, I think my tables are correct.

Channel Arrangement for NeroAacEnc and faac 5.1 WAV input

FormatChan 0Chan 1Chan 2Chan 3Chan 4Chan 5
NeroAacEncFLFRFCLFESLSR
faacSLSRFLFRCLFE

Multi Channel 5.1 Channel Maps for encoding AAC 5.1

Therefore, based on the assumptions above, here are the mplayer channel mappings that should correctly present AC3 5.1, DTS 5.1 and WAV 5.1 to 'NeroAacEnc' and 'faac'

AC3 5.1 -> AAC 5.1 Encoder Input

mplayer Channel Map - AC3 5.1 to NeroAacEnc and faac

Channel NameSource AC3 5.1 Chan No.Input NeroAacEnc Chan No.mplayer Map
FL000:0
C121:2
FR212:1
SL343:4
SR454:5
LFE535:3
Channel NameSource AC3 5.1 Chan No.Input faac Chan No.mplayer Map
FL020:2
C141:4
FR232:3
SL303:0
SR414:1
LFE555:5

DTS 5.1 -> AAC 5.1 Encoder Input

mplayer Channel Map - DTS 5.1 to NeroAacEnc and faac

Channel NameSource DTS 5.1 Chan No.Input NeroAacEnc Chan No.mplayer Map
C020:2
FL101:0
FR212:1
SL343:4
SR454:5
SR535:3
Channel NameSource DTS 5.1 Chan No.Input faac Chan No.mplayer Map
C040:4
FL121:2
FR232:3
SL303:0
SR414:1
SR555:5

WAV 5.1 -> AAC 5.1 Encoder Input

mplayer Channel Map - WAV 5.1 to NeroAacEnc and faac

Channel NameSource WAV 5.1 Chan No.Input NeroAacEnc Chan No.mplayer Map
FL000:0
FR111:1
C222:2
LFE333:3
SL444:4
SR555:5
Channel NameSource WAV 5.1 Chan No.Input faac Chan No.mplayer Map
FL020:2
FR131:3
C242:4
LFE353:5
SL404:0
SR515:1

The mplayer Methods

Here are the methods using 'mplayer' with 'faac' and 'NeroAacEnc'. I use 'NeroAacEnc' since it is a better quality encoder, but I've include details for use 'faac' if you can't (or won't) use 'NeroAacEnc'.

AC3 5.1 to AAC 5.1 using mplayer and NeroAacEnc

 mkfifo audiodump.wav
 neroAacEnc -ignorelength -q 0.30 -if audiodump.wav -of movie_6ch.aac &  mplayer movie.mkv -ac ffac3 -channels 6 -af format=s16le,channels=6:6:0:0:1:2:2:1:3:4:4:5:5:3 -vo null -vc null -ao pcm:fast:waveheader:file=audiodump.wav -novideo -quiet -nolirc
 rm audiodump.wav

AC3 5.1 to AAC 5.1 using mplayer and faac

 mkfifo audiodump.wav
 faac -q 234 -o movie_6ch.aac -P -C 6 -X --mpeg-vers 4 audiodump.wav & mplayer movie.mkv -ac ffac3 -channels 6 -af format=s16le,channels=6:6:0:2:1:4:2:3:3:0:4:1:5:5 -vo null -vc null -ao pcm:fast:waveheader:file=audiodump.wav -novideo -quiet -nolirc
 rm audiodump.wav

DTS 5.1 to AAC 5.1 using mplayer and NeroAacEnc

 mkfifo audiodump.wav
 neroAacEnc -ignorelength -q 0.30 -if audiodump.wav -of movie_6ch.aac &  mplayer movie.mkv -ac ffdca -channels 6 -af format=s16le,channels=6:6:0:2:1:0:2:1:3:4:4:5:5:3 -vo null -vc null -ao pcm:fast:waveheader:file=audiodump.wav -novideo -quiet -nolirc
 rm audiodump.wav

DTS 5.1 to AAC 5.1 using mplayer and faac

 mkfifo audiodump.wav
 faac -q 234 -o movie_6ch.aac -P -C 6 -X --mpeg-vers 4 audiodump.wav & mplayer movie.mkv -ac ffdca -channels 6 -af format=s16le,channels=6:6:0:4:1:2:2:3:3:0:4:1:5:5 -vo null -vc null -ao pcm:fast:waveheader:file=audiodump.wav -novideo -quiet -nolirc
 rm audiodump.wav

Creating the MPEG-4

Now we need to make a MPEG-4 file with the video and audio we extracted/converted, specifying the video's frame rate that we noted earlier.

 MP4Box -add movie.h264 -add movie_2ch.aac -fps 23.976 movie_2ch.mp4

Sadly, if MP4Box is used the split the .mp4 the PS3 will only play the first part, any subsequent parts refuse to play. This is why we split the .mkv, if it is large than 4GB, and then convert those .mkv parts. It ensures compatibility.

References

$Id: ConvertingMKV,v 1.83 2010/07/27 15:04:14 martin Exp $

Wiki Index All Recent Edit Top