Skip to content

Commit

Permalink
Fix file name collision with normal/ASR/translated subs with the same…
Browse files Browse the repository at this point in the history
… language
  • Loading branch information
MoojMidge committed Mar 5, 2024
1 parent cb6fcef commit 05a4eb8
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions resources/lib/youtube_plugin/youtube/helper/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,20 @@ def get_subtitles(self):
subtitles = {}
has_asr = False
for lang in allowed_langs:
track, track_lang, track_language, track_kind, is_translation = (
track, track_lang, track_language, track_kind = (
self._get_track(lang, use_asr=use_asr)
)
if not track:
continue
if track_kind == 'asr':
if has_asr:
continue
has_asr = True
subtitles[lang] = {
if track_kind:
if track_kind == 'asr':
if has_asr:
continue
has_asr = True
track_key = '_'.join((track_lang, track_kind))
else:
track_key = track_lang
subtitles[track_key] = {
'default': track_lang in preferred_lang,
'original': track_lang == original_lang,
'kind': track_kind,
Expand All @@ -202,7 +206,6 @@ def get_subtitles(self):
'url': self._get_url(
caption_track=track,
lang=track_lang,
is_translation=is_translation,
sub_format='vtt',
),
}
Expand All @@ -226,7 +229,6 @@ def get_all(self, sub_format='vtt'):
track_language = self._get_language_name(track)
url = self._get_url(
caption_track=track,
lang=track_lang,
sub_format=sub_format,
download=False,
)
Expand All @@ -245,30 +247,33 @@ def get_all(self, sub_format='vtt'):
'url': url,
}

translation_base = self.defaults['base']
if translation_base:
for track in self.translation_langs:
track_lang = track.get('languageCode')
if track_lang in subtitles:
continue
track_language = self._get_language_name(track)
url = self._get_url(
caption_track=translation_base,
lang=track_lang,
is_translation=True,
sub_format=sub_format,
download=False,
)
if url:
subtitles[track_lang] = {
'default': track_lang in preferred_lang,
'original': track_lang == original_lang,
'kind': 'translation',
'lang': track_lang,
'language': track_language,
'mime_type': mime_type,
'url': url,
}
base_track = self.defaults['base']
base_lang = self.defaults['base_lang']
if not base_track:
return subtitles

for track in self.translation_langs:
track_lang = track.get('languageCode')
if not track_lang or track_lang in subtitles:
continue
track_language = self._get_language_name(track)
url = self._get_url(
caption_track=base_track,
lang=track_lang,
sub_format=sub_format,
download=False,
)
track_key = '_'.join((base_lang, track_lang))
if url:
subtitles[track_key] = {
'default': track_lang in preferred_lang,
'original': track_lang == original_lang,
'kind': 'translation',
'lang': track_lang,
'language': track_language,
'mime_type': mime_type,
'url': url,
}

return subtitles

Expand Down Expand Up @@ -299,12 +304,10 @@ def _prompt(self):
track = self.caption_tracks[choice]
track_kind = track.get('kind')
choice = captions[choice - num_captions]
is_translation = False
elif num_captions <= choice < num_total:
track = self.defaults['base']
track_kind = 'translation'
choice = translations[choice - num_captions]
is_translation = True
else:
self._context.log_debug('Subtitle selection cancelled')
return None
Expand All @@ -314,7 +317,6 @@ def _prompt(self):
url = self._get_url(
caption_track=track,
lang=lang,
is_translation=is_translation,
sub_format='vtt',
)
if url:
Expand All @@ -330,15 +332,25 @@ def _prompt(self):
},
}
self._context.log_debug('No subtitle found for selection: |{lang}|'
.format(lang=choice[0]))
.format(lang=lang))
return None

def _get_url(self,
caption_track,
lang,
is_translation=False,
lang=None,
sub_format='vtt',
download=None):
tlang = None
base_lang = caption_track.get('languageCode')
kind = caption_track.get('kind')
if lang and lang != base_lang:
tlang = lang
lang = '_'.join((base_lang, tlang))
elif kind == 'asr':
lang = '_'.join((base_lang, kind))
else:
lang = base_lang

if download is None:
download = self.pre_download
if download:
Expand Down Expand Up @@ -366,9 +378,9 @@ def _get_url(self,
base_url,
('type', 'track'),
('fmt', sub_format),
('tlang', lang) if is_translation else (None, None),
('tlang', tlang) if tlang else (None, None),
)
if not is_translation:
if not tlang:
self._context.log_debug('Subtitles._get_url: |{lang}: {url}|'
.format(lang=lang, url=subtitle_url))

Expand Down Expand Up @@ -404,11 +416,10 @@ def _get_track(self,
language=None,
use_asr=None):
sel_track = sel_lang = sel_language = sel_kind = None
is_translation = False

if lang == 'ASR':
if use_asr is False:
return None, None, None, None, False
return None, None, None, None
if use_asr is None:
use_asr = True
lang = None
Expand Down Expand Up @@ -446,15 +457,14 @@ def _get_track(self,
sel_lang = lang
sel_language = self._get_language_name(track)
sel_kind = 'translation'
is_translation = True
break

if sel_track:
return sel_track, sel_lang, sel_language, sel_kind, is_translation
return sel_track, sel_lang, sel_language, sel_kind

self._context.log_debug('Subtitles._get - no subtitle for: |{lang}|'
.format(lang=lang))
return None, None, None, None, False
return None, None, None, None

@staticmethod
def _get_language_name(track):
Expand Down

0 comments on commit 05a4eb8

Please sign in to comment.