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

A “two steps HOWTO” to fix your broken Matroska file.
Downloading High Definition(HD, 720p, 1080p, x264 encoded) content via torrent or other isn’t unusual nowadays anymore. Unfortunately such content is very big in size, and you have sometimes to wait a very long time until your latest serial has completely hit your hard disk drive. Sometimes it happens the only available source is an archive file (.rar) and you have to unrar it first, before you can watch it. And sometimes you encounter also this message on unpacking it:

unrar x filename.rar
Extracting filename.mkv 99%
filename2.avi - CRC failed
Total errors: 1

If this happens, you already know, the content isn’t in a good shape, and you just hope there are just a few unimportant bytes missing. So you unpack it again with the “-kb” switch of the unrar command, to keep the broken file (keep broken) on your hard disk:

unrar x -kb filename.rar

Here I thought, it would be wise to try to repair the broken rar-archive first (rar r filename.rar), but it turned out, that the result remains the same, and you have again to choose the “-kb” switch, else the files won’t be written onto your disk, and nothing in fact is really repaired, unless your archive posses a recovery record, which had have to be created with the “-rr[N]” switch before. I’m not an expert on that, but I’ve never encountered such a record in the wild.

So OK, what now?

Here you are now with the broken Matroska video file, and mplayer crashes after a few minutes playing. What if one could repair the broken stream? I mean, you won’t get broken video information back, if they aren’t there, you won’t see this sequence anymore, it’s gone for ever. But what if it’s just a broken container (.mkv) and only just a few frames are missing. Then it should be worth to fix the broken video file.

I’ve found a possibility to repair broken Matroska (.mkv) files with free Linux tools. What you need is a current mkvtoolnix package.
Ubuntu 8.10 provides mkvtoolnix version 2.0.2, which very likely won’t work, since there is a very ugly bug in it. Chance is high you’ll see this error:

Error: '23,976fps' is not a valid default duration in '--default-duration 23,976fps'.

if your footage is not 25 frames/second (fps), what is also very likely! Read below how to get a current version (2.4.0), it’s quite easy.
[UPDATE 2008-12-07] Intrepid Ibex (8.10) provides now mkvtoolnix version 2.4.1. Just skip the step with installing new mkvtoolnix part. Just perform on your system an ‘apt-get update && apt-get upgrade‘, and follow the instruction here.

Ok – let’s go:

Installing MKVToolnix on a Ubuntu 8.10 (Intrapid Ibex):

  1. Insert these lines into your /etc/apt/sources.list

    deb ./
    deb-src ./

  2. perform a

    aptitude update
    aptitude install mkvtoolnix

  3. Verify the version number of the mkvtoolnix application with this command:

  4. mkvinfo -V

    something like this should return:

    mkvinfo v2.4.0 ('Fumbling Towards Ecstasy') built on Nov 4 2008 00:30:11

Check this site for your Linux distribution, it’s a really good archive, and the chance is high your .deb/.rpm binaries are also available for your distro.

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.