MKV to M2TS conversion script

The Play Station 3 can’t play MKV file. Therefore I’ve written a script that creates a PlayStation 3 compatible M2TS from a MKV, assuming video is H.264 and audio is AC3 or DTS with as little re-encoding as possible. Any subtitles in the MKV are preserved in the M2TS although the PS3 can’t display subtitles in M2TS containers. Optionally splits the M2TS, if it is greater than 4GB, to maintain FAT32 compatibility. Unlike other MKV to M2TS solutions, this script doesn’t create any intermediate files during the conversion. The PS3 can’t play DTS audio streams in M2TS containers, therefore DTS audio is transcoded to AC3.

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

MKV-to-M2TS.sh was released on April 23rd 2009 to help celebrate St. George’s Day.

Posted on April 23, 2009 at 5:00 pm by Martin · Permalink
In: Audio & Video · Tagged with: , , , ,

16 Responses

Subscribe to comments via RSS

  1. Written by Martin
    on May 11, 2009 at 9:53 pm
    Reply · Permalink

    v1.1 has just been released.

    http://code.flexion.org/MKV-to-M2TS.html

  2. Written by Dirk
    on June 11, 2009 at 6:58 pm
    Reply · Permalink

    Small improvement to the script:

    When you grep for the video stream use “: video” in stead of just video. Otherwise the script fails when the path of your movie contains the word video, it much less likely that it contains “: video”.

    eg.
    VIDEO_ID=`grep “: video” ${MKV_TRACKS} | cut -d’ ‘ -f3 | sed ‘s/://’`

    Nice script!

  3. Written by Twocats
    on July 29, 2009 at 10:49 pm
    Reply · Permalink

    Hello,
    I’m, with another develloper (Sioban), the creator of this little scipt :
    http://twocats.free.fr/pub/h264tobox/h264tobox-1.2.2.tar.gz
    The goal is the same of yours with more options :

    Support in input :
    mkv
    avi
    mp4
    m2ts
    ts

    In output :
    ts
    m2ts
    avchd
    brd
    avi
    mkv

    Audio conversion available in AC3 for :
    DTS
    FLAC
    MP3
    PCM
    Vorbis
    TTA

    The script is in french (but the code is in bash :) ) and in GPL 2 and you can use it if you want.

    The topic of the script is here :
    http://forum.hardware.fr/hfr/OSAlternatifs/Logiciels-2/h264tobox-freebox-playstation-sujet_67428_1.htm

    Twocats

    P.S. : Sorry for my poor english :(

  4. Written by Jukka
    on November 24, 2009 at 11:58 am
    Reply · Permalink

    Hi, my friend Jarkko made a small change to your mkv-to-m2ts script. It now asks which audio and subtitle tracks to convert by using –ask. If you like, I can sent this modified script to you by email. It is a “cheap and dirty” solution but it works!

  5. Written by Martin
    on December 3, 2009 at 11:45 am
    Reply · Permalink

    @Jukka, please send along any modifications they will be appreciated. That said, I am working on a new script call StreamWeaver which combines the features of several scripts. StreamWeaver asks which audio and video tracks to convert. If you don’t give any audio and video tracks it displays a list of which ones to choose from. I hope to release this new script in January, maybe sooner.

  6. Written by Jukka
    on December 3, 2009 at 2:01 pm
    Reply · Permalink

    You can find the modified skript from following link:

    http://forum.ubuntu-fi.org/index.php?action=dlattach;topic=24763.0;attach=3085

    Like I said earlier, it is a bit cheap and dirty solution.

    I am really looking forward the StreamWeaver skript :) I have used quite many different tools/skipts/programs to convert MKV files to my PS3.

    I need sometimes also subs when watching non-English movies which is really a pain in PS3. Only AVCHD in USB stick works at the moment but still I need to author the AVCHD folder to get it work. So far I have not found any solution to do it that works on ubuntu (without wine). Alternatively the subs has to be “added/burned” into movie file in order to make them work. It takes forever and life is too short.

    Thank you for your very educational wiki’s and other stuff!

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

    @Jukka: Thanks for the link. I’ll take a look at the changes. I might release a final version of MKV-to-M2TS to incorporate your stuff.

    I’m interested in what you’ve said about AVCHD, I’ve not experimented with it but it sounds like something I should look into. I have a number of films that are not English language audio and haven’t yet found a way to watch them, with subs, on the PS3. I’ll do some research but any additional information/links you can provide would be welcome :-)

  8. Written by Jukka
    on December 4, 2009 at 1:13 pm
    Reply · Permalink

    Sorry for sending spam. I had some problems when pasting text from outlook :(

    @Martin: Easiest way to try PS3 compatible AVCHD is to create a movie from MKV file by tsMuxer with AVCHD option selected (remember to split it to 4 GB for Fat32 compatibility). Then use AVCHDme20 program for authoring AVCHD folder (Win only). It will check FAT32 compatibility, rename the files according to AVCHD 8.3 format and do the CLPI validity check. More information can be found from here: http://en.wikipedia.org/wiki/.m2ts (if something more is required that I am not sure). The you need to copy the AVCHD folder to your USB stick (to root). After connecting your USB stick to PS3, select the USB stick from the PS3 menu and press the green triangle button instead of X. You should see your movie now with subs.

    You may find some nice ideas from h264tobox script (at least compared to one I send you). Unfortunately my French skills are a bit poor so I cannot use it but I did evaluated it by using Google Translate (comments) to get the idea how it works.

    StreamWeawer sounds perfect! Looking forward to it.

    Please delete the two earlier postings.

  9. Written by Martin
    on December 4, 2009 at 5:33 pm
    Reply · Permalink

    @Jukka: Thanks for the information. I’ll do some research. I’m not sure when StreamWeaver will be ready but I have most of the basic functionality completed. Check my blog from time to time to check on my progress.

  10. Written by zioalex
    on December 17, 2009 at 11:23 am
    Reply · Permalink

    Hi Martin,
    thanks for your very useful script. I’m making some improvement ( support multi audio track and subtitles )…When I’ll finish I send you the mods.

    I have found some audio sync problem; have you find any solution to that…or have you any idea on how to automating calc the timeshift option for tsMuxeR?

    Thanks
    Zioalex

  11. Written by Martin
    on December 19, 2009 at 9:16 am
    Reply · Permalink

    @zioalex: Please do send me you mods, I would love to integrate any improvements :-) I have seen audio sync problems once, when ripping a Blu-Ray disc of mine. This is how I cam to create StreamWeaver (still unfinished) and it can automatically adjust audio stream offsets. Off the top of my head I can’t remember if I get the offsets from tsMuxeR or mediainfo, but the offsets are the in the source stream. I’ll try and get StreamWeaver to a point of completion for an initial release.

  12. Written by Martin
    on January 7, 2010 at 11:56 am
    Reply · Permalink

    @Jukka: I did some more work on StreamWeaver over the holidays. I can process MKV, Blu-Ray (M2TS), HD-DVD (EVO) and MPEG-4 files as input. I’ve been researching the AVCHD stuff and found and excellent guide for Linux, all of which should be possible to automate.

    http://elliot.orwells.org/?p=425

  13. Written by zioalex
    on January 8, 2010 at 10:46 am
    Reply · Permalink

    Martin, I post the mods directly here on the blog?

  14. Written by Jukka
    on January 14, 2010 at 1:30 pm
    Reply · Permalink

    @Martin: I have been looking the most recent wiki pages daily basis but yesterday I had a look on this page again by accident and noticed your post. I had also a look on the web page you referred on your post. This AVCHD is much more complicated that I could ever imagine. But if all the phases to create PS3 compliant AVCHD can be automated by your script then you are have really saved the day for me.

    Your StreamWeaver script seems to be all-in-one kind of tool to make movie conversions. If it can accept all those containers as an input then I don’t need any other tool when making conversions (and no need for additional win tool which is a big thing for me). Thanks… I already credited you to my girl friend when we were watching nice movie on the other day ;) She is happy too…

  15. Written by krislec
    on January 17, 2010 at 2:32 pm
    Reply · Permalink

    Hi,
    First : Thanks for this great script ! only yours works well for me to use with my ps3 and my readynas.
    But i have some troubles with somes of my mkv files.
    Your scripts tell me that audio is not in DTS but it is !
    I try to modified the script but it didn’t work. I’m not very good in bash (and english too !)… so I need your help
    Thank.

    The script :
    MKV-to-M2TS v1.1 – Creates a PlayStation 3 compatible M2TS from a MKV.
    Copyright (c) 2009 Flexion.Org, http://flexion.org. MIT License

    DTS DTS
    ./MKV-to-M2TS.sh: line 82: [: 4 : opérateur binaire attendu
    ./MKV-to-M2TS.sh: line 90: [: trop d’arguments
    WARNING! H.264 stream does not contain fps field. Defaulting to 23.976.
    ERROR! The audio track is not DTS or AC3. I can’t process DTS
    DTS, please use a different tool.

    The mkv:
    mkvinfo Hannah.Montana.Le.Film.mkv
    + EBML head
    |+ Doc type: matroska
    |+ Doc type version: 2
    |+ Doc type read version: 2
    + Segment, size 7041986750
    |+ Seek head (subentries will be skipped)
    |+ EbmlVoid (size: 4025)
    |+ Segment information
    | + Timecode scale: 1000000
    | + Muxing application: libebml v0.7.7 + libmatroska v0.8.1
    | + Writing application: mkvmerge v2.9.8 (‘C’est le bon’) built on Aug 13 2009 12:49:06
    | + Duration: 6140.139s (01:42:20.139)
    | + Date: Wed Sep 9 04:16:13 2009 UTC
    | + Title: Hannah Montana, Le Film 2009 720p BluRay Multi DTS x264-GAÏA
    | + Segment UID: 0xb4 0×19 0xd6 0×82 0x1b 0×64 0xa9 0×83 0x9f 0x7e 0xe8 0x2d 0x0a 0xbd 0xd4 0×55
    |+ Segment tracks
    | + A track
    | + Track number: 1
    | + Track UID: 692534719
    | + Track type: video
    | + Enabled: 1
    | + Default flag: 1
    | + Forced flag: 0
    | + Lacing flag: 0
    | + MinCache: 1
    | + Timecode scale: 1
    | + Max BlockAddition ID: 0
    | + Codec ID: V_MPEG4/ISO/AVC
    | + Codec decode all: 1
    | + CodecPrivate, length 40
    | + Default duration: 41.708ms (23.976 fps for a video track)
    | + Language: eng
    | + Video track
    | + Pixel width: 1280
    | + Pixel height: 720
    | + Interlaced: 0
    | + Display width: 1280
    | + Display height: 720
    | + A track
    | + Track number: 2
    | + Track UID: 2186312836
    | + Track type: audio
    | + Enabled: 1
    | + Default flag: 1
    | + Forced flag: 0
    | + Lacing flag: 1
    | + MinCache: 0
    | + Timecode scale: 1
    | + Max BlockAddition ID: 0
    | + Codec ID: A_DTS
    | + Codec decode all: 1
    | + Language: fre
    | + Audio track
    | + Sampling frequency: 48000
    | + Channels: 6
    | + A track
    | + Track number: 3
    | + Track UID: 1434302624
    | + Track type: audio
    | + Enabled: 1
    | + Default flag: 0
    | + Forced flag: 0
    | + Lacing flag: 1
    | + MinCache: 0
    | + Timecode scale: 1
    | + Max BlockAddition ID: 0
    | + Codec ID: A_DTS
    | + Codec decode all: 1
    | + Language: eng
    | + Audio track
    | + Sampling frequency: 48000
    | + Channels: 6
    | + A track
    | + Track number: 4
    | + Track UID: 3126281388
    | + Track type: subtitles
    | + Enabled: 1
    | + Default flag: 1
    | + Forced flag: 0
    | + Lacing flag: 0
    | + MinCache: 0
    | + Timecode scale: 1
    | + Max BlockAddition ID: 0
    | + Codec ID: S_TEXT/UTF8
    | + Codec decode all: 1
    | + Language: fre
    | + Name: French Forced
    | + A track
    | + Track number: 5
    | + Track UID: 3563488661
    | + Track type: subtitles
    | + Enabled: 1
    | + Default flag: 0
    | + Forced flag: 0
    | + Lacing flag: 0
    | + MinCache: 0
    | + Timecode scale: 1
    | + Max BlockAddition ID: 0
    | + Codec ID: S_TEXT/UTF8
    | + Codec decode all: 1
    | + Language: fre
    | + Name: French
    |+ EbmlVoid (size: 1024)
    |+ Cluster

  16. Written by Jukka
    on February 10, 2010 at 1:56 pm
    Reply · Permalink

    @Martin: I have created couple of AVCHD movies based on the instructions on page http://elliot.orwells.org/?p=425. I had some issues but eventually I get them done. It seems that x264 quality defines how long the conversion takes (my experience between 2,5 – 8 hours). I am quite picky that the quality is as good as possible . Only thing is that it takes quite many steps to create one movie so I wonder how is your StreamWeaver progressing? ;)

Subscribe to comments via RSS

Leave a Reply