How to Extract Video & Audio from a Corrupt File with Codec Id Mismatch (wrong track type)?

1

The Problem:
There is a bunch of video files that can only be played with a custom player and I'd like to reverse engineer it so that the video can be played on common media players (like VLC or MPC).

What I have discovered so far:

  • Probing the files revealed that they are EBML type which is used by the Matroska Media containers.

  • So I changed the file extension to .mkv and further examined the file with MKVToolNix and FFMPEG.

  • MKVToolNix probing revealed that there was a video track encoded with h.264 and an audio track encoded with AC3 encoding.

  • But the audio track had been "marked" as a video and the video track as an audio, by deliberately changing the "track type" tag.

MKVToolNix screenshot of the track info:

MKVToolNix screenshot of the track info

  • FFMPEG also confirms the same:

[matroska,webm @ 0x7f966c800a00] Failed to open codec in avformat_find_stream_info

[h264 @ 0x7f966c811a00] Codec type or id mismatches
[ac3 @ 0x7f966c812600] Codec type or id mismatches

[matroska,webm @ 0x7f966c800a00] Could not find codec parameters for stream 0 (Audio: h264 (High), 0 channels): unspecified sample rate Consider increasing the value for the 'analyzeduration' and 'probesize' options

[matroska,webm @ 0x7f966c800a00] Could not find codec parameters for stream 1 (Video: ac3, none, 192 kb/s): unspecified size Consider increasing the value for the 'analyzeduration' and 'probesize' options

(redacted)

Duration: 01:46:42.00, start: 0.035000, bitrate: 2629 kb/s
... Stream #0:0(eng): Audio: h264 (High), 0 channels (default)

Stream #0:1(eng): Video: ac3, none, 192 kb/s, 31.25 fps, 31.25 tbr, 1k tbn, 1k tbc (default)

Doubts:

  1. Is it possible to edit the Matroska container and change the "track type" tag of the media tracks?

  2. Is it possible to use ffmpeg to specify the correct input codec for both the video and audio track, and extract them (preferably without reencoding)?

sfxedit

Posted 2019-05-27T19:14:35.800

Reputation: 21

1With ffmpeg, you can't force decoders of non-matching type, and since find_stream_info fails, you can't extract the streams either. I've a patch, now stalled, to allow to do the latter, which I need to rework. For now, try to find a low-level MKV editor which allows you to edit stream type. – Gyan – 2019-05-27T19:44:00.627

Answers

1

I couldn't file any EBML editor or Matroska Media container editor that would allow me to easily change the values of the "track type" in the Matroska files.

So it was down to using a Hex Editor to edit the binary files directly. It would have been a time consuming task if not for MKVToolNix. The MKVToolNix Info tool now includes the nifty feature of specifying the position of the data (of a tag) in a Matroska file!

According to it "Track Type: audio" is located at position 414 and "Track Type: video" is located at position 532 in the Matroska file.

Screenshot of MKVToolNix Info View:

screenshot of MKVToolNix Info View

After opening the file in the hex editor, just search for the data in the position pointed to by MKVToolNix Info. Screenshot:

screenshot of Hex Editor

We find that position 414 contains the data 83 81 02 (audio - for the tag 'Track Type') and position 532 has the data 83 81 01 (video - for the tag 'Track Type'). So we just need to interchange and replace this data.

(A shout out to Synalyze It! Pro for macOS - I used the trial version - for making hex editing so simple!)

sfxedit

Posted 2019-05-27T19:14:35.800

Reputation: 21

We always appreciate contributions in the Suggested Edit queue. However, if you goal is to "Correct spelling / grammar," please ensure that you don't misspell the words that you add. In this particular scenario, acessaccess.

– Run5k – 2020-02-19T22:24:45.923

@Run5k Oops. I must have made the typo as I was typing on a mobile device. I'll certainly pay more attention the next time when I make edits. – sfxedit – 2020-02-20T19:40:42.843