feat(tracks): add subtitle.group_by to keep a language with its variants

sort_subtitles applied the language sort before the type sort. Python sorts
are stable, so the last pass wins and type became the major key: every forced
track, then every normal, then every SDH, each block ordered by language. In a
title with many languages this put a track far from its own SDH version, for
example Finnish at track 5 and Finnish SDH at track 42.

Add a group_by parameter that selects the major key. "type" is the default and
keeps the current order byte for byte. "language" swaps the two passes so each
language stays next to its own variants. Read it from the new subtitle.group_by
config key.

Also correct the docs: type_priority was described as sorting the variants
within each language, which was never true. It ranks the types across all
languages.
This commit is contained in:
imSp4rky
2026-08-02 10:45:28 -06:00
parent bf85c948b4
commit 8baf064750
5 changed files with 141 additions and 24 deletions
+32 -15
View File
@@ -289,6 +289,7 @@ class Tracks:
self,
by_language: Optional[Sequence[Union[str, Language]]] = None,
type_priority: Optional[Sequence[str]] = None,
group_by: Optional[str] = None,
) -> None:
"""
Sort subtitle tracks by various track attributes to a common P2P standard.
@@ -299,30 +300,46 @@ class Tracks:
- then rest ascending alphabetically after the prioritized groups
(Each section ascending alphabetically, but separated)
Language Group Order:
Type Order:
- Forced
- Normal
- Hard of Hearing (SDH/CC)
(Least to most captions expected in the subtitle)
type_priority overrides the Language Group Order with an explicit ranking of
"forced", "normal", and "sdh" (cc counts as sdh); unlisted types fall to the end.
The first track after sorting receives the default flag at mux time, so this also
controls which subtitle type becomes default.
type_priority overrides the Type Order with an explicit ranking of "forced",
"normal", and "sdh" (cc counts as sdh); unlisted types fall to the end.
group_by sets the major key. "type" (default) keeps every forced track together,
then every normal, then every SDH, each block ascending by language. "language"
groups by language instead, so Finnish sits next to Finnish SDH, with the Type
Order applied inside each language.
"""
if not self.subtitles:
return
# language groups
self.subtitles.sort(key=lambda x: str(x.language))
if type_priority:
rank = {str(t).lower(): i for i, t in enumerate(type_priority)}
default_rank = len(rank)
self.subtitles.sort(
key=lambda x: rank.get("forced" if x.forced else "sdh" if (x.sdh or x.cc) else "normal", default_rank)
)
def by_type() -> None:
if type_priority:
rank = {str(t).lower(): i for i, t in enumerate(type_priority)}
default_rank = len(rank)
self.subtitles.sort(
key=lambda x: rank.get(
"forced" if x.forced else "sdh" if (x.sdh or x.cc) else "normal", default_rank
)
)
else:
self.subtitles.sort(key=lambda x: x.sdh or x.cc)
self.subtitles.sort(key=lambda x: x.forced, reverse=True)
def by_lang() -> None:
self.subtitles.sort(key=lambda x: str(x.language))
# stable sorts, so the last pass is the major key
if str(group_by or "type").lower() == "language":
by_type()
by_lang()
else:
self.subtitles.sort(key=lambda x: x.sdh or x.cc)
self.subtitles.sort(key=lambda x: x.forced, reverse=True)
by_lang()
by_type()
# sections
for language in reversed(by_language or []):
if str(language) == "all":