Opened 5 years ago

Closed 5 years ago

#3491 closed defect (fixed)

MKV muxer corrupts subtitles

Reported by: ValdikSS Owned by: ValdikSS
Priority: normal Component: avformat
Version: git-master Keywords: mkv ass
Cc: nfxjfg@googlemail.com Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: 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

Attachments (1)

diff_between_remuxed_subtitles_extracted_with_mkvextract_and_ffmpeg.diff.gz (146.1 KB) - added by ValdikSS 5 years ago.
diff between remuxed subtitles extracted with mkvextract and ffmpeg

Download all attachments as: .zip

Change History (15)

comment:1 follow-up: Changed 5 years ago by gjdfgh

  • Cc nfxjfg@googlemail.com added

Can you post the diff?

comment:2 follow-up: Changed 5 years ago by cehoyos

Is this a regression?

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

Replying to cehoyos:

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 5 years ago by ValdikSS (previous) (diff)

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

Replying to gjdfgh:

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 5 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: Changed 5 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 5 years ago by ValdikSS

Replying to 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.

It doesn't work with this subtitles.

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

comment:8 Changed 5 years ago by gjdfgh

Actually, I was wrong.

The matroska file contains bogus packets with duplicate ReadOrder? values.

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.

Version 0, edited 5 years ago by gjdfgh (next)

Changed 5 years ago by ValdikSS

diff between remuxed subtitles extracted with mkvextract and ffmpeg

comment:9 Changed 5 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 5 years ago by ValdikSS (previous) (diff)

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

Replying to 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.

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 5 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
152,2,OP_Romaji_1,,0,0,0,,{\be1\an5\pos(779,61)\fscy206\fscx226\1c&HFFFFFF&\bord0\shad0}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 5 years ago by cehoyos

  • Priority changed from important to normal

comment:13 Changed 5 years ago by ValdikSS

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

comment:14 Changed 5 years ago by cehoyos

  • Keywords mkv ass added
  • Resolution set to fixed
  • Status changed from open to closed
  • Version changed from unspecified to git-master

Please reopen if this ticket is still reproducible, a larger patch that includes the change you suggested was committed some time ago.

Note: See TracTickets for help on using tickets.