MKV to MPEG-4 conversion script

The Play Station 3 can’t play MKV file. Therefore I’ve written a script that creates a PlayStation 3 or Xbox 360 compatible MPEG-4 from Matroska providing the video is H.264 and audio is AC3 or DTS. Xbox 360 compatibility requires that audio is forcibly downmixed to stereo with ‘–stereo’. AAC 5.1 audio will have the correct channel assignments when transcoding from AC3 5.1 and DTS 5.1. If ‘neroAacEnc’ is installed then it in preference to ‘faac’ for encoding the AAC audio as it produces better quality output. neroAacEnc is optional.

The script does as little re-encoding as possible, only the audio and subtitles are re-encoded or converted. The script can detect profile 5.1 H.264 and patch it to 4.1 in under a second. Any subtitles in the Matroska are preserved. If ‘mp4creator’ is used the subtitles are extracted stored in a seperate file. If ‘MP4Box’ is used (default) the subtitles are converted to GPAC Timed Text and muxed into the resulting
MPEG-4. The PS3 can’t display these subtitles but some software players can.

The script can optionally split the Matroska if it is greater than 4GB to ensure Play Station 3, Xbox 360 and FAT32 compatibility.

This script works on Ubuntu Linux and should work on any other Linux/Unix flavour and possibly Mac OS X providing you have the required tools installed.

Posted on August 27, 2009 at 8:48 pm by Martin · Permalink
In: Audio & Video · Tagged with: , , , , , , , , , ,

9 Responses

Subscribe to comments via RSS

  1. Written by John Goodwin
    on August 28, 2009 at 2:23 am
    Reply · Permalink

    Great blog, I’ve just spent the last hour or so browsing through it.

    You have some great articles which I will explore in depth tomorrow which are of interest to me as a fellow Ubuntu user and HP-UX SysAdmin in my spare time… oh, I meant work.

    Keep up the good work!

    Kind regards

    John Goodwin.

    P.S. Discovered via Google for Blubuntu advice

  2. Written by Bill
    on September 8, 2009 at 11:17 pm
    Reply · Permalink

    I’m getting the following errors while using your script:

    sox soxio: Failed reading `-’: unknown file type `auto’
    ERROR: could not parse WAV file

    and

    Unknown input file type
    Error importing True_Blood_S02E11_720p_HDTV_X264_DIMENSION_2ch.m4a: Requested URL is not valid or cannot be found
    Removing temporary files
    – True_Blood_S02E11_720p_HDTV_X264_DIMENSION.h264
    -
    – True_Blood_S02E11_720p_HDTV_X264_DIMENSION_2ch.m4a
    – True_Blood_S02E11_720p_HDTV_X264_DIMENSION_2ch.aac
    All Done!

    I only end up with
    True_Blood_S02E11_720p_HDTV_X264_DIMENSION.AC3

    For output. Seems to be something with sox.

    Thanks,
    Bill

  3. Written by Bill
    on September 8, 2009 at 11:47 pm
    Reply · Permalink

    Ok, I fixed the unknown file type ‘auto’ with:

    sudo apt-get install libsox-fmt-all

    Now I still getting the failed reading ‘-’

    Bill

  4. Written by Bill
    on September 8, 2009 at 11:52 pm
    Reply · Permalink

    Sorry to be a pest, but it did out put an mp4 file with the –faac flag….shrug. I do have NeroAacEnc installed and residing in my path of ~/bin.

  5. Written by Martin
    on September 20, 2009 at 8:59 am
    Reply · Permalink

    @Bill: Sorry to hear you’re having difficulties and sorry for the late reply, I must do better at approving comments quickly. I’ve not seen the error you are reporting but if I encounter the problem myself, I’ll endeavour to fix it. I am pleased to here that you can work around the issue using the ‘–faac’ option.

    Thanks for posting the package dependency it may help others, although I recommend installing packages with ‘aptitude’ to ensure all recommended packages are also installed. It can help avoid the issues like the one you ran into.

  6. Written by saúl
    on November 10, 2009 at 9:06 am
    Reply · Permalink

    Your script don’t work with more than one audio track, i modify this lines:
    AUDIO_ID=`grep audio ${MKV_TRACKS} | cut -d’ ‘ -f3 | sed ‘s/://’ | head -n 1`
    AUDIO_FORMAT=`grep audio ${MKV_TRACKS} | cut -d’ ‘ -f5 | sed ‘s/(\|A_\|)//g’ | head -n 1`

    and add this lines below of the past lines:
    if [ "$VIDEO_ID" = "" ]; then VIDEO_ID=0; fi
    if [ "$AUDIO_ID" = "" ]; then AUDIO_ID=0; fi
    if [ "$SUBS_ID" = "" ]; then SUBS_ID=0 ; fi

    Thanks for share your work!

    Sorry for my bad english. I’m mexican.

  7. Written by mathew
    on December 3, 2009 at 3:02 pm
    Reply · Permalink

    Wow, it works!

    I must have spent hours trying various alleged methods to convert MKV files into standard MP4s without re-encoding the video. Yours is the first that has resulted in an MP4 that’ll actually be played by QuickTime and the PS3.

  8. Written by Martin
    on December 4, 2009 at 6:44 am
    Reply · Permalink

    @saúl: Thanks for your code. I’ll release a new version that incorporates your changes.

    @mathew: Glad it worked, thanks for the endorsement :-)

  9. Written by Marcelo Culotta
    on September 1, 2011 at 5:23 pm
    Reply · Permalink

    For converting MKV DTS to MKV ac3 in UBUNTU, I use two applications:
    WinFF and MKVmerge (it shows as MKV files creator in the Applications menu)

    1. WinFF:For creating the AC3 audio track, I open WinFF, then I Add the MKV file that contains the DTS track. In Output details, I select

    Convet to Audio
    Device Preset Ac3 – 384kbps Stereo
    Output Folder Any folder you like (I use the one containing the MKV file)

    This will create an audio file with the same name as the MKV file, but with the ac3 extension

    2. MKV files creator (mkvmerge)

    Input Files: add the MKV file for which you created the ac3 audio track.
    In the Tracks, chapters and tags, UNSELECT the DTS audio file
    Input files again: add the ac3 file that you created with WinFF
    Start muxing

    This creates a file with the same name as the MKV file, except it has a (1) added to it that will have the ac3 audio track for playing as 2 channel stereo.

    I hope this helps!

Subscribe to comments via RSS

Leave a Reply