Merge pull request #68 from peet1993/master

[Youtube] Fixed problem with new youtube player
pull/5/head
Tom-Oliver Heidel 4 years ago committed by GitHub
commit 8934f61717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1390,6 +1390,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
# https://github.com/ytdl-org/youtube-dl/pull/7599) # https://github.com/ytdl-org/youtube-dl/pull/7599)
r';ytplayer\.config\s*=\s*({.+?});ytplayer', r';ytplayer\.config\s*=\s*({.+?});ytplayer',
r';ytplayer\.config\s*=\s*({.+?});', r';ytplayer\.config\s*=\s*({.+?});',
r'ytInitialPlayerResponse\s*=\s*({.+?});var meta'
) )
config = self._search_regex( config = self._search_regex(
patterns, webpage, 'ytplayer.config', default=None) patterns, webpage, 'ytplayer.config', default=None)
@ -1454,10 +1455,11 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
self._downloader.report_warning(err_msg) self._downloader.report_warning(err_msg)
return {} return {}
try: try:
args = player_config['args'] if "args" in player_config and "ttsurl" in player_config["args"]:
caption_url = args.get('ttsurl') args = player_config['args']
if caption_url: caption_url = args['ttsurl']
timestamp = args['timestamp'] timestamp = args['timestamp']
# We get the available subtitles # We get the available subtitles
list_params = compat_urllib_parse_urlencode({ list_params = compat_urllib_parse_urlencode({
'type': 'list', 'type': 'list',
@ -1513,40 +1515,50 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
return captions return captions
# New captions format as of 22.06.2017 # New captions format as of 22.06.2017
player_response = args.get('player_response') if "args" in player_config:
if player_response and isinstance(player_response, compat_str): player_response = player_config["args"].get('player_response')
player_response = self._parse_json( else:
player_response, video_id, fatal=False) # New player system (ytInitialPlayerResponse) as of October 2020
if player_response: player_response = player_config
renderer = player_response['captions']['playerCaptionsTracklistRenderer']
caption_tracks = renderer['captionTracks'] if player_response:
for caption_track in caption_tracks: if isinstance(player_response, compat_str):
if 'kind' not in caption_track: player_response = self._parse_json(
# not an automatic transcription player_response, video_id, fatal=False)
continue
base_url = caption_track['baseUrl'] renderer = player_response['captions']['playerCaptionsTracklistRenderer']
sub_lang_list = [] caption_tracks = renderer['captionTracks']
for lang in renderer['translationLanguages']: for caption_track in caption_tracks:
lang_code = lang.get('languageCode') if 'kind' not in caption_track:
if lang_code: # not an automatic transcription
sub_lang_list.append(lang_code) continue
return make_captions(base_url, sub_lang_list) base_url = caption_track['baseUrl']
sub_lang_list = []
self._downloader.report_warning("Couldn't find automatic captions for %s" % video_id) for lang in renderer['translationLanguages']:
return {} lang_code = lang.get('languageCode')
# Some videos don't provide ttsurl but rather caption_tracks and if lang_code:
# caption_translation_languages (e.g. 20LmZk1hakA) sub_lang_list.append(lang_code)
# Does not used anymore as of 22.06.2017 return make_captions(base_url, sub_lang_list)
caption_tracks = args['caption_tracks']
caption_translation_languages = args['caption_translation_languages'] self._downloader.report_warning("Couldn't find automatic captions for %s" % video_id)
caption_url = compat_parse_qs(caption_tracks.split(',')[0])['u'][0] return {}
sub_lang_list = []
for lang in caption_translation_languages.split(','): if "args" in player_config:
lang_qs = compat_parse_qs(compat_urllib_parse_unquote_plus(lang)) args = player_config["args"]
sub_lang = lang_qs.get('lc', [None])[0]
if sub_lang: # Some videos don't provide ttsurl but rather caption_tracks and
sub_lang_list.append(sub_lang) # caption_translation_languages (e.g. 20LmZk1hakA)
return make_captions(caption_url, sub_lang_list) # Does not used anymore as of 22.06.2017
caption_tracks = args['caption_tracks']
caption_translation_languages = args['caption_translation_languages']
caption_url = compat_parse_qs(caption_tracks.split(',')[0])['u'][0]
sub_lang_list = []
for lang in caption_translation_languages.split(','):
lang_qs = compat_parse_qs(compat_urllib_parse_unquote_plus(lang))
sub_lang = lang_qs.get('lc', [None])[0]
if sub_lang:
sub_lang_list.append(sub_lang)
return make_captions(caption_url, sub_lang_list)
# An extractor error can be raise by the download process if there are # An extractor error can be raise by the download process if there are
# no automatic captions but there are subtitles # no automatic captions but there are subtitles
except (KeyError, IndexError, ExtractorError): except (KeyError, IndexError, ExtractorError):
@ -1822,21 +1834,24 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
# Try looking directly into the video webpage # Try looking directly into the video webpage
ytplayer_config = self._get_ytplayer_config(video_id, video_webpage) ytplayer_config = self._get_ytplayer_config(video_id, video_webpage)
if ytplayer_config: if ytplayer_config:
args = ytplayer_config['args'] args = ytplayer_config.get("args")
if args.get('url_encoded_fmt_stream_map') or args.get('hlsvp'): if args is not None:
# Convert to the same format returned by compat_parse_qs if args.get('url_encoded_fmt_stream_map') or args.get('hlsvp'):
video_info = dict((k, [v]) for k, v in args.items()) # Convert to the same format returned by compat_parse_qs
add_dash_mpd(video_info) video_info = dict((k, [v]) for k, v in args.items())
# Rental video is not rented but preview is available (e.g. add_dash_mpd(video_info)
# https://www.youtube.com/watch?v=yYr8q0y5Jfg, # Rental video is not rented but preview is available (e.g.
# https://github.com/ytdl-org/youtube-dl/issues/10532) # https://www.youtube.com/watch?v=yYr8q0y5Jfg,
if not video_info and args.get('ypc_vid'): # https://github.com/ytdl-org/youtube-dl/issues/10532)
return self.url_result( if not video_info and args.get('ypc_vid'):
args['ypc_vid'], YoutubeIE.ie_key(), video_id=args['ypc_vid']) return self.url_result(
if args.get('livestream') == '1' or args.get('live_playback') == 1: args['ypc_vid'], YoutubeIE.ie_key(), video_id=args['ypc_vid'])
is_live = True if args.get('livestream') == '1' or args.get('live_playback') == 1:
if not player_response: is_live = True
player_response = extract_player_response(args.get('player_response'), video_id) if not player_response:
player_response = extract_player_response(args.get('player_response'), video_id)
elif not player_response:
player_response = ytplayer_config
if not video_info or self._downloader.params.get('youtube_include_dash_manifest', True): if not video_info or self._downloader.params.get('youtube_include_dash_manifest', True):
add_dash_mpd_pr(player_response) add_dash_mpd_pr(player_response)
else: else:
@ -1866,8 +1881,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
age_gate = False age_gate = False
# Try looking directly into the video webpage # Try looking directly into the video webpage
ytplayer_config = self._get_ytplayer_config(video_id, video_webpage) ytplayer_config = self._get_ytplayer_config(video_id, video_webpage)
if ytplayer_config: args = ytplayer_config.get("args")
args = ytplayer_config['args'] if args is not None:
if args.get('url_encoded_fmt_stream_map') or args.get('hlsvp'): if args.get('url_encoded_fmt_stream_map') or args.get('hlsvp'):
# Convert to the same format returned by compat_parse_qs # Convert to the same format returned by compat_parse_qs
video_info = dict((k, [v]) for k, v in args.items()) video_info = dict((k, [v]) for k, v in args.items())
@ -1882,6 +1897,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
is_live = True is_live = True
if not player_response: if not player_response:
player_response = extract_player_response(args.get('player_response'), video_id) player_response = extract_player_response(args.get('player_response'), video_id)
elif not player_response:
player_response = ytplayer_config
if not video_info or self._downloader.params.get('youtube_include_dash_manifest', True): if not video_info or self._downloader.params.get('youtube_include_dash_manifest', True):
add_dash_mpd_pr(player_response) add_dash_mpd_pr(player_response)

Loading…
Cancel
Save