asbplayer icon indicating copy to clipboard operation
asbplayer copied to clipboard

Fix: Previous Subtitle Navigation with Multiple Tracks #578

Open JSchoreels opened this issue 2 weeks ago • 2 comments

Fix: Previous Subtitle Navigation with Multiple Tracks

When two subtitle tracks are enabled, pressing "Seek to previous subtitle" would stay on the current subtitle instead of jumping to the actual previous one.

The bug was in the adjacentSubtitle function at line 23 of common/key-binder/key-binder.ts. The original code was:

adjacentSubtitleIndex = now < s.end ? Math.max(0, i - 1) : i;

This assumed that i - 1 would give the previous subtitle. But when multiple tracks are enabled, the subtitles array contains all tracks interleaved and sorted by time. So i - 1 might point to a different track's subtitle.

For example, at timestamp 631787ms with these subtitles:

[323] Track 1: 629295-632129ms  "Of course, it is."
[324] Track 0: 629296-632130ms  "もちろんそうです"
[325] Track 1: 631130-635343ms  "Monsters inside the dungeon" (current)
[326] Track 0: 631131-635344ms  "ダンジョンの中のモンスター"

The algorithm would loop through all subtitles looking for the minimum time difference. It would correctly find subtitle 323 as the previous one for Track 1. But then it would continue looping and check subtitle 326 (Track 0), which has an even smaller diff. From there, searching backward for Track 1's previous subtitle would incorrectly find 325 - the current subtitle.

The fix identifies which track is currently showing, then when seeking backward, explicitly searches for the previous subtitle of that same track and breaks out of the loop to avoid it being overwritten.

JSchoreels avatar Dec 08 '25 22:12 JSchoreels

I changed a bit the implementation to search the current subtitles by binary search since the subs list is a sorted list

JSchoreels avatar Dec 08 '25 22:12 JSchoreels

FYI I changed a bit the logic, I realized that sometimes not all subtitles are "paired" so going for the previous one for the same track might jump over other subtitles.

For examples, the japanese subs might have some entries to describe what is happening while the english only appears when there's something said, or if something japanese is written in the current scene.

So I changed the implementation to just look backward from the current index, but consider to jump only for subtitle with a bigger difference than a certain delta. I set the delta to 100ms and it seems to work fine in the examples I tried.

I also created another util function findAllCurrentSubtitles that allow to find all sub ID overlapping a certain timestamp. It's not used in the code itself because the main algo doesn't really need it (and I personally thought it would complexify the main logic).

There's a lot of edge cases that could happen especially with overlapping subtitles (if for example a sub is starting from the start of the video and ends at the end), but I guess there's no perfect implementation for it (and please keep in mind right now the current one is just not working at all for users with multiples tracks)

JSchoreels avatar Dec 09 '25 09:12 JSchoreels