Dear all,
at first, I apologize that I can’t report this bug on Gitlab. I tried to register there, but they force me to give them my phone number for that purpose. This definitely won’t happen.
I am using the most recent version 84.0 of MKVToolnix / mkvmerge under Windows 10 x64 Enterprise. The following bug report is related to this post.
Bug description / wrong behaviour
Using mkvmerge
on the command line, I am converting M2TS files to MKV files, choosing only certain tracks, and using external timestamp files. The source files contain PGS subtitle tracks that are also merged into the result file using external timestamp files. The timestamp files are format 2.
When I play the resulting file in MKV players (tested MPC-HC and VLC), everything works except the PGS subtitles. Some subtitles remain on the screen way too long. That is, a subtitle may remain on the screen for minutes where it actually should remain only for seconds.
This problem does not appear when I let mkvmerge
create the result file without external timestamp files for the subtitle tracks.
I have analyzed the cause of problem as far as it is possible for me (being a complete newbie in that area). The problem does nothing have to do with players, but seems to be due to a bug in mkvmerge
. I’ll first describe it on a high level and then show some details from a test I did today.
Usually, displaying a PGS subtitle comprises two actions. First, the subtitle appears, and then disappears again. Disappearance of a subtitle is effected by letting the next subtitle appear. This next subtitle can be (and in most cases, is) an “invisible” (transparent) subtitle. The duration of a subtitle then is just the difference between the PTS of that subtitle and the PTS of the next subtitle.
According to a statement from the developer of mkvmerge
, that’s also the way that mkvmerge
follows to compute a PGS subtitle’s duration.
Now let’s consider some test results. I have a M2TS input file from which I would like to merge tracks 0 (video), 1 (DTS-HD MA audio) and 7 (PGS subtitles) into a MKV file. The input file is named input.m2ts
, the MKV output file is named output.mkv
Let us focus on a PGS subtitle that appears roughly at second 85 and disappears roughly at second 92. To analyze what happens, I use the mkvinfo
tool and look at the respective clusters. In the following screenshots, track 1 is the video track, track 2 is the audio track and track 3 is the PGS subtitle track.
Test 1: no external timestamp files
The command line:
mkvmerge -o output.mkv -d 0 -a 1 -s 7 input.m2ts
After loading output.mkv into the mkvinfo tool, we can find the following two clusters that relate to the subtitle in question:
A few things to note here:
- The subtitle is encoded in simple blocks.
- The first screenshot shows the cluster where the subtitle appears (PTS 85.586).
- The second screenshot shows the cluster where the subtitle disappears (PTS 92.551).
- Note that there are no more subtitles in the source file. That is, the second screenshot does not show the PTS of the next visible subtitle, but the PTS of a “subtitle” that makes the current subtitle disappear.
That is how things are expected to work: One subtitle frame that makes the subtitle appear with PTS 1 (85.586), one corresponding subtitle frame that makes it disappear with PTS 2 (92.551), where PTS 1 minus PTS 2 is the subtitle duration.
[ As a side note, mkvmerge
calculates the subtitle PTS as 85.586, but a few other well-respected tools say that it is 85.669. This may be due to a general offset, which would be OK. However, the second PTS that mkvmerge
calculates (that is, the time when the subtitle disappears, 92.551) is exactly the same that those other tools report, too. Therefore, the duration of the subtitle probably is not as intended. But let’s focus on the actual problem and ignore this one for the time being. ]
Test 2: External timestamp file for the PGS subtitle track
For the subtitle in question, there are the following timestamps in the external timestamp file:
85669
92551
The first one is the PTS where the subtitle should appear, the second one is the PTS where it should disappear. The command line to create the MKV file, using the external subtitle timestamp file with name 7.timestamp
, is:
mkvmerge -o output.mkv --timestamps 7:7.timestamp -d 0 -a 1 -s 7 input.m2ts
After loading output.mkv
into mkvinfo
, we can find the following:
There is no second screenshot in this case, because this is now the only place that refers to the subtitle in question. In other words, there is no later cluster where this subtitle disappears by replacing it with a transparent subtitle, or where track 3 is referenced in any way at all. This may or may not be a problem - I really don’t know.
The first real problem is that mkvmerge
does not respect the PTS that is in the timestamp file. The PTS for appearance of the subtitle in the timestamp file is 85.669, but the screenshot shows that mkvmerge
keeps using 85.586
(which is the value from the previous test without timestamp file). There is definitely no timestamp with this value in the timestamp file.
The second real problem is that mkvmerge
calculates the duration in a wrong way. From the screenshot, we see that it is very small (84 ms). I needed a while until I understood where that value comes from, but then it became clear: It is the PTS from the timestamp file (85.669) minus the PTS that mkvmerge
uses instead (85.586) plus 1. The plus 1 may be due to rounding.
Test 3: Same as test 2, but with other PTS value in the timestamp file
I tried to verify my findings and therefore in the timestamp file replaced 85.669 by 85.586 (the value mkvmerge
insisted on anyway). The result was as expected:
Obviously, the duration again has been calculated according to the formula mentioned above: (PTS from the timestamp file (85.586)) minus (PTS mkvmerge
uses (85.856)) plus 1 = 1.
Summary
When merging a MKV file, mkvmerge
does not behave as intended and described when using external timestamp files for PGS subtitle tracks. Notably, timestamps from the timestamp file may be ignored (in that they do not become the subtitle PTS in the MKV file), and the duration of a subtitle in the MKV file may be not equal to the PTS of the next subtitle minus the PTS of the subtitle. I believe that both problems are due to a bug.
I’ll now try to upload the input file, the two variants of the timestamp file and the three output files to your sftp server. I’ll put the files into the directory Unico
.
Please note that everything I have written above pertains only to the subtitle I have investigated. I’ll conduct further tests to find out whether the other (previous) subtitles work correctly.
Thank you very much for your great software, your time and your support!