From 175ffbc71f64fe9c80c3c3d30c45031e09aa8d93 Mon Sep 17 00:00:00 2001 From: AnimeDL Date: Sun, 24 Mar 2024 15:15:45 -0700 Subject: [PATCH] vtt2ass: Improve Merging Logic for subtitles Improves merging logic for subtitles by adding a check to see if the time is within a deviation of 2. This code is cursed. Addresses #608 --- modules/module.vtt2ass.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/modules/module.vtt2ass.ts b/modules/module.vtt2ass.ts index ab681ce..54a44b1 100644 --- a/modules/module.vtt2ass.ts +++ b/modules/module.vtt2ass.ts @@ -286,16 +286,20 @@ function convert(css: Css, vtt: Vtt[]) { * This checks if a subtitle should be multi-line, and if it is, pops the just inserted * subtitle and the previous subtitle, and merges them into a single subtitle. */ - if ( - previousLine?.start == x.start && - previousLine.type == x.type && - previousLine.style == x.style && - !previousLine?.text.includes('\\pos') && - !x.text.includes('\\pos') - ) { - events[x.type as keyof typeof events].pop(); - const previousLinePop = events[x.type as keyof typeof events].pop(); - events[x.type as keyof typeof events].push(previousLinePop + '\\N'+x.text); + if (previousLine) { + const previousStart = parseFloat(previousLine.start.split(':').join('').split('.').join('')); + const currentStart = parseFloat(x.start.split(':').join('').split('.').join('')); + if ( + (currentStart - previousStart) <= 2 && + previousLine.type == x.type && + previousLine.style == x.style && + !previousLine.text.includes('\\pos') && + !x.text.includes('\\pos') + ) { + events[x.type as keyof typeof events].pop(); + const previousLinePop = events[x.type as keyof typeof events].pop(); + events[x.type as keyof typeof events].push(previousLinePop + '\\N'+x.text); + } } previousLine = x; }