Skip to content

Commit

Permalink
Disable timestamp shifting in subtitle-only MKV files
Browse files Browse the repository at this point in the history
For normal media file, the starting timestamp is shifted to 0. This is necessary in order to playback trimmed audio/video that has a non-zero starting timestamp.
However, if the file only contains subtitle, we do not want to shift the start time of first line to 0. Because subtitles are never played alone, but usually attached to an audio/video. There is no way to get the starting timestamp of the audio/video in a subtitle-only MKV, so assuming 0 is a safe guess.

Fix TypesettingTools#91
  • Loading branch information
wangqr committed Apr 9, 2021
1 parent 4d6fb0b commit 0e00f6e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/MatroskaParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/

// WARNING: This library is patched by Aegisub to NOT shift timestamps. The modified parts are included in #ifdef AEGISUB blocks
// We are only using this library to extract subtitles from MKV files. The patches may break reading other tracks.
// Do NOT use the modified version to read non-subtitle tracks!
#define AEGISUB

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
Expand Down Expand Up @@ -2720,12 +2725,29 @@ static void parseFile(MatroskaFile *mf) {
if (!mf->seen.Cluster)
mf->pCluster = mf->pSegmentTop;

#ifdef AEGISUB
// In a subtitle-only MKV, the start time of first block is meaningful. Therefore do not shift offset=(-firstTimecode) globally
// By setting firstTimecode to 0, timestamps will not be shifted during parsing.
int subtitle_only = 1;
for (unsigned tracknum=0;tracknum<mf->nTracks;++tracknum)
if (mf->Tracks[tracknum]->Type != TT_SUB) {
subtitle_only = 0;
break;
}
if (subtitle_only) {
mf->firstTimecode = 0;
}
else {
#endif
adjust = mf->firstTimecode * mf->Seg.TimecodeScale;

for (i=0;i<mf->nChapters;++i)
fixupChapter(adjust, &mf->Chapters[i]);

fixupCues(mf);
#ifdef AEGISUB
}
#endif

// release extra memory
ARELEASE(mf,mf,Tracks);
Expand Down

0 comments on commit 0e00f6e

Please sign in to comment.