feat(tracks): add subtitle.dedupe_codecs to keep one codec per subtitle variant

This commit is contained in:
imSp4rky
2026-10-31 16:35:01 -06:00
parent 65e807bf5f
commit f9c1c3d390
5 changed files with 52 additions and 3 deletions
+4
View File
@@ -2453,6 +2453,10 @@ class dl:
level="INFO", operation="get_tracks", service=self.service, context=tracks_info
)
if dedupe_codecs := config.subtitle.get("dedupe_codecs"):
codecs = map(SUBTITLE_CODEC.convert, dedupe_codecs)
title.tracks.dedupe_subtitle_codecs([c for c in codecs if isinstance(c, Subtitle.Codec)])
# a plain track covers an SDH track only when it is the same variant, as -sl selects them,
# so en-US SDH is stripped next to a plain en-GB: -sl en-US would otherwise keep only the SDH
if config.subtitle.get("strip_sdh", True):
+17
View File
@@ -308,6 +308,23 @@ class Tracks:
kept.append(track)
setattr(self, kind, kept)
def dedupe_subtitle_codecs(self, priority: Sequence[Subtitle.Codec]) -> None:
"""
Drop a subtitle when the same language, flag set and name exists in a codec earlier in priority.
A subtitle in an unlisted codec is always kept.
"""
rank = {codec: i for i, codec in reversed(list(enumerate(priority)))}
def variant(sub: Subtitle) -> tuple:
signature = self.signature(sub)
return signature[:2] + signature[3:] # the signature without its codec
best: dict[tuple, int] = {}
for sub in self.subtitles:
if sub.codec in rank:
best[variant(sub)] = min(best.get(variant(sub), rank[sub.codec]), rank[sub.codec])
self.subtitles = [s for s in self.subtitles if s.codec not in rank or rank[s.codec] == best[variant(s)]]
def sort_videos(
self, by_language: Optional[Sequence[Union[str, Language]]] = None, exact_match: bool = False
) -> None: