Friday, September 04, 2015

How to Split a Matroska MKV file without re encode or any dumb software

The command line is:
"C:\Program Files\MKVToolNix\mkvmerge.exe" -o NewFile.mkv -v --split 3G --link "TheVeryLargeFileYouWantToSplit.mkv"

For a 4 gig file on an SSD, it took a total of 44 seconds.

You can split by size, time, chapters, precise frames, etc.... (note that in my example it would be 3G for 3 Gigabytes) or you can use (01:00:00 for 1 hour chunks)

The crucial option here is the --link option which is dryly explained below, but it basically means that the file looks, acts, feels like you never actually split it, but now it exists on the hard drive in multiple files, that are referencing each other. Your player software will continue on with no gaps, jumps, and the chapters and elapsed time intact. (hopefully you are using MPC-HC & LAV codecs on your computer).
(the -v is for verbose = prints status of what its doing )

Using the program MediaInfo, you can open the MKV file you want to split, and read the version of MKVMerge that was used to create it, and its recommended to use at least that version to edit/split/link it, in case its using some new features (probably not - and if not, you can use older mkvmerge versions to split newer files). Also - 
Its possible that you can download too new of a version of MKVToolNix that is incompatible with your player, as sometimes the latest version will write an incompatible header for older players/boxes.  I'm using because i've tested all my devices on it, and using version 8.3.0 which is the latest version as of this blog post, might break small things in edge case scenarios and I won't know until its too late when I go to try to play it back (So i've heard). So test on standalone bluray players/Stream boxes, and if you experience problems on playback, try to download the next major old version, unless you need some new feature, at the cost of retro compatibility. The changelogs are here (and really quite hard to read, but CTRL+F search for "Released v" which you can also use to search builds by date). 


Multiple ways to split:

 as per the --help switch of mkvmerge.exe:

 File splitting, linking, appending and concatenating (more global options):
  --split <d[K,M,G]|HH:MM:SS|s>
                           Create a new file after d bytes (KB, MB, GB)
                           or after a specific time.
  --split timecodes:A[,B...]
                           Create a new file after each timecode A, B
  --split parts:start1-end1[,[+]start2-end2,...]
                           Keep ranges of timecodes start-end, either in
                           separate files or append to previous range's file
                           if prefixed with '+'.
  --split parts-frames:start1-end1[,[+]start2-end2,...]
                           Same as 'parts:', but 'startN'/'endN' are frame/
                           field numbers instead of timecodes.
  --split frames:A[,B...]
                           Create a new file after each frame/field A, B
  --split chapters:all|A[,B...]
                           Create a new file before each chapter (with 'all')
                           or before chapter numbers A, B etc.
  --split-max-files <n>    Create at most n files.


Copied from the documentation file in "C:\Program Files\MKVToolNix\doc\en\mkvmerge.html", Section 11:

11. File linking

Matroska(tm) supports file linking which simply says that a specific file is the predecessor or successor of the current file. To be precise, it's not really the files that are linked but the Matroska(tm) segments. As most files will probably only contain one Matroska(tm) segment the following explanations use the term 'file linking' although 'segment linking' would be more appropriate.
Each segment is identified by a unique 128 bit wide segment UID. This UID is automatically generated by mkvmerge(1). The linking is done primarily via putting the segment UIDs (short: SID) of the previous/next file into the segment header information. mkvinfo(1) prints these SIDs if it finds them.
If a file is split into several smaller ones and linking is used then the timecodes will not start at 0 again but will continue where the last file has left off. This way the absolute time is kept even if the previous files are not available (e.g. when streaming). If no linking is used then the timecodes should start at 0 for each file. By default mkvmerge(1) does not use file linking. If you want that you can turn it on with the --link option. This option is only useful if splitting is activated as well.
Regardless of whether splitting is active or not the user can tell mkvmerge(1) to link the produced files to specific SIDs. This is achieved with the options --link-to-previous and --link-to-next. These options accept a segment SIDin the format that mkvinfo(1) outputs: 16 hexadecimal numbers between 0x00 and 0xff prefixed with '0x' each, e.g. '0x41 0xda 0x73 0x66 0xd9 0xcf 0xb2 0x1e 0xae 0x78 0xeb 0xb4 0x5e 0xca 0xb3 0x93'. Alternatively a shorter form can be used: 16 hexadecimal numbers between 0x00 and 0xff without the '0x' prefixes and without the spaces, e.g. '41da7366d9cfb21eae78ebb45ecab393'.
If splitting is used then the first file is linked to the SID given with --link-to-previous and the last file is linked to the SID given with --link-to-next. If splitting is not used then the one output file will be linked to both of the twoSIDs.


Side Note as a reason for creating this blog post: 

-You can not just use any old file cutter utility to chop the file in half and then repair it.
-If you search for how to do this online, you get about 100 horrible freeware,shareware, malware laden and general JUNK video editor programs. So annoying....