Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions skills/watch/scripts/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,28 @@ def matches(key):
if language != 'auto':
return key == target or base == target or ('-' not in language and base.split('-')[0] == target)
return target and (base == target or base.split('-')[0] == str(target).split('-')[0])
def english_first(key): # Unknown source language: prefer English over alphabetical order.
return key.removesuffix('-orig').split('-')[0] != 'en', key
def english_first(key): # Unknown source language: prefer English (original ASR first) over alphabetical order.
return key.removesuffix('-orig').split('-')[0] != 'en', not key.endswith('-orig'), key
for kind, tracks in (('manual', manual), ('automatic', automatic)):
keys = sorted(tracks, key=english_first)
if target:
keys = sorted((k for k in keys if matches(k)), key=lambda k: (k != target, k != f'{target}-orig', k))
if keys:
key = keys[0]
# Auto mode must prefer original ASR to translated/dubbed metadata.
# Auto mode must prefer original ASR to translated/dubbed metadata. With one
# original track it is the source language. Dubbed videos list one original per
# audio language, so then prefer the metadata language, then English, never the
# alphabetically first (which picked Arabic for English videos).
if kind == 'automatic' and language == 'auto' and originals:
key = originals[0]
if len(originals) == 1:
key = originals[0]
else:
wanted = [str(target).split('-')[0]] if target else []
for lang in [*wanted, 'en']:
hit = next((o for o in originals if o[:-5].split('-')[0] == lang), None)
if hit:
key = hit
break
base = key.removesuffix('-orig')
provenance = 'original' if original and base.split('-')[0] == original.split('-')[0] else 'unknown'
if language != 'auto' and original and base.split('-')[0] != original.split('-')[0]:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ def test_original_track_overrides_dubbed_metadata(language):
assert download.select_caption(info)['key'] == f'{language}-orig'


def test_many_original_tracks_follow_metadata_then_english():
# Videos with dubbed audio list one "-orig" ASR track per audio language. Picking the
# alphabetically first one returned ar-orig for an English video (CvT_EPyWcdo, 2026-11-07).
origs = ('ar-orig', 'bn-orig', 'de-orig', 'en-orig', 'fr-orig')
info = {'extractor_key': 'Youtube', 'language': 'en-US', 'automatic_captions': _tracks(*origs, 'ar', 'en')}
assert download.select_caption(info)['key'] == 'en-orig'
info['language'] = 'de'
assert download.select_caption(info)['key'] == 'de-orig'
del info['language']
assert download.select_caption(info)['key'] == 'en-orig'
info['language'] = 'ja' # no matching original: fall back to English, never Arabic
assert download.select_caption(info)['key'] == 'en-orig'


def test_unknown_language_prefers_english():
# "Me at the zoo" reports no language and has manual de + en tracks.
picked = download.select_caption({'extractor_key': 'Youtube', 'subtitles': _tracks('de', 'en')})
Expand Down