[youtube] partial fix for #2857

This commit is contained in:
Mort Yao 2020-12-26 18:45:55 +01:00
parent 5a890eac53
commit 6be1d0308e
No known key found for this signature in database
GPG Key ID: 07DA00CB78203251

View File

@ -203,28 +203,43 @@ class YouTube(VideoExtractor):
# Parse video page (for DASH) # Parse video page (for DASH)
video_page = get_content('https://www.youtube.com/watch?v=%s' % self.vid) video_page = get_content('https://www.youtube.com/watch?v=%s' % self.vid)
try: try:
ytplayer_config = json.loads(re.search('ytplayer.config\s*=\s*([^\n]+?});', video_page).group(1)) try:
# Complete ytplayer_config
ytplayer_config = json.loads(re.search('ytplayer.config\s*=\s*([^\n]+?});', video_page).group(1))
# Workaround: get_video_info returns bad s. Why? # Workaround: get_video_info returns bad s. Why?
if 'url_encoded_fmt_stream_map' not in ytplayer_config['args']: if 'url_encoded_fmt_stream_map' not in ytplayer_config['args']:
stream_list = json.loads(ytplayer_config['args']['player_response'])['streamingData']['formats'] stream_list = json.loads(ytplayer_config['args']['player_response'])['streamingData']['formats']
else: else:
stream_list = ytplayer_config['args']['url_encoded_fmt_stream_map'].split(',') stream_list = ytplayer_config['args']['url_encoded_fmt_stream_map'].split(',')
#stream_list = ytplayer_config['args']['adaptive_fmts'].split(',') #stream_list = ytplayer_config['args']['adaptive_fmts'].split(',')
if 'assets' in ytplayer_config: if 'assets' in ytplayer_config:
self.html5player = 'https://www.youtube.com' + ytplayer_config['assets']['js'] self.html5player = 'https://www.youtube.com' + ytplayer_config['assets']['js']
elif re.search('([^"]*/base\.js)"', video_page): elif re.search('([^"]*/base\.js)"', video_page):
self.html5player = 'https://www.youtube.com' + re.search('([^"]*/base\.js)"', video_page).group(1) self.html5player = 'https://www.youtube.com' + re.search('([^"]*/base\.js)"', video_page).group(1)
self.html5player = self.html5player.replace('\/', '/') # unescape URL self.html5player = self.html5player.replace('\/', '/') # unescape URL
else: else:
self.html5player = None self.html5player = None
except:
# ytplayer_config = {args:{raw_player_response:ytInitialPlayerResponse}}
ytInitialPlayerResponse = json.loads(re.search('ytInitialPlayerResponse\s*=\s*([^\n]+?});', video_page).group(1))
stream_list = ytInitialPlayerResponse['streamingData']['formats']
#stream_list = ytInitialPlayerResponse['streamingData']['adaptiveFormats']
if re.search('([^"]*/base\.js)"', video_page):
self.html5player = 'https://www.youtube.com' + re.search('([^"]*/base\.js)"', video_page).group(1)
else:
self.html5player = None
except: except:
if 'url_encoded_fmt_stream_map' not in video_info: if 'url_encoded_fmt_stream_map' not in video_info:
stream_list = json.loads(video_info['player_response'][0])['streamingData']['formats'] stream_list = json.loads(video_info['player_response'][0])['streamingData']['formats']
else: else:
stream_list = video_info['url_encoded_fmt_stream_map'][0].split(',') stream_list = video_info['url_encoded_fmt_stream_map'][0].split(',')
if re.search('([^"]*/base\.js)"', video_page): if re.search('([^"]*/base\.js)"', video_page):
self.html5player = 'https://www.youtube.com' + re.search('([^"]*/base\.js)"', video_page).group(1) self.html5player = 'https://www.youtube.com' + re.search('([^"]*/base\.js)"', video_page).group(1)
else: else: