How to fix/repair broken Matroska (.mkv) files with Linux

Demux your file now:

  1. You have now to demux the file into audio and video files. But you have to determine what streams the footage contains, and you have to remember them and the fps rate. Example:

    mkvinfo -s filename.mkv | head -3

    results in these lines. Now you have to look for the appropriate language.

    Track 1: video, codec ID: V_MPEG4/ISO/AVC, default duration: 41.708ms (23.976 fps for a video track), language: eng, pixel width: 1920, pixel height: 1040, display width: 1920, display height: 1040
    Track 2: audio, codec ID: A_AC3, default duration: 32.000ms (31.250 fps for a video track), language: ger, sampling freq: 48000, channels: 6
    Track 3: audio, codec ID: A_DTS, language: eng, sampling freq: 48000, channels: 6

    If you need the English track, you have to remember “Track 3”. Your video track number is “Track 1”.

  2. Now you’ve got to extract the file:

    mkvextract tracks filename.mkv 1:mkv_video 3:mkv_audio_eng

    It’s up to you, how your files are called, these are my names, I’ve used for this example, please adjust them accordingly:

    1. filename.mkv
    2. mkv_video
    3. mkv_audio_eng

Merge your files:

    Now you just simply have to merge them again. The repairing part is actually the extraction itself, because a plausibility check of the content is done, and corrected on the fly.

  1. mkvmerge --default-duration 0:23.976fps mkv_video mkv_audio_enl -o my_output_filename.mkv

    The fps value is taken from the very first step (mkvmerge -i). The first output line gave you the frames per second rate (fps). Take this value, else you’ll suffer from an extreme evil audio de-sync.

I’m on a Q6600 quadcore machine with a RAID0. The whole repairing process of a 8GB Matroska file took me about less than 15mins (conservative estimation). If you are on a slow machine and a remote network it could take significantly longer, but not ages, since it’s no trancoding, but a demuxing.

That’s it.

Please report bug, errors, typos. If you really liked this article, click on any of the sponsored links 😉 Thanks.

10 thoughts on “How to fix/repair broken Matroska (.mkv) files with Linux

  1. The situation: I want to download only the last part of a rar archive (eg. filename.r34) from a movie/TV/etc, only to see something in the credits. Of course rar won’t let me unpack it so I grab the “filename.rar” as well, rename r34 to r00 and extract (-keep broken and all). So I have my “filename.mkv” which plays until it gets to the broken-force/joined part. Your’s would be my only solution but I couldn’t get your results. The mkvextract only extracts up to the first part and the rest it ignores. Maybe the gap is just to big for it.
    Windows version 2.4.0
    It’s a nice idea though.

  2. Sounds like a good idea but unfortunately, it crashes on my AMD64 Ubuntu 8.10 system.

    *** glibc detected *** mkvextract: free(): invalid pointer: 0x000000000080404a ***
    ======= Backtrace: =========

    … and the eventual

    Aborted (core dumped)

  3. So the 2.4.0 behaves differently in with Linux(or just Ubuntu)? Because I get at least the first part extracted.
    In fact I even tried looking for the h264 format specs but it just gets too complicated to even find them. Because then I’d just delete a few (hundred-thousand) bytes and have a proper-broken file to work with.
    But I guess the TAP-TZX format, Speccy era, is long gone and so is it’s simplicity (If you don’t know never mind. The more in the past we travel the simpler…)
    But it is disappointing to have such a weakness in the format and in general in the way it handles broken files-which it just doesn’t. That never happens in an XviD/DivX. More of the simpler past examples…

  4. @Iznogoud
    hm, yes – I can feel what you think 😉
    I’ve done my tests by just filling hundrets of bytes with FFs with hexedit. But I’ve never had the idea just to extradt part.r78 or stuff to see the credits.
    It’s a nice feedback though, because it shows this solution is limited to minor damaged files only.


  5. @acmelab68
    That’s the repo that I used. The weird thing is, I don’t think there’s much wrong with the file because it plays fine in mplayer. I am however having problems re-encoding it with mencoder, even when doing something as simple as -oac copy -ovc copy so there’s definitely something wrong with the file.

    FileName : Superman.The.Elite.2012.DVDRip.XviD.Ac3.Feel-Free.mkv
    Header Data :
    52 61 72 21 1a 07 00 ce 99 73 80 00 0d 00 00 00 Rar!…..s……
    00 00 00 00 9f 93 17 0f 02 11 4f 84 68 c7 90 18 ……….O.h…
    49 ed 38 b8 16 31 f1 35 d3 dd 08 bc e2 a2 fe 30 I.8..1.5…….0
    a7 f2 06 02 84 a0 7a cf 89 c5 4e 7d 09 96 20 8a ……z…N}.. .
    95 88 d5 cd 6a f8 f9 80 3d 94 50 c0 8f 81 64 97 ….j…=.P…d.
    fd e3 6d bf d0 dc e8 97 c2 21 db 18 7f bd b8 11 ..m……!..…
    f7 39 4f b6 2b 23 a4 28 8c 97 a6 0a c7 ad 32 79 .9O.+#.(……2y
    d6 6a 7c b6 90 9b 23 3f 02 e1 f8 58 e4 9f fd 2e .j|…#?…X….
    23 6d 34 04 c6 9c d8 29 29 4f 54 a7 16 2f b6 de #m4….))OT../..
    c4 8c f8 33 07 17 e7 aa b7 b9 bf 6f 4f 09 1d ad …3…….oO…
    16 29 1d 3d ff f3 ee 5f c5 aa 0d 4d 65 be 76 ca .).=…_…Me.v.
    49 b7 3d 34 d2 c0 f7 c7 75 06 2e 93 37 06 c4 2a I.=4….u…7..*
    9e 38 b0 3a 21 da 0e 2c 06 bb ba 1e 22 c4 30 4e .8.:!..,….”.0N
    a9 a0 e2 b8 d3 9b e5 d0 2c e7 6b 68 8c 4b 95 6d ……..,.kh.K.m
    5e bd 09 82 64 6e 95 91 44 bb a2 77 17 d4 96 18 ^…dn..D..w….
    c9 f0 a0 60 59 38 cd 3b a1 2c 59 10 e9 a1 a3 73 …`Y8.;.,Y….s
    ec f6 ee 7d 97 b3 b0 d2 34 d4 56 0f 9f 93 17 0f …}….4.V…..
    02 11 4f 84 b3 15 c9 4d b2 52 4c 4e d5 e9 dc dc ..O….M.RLN….
    e6 4f 8c ce a3 03 2d 69 85 6c e2 b2 de 38 82 f3 .O….-i.l…8..
    0f 85 e8 05 3d de 5e 71 54 f1 6e 62 8b ac 54 e7 ….=.^qT.nb..T.
    7b 8f 90 ec 69 92 40 bb 7b 9e 0a 3b e1 38 6e 23 {…i.@.{..;.8n#
    76 ec 9d 6d aa 9e 39 8e 12 5c 39 45 2d 44 af b9 v..m..9..9E-D..
    02 89 ba 90 67 fe b3 0b 79 7d 82 12 a0 5f 36 33 ….g…y}…_63
    8c b4 49 1c 03 f8 73 2d 23 3e 18 59 f6 d1 78 65 ..I…s-#>.Y..xe
    0f ff 42 4e 7c df 99 bc 85 6a 46 ae dc 6c 62 65 ..BN|….jF..lbe
    3b 02 6d 6b 3b 33 6f 2d dc ea d7 84 9a 71 e4 82 ;.mk;3o-…..q..
    79 19 80 a3 a0 7a f7 6d f7 e6 56 a5 bf 6a 6f df y…
    d8 19 ff b3 81 a2 71 13 88 80 ba f6 e3 aa c3 fe ……q………
    e2 4b 73 e6 9f 93 17 0f 02 11 4f 84 9c a4 db 91 .Ks…….O…..
    cf e3 2e 74 e0 0b c8 e1 a4 1c f2 df 4c 59 c6 0c …t……..LY..
    ab f1 a5 8e 50 ac 11 b9 a9 18 90 5b 55 6b 83 bd ….P……[Uk..
    22 91 cc 31 f6 3a fb b6 50 2d d4 e5 88 a5 52 5a “..1.:..P-….RZ

    how can i fixed it ..plzz tell me

Comments are closed.