1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2025-01-08 17:05:37 +00:00

[YouTube] Refactor subtitle processing

* move to internal function
* use `traverse-obj()`
This commit is contained in:
dirkf 2025-01-06 01:24:30 +00:00
parent ab7c61ca29
commit 00ad2b8ca1

View file

@ -2415,9 +2415,9 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
'is_live': is_live, 'is_live': is_live,
} }
pctr = try_get( pctr = traverse_obj(
player_response, player_response,
lambda x: x['captions']['playerCaptionsTracklistRenderer'], dict) ('captions', 'playerCaptionsTracklistRenderer', T(dict)))
if pctr: if pctr:
def process_language(container, base_url, lang_code, query): def process_language(container, base_url, lang_code, query):
lang_subs = [] lang_subs = []
@ -2431,28 +2431,30 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
}) })
container[lang_code] = lang_subs container[lang_code] = lang_subs
subtitles = {} def process_subtitles():
for caption_track in (pctr.get('captionTracks') or []): subtitles = {}
base_url = caption_track.get('baseUrl') for caption_track in traverse_obj(pctr, (
if not base_url: 'captionTracks', lambda _, v: v.get('baseUrl'))):
continue if not base_url:
if caption_track.get('kind') != 'asr':
lang_code = caption_track.get('languageCode')
if not lang_code:
continue continue
process_language( if caption_track.get('kind') != 'asr':
subtitles, base_url, lang_code, {}) lang_code = caption_track.get('languageCode')
continue if not lang_code:
automatic_captions = {} continue
for translation_language in (pctr.get('translationLanguages') or []): process_language(
translation_language_code = translation_language.get('languageCode') subtitles, base_url, lang_code, {})
if not translation_language_code:
continue continue
process_language( automatic_captions = {}
automatic_captions, base_url, translation_language_code, for translation_language in traverse_obj(pctr, (
{'tlang': translation_language_code}) 'translationLanguages', lambda _, v: v.get('languageCode'))):
info['automatic_captions'] = automatic_captions translation_language_code = translation_language['languageCode']
info['subtitles'] = subtitles process_language(
automatic_captions, base_url, translation_language_code,
{'tlang': translation_language_code})
info['automatic_captions'] = automatic_captions
info['subtitles'] = subtitles
process_subtitles()
parsed_url = compat_urllib_parse_urlparse(url) parsed_url = compat_urllib_parse_urlparse(url)
for component in (parsed_url.fragment, parsed_url.query): for component in (parsed_url.fragment, parsed_url.query):