Opened 7 years ago

Closed 6 years ago

# MKV muxer corrupts subtitles

Reported by: Owned by: ValdikSS ValdikSS normal avformat git-master mkv ass nfxjfg@googlemail.com no no

### Description

Summary of the bug:
Remuxing MKV with embedded subtitles can cause some sort of subtitles corruption in the resulting MKV file. Some lines or karaoke effects could disappear in certain video players.

How to reproduce:
I've uploaded 2 files into incoming folder on FTP: mkv_subtitles.mkv (http://yadi.sk/d/NM69ZxZQKzNn7) and mkv_subtitles_ffmpeg.mkv (http://yadi.sk/d/qDFmsDPQKzNoQ). The second one was remuxed with the following command:

ffmpeg -i mkv_subtitles.mkv -map 0 -map_metadata 0 -c copy mkv_subtitles_ffmpeg.mkv


Subtitles in the first file are played correctly while in ffmpeg-remuxed file some karaoke effects and english translation is missing (tested with cmplayer, vlc, mpv and mplayer. It works correctly only in mplayer).

If you extract subtitles with

mkvextract tracks mkv_subtitles.mkv 1:sub_orig.ass
mkvextract tracks mkv_subtitles_ffmpeg.mkv 1:sub_ffmpeg.ass


you can see the difference between them (with diff).

But if you remux MKV with extracted subtitles (extracted either with ffmpeg or mkvextract, either from original or remuxed mkv with broken subtitles), it works fine.

ffmpeg -i mkv_subtitles.mkv -i sub_ffmpeg.ass -map 0 -map -0:s -map 1 -c copy test.mkv


### comment:1 follow-up: ↓ 4 Changed 7 years ago by gjdfgh

Can you post the diff?

### comment:2 follow-up: ↓ 3 Changed 7 years ago by cehoyos

Is this a regression?

### comment:3 in reply to: ↑ 2 Changed 7 years ago by ValdikSS

Is this a regression?

I'm not sure. If this is a regression, then it was introduced a long time ago, more then 1.5 years.
Here is IRC log with the same issue from November 2012
https://gist.github.com/klaxa/197efb68e549d01193b8

Last edited 7 years ago by ValdikSS (previous) (diff)

### comment:4 in reply to: ↑ 1 Changed 7 years ago by ValdikSS

Can you post the diff?

Subtitles extracted from original file with ffmpeg are the same as extracted from remuxed file with ffmpeg.
Subtitles extracted from remuxed file with ffmpeg are different from extracted with mkvextract. Here is the diff.

### comment:5 Changed 7 years ago by gjdfgh

Sorry, your diff is pretty useless. Appears it thinks _all_ lines are different, maybe due to line ending issues or so.

Anyway, I made my own diff. Looking at this, it seems like ffmpeg reorders some lines when it shouldn't. In general, ffmpeg doesn't respect the order of the lines (ReadOrder? field internal to matroska ASS packets), and this is probably the problen. But I'm surprised that this has such a large effect.

### comment:6 follow-ups: ↓ 7 ↓ 10 Changed 7 years ago by Cigaes

That happens because, by default, the Matroska demuxer uses some legacy format for demuxed ASS packets.
You can get it to use the newer format, and thus preserve the read order, by adding -c:s ass as input option.

### comment:7 in reply to: ↑ 6 Changed 7 years ago by ValdikSS

That happens because, by default, the Matroska demuxer uses some legacy format for demuxed ASS packets.
You can get it to use the newer format, and thus preserve the read order, by adding -c:s ass as input option.

It doesn't work with this subtitles.

[ass @ 0x1156660] ASS encoder supports only one ASS rectangle field.


### comment:8 Changed 7 years ago by gjdfgh

Actually, I was wrong.

The matroska file contains bogus packets with duplicate ReadOrder? values. (Edit: I mean the ffmpeg produced matroska file. The source file is ok.)

The ffmpeg produced mkv files are thus 100% broken.

With mpv, it actually works with --demuxer=lavf, because that uses the old way to demux Matroska ASS packets, which "happens" to work (I can explain details). MPlayer works because it uses the libavformat demuxer.

What Cigaes said probably applies here.

Last edited 7 years ago by gjdfgh (previous) (diff)

### Changed 7 years ago by ValdikSS

diff between remuxed subtitles extracted with mkvextract and ffmpeg

### comment:9 Changed 7 years ago by ValdikSS

Just updated diff file with correct one.

This is not only this file or only this subtitles issue. I have two more mkvs with this issue.

Last edited 7 years ago by ValdikSS (previous) (diff)

### comment:10 in reply to: ↑ 6 Changed 7 years ago by ValdikSS

That happens because, by default, the Matroska demuxer uses some legacy format for demuxed ASS packets.
You can get it to use the newer format, and thus preserve the read order, by adding -c:s ass as input option.

Sorry, it seems like i've added it after -i. Adding before -i solves the problem.
Should this option be used by default?

### comment:11 Changed 7 years ago by gjdfgh

Depending on the reader, the extracted subtitles won't show the problem, btw.

libass uses the ReadOrder? field to remove duplicates (so if two events have the same ReadOrder? value, one of them is removed, even if they are different).

Extract from the file:

151,2,OP_Romaji_1,,0,0,0,,{\be1\an5\pos(784,56)\fscy230\fscx182\1c&HFFFFFF&\bord0\shad0}no
152,27,OP_Romaji_1,,0,0,0,,{\be1\an5\pos(774,50)\alpha&H79&}no
153,27,OP_Romaji_1,,0,0,0,,{\be1\an5\pos(774,46)\alpha&H79&}no


The 3rd event is never displayed, because it has the same ReadOrder? as the 2nd.

### comment:12 Changed 7 years ago by cehoyos

• Priority changed from important to normal

### comment:13 Changed 6 years ago by ValdikSS

• Component changed from undetermined to avformat
• Owner set to ValdikSS
• Status changed from new to open