MP4 to MKV with DTS audio
I'm currently in the process of ripping my DVD collection to my NAS. This means I can chuck all my DVDs in a box and put them in the loft, along-side all my CDs that went to MP3 years ago.
I started this process and ripped everything to MP4 containers, as I figured that this would be best supported by media streamers. And whilst it was fine with my WD media-streamer, my other one has trouble with DTS audio inside MP4. So I need to change the offending films to MKV.
As the actual encoded film and audio tracks already encoded in a suitable format, I don't want to re-encode the films, which would result in a loss of quality. So I needed to extract the tracks from the MP4 container and stuff them into a MKV container. This is easily done in Linux with two sets of utilities:
Both of these were available with my distribution so it was a simple one-click install.
mkvmerge does a good job of converting MP4s into MKV in one hit - but I had a few problems with DTS tracks. There may be a slicker way of doing this, but this seemed to do the trick for me.
Get file information
MP4Box -info [filename].mp4
Extract tracks
First off, extract the DTS track, and the subtitles track (if you have one). The [name] parameter doesn't need an extension, a relevant one will be added:
MP4Box -out [name] -nhml [dts track #] [filename].mp4
This will produce a *.nhml file and a *.media file.
MP4Box -out [name] -raw [subtitle track #] [filename].mp4
This will produce a *.idx and a *.sub file.
Stuff it all into a MKV
mkvmerge -o [final filename].mkv [filename].mp4 [name].media [name].sub
You will see an error about the DTS track from the MP4 file, but this can be ignored as we're manually stuffing that. This process will copy the video stream from the original file, and the extra streams we've created manually. You can delete all the files apart from the MKV once you're done.