3 from __future__
import unicode_literals
14 from .common
import InfoExtractor
, SearchInfoExtractor
15 from ..jsinterp
import JSInterpreter
16 from ..swfinterp
import SWFInterpreter
17 from ..compat
import (
22 compat_urllib_parse_unquote
,
23 compat_urllib_parse_unquote_plus
,
24 compat_urllib_parse_urlencode
,
25 compat_urllib_parse_urlparse
,
58 class YoutubeBaseInfoExtractor(InfoExtractor
):
59 """Provide base functions for Youtube extractors"""
60 _LOGIN_URL
= 'https://accounts.google.com/ServiceLogin'
61 _TWOFACTOR_URL
= 'https://accounts.google.com/signin/challenge'
63 _LOOKUP_URL
= 'https://accounts.google.com/_/signin/sl/lookup'
64 _CHALLENGE_URL
= 'https://accounts.google.com/_/signin/sl/challenge'
65 _TFA_URL
= 'https://accounts.google.com/_/signin/challenge?hl=en&TL={0}'
68 r
'embed|e|watch_popup|channel|c|user|playlist|watch|w|v|movies|results|shared|'
69 r
'storefront|oops|index|account|reporthistory|t/terms|about|upload|signin|logout|'
70 r
'feed/(?:watch_later|history|subscriptions|library|trending|recommended)')
72 _NETRC_MACHINE
= 'youtube'
73 # If True it will raise an error if no login info is provided
74 _LOGIN_REQUIRED
= False
76 _PLAYLIST_ID_RE
= r
'(?:(?:PL|LL|EC|UU|FL|RD|UL|TL|PU|OLAK5uy_)[0-9A-Za-z-_]{10,}|RDMM|WL|LL|LM)'
78 def _set_language(self
):
80 '.youtube.com', 'PREF', 'f1=50000000&f6=8&hl=en',
81 # YouTube sets the expire time to about two months
82 expire_time
=time
.time() + 2 * 30 * 24 * 3600)
84 def _ids_to_results(self
, ids
):
86 self
.url_result(vid_id
, 'Youtube', video_id
=vid_id
)
91 Attempt to log in to YouTube.
92 True is returned if successful or skipped.
93 False is returned if login failed.
95 If _LOGIN_REQUIRED is set and no authentication was provided, an error is raised.
97 username
, password
= self
._get
_login
_info
()
98 # No authentication to be performed
100 if self
._LOGIN
_REQUIRED
and self
._downloader
.params
.get('cookiefile') is None:
101 raise ExtractorError('No login info available, needed for using %s.' % self
.IE_NAME
, expected
=True)
102 if self
._downloader
.params
.get('cookiefile') and False: # TODO remove 'and False' later - too many people using outdated cookies and open issues, remind them.
103 self
.to_screen('[Cookies] Reminder - Make sure to always use up to date cookies!')
106 login_page
= self
._download
_webpage
(
107 self
._LOGIN
_URL
, None,
108 note
='Downloading login page',
109 errnote
='unable to fetch login page', fatal
=False)
110 if login_page
is False:
113 login_form
= self
._hidden
_inputs
(login_page
)
115 def req(url
, f_req
, note
, errnote
):
116 data
= login_form
.copy()
119 'checkConnection': 'youtube',
120 'checkedDomains': 'youtube',
122 'deviceinfo': '[null,null,null,[],null,"US",null,null,[],"GlifWebSignIn",null,[null,null,[]]]',
123 'f.req': json
.dumps(f_req
),
124 'flowName': 'GlifWebSignIn',
125 'flowEntry': 'ServiceLogin',
126 # TODO: reverse actual botguard identifier generation algo
127 'bgRequest': '["identifier",""]',
129 return self
._download
_json
(
130 url
, None, note
=note
, errnote
=errnote
,
131 transform_source
=lambda s
: re
.sub(r
'^[^[]*', '', s
),
133 data
=urlencode_postdata(data
), headers
={
134 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
135 'Google-Accounts-XSRF': 1,
139 self
._downloader
.report_warning(message
)
143 None, [], None, 'US', None, None, 2, False, True,
147 'https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26action_handle_signin%3Dtrue%26hl%3Den%26app%3Ddesktop%26feature%3Dsign_in_button&hl=en&service=youtube&uilel=3&requestPath=%2FServiceLogin&Page=PasswordSeparationSignIn',
149 1, [None, None, []], None, None, None, True
154 lookup_results
= req(
155 self
._LOOKUP
_URL
, lookup_req
,
156 'Looking up account info', 'Unable to look up account info')
158 if lookup_results
is False:
161 user_hash
= try_get(lookup_results
, lambda x
: x
[0][2], compat_str
)
163 warn('Unable to extract user hash')
168 None, 1, None, [1, None, None, None, [password
, None, True]],
170 None, None, [2, 1, None, 1, 'https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fnext%3D%252F%26action_handle_signin%3Dtrue%26hl%3Den%26app%3Ddesktop%26feature%3Dsign_in_button&hl=en&service=youtube&uilel=3&requestPath=%2FServiceLogin&Page=PasswordSeparationSignIn', None, [], 4],
171 1, [None, None, []], None, None, None, True
174 challenge_results
= req(
175 self
._CHALLENGE
_URL
, challenge_req
,
176 'Logging in', 'Unable to log in')
178 if challenge_results
is False:
181 login_res
= try_get(challenge_results
, lambda x
: x
[0][5], list)
183 login_msg
= try_get(login_res
, lambda x
: x
[5], compat_str
)
185 'Unable to login: %s' % 'Invalid password'
186 if login_msg
== 'INCORRECT_ANSWER_ENTERED' else login_msg
)
189 res
= try_get(challenge_results
, lambda x
: x
[0][-1], list)
191 warn('Unable to extract result entry')
194 login_challenge
= try_get(res
, lambda x
: x
[0][0], list)
196 challenge_str
= try_get(login_challenge
, lambda x
: x
[2], compat_str
)
197 if challenge_str
== 'TWO_STEP_VERIFICATION':
198 # SEND_SUCCESS - TFA code has been successfully sent to phone
199 # QUOTA_EXCEEDED - reached the limit of TFA codes
200 status
= try_get(login_challenge
, lambda x
: x
[5], compat_str
)
201 if status
== 'QUOTA_EXCEEDED':
202 warn('Exceeded the limit of TFA codes, try later')
205 tl
= try_get(challenge_results
, lambda x
: x
[1][2], compat_str
)
207 warn('Unable to extract TL')
210 tfa_code
= self
._get
_tfa
_info
('2-step verification code')
214 'Two-factor authentication required. Provide it either interactively or with --twofactor <code>'
215 '(Note that only TOTP (Google Authenticator App) codes work at this time.)')
218 tfa_code
= remove_start(tfa_code
, 'G-')
221 user_hash
, None, 2, None,
223 9, None, None, None, None, None, None, None,
224 [None, tfa_code
, True, 2]
228 self
._TFA
_URL
.format(tl
), tfa_req
,
229 'Submitting TFA code', 'Unable to submit TFA code')
231 if tfa_results
is False:
234 tfa_res
= try_get(tfa_results
, lambda x
: x
[0][5], list)
236 tfa_msg
= try_get(tfa_res
, lambda x
: x
[5], compat_str
)
238 'Unable to finish TFA: %s' % 'Invalid TFA code'
239 if tfa_msg
== 'INCORRECT_ANSWER_ENTERED' else tfa_msg
)
242 check_cookie_url
= try_get(
243 tfa_results
, lambda x
: x
[0][-1][2], compat_str
)
246 'LOGIN_CHALLENGE': "This device isn't recognized. For your security, Google wants to make sure it's really you.",
247 'USERNAME_RECOVERY': 'Please provide additional information to aid in the recovery process.',
248 'REAUTH': "There is something unusual about your activity. For your security, Google wants to make sure it's really you.",
250 challenge
= CHALLENGES
.get(
252 '%s returned error %s.' % (self
.IE_NAME
, challenge_str
))
253 warn('%s\nGo to https://accounts.google.com/, login and solve a challenge.' % challenge
)
256 check_cookie_url
= try_get(res
, lambda x
: x
[2], compat_str
)
258 if not check_cookie_url
:
259 warn('Unable to extract CheckCookie URL')
262 check_cookie_results
= self
._download
_webpage
(
263 check_cookie_url
, None, 'Checking cookie', fatal
=False)
265 if check_cookie_results
is False:
268 if 'https://myaccount.google.com/' not in check_cookie_results
:
269 warn('Unable to log in')
274 def _download_webpage_handle(self
, *args
, **kwargs
):
275 query
= kwargs
.get('query', {}).copy()
276 kwargs
['query'] = query
277 return super(YoutubeBaseInfoExtractor
, self
)._download
_webpage
_handle
(
278 *args
, **compat_kwargs(kwargs
))
280 def _get_yt_initial_data(self
, video_id
, webpage
):
281 config
= self
._search
_regex
(
282 (r
'window\["ytInitialData"\]\s*=\s*(.*?)(?<=});',
283 r
'var\s+ytInitialData\s*=\s*(.*?)(?<=});'),
284 webpage
, 'ytInitialData', default
=None)
286 return self
._parse
_json
(
287 uppercase_escape(config
), video_id
, fatal
=False)
289 def _real_initialize(self
):
290 if self
._downloader
is None:
293 if not self
._login
():
296 _DEFAULT_API_DATA
= {
300 'clientVersion': '2.20201021.03.00',
305 _YT_INITIAL_DATA_RE
= r
'(?:window\s*\[\s*["\']ytInitialData
["\']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;'
306 _YT_INITIAL_PLAYER_RESPONSE_RE = r'ytInitialPlayerResponse\s*=\s*({.+?})\s*;'
307 _YT_INITIAL_BOUNDARY_RE = r'(?:var\s+meta|</script|\n)'
309 def _call_api(self, ep, query, video_id):
310 data = self._DEFAULT_API_DATA.copy()
313 response = self._download_json(
314 'https://www.youtube.com/youtubei/v1/%s' % ep, video_id=video_id,
315 note='Downloading API JSON', errnote='Unable to download API page',
316 data=json.dumps(data).encode('utf8'),
317 headers={'content-type': 'application/json'},
318 query={'key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'})
322 def _extract_yt_initial_data(self, video_id, webpage):
323 return self._parse_json(
325 (r'%s\s*%s' % (self._YT_INITIAL_DATA_RE, self._YT_INITIAL_BOUNDARY_RE),
326 self._YT_INITIAL_DATA_RE), webpage, 'yt initial data'),
329 def _extract_ytcfg(self, video_id, webpage):
330 return self._parse_json(
332 r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', webpage, 'ytcfg',
333 default='{}'), video_id, fatal=False)
336 class YoutubeIE(YoutubeBaseInfoExtractor):
337 IE_DESC = 'YouTube.com'
338 _VALID_URL = r"""(?x)^
340 (?:https?://|//) # http(s):// or protocol-independent URL
341 (?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie|kids)?\.com/|
342 (?:www\.)?deturl\.com/www\.youtube\.com/|
343 (?:www\.)?pwnyoutube\.com/|
344 (?:www\.)?hooktube\.com/|
345 (?:www\.)?yourepeat\.com/|
346 tube\.majestyc\.net/|
347 # Invidious instances taken from https://github.com/omarroth/invidious/wiki/Invidious-Instances
348 (?:(?:www|dev)\.)?invidio\.us/|
349 (?:(?:www|no)\.)?invidiou\.sh/|
350 (?:(?:www|fi)\.)?invidious\.snopyta\.org/|
351 (?:www\.)?invidious\.kabi\.tk/|
352 (?:www\.)?invidious\.13ad\.de/|
353 (?:www\.)?invidious\.mastodon\.host/|
354 (?:www\.)?invidious\.zapashcanon\.fr/|
355 (?:www\.)?invidious\.kavin\.rocks/|
356 (?:www\.)?invidious\.tube/|
357 (?:www\.)?invidiou\.site/|
358 (?:www\.)?invidious\.site/|
359 (?:www\.)?invidious\.xyz/|
360 (?:www\.)?invidious\.nixnet\.xyz/|
361 (?:www\.)?invidious\.drycat\.fr/|
362 (?:www\.)?tube\.poal\.co/|
363 (?:www\.)?tube\.connect\.cafe/|
364 (?:www\.)?vid\.wxzm\.sx/|
365 (?:www\.)?vid\.mint\.lgbt/|
366 (?:www\.)?yewtu\.be/|
367 (?:www\.)?yt\.elukerio\.org/|
368 (?:www\.)?yt\.lelux\.fi/|
369 (?:www\.)?invidious\.ggc-project\.de/|
370 (?:www\.)?yt\.maisputain\.ovh/|
371 (?:www\.)?invidious\.13ad\.de/|
372 (?:www\.)?invidious\.toot\.koeln/|
373 (?:www\.)?invidious\.fdn\.fr/|
374 (?:www\.)?watch\.nettohikari\.com/|
375 (?:www\.)?kgg2m7yk5aybusll\.onion/|
376 (?:www\.)?qklhadlycap4cnod\.onion/|
377 (?:www\.)?axqzx4s6s54s32yentfqojs3x5i7faxza6xo3ehd4bzzsg2ii4fv2iid\.onion/|
378 (?:www\.)?c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid\.onion/|
379 (?:www\.)?fz253lmuao3strwbfbmx46yu7acac2jz27iwtorgmbqlkurlclmancad\.onion/|
380 (?:www\.)?invidious\.l4qlywnpwqsluw65ts7md3khrivpirse744un3x7mlskqauz5pyuzgqd\.onion/|
381 (?:www\.)?owxfohz4kjyv25fvlqilyxast7inivgiktls3th44jhk3ej3i7ya\.b32\.i2p/|
382 (?:www\.)?4l2dgddgsrkf2ous66i6seeyi6etzfgrue332grh2n7madpwopotugyd\.onion/|
383 youtube\.googleapis\.com/) # the various hostnames, with wildcard subdomains
384 (?:.*?\#/)? # handle anchor (#/) redirect urls
385 (?: # the various things that can precede the ID:
386 (?:(?:v|embed|e)/(?!videoseries)) # v/ or embed/ or e/
387 |(?: # or the v= param in all its forms
388 (?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
389 (?:\?|\#!?) # the params delimiter ? or # or #!
390 (?:.*?[&;])?? # any other preceding param (like /?s=tuff&v=xxxx or ?s=tuff&v=V36LpHqtcDY)
395 youtu\.be| # just youtu.be/xxxx
396 vid\.plus| # or vid.plus/xxxx
397 zwearz\.com/watch| # or zwearz.com/watch/xxxx
399 |(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=
401 )? # all until now is optional -> you can pass the naked ID
402 (?P<id>[0-9A-Za-z_-]{11}) # here is it! the YouTube video ID
405 %(playlist_id)s| # combined list/video URLs are handled by the playlist IE
406 WL # WL are handled by the watch later IE
409 (?(1).+)? # if we found the ID, everything can follow
410 $""" % {'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE}
411 _NEXT_URL_RE = r'[\?&]next_url=([^&]+)'
413 r'/(?P<id>[a-zA-Z0-9_-]{8,})/player_ias\.vflset(?:/[a-zA-Z]{2,3}_[a-zA-Z]{2,3})?/base\.(?P<ext>[a-z]+)$',
414 r'\b(?P<id>vfl[a-zA-Z0-9_-]+)\b.*?\.(?P<ext>[a-z]+)$',
417 '5': {'ext': 'flv', 'width': 400, 'height': 240, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
418 '6': {'ext': 'flv', 'width': 450, 'height': 270, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
419 '13': {'ext': '3gp', 'acodec': 'aac', 'vcodec': 'mp4v'},
420 '17': {'ext': '3gp', 'width': 176, 'height': 144, 'acodec': 'aac', 'abr': 24, 'vcodec': 'mp4v'},
421 '18': {'ext': 'mp4', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 96, 'vcodec': 'h264'},
422 '22': {'ext': 'mp4', 'width': 1280, 'height': 720, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
423 '34': {'ext': 'flv', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
424 '35': {'ext': 'flv', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
425 # itag 36 videos are either 320x180 (BaW_jenozKc) or 320x240 (__2ABJjxzNo), abr varies as well
426 '36': {'ext': '3gp', 'width': 320, 'acodec': 'aac', 'vcodec': 'mp4v'},
427 '37': {'ext': 'mp4', 'width': 1920, 'height': 1080, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
428 '38': {'ext': 'mp4', 'width': 4096, 'height': 3072, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
429 '43': {'ext': 'webm', 'width': 640, 'height': 360, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
430 '44': {'ext': 'webm', 'width': 854, 'height': 480, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
431 '45': {'ext': 'webm', 'width': 1280, 'height': 720, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
432 '46': {'ext': 'webm', 'width': 1920, 'height': 1080, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
433 '59': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
434 '78': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
438 '82': {'ext': 'mp4', 'height': 360, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
439 '83': {'ext': 'mp4', 'height': 480, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
440 '84': {'ext': 'mp4', 'height': 720, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
441 '85': {'ext': 'mp4', 'height': 1080, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
442 '100': {'ext': 'webm', 'height': 360, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8', 'preference': -20},
443 '101': {'ext': 'webm', 'height': 480, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
444 '102': {'ext': 'webm', 'height': 720, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
446 # Apple HTTP Live Streaming
447 '91': {'ext': 'mp4', 'height': 144, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
448 '92': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
449 '93': {'ext': 'mp4', 'height': 360, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
450 '94': {'ext': 'mp4', 'height': 480, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
451 '95': {'ext': 'mp4', 'height': 720, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
452 '96': {'ext': 'mp4', 'height': 1080, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
453 '132': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
454 '151': {'ext': 'mp4', 'height': 72, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 24, 'vcodec': 'h264', 'preference': -10},
457 '133': {'ext': 'mp4', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'h264'},
458 '134': {'ext': 'mp4', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'h264'},
459 '135': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
460 '136': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264'},
461 '137': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264'},
462 '138': {'ext': 'mp4', 'format_note': 'DASH video', 'vcodec': 'h264'}, # Height can vary (https://github.com/ytdl-org/youtube-dl/issues/4559)
463 '160': {'ext': 'mp4', 'height': 144, 'format_note': 'DASH video', 'vcodec': 'h264'},
464 '212': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
465 '264': {'ext': 'mp4', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'h264'},
466 '298': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
467 '299': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
468 '266': {'ext': 'mp4', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'h264'},
471 '139': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 48, 'container': 'm4a_dash'},
472 '140': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 128, 'container': 'm4a_dash'},
473 '141': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 256, 'container': 'm4a_dash'},
474 '256': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
475 '258': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
476 '325': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'dtse', 'container': 'm4a_dash'},
477 '328': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'ec-3', 'container': 'm4a_dash'},
480 '167': {'ext': 'webm', 'height': 360, 'width': 640, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
481 '168': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
482 '169': {'ext': 'webm', 'height': 720, 'width': 1280, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
483 '170': {'ext': 'webm', 'height': 1080, 'width': 1920, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
484 '218': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
485 '219': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
486 '278': {'ext': 'webm', 'height': 144, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp9'},
487 '242': {'ext': 'webm', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'vp9'},
488 '243': {'ext': 'webm', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'vp9'},
489 '244': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
490 '245': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
491 '246': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
492 '247': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9'},
493 '248': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9'},
494 '271': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9'},
495 # itag 272 videos are either 3840x2160 (e.g. RtoitU2A-3E) or 7680x4320 (sLprVF6d7Ug)
496 '272': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
497 '302': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
498 '303': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
499 '308': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
500 '313': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
501 '315': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
504 '171': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 128},
505 '172': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 256},
507 # Dash webm audio with opus inside
508 '249': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 50},
509 '250': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 70},
510 '251': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 160},
513 '_rtmp': {'protocol': 'rtmp'},
515 # av01 video only formats sometimes served with "unknown
" codecs
516 '394': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
517 '395': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
518 '396': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
519 '397': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
521 _SUBTITLE_FORMATS = ('json3', 'srv1', 'srv2', 'srv3', 'ttml', 'vtt')
528 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&t=1s&end=9',
532 'title': 'youtube-dl test video "\'/\\ä↭𝕐
',
533 'uploader
': 'Philipp Hagemeister
',
534 'uploader_id
': 'phihag
',
535 'uploader_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/user
/phihag
',
536 'channel_id
': 'UCLqxVugv74EIW3VWh2NOa3Q
',
537 'channel_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/channel
/UCLqxVugv74EIW3VWh2NOa3Q
',
538 'upload_date
': '20121002',
539 'description
': 'test chars
: "\'/\\ä↭𝕐\ntest URL: https://github.com/rg3/youtube-dl/issues/1892\n\nThis is a test video for youtube-dl.\n\nFor more information, contact phihag@phihag.de .',
540 'categories': ['Science & Technology'],
541 'tags': ['youtube-dl'],
545 'dislike_count': int,
551 'url': '//www.YouTube.com/watch?v=yZIXLfi8CZQ',
552 'note': 'Embed-only video (#1746)',
556 'upload_date': '20120608',
557 'title': 'Principal Sexually Assaults A Teacher - Episode 117 - 8th June 2012',
558 'description': 'md5:09b78bd971f1e3e289601dfba15ca4f7',
559 'uploader': 'SET India',
560 'uploader_id': 'setindia',
561 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/setindia',
566 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&v=yZIXLfi8CZQ',
567 'note': 'Use the first video ID in the URL',
571 'title': 'youtube-dl test video "\'/\\ä↭𝕐
',
572 'uploader
': 'Philipp Hagemeister
',
573 'uploader_id
': 'phihag
',
574 'uploader_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/user
/phihag
',
575 'upload_date
': '20121002',
576 'description
': 'test chars
: "\'/\\ä↭𝕐\ntest URL: https://github.com/rg3/youtube-dl/issues/1892\n\nThis is a test video for youtube-dl.\n\nFor more information, contact phihag@phihag.de .',
577 'categories': ['Science & Technology'],
578 'tags': ['youtube-dl'],
582 'dislike_count': int,
585 'skip_download': True,
589 'url': 'https://www.youtube.com/watch?v=a9LDPn-MO4I',
590 'note': '256k DASH audio (format 141) via DASH manifest',
594 'upload_date': '20121002',
595 'uploader_id': '8KVIDEO',
596 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/8KVIDEO',
598 'uploader': '8KVIDEO',
599 'title': 'UHDTV TEST 8K VIDEO.mp4'
602 'youtube_include_dash_manifest': True,
605 'skip': 'format 141 not served anymore',
607 # DASH manifest with encrypted signature
609 'url': 'https://www.youtube.com/watch?v=IB3lcPjvWLA',
613 'title': 'Afrojack, Spree Wilson - The Spark (Official Music Video) ft. Spree Wilson',
614 'description': 'md5:8f5e2b82460520b619ccac1f509d43bf',
616 'uploader': 'AfrojackVEVO',
617 'uploader_id': 'AfrojackVEVO',
618 'upload_date': '20131011',
621 'youtube_include_dash_manifest': True,
622 'format': '141/bestaudio[ext=m4a]',
627 'url': 'https://www.youtube.com/watch?v=T4XJQO3qol8',
632 'upload_date': '20100909',
633 'uploader': 'Amazing Atheist',
634 'uploader_id': 'TheAmazingAtheist',
635 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/TheAmazingAtheist',
636 'title': 'Burning Everyone\'s Koran',
637 'description': 'SUBSCRIBE: http://www.youtube.com/saturninefilms\n\nEven Obama has taken a stand against freedom on this issue: http://www.huffingtonpost.com/2010/09/09/obama-gma-interview-quran_n_710282.html',
640 # Normal age-gate video (embed allowed)
642 'url': 'https://youtube.com/watch?v=HtVdAasjOgU',
646 'title': 'The Witcher 3: Wild Hunt - The Sword Of Destiny Trailer',
647 'description': r're:(?s).{100,}About the Game\n.*?The Witcher 3: Wild Hunt.{100,}',
649 'uploader': 'The Witcher',
650 'uploader_id': 'WitcherGame',
651 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/WitcherGame',
652 'upload_date': '20140605',
656 # video_info is None (https://github.com/ytdl-org/youtube-dl/issues/4421)
657 # YouTube Red ad is not captured for creator
659 'url': '__2ABJjxzNo',
664 'upload_date': '20100430',
665 'uploader_id': 'deadmau5',
666 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/deadmau5',
667 'creator': 'Dada Life, deadmau5',
668 'description': 'md5:12c56784b8032162bb936a5f76d55360',
669 'uploader': 'deadmau5',
670 'title': 'Deadmau5 - Some Chords (HD)',
671 'alt_title': 'This Machine Kills Some Chords',
673 'expected_warnings': [
674 'DASH manifest missing',
677 # Olympics (https://github.com/ytdl-org/youtube-dl/issues/4431)
679 'url': 'lqQg6PlCWgI',
684 'upload_date': '20150827',
685 'uploader_id': 'olympic',
686 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/olympic',
687 'description': 'HO09 - Women - GER-AUS - Hockey - 31 July 2012 - London 2012 Olympic Games',
688 'uploader': 'Olympic',
689 'title': 'Hockey - Women - GER-AUS - London 2012 Olympic Games',
692 'skip_download': 'requires avconv',
697 'url': 'https://www.youtube.com/watch?v=_b-2C3KPAM0',
701 'stretched_ratio': 16 / 9.,
703 'upload_date': '20110310',
704 'uploader_id': 'AllenMeow',
705 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/AllenMeow',
706 'description': 'made by Wacom from Korea | 字幕&加油添醋 by TY\'s Allen | 感謝heylisa00cavey1001同學熱情提供梗及翻譯',
708 'title': '[A-made] 變態妍字幕版 太妍 我就是這樣的人',
711 # url_encoded_fmt_stream_map is empty string
713 'url': 'qEJwOuvDf7I',
717 'title': 'Обсуждение судебной практики по выборам 14 сентября 2014 года в Санкт-Петербурге',
719 'upload_date': '20150404',
720 'uploader_id': 'spbelect',
721 'uploader': 'Наблюдатели Петербурга',
724 'skip_download': 'requires avconv',
726 'skip': 'This live event has ended.',
728 # Extraction from multiple DASH manifests (https://github.com/ytdl-org/youtube-dl/pull/6097)
730 'url': 'https://www.youtube.com/watch?v=FIl7x6_3R5Y',
734 'title': 'md5:7b81415841e02ecd4313668cde88737a',
735 'description': 'md5:116377fd2963b81ec4ce64b542173306',
737 'upload_date': '20150625',
738 'uploader_id': 'dorappi2000',
739 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/dorappi2000',
740 'uploader': 'dorappi2000',
741 'formats': 'mincount:31',
743 'skip': 'not actual anymore',
745 # DASH manifest with segment_list
747 'url': 'https://www.youtube.com/embed/CsmdDsKjzN8',
748 'md5': '8ce563a1d667b599d21064e982ab9e31',
752 'upload_date': '20150501', # According to '<meta itemprop="datePublished
"', but in other places it's 20150510
753 'uploader': 'Airtek',
754 'description': 'Retransmisión en directo de la XVIII media maratón de Zaragoza.',
755 'uploader_id': 'UCzTzUmjXxxacNnL8I3m4LnQ',
756 'title': 'Retransmisión XVIII Media maratón Zaragoza 2015',
759 'youtube_include_dash_manifest': True,
760 'format': '135', # bestvideo
762 'skip': 'This live event has ended.',
765 # Multifeed videos (multiple cameras), URL is for Main Camera
766 'url': 'https://www.youtube.com/watch?v=jqWvoWXjCVs',
769 'title': 'teamPGP: Rocket League Noob Stream',
770 'description': 'md5:dc7872fb300e143831327f1bae3af010',
776 'title': 'teamPGP: Rocket League Noob Stream (Main Camera)',
777 'description': 'md5:dc7872fb300e143831327f1bae3af010',
779 'upload_date': '20150721',
780 'uploader': 'Beer Games Beer',
781 'uploader_id': 'beergamesbeer',
782 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/beergamesbeer',
783 'license': 'Standard YouTube License',
789 'title': 'teamPGP: Rocket League Noob Stream (kreestuh)',
790 'description': 'md5:dc7872fb300e143831327f1bae3af010',
792 'upload_date': '20150721',
793 'uploader': 'Beer Games Beer',
794 'uploader_id': 'beergamesbeer',
795 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/beergamesbeer',
796 'license': 'Standard YouTube License',
802 'title': 'teamPGP: Rocket League Noob Stream (grizzle)',
803 'description': 'md5:dc7872fb300e143831327f1bae3af010',
805 'upload_date': '20150721',
806 'uploader': 'Beer Games Beer',
807 'uploader_id': 'beergamesbeer',
808 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/beergamesbeer',
809 'license': 'Standard YouTube License',
815 'title': 'teamPGP: Rocket League Noob Stream (zim)',
816 'description': 'md5:dc7872fb300e143831327f1bae3af010',
818 'upload_date': '20150721',
819 'uploader': 'Beer Games Beer',
820 'uploader_id': 'beergamesbeer',
821 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/beergamesbeer',
822 'license': 'Standard YouTube License',
826 'skip_download': True,
828 'skip': 'This video is not available.',
831 # Multifeed video with comma in title (see https://github.com/ytdl-org/youtube-dl/issues/8536)
832 'url': 'https://www.youtube.com/watch?v=gVfLd0zydlo',
835 'title': 'DevConf.cz 2016 Day 2 Workshops 1 14:00 - 15:30',
838 'skip': 'Not multifeed anymore',
841 'url': 'https://vid.plus/FlRa-iH7PGw',
842 'only_matching': True,
845 'url': 'https://zwearz.com/watch/9lWxNJF-ufM/electra-woman-dyna-girl-official-trailer-grace-helbig.html',
846 'only_matching': True,
849 # Title with JS-like syntax "};" (see https://github.com/ytdl-org/youtube-dl/issues/7468)
850 # Also tests cut-off URL expansion in video description (see
851 # https://github.com/ytdl-org/youtube-dl/issues/1892,
852 # https://github.com/ytdl-org/youtube-dl/issues/8164)
853 'url': 'https://www.youtube.com/watch?v=lsguqyKfVQg',
857 'title': '{dark walk}; Loki/AC/Dishonored; collab w/Elflover21',
858 'alt_title': 'Dark Walk - Position Music',
859 'description': 'md5:8085699c11dc3f597ce0410b0dcbb34a',
861 'upload_date': '20151119',
862 'uploader_id': 'IronSoulElf',
863 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/IronSoulElf',
864 'uploader': 'IronSoulElf',
865 'creator': 'Todd Haberman, Daniel Law Heath and Aaron Kaplan',
866 'track': 'Dark Walk - Position Music',
867 'artist': 'Todd Haberman, Daniel Law Heath and Aaron Kaplan',
868 'album': 'Position Music - Production Music Vol. 143 - Dark Walk',
871 'skip_download': True,
875 # Tags with '};' (see https://github.com/ytdl-org/youtube-dl/issues/7468)
876 'url': 'https://www.youtube.com/watch?v=Ms7iBXnlUO8',
877 'only_matching': True,
880 # Video with yt:stretch=17:0
881 'url': 'https://www.youtube.com/watch?v=Q39EVAstoRM',
885 'title': 'Clash Of Clans#14 Dicas De Ataque Para CV 4',
886 'description': 'md5:ee18a25c350637c8faff806845bddee9',
887 'upload_date': '20151107',
888 'uploader_id': 'UCCr7TALkRbo3EtFzETQF1LA',
889 'uploader': 'CH GAMER DROID',
892 'skip_download': True,
894 'skip': 'This video does not exist.',
897 # Video licensed under Creative Commons
898 'url': 'https://www.youtube.com/watch?v=M4gD1WSo5mA',
902 'title': 'md5:e41008789470fc2533a3252216f1c1d1',
903 'description': 'md5:a677553cf0840649b731a3024aeff4cc',
905 'upload_date': '20150127',
906 'uploader_id': 'BerkmanCenter',
907 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/BerkmanCenter',
908 'uploader': 'The Berkman Klein Center for Internet & Society',
909 'license': 'Creative Commons Attribution license (reuse allowed)',
912 'skip_download': True,
916 # Channel-like uploader_url
917 'url': 'https://www.youtube.com/watch?v=eQcmzGIKrzg',
921 'title': 'Democratic Socialism and Foreign Policy | Bernie Sanders',
922 'description': 'md5:dda0d780d5a6e120758d1711d062a867',
924 'upload_date': '20151119',
925 'uploader': 'Bernie Sanders',
926 'uploader_id': 'UCH1dpzjCEiGAt8CXkryhkZg',
927 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCH1dpzjCEiGAt8CXkryhkZg',
928 'license': 'Creative Commons Attribution license (reuse allowed)',
931 'skip_download': True,
935 'url': 'https://www.youtube.com/watch?feature=player_embedded&amp;v=V36LpHqtcDY',
936 'only_matching': True,
939 # YouTube Red paid video (https://github.com/ytdl-org/youtube-dl/issues/10059)
940 'url': 'https://www.youtube.com/watch?v=i1Ko8UG-Tdo',
941 'only_matching': True,
944 # Rental video preview
945 'url': 'https://www.youtube.com/watch?v=yYr8q0y5Jfg',
949 'title': 'Piku - Trailer',
950 'description': 'md5:c36bd60c3fd6f1954086c083c72092eb',
951 'upload_date': '20150811',
952 'uploader': 'FlixMatrix',
953 'uploader_id': 'FlixMatrixKaravan',
954 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/FlixMatrixKaravan',
955 'license': 'Standard YouTube License',
958 'skip_download': True,
960 'skip': 'This video is not available.',
963 # YouTube Red video with episode data
964 'url': 'https://www.youtube.com/watch?v=iqKdEhx-dD4',
968 'title': 'Isolation - Mind Field (Ep 1)',
969 'description': 'md5:46a29be4ceffa65b92d277b93f463c0f',
971 'upload_date': '20170118',
972 'uploader': 'Vsauce',
973 'uploader_id': 'Vsauce',
974 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Vsauce',
975 'series': 'Mind Field',
980 'skip_download': True,
982 'expected_warnings': [
983 'Skipping DASH manifest',
987 # The following content has been identified by the YouTube community
988 # as inappropriate or offensive to some audiences.
989 'url': 'https://www.youtube.com/watch?v=6SJNVb0GnPI',
993 'title': 'Race Differences in Intelligence',
994 'description': 'md5:5d161533167390427a1f8ee89a1fc6f1',
996 'upload_date': '20140124',
997 'uploader': 'New Century Foundation',
998 'uploader_id': 'UCEJYpZGqgUob0zVVEaLhvVg',
999 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCEJYpZGqgUob0zVVEaLhvVg',
1002 'skip_download': True,
1007 'url': '1t24XAntNCY',
1008 'only_matching': True,
1011 # geo restricted to JP
1012 'url': 'sJL6WA-aGkQ',
1013 'only_matching': True,
1016 'url': 'https://invidio.us/watch?v=BaW_jenozKc',
1017 'only_matching': True,
1021 'url': 'https://www.youtube.com/watch?v=s7_qI6_mIXc',
1022 'only_matching': True,
1025 # Video with unsupported adaptive stream type formats
1026 'url': 'https://www.youtube.com/watch?v=Z4Vy8R84T1U',
1028 'id': 'Z4Vy8R84T1U',
1030 'title': 'saman SMAN 53 Jakarta(Sancety) opening COFFEE4th at SMAN 53 Jakarta',
1031 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
1033 'upload_date': '20130923',
1034 'uploader': 'Amelia Putri Harwita',
1035 'uploader_id': 'UCpOxM49HJxmC1qCalXyB3_Q',
1036 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCpOxM49HJxmC1qCalXyB3_Q',
1037 'formats': 'maxcount:10',
1040 'skip_download': True,
1041 'youtube_include_dash_manifest': False,
1043 'skip': 'not actual anymore',
1046 # Youtube Music Auto-generated description
1047 'url': 'https://music.youtube.com/watch?v=MgNrAu2pzNs',
1049 'id': 'MgNrAu2pzNs',
1051 'title': 'Voyeur Girl',
1052 'description': 'md5:7ae382a65843d6df2685993e90a8628f',
1053 'upload_date': '20190312',
1054 'uploader': 'Stephen - Topic',
1055 'uploader_id': 'UC-pWHpBjdGG69N9mM2auIAA',
1056 'artist': 'Stephen',
1057 'track': 'Voyeur Girl',
1058 'album': 'it\'s too much love to know my dear',
1059 'release_date': '20190313',
1060 'release_year': 2019,
1063 'skip_download': True,
1067 'url': 'https://www.youtubekids.com/watch?v=3b8nCWDgZ6Q',
1068 'only_matching': True,
1071 # invalid -> valid video id redirection
1072 'url': 'DJztXj2GPfl',
1074 'id': 'DJztXj2GPfk',
1076 'title': 'Panjabi MC - Mundian To Bach Ke (The Dictator Soundtrack)',
1077 'description': 'md5:bf577a41da97918e94fa9798d9228825',
1078 'upload_date': '20090125',
1079 'uploader': 'Prochorowka',
1080 'uploader_id': 'Prochorowka',
1081 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Prochorowka',
1082 'artist': 'Panjabi MC',
1083 'track': 'Beware of the Boys (Mundian to Bach Ke) - Motivo Hi-Lectro Remix',
1084 'album': 'Beware of the Boys (Mundian To Bach Ke)',
1087 'skip_download': True,
1091 # empty description results in an empty string
1092 'url': 'https://www.youtube.com/watch?v=x41yOUIvK2k',
1094 'id': 'x41yOUIvK2k',
1096 'title': 'IMG 3456',
1098 'upload_date': '20170613',
1099 'uploader_id': 'ElevageOrVert',
1100 'uploader': 'ElevageOrVert',
1103 'skip_download': True,
1107 # with '};' inside yt initial data (see [1])
1108 # see [2] for an example with '};' inside ytInitialPlayerResponse
1109 # 1. https://github.com/ytdl-org/youtube-dl/issues/27093
1110 # 2. https://github.com/ytdl-org/youtube-dl/issues/27216
1111 'url': 'https://www.youtube.com/watch?v=CHqg6qOn4no',
1113 'id': 'CHqg6qOn4no',
1115 'title': 'Part 77 Sort a list of simple types in c#',
1116 'description': 'md5:b8746fa52e10cdbf47997903f13b20dc',
1117 'upload_date': '20130831',
1118 'uploader_id': 'kudvenkat',
1119 'uploader': 'kudvenkat',
1122 'skip_download': True,
1126 # another example of '};' in ytInitialData
1127 'url': 'https://www.youtube.com/watch?v=gVfgbahppCY',
1128 'only_matching': True,
1131 'url': 'https://www.youtube.com/watch_popup?v=63RmMXCd_bQ',
1132 'only_matching': True,
1136 def __init__(self, *args, **kwargs):
1137 super(YoutubeIE, self).__init__(*args, **kwargs)
1138 self._player_cache = {}
1140 def report_video_info_webpage_download(self, video_id):
1141 """Report attempt to download video info webpage."""
1142 self.to_screen('%s: Downloading video info webpage' % video_id)
1144 def report_information_extraction(self, video_id):
1145 """Report attempt to extract video information."""
1146 self.to_screen('%s: Extracting video information' % video_id)
1148 def report_unavailable_format(self, video_id, format):
1149 """Report extracted video URL."""
1150 self.to_screen('%s: Format %s not available' % (video_id, format))
1152 def report_rtmp_download(self):
1153 """Indicate the download will use the RTMP protocol."""
1154 self.to_screen('RTMP download detected')
1156 def _signature_cache_id(self, example_sig):
1157 """ Return a string representation of a signature """
1158 return '.'.join(compat_str(len(part)) for part in example_sig.split('.'))
1161 def _extract_player_info(cls, player_url):
1162 for player_re in cls._PLAYER_INFO_RE:
1163 id_m = re.search(player_re, player_url)
1167 raise ExtractorError('Cannot identify player %r' % player_url)
1168 return id_m.group('ext'), id_m.group('id')
1170 def _extract_signature_function(self, video_id, player_url, example_sig):
1171 player_type, player_id = self._extract_player_info(player_url)
1173 # Read from filesystem cache
1174 func_id = '%s_%s_%s' % (
1175 player_type, player_id, self._signature_cache_id(example_sig))
1176 assert os.path.basename(func_id) == func_id
1178 cache_spec = self._downloader.cache.load('youtube-sigfuncs', func_id)
1179 if cache_spec is not None:
1180 return lambda s: ''.join(s[i] for i in cache_spec)
1183 'Downloading player %s' % player_url
1184 if self._downloader.params.get('verbose') else
1185 'Downloading %s player %s' % (player_type, player_id)
1187 if player_type == 'js':
1188 code = self._download_webpage(
1189 player_url, video_id,
1191 errnote='Download of %s failed' % player_url)
1192 res = self._parse_sig_js(code)
1193 elif player_type == 'swf':
1194 urlh = self._request_webpage(
1195 player_url, video_id,
1197 errnote='Download of %s failed' % player_url)
1199 res = self._parse_sig_swf(code)
1201 assert False, 'Invalid player type %r' % player_type
1203 test_string = ''.join(map(compat_chr, range(len(example_sig))))
1204 cache_res = res(test_string)
1205 cache_spec = [ord(c) for c in cache_res]
1207 self._downloader.cache.store('youtube-sigfuncs', func_id, cache_spec)
1210 def _print_sig_code(self, func, example_sig):
1211 def gen_sig_code(idxs):
1212 def _genslice(start, end, step):
1213 starts = '' if start == 0 else str(start)
1214 ends = (':%d' % (end + step)) if end + step >= 0 else ':'
1215 steps = '' if step == 1 else (':%d' % step)
1216 return 's[%s%s%s]' % (starts, ends, steps)
1219 # Quelch pyflakes warnings - start will be set when step is set
1220 start = '(Never used)'
1221 for i, prev in zip(idxs[1:], idxs[:-1]):
1222 if step is not None:
1223 if i - prev == step:
1225 yield _genslice(start, prev, step)
1228 if i - prev in [-1, 1]:
1233 yield 's[%d]' % prev
1237 yield _genslice(start, i, step)
1239 test_string = ''.join(map(compat_chr, range(len(example_sig))))
1240 cache_res = func(test_string)
1241 cache_spec = [ord(c) for c in cache_res]
1242 expr_code = ' + '.join(gen_sig_code(cache_spec))
1243 signature_id_tuple = '(%s)' % (
1244 ', '.join(compat_str(len(p)) for p in example_sig.split('.')))
1245 code = ('if tuple(len(p) for p in s.split(\'.\')) == %s:\n'
1246 ' return %s\n') % (signature_id_tuple, expr_code)
1247 self.to_screen('Extracted signature function:\n' + code)
1249 def _parse_sig_js(self, jscode):
1250 funcname = self._search_regex(
1251 (r'\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
1252 r'\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
1253 r'(?:\b|[^a-zA-Z0-9$])(?P<sig>[a-zA-Z0-9$]{2})\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)',
1254 r'(?P<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)',
1256 r'(["\'])signature\
1\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1257 r'\
.sig\|\|
(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1258 r'yt\
.akamaized\
.net
/\
)\s
*\|\|\s
*.*?\s
*[cs
]\s
*&&\s
*[adf
]\
.set\
([^
,]+\s
*,\s
*(?
:encodeURIComponent\s
*\
()?\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1259 r'\b[cs
]\s
*&&\s
*[adf
]\
.set\
([^
,]+\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1260 r'\b[a
-zA
-Z0
-9]+\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1261 r'\bc\s
*&&\s
*a\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1262 r'\bc\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1263 r'\bc\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
('),
1264 jscode, 'Initial JS player signature function name
', group='sig
')
1266 jsi = JSInterpreter(jscode)
1267 initial_function = jsi.extract_function(funcname)
1268 return lambda s: initial_function([s])
1270 def _parse_sig_swf(self, file_contents):
1271 swfi = SWFInterpreter(file_contents)
1272 TARGET_CLASSNAME = 'SignatureDecipher
'
1273 searched_class = swfi.extract_class(TARGET_CLASSNAME)
1274 initial_function = swfi.extract_function(searched_class, 'decipher
')
1275 return lambda s: initial_function([s])
1277 def _decrypt_signature(self, s, video_id, player_url, age_gate=False):
1278 """Turn the encrypted s field into a working signature"""
1280 if player_url is None:
1281 raise ExtractorError('Cannot decrypt signature without player_url
')
1283 if player_url.startswith('//'):
1284 player_url = 'https
:' + player_url
1285 elif not re.match(r'https?
://', player_url):
1286 player_url = compat_urlparse.urljoin(
1287 'https
://www
.youtube
.com
', player_url)
1289 player_id = (player_url, self._signature_cache_id(s))
1290 if player_id not in self._player_cache:
1291 func = self._extract_signature_function(
1292 video_id, player_url, s
1294 self._player_cache[player_id] = func
1295 func = self._player_cache[player_id]
1296 if self._downloader.params.get('youtube_print_sig_code
'):
1297 self._print_sig_code(func, s)
1299 except Exception as e:
1300 tb = traceback.format_exc()
1301 raise ExtractorError(
1302 'Signature extraction failed
: ' + tb, cause=e)
1304 def _get_subtitles(self, video_id, webpage, has_live_chat_replay):
1306 subs_doc = self._download_xml(
1307 'https
://video
.google
.com
/timedtext?hl
=en
&type=list&v
=%s' % video_id,
1308 video_id, note=False)
1309 except ExtractorError as err:
1310 self._downloader.report_warning('unable to download video subtitles
: %s' % error_to_compat_str(err))
1314 for track in subs_doc.findall('track
'):
1315 lang = track.attrib['lang_code
']
1316 if lang in sub_lang_list:
1319 for ext in self._SUBTITLE_FORMATS:
1320 params = compat_urllib_parse_urlencode({
1324 'name
': track.attrib['name
'].encode('utf
-8'),
1326 sub_formats.append({
1327 'url
': 'https
://www
.youtube
.com
/api
/timedtext?
' + params,
1330 sub_lang_list[lang] = sub_formats
1331 if has_live_chat_replay:
1332 sub_lang_list['live_chat
'] = [
1334 'video_id
': video_id,
1336 'protocol
': 'youtube_live_chat_replay
',
1339 if not sub_lang_list:
1340 self._downloader.report_warning('video doesn
\'t have subtitles
')
1342 return sub_lang_list
1344 def _get_ytplayer_config(self, video_id, webpage):
1346 # User data may contain arbitrary character sequences that may affect
1347 # JSON extraction with regex, e.g. when '};' is contained the second
1348 # regex won't capture the whole JSON
. Yet working around by trying more
1349 # concrete regex first keeping in mind proper quoted string handling
1350 # to be implemented in future that will replace this workaround (see
1351 # https://github.com/ytdl-org/youtube-dl/issues/7468,
1352 # https://github.com/ytdl-org/youtube-dl/pull/7599)
1353 r
';ytplayer\.config\s*=\s*({.+?});ytplayer',
1354 r
';ytplayer\.config\s*=\s*({.+?});',
1356 config
= self
._search
_regex
(
1357 patterns
, webpage
, 'ytplayer.config', default
=None)
1359 return self
._parse
_json
(
1360 uppercase_escape(config
), video_id
, fatal
=False)
1362 def _get_automatic_captions(self
, video_id
, player_response
, player_config
):
1363 """We need the webpage for getting the captions url, pass it as an
1364 argument to speed up the process."""
1365 self
.to_screen('%s: Looking for automatic captions' % video_id
)
1366 err_msg
= 'Couldn\'t find automatic captions for %s' % video_id
1367 if not (player_response
or player_config
):
1368 self
._downloader
.report_warning(err_msg
)
1371 args
= player_config
.get('args') if player_config
else {}
1372 caption_url
= args
.get('ttsurl')
1374 timestamp
= args
['timestamp']
1375 # We get the available subtitles
1376 list_params
= compat_urllib_parse_urlencode({
1381 list_url
= caption_url
+ '&' + list_params
1382 caption_list
= self
._download
_xml
(list_url
, video_id
)
1383 original_lang_node
= caption_list
.find('track')
1384 if original_lang_node
is None:
1385 self
._downloader
.report_warning('Video doesn\'t have automatic captions')
1387 original_lang
= original_lang_node
.attrib
['lang_code']
1388 caption_kind
= original_lang_node
.attrib
.get('kind', '')
1391 for lang_node
in caption_list
.findall('target'):
1392 sub_lang
= lang_node
.attrib
['lang_code']
1394 for ext
in self
._SUBTITLE
_FORMATS
:
1395 params
= compat_urllib_parse_urlencode({
1396 'lang': original_lang
,
1400 'kind': caption_kind
,
1402 sub_formats
.append({
1403 'url': caption_url
+ '&' + params
,
1406 sub_lang_list
[sub_lang
] = sub_formats
1407 return sub_lang_list
1409 def make_captions(sub_url
, sub_langs
):
1410 parsed_sub_url
= compat_urllib_parse_urlparse(sub_url
)
1411 caption_qs
= compat_parse_qs(parsed_sub_url
.query
)
1413 for sub_lang
in sub_langs
:
1415 for ext
in self
._SUBTITLE
_FORMATS
:
1417 'tlang': [sub_lang
],
1420 sub_url
= compat_urlparse
.urlunparse(parsed_sub_url
._replace
(
1421 query
=compat_urllib_parse_urlencode(caption_qs
, True)))
1422 sub_formats
.append({
1426 captions
[sub_lang
] = sub_formats
1429 # New captions format as of 22.06.2017
1431 renderer
= player_response
['captions']['playerCaptionsTracklistRenderer']
1432 base_url
= renderer
['captionTracks'][0]['baseUrl']
1434 for lang
in renderer
['translationLanguages']:
1435 lang_code
= lang
.get('languageCode')
1437 sub_lang_list
.append(lang_code
)
1438 return make_captions(base_url
, sub_lang_list
)
1440 # Some videos don't provide ttsurl but rather caption_tracks and
1441 # caption_translation_languages (e.g. 20LmZk1hakA)
1442 # Does not used anymore as of 22.06.2017
1443 caption_tracks
= args
['caption_tracks']
1444 caption_translation_languages
= args
['caption_translation_languages']
1445 caption_url
= compat_parse_qs(caption_tracks
.split(',')[0])['u'][0]
1447 for lang
in caption_translation_languages
.split(','):
1448 lang_qs
= compat_parse_qs(compat_urllib_parse_unquote_plus(lang
))
1449 sub_lang
= lang_qs
.get('lc', [None])[0]
1451 sub_lang_list
.append(sub_lang
)
1452 return make_captions(caption_url
, sub_lang_list
)
1453 # An extractor error can be raise by the download process if there are
1454 # no automatic captions but there are subtitles
1455 except (KeyError, IndexError, ExtractorError
):
1456 self
._downloader
.report_warning(err_msg
)
1459 def _mark_watched(self
, video_id
, video_info
, player_response
):
1460 playback_url
= url_or_none(try_get(
1462 lambda x
: x
['playbackTracking']['videostatsPlaybackUrl']['baseUrl']) or try_get(
1463 video_info
, lambda x
: x
['videostats_playback_base_url'][0]))
1464 if not playback_url
:
1466 parsed_playback_url
= compat_urlparse
.urlparse(playback_url
)
1467 qs
= compat_urlparse
.parse_qs(parsed_playback_url
.query
)
1469 # cpn generation algorithm is reverse engineered from base.js.
1470 # In fact it works even with dummy cpn.
1471 CPN_ALPHABET
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
1472 cpn
= ''.join((CPN_ALPHABET
[random
.randint(0, 256) & 63] for _
in range(0, 16)))
1478 playback_url
= compat_urlparse
.urlunparse(
1479 parsed_playback_url
._replace
(query
=compat_urllib_parse_urlencode(qs
, True)))
1481 self
._download
_webpage
(
1482 playback_url
, video_id
, 'Marking watched',
1483 'Unable to mark watched', fatal
=False)
1486 def _extract_urls(webpage
):
1487 # Embedded YouTube player
1489 unescapeHTML(mobj
.group('url'))
1490 for mobj
in re
.finditer(r
'''(?x)
1500 (?P
<url
>(?
:https?
:)?
//(?
:www\
.)?
youtube(?
:-nocookie
)?\
.com
/
1501 (?
:embed|v|p
)/[0-9A
-Za
-z_
-]{11}
.*?
)
1504 # lazyYT YouTube embed
1505 entries.extend(list(map(
1507 re.findall(r'class="lazyYT" data-youtube-id="([^"]+)"', webpage))))
1509 # Wordpress "YouTube Video Importer" plugin
1510 matches = re.findall(r'''(?x
)<div
[^
>]+
1511 class=(?P
<q1
>[\'"])[^\'"]*\byvii
_single
_video
_player
\b[^
\'"]*(?P=q1)[^>]+
1512 data-video_id=(?P<q2>[\'"])([^
\'"]+)(?P=q2)''', webpage)
1513 entries.extend(m[-1] for m in matches)
1518 def _extract_url(webpage):
1519 urls = YoutubeIE._extract_urls(webpage)
1520 return urls[0] if urls else None
1523 def extract_id(cls, url):
1524 mobj = re.match(cls._VALID_URL, url, re.VERBOSE)
1526 raise ExtractorError('Invalid URL: %s' % url)
1527 video_id = mobj.group(2)
1530 def _extract_chapters_from_json(self, webpage, video_id, duration):
1533 data = self._extract_yt_initial_data(video_id, webpage)
1534 if not data or not isinstance(data, dict):
1536 chapters_list = try_get(
1538 lambda x: x['playerOverlays']
1539 ['playerOverlayRenderer']
1540 ['decoratedPlayerBarRenderer']
1541 ['decoratedPlayerBarRenderer']
1543 ['chapteredPlayerBarRenderer']
1546 if not chapters_list:
1549 def chapter_time(chapter):
1550 return float_or_none(
1553 lambda x: x['chapterRenderer']['timeRangeStartMillis'],
1557 for next_num, chapter in enumerate(chapters_list, start=1):
1558 start_time = chapter_time(chapter)
1559 if start_time is None:
1561 end_time = (chapter_time(chapters_list[next_num])
1562 if next_num < len(chapters_list) else duration)
1563 if end_time is None:
1566 chapter, lambda x: x['chapterRenderer']['title']['simpleText'],
1569 'start_time': start_time,
1570 'end_time': end_time,
1576 def _extract_chapters_from_description(description, duration):
1579 chapter_lines = re.findall(
1580 r'(?:^|<br\s*/>)([^<]*<a[^>]+onclick=["\']yt\
.www\
.watch\
.player\
.seekTo
[^
>]+>(\d{1,2}
:\d{1,2}
(?
::\d{1,2}
)?
)</a
>[^
>]*)(?
=$|
<br\s
*/>)',
1582 if not chapter_lines:
1585 for next_num, (chapter_line, time_point) in enumerate(
1586 chapter_lines, start=1):
1587 start_time = parse_duration(time_point)
1588 if start_time is None:
1590 if start_time > duration:
1592 end_time = (duration if next_num == len(chapter_lines)
1593 else parse_duration(chapter_lines[next_num][1]))
1594 if end_time is None:
1596 if end_time > duration:
1598 if start_time > end_time:
1600 chapter_title = re.sub(
1601 r'<a
[^
>]+>[^
<]+</a
>', '', chapter_line).strip(' \t-')
1602 chapter_title = re.sub(r'\s
+', ' ', chapter_title)
1604 'start_time
': start_time,
1605 'end_time
': end_time,
1606 'title
': chapter_title,
1610 def _extract_chapters(self, webpage, description, video_id, duration):
1611 return (self._extract_chapters_from_json(webpage, video_id, duration)
1612 or self._extract_chapters_from_description(description, duration))
1614 def _real_extract(self, url):
1615 url, smuggled_data = unsmuggle_url(url, {})
1618 'http
' if self._downloader.params.get('prefer_insecure
', False)
1623 parsed_url = compat_urllib_parse_urlparse(url)
1624 for component in [parsed_url.fragment, parsed_url.query]:
1625 query = compat_parse_qs(component)
1626 if start_time is None and 't
' in query:
1627 start_time = parse_duration(query['t
'][0])
1628 if start_time is None and 'start
' in query:
1629 start_time = parse_duration(query['start
'][0])
1630 if end_time is None and 'end
' in query:
1631 end_time = parse_duration(query['end
'][0])
1633 # Extract original video URL from URL with redirection, like age verification, using next_url parameter
1634 mobj = re.search(self._NEXT_URL_RE, url)
1636 url = proto + '://www
.youtube
.com
/' + compat_urllib_parse_unquote(mobj.group(1)).lstrip('/')
1637 video_id = self.extract_id(url)
1640 url = proto + '://www
.youtube
.com
/watch?v
=%s&gl
=US
&hl
=en
&has_verified
=1&bpctr
=9999999999' % video_id
1641 video_webpage, urlh = self._download_webpage_handle(url, video_id)
1643 qs = compat_parse_qs(compat_urllib_parse_urlparse(urlh.geturl()).query)
1644 video_id = qs.get('v
', [None])[0] or video_id
1646 # Attempt to extract SWF player URL
1647 mobj = re.search(r'swfConfig
.*?
"(https?:\\/\\/.*?watch.*?-.*?\.swf)"', video_webpage)
1648 if mobj is not None:
1649 player_url = re.sub(r'\\(.)', r'\
1', mobj.group(1))
1655 def add_dash_mpd(video_info):
1656 dash_mpd = video_info.get('dashmpd
')
1657 if dash_mpd and dash_mpd[0] not in dash_mpds:
1658 dash_mpds.append(dash_mpd[0])
1660 def add_dash_mpd_pr(pl_response):
1661 dash_mpd = url_or_none(try_get(
1662 pl_response, lambda x: x['streamingData
']['dashManifestUrl
'],
1664 if dash_mpd and dash_mpd not in dash_mpds:
1665 dash_mpds.append(dash_mpd)
1670 def extract_view_count(v_info):
1671 return int_or_none(try_get(v_info, lambda x: x['view_count
'][0]))
1673 def extract_player_response(player_response, video_id):
1674 pl_response = str_or_none(player_response)
1677 pl_response = self._parse_json(pl_response, video_id, fatal=False)
1678 if isinstance(pl_response, dict):
1679 add_dash_mpd_pr(pl_response)
1682 def extract_embedded_config(embed_webpage, video_id):
1683 embedded_config = self._search_regex(
1684 r'setConfig\
(({.*}
)\
);',
1685 embed_webpage, 'ytInitialData
', default=None)
1687 return embedded_config
1689 player_response = {}
1693 embed_webpage = None
1694 if (self._og_search_property('restrictions
:age
', video_webpage, default=None) == '18+'
1695 or re.search(r'player
-age
-gate
-content
">', video_webpage) is not None):
1696 cookie_keys = self._get_cookies('https://www.youtube.com').keys()
1698 # We simulate the access to the video from www.youtube.com/v/{video_id}
1699 # this can be viewed without login into Youtube
1700 url = proto + '://www.youtube.com/embed/%s' % video_id
1701 embed_webpage = self._download_webpage(url, video_id, 'Downloading embed webpage')
1702 ext = extract_embedded_config(embed_webpage, video_id)
1703 # playabilityStatus = re.search(r'{\\\"status\\\":\\\"(?P<playabilityStatus>[^\"]+)\\\"', ext)
1704 playable_in_embed = re.search(r'{\\\"playableInEmbed\\\":(?P<playableinEmbed>[^\,]+)', ext)
1705 if not playable_in_embed:
1706 self.to_screen('Could not determine whether playabale in embed for video %s' % video_id)
1707 playable_in_embed = ''
1709 playable_in_embed = playable_in_embed.group('playableinEmbed')
1710 # check if video is only playable on youtube in other words not playable in embed - if so it requires auth (cookies)
1711 # if re.search(r'player-unavailable">', embed_webpage) is not None:
1712 if playable_in_embed == 'false
':
1714 # TODO apply this patch when Support for Python 2.6(!) and above drops
1715 if ({'VISITOR_INFO1_LIVE', 'HSID', 'SSID', 'SID'} <= cookie_keys
1716 or {'VISITOR_INFO1_LIVE', '__Secure-3PSID', 'LOGIN_INFO'} <= cookie_keys):
1718 if (set(('VISITOR_INFO1_LIVE
', 'HSID
', 'SSID
', 'SID
')) <= set(cookie_keys)
1719 or set(('VISITOR_INFO1_LIVE
', '__Secure
-3PSID
', 'LOGIN_INFO
')) <= set(cookie_keys)):
1721 # Try looking directly into the video webpage
1722 ytplayer_config = self._get_ytplayer_config(video_id, video_webpage)
1724 args = ytplayer_config.get("args")
1725 if args is not None:
1726 if args.get('url_encoded_fmt_stream_map
') or args.get('hlsvp
'):
1727 # Convert to the same format returned by compat_parse_qs
1728 video_info = dict((k, [v]) for k, v in args.items())
1729 add_dash_mpd(video_info)
1730 # Rental video is not rented but preview is available (e.g.
1731 # https://www.youtube.com/watch?v=yYr8q0y5Jfg,
1732 # https://github.com/ytdl-org/youtube-dl/issues/10532)
1733 if not video_info and args.get('ypc_vid
'):
1734 return self.url_result(
1735 args['ypc_vid
'], YoutubeIE.ie_key(), video_id=args['ypc_vid
'])
1736 if args.get('livestream
') == '1' or args.get('live_playback
') == 1:
1738 if not player_response:
1739 player_response = extract_player_response(args.get('player_response
'), video_id)
1740 elif not player_response:
1741 player_response = ytplayer_config
1742 if not video_info or self._downloader.params.get('youtube_include_dash_manifest
', True):
1743 add_dash_mpd_pr(player_response)
1745 raise ExtractorError('Video
is age restricted
and only playable on Youtube
. Requires cookies
!', expected=True)
1747 data = compat_urllib_parse_urlencode({
1748 'video_id
': video_id,
1749 'eurl
': 'https
://youtube
.googleapis
.com
/v
/' + video_id,
1750 'sts
': self._search_regex(
1751 r'"sts"\s
*:\s
*(\d
+)', embed_webpage, 'sts
', default=''),
1753 video_info_url = proto + '://www
.youtube
.com
/get_video_info?
' + data
1755 video_info_webpage = self._download_webpage(
1756 video_info_url, video_id,
1757 note='Refetching age
-gated info webpage
',
1758 errnote='unable to download video info webpage
')
1759 except ExtractorError:
1760 video_info_webpage = None
1761 if video_info_webpage:
1762 video_info = compat_parse_qs(video_info_webpage)
1763 pl_response = video_info.get('player_response
', [None])[0]
1764 player_response = extract_player_response(pl_response, video_id)
1765 add_dash_mpd(video_info)
1766 view_count = extract_view_count(video_info)
1769 # Try looking directly into the video webpage
1770 ytplayer_config = self._get_ytplayer_config(video_id, video_webpage)
1772 args = ytplayer_config.get('args
', {})
1773 if args.get('url_encoded_fmt_stream_map
') or args.get('hlsvp
'):
1774 # Convert to the same format returned by compat_parse_qs
1775 video_info = dict((k, [v]) for k, v in args.items())
1776 add_dash_mpd(video_info)
1777 # Rental video is not rented but preview is available (e.g.
1778 # https://www.youtube.com/watch?v=yYr8q0y5Jfg,
1779 # https://github.com/ytdl-org/youtube-dl/issues/10532)
1780 if not video_info and args.get('ypc_vid
'):
1781 return self.url_result(
1782 args['ypc_vid
'], YoutubeIE.ie_key(), video_id=args['ypc_vid
'])
1783 if args.get('livestream
') == '1' or args.get('live_playback
') == 1:
1785 if not player_response:
1786 player_response = extract_player_response(args.get('player_response
'), video_id)
1787 if not video_info or self._downloader.params.get('youtube_include_dash_manifest
', True):
1788 add_dash_mpd_pr(player_response)
1790 if not video_info and not player_response:
1791 player_response = extract_player_response(
1793 (r'%s\s
*%s' % (self._YT_INITIAL_PLAYER_RESPONSE_RE, self._YT_INITIAL_BOUNDARY_RE),
1794 self._YT_INITIAL_PLAYER_RESPONSE_RE), video_webpage,
1795 'initial player response
', default='{}'),
1798 def extract_unavailable_message():
1800 for tag, kind in (('h1
', 'message
'), ('div
', 'submessage
')):
1801 msg = self._html_search_regex(
1802 r'(?s
)<{tag}
[^
>]+id=["\']unavailable-{kind}["\'][^
>]*>(.+?
)</{tag}
>'.format(tag=tag, kind=kind),
1803 video_webpage, 'unavailable
%s' % kind, default=None)
1805 messages.append(msg)
1807 return '\n'.join(messages)
1809 if not video_info and not player_response:
1810 unavailable_message = extract_unavailable_message()
1811 if not unavailable_message:
1812 unavailable_message = 'Unable to extract video data
'
1813 raise ExtractorError(
1814 'YouTube said
: %s' % unavailable_message, expected=True, video_id=video_id)
1816 if not isinstance(video_info, dict):
1819 video_details = try_get(
1820 player_response, lambda x: x['videoDetails
'], dict) or {}
1822 microformat = try_get(
1823 player_response, lambda x: x['microformat
']['playerMicroformatRenderer
'], dict) or {}
1825 video_title = video_info.get('title
', [None])[0] or video_details.get('title
')
1827 self._downloader.report_warning('Unable to extract video title
')
1830 description_original = video_description = get_element_by_id("eow-description", video_webpage)
1831 if video_description:
1834 redir_url = compat_urlparse.urljoin(url, m.group(1))
1835 parsed_redir_url = compat_urllib_parse_urlparse(redir_url)
1836 if re.search(r'^
(?
:www\
.)?
(?
:youtube(?
:-nocookie
)?\
.com|youtu\
.be
)$
', parsed_redir_url.netloc) and parsed_redir_url.path == '/redirect
':
1837 qs = compat_parse_qs(parsed_redir_url.query)
1843 description_original = video_description = re.sub(r'''(?x)
1845 (?:[a-zA-Z-]+="[^"]*"\s+)*?
1846 (?:title|href)="([^"]+)"\s+
1847 (?:[a-zA-Z-]+="[^"]*"\s+)*?
1851 ''', replace_url, video_description)
1852 video_description = clean_html(video_description)
1854 video_description = video_details.get('shortDescription
')
1855 if video_description is None:
1856 video_description = self._html_search_meta('description
', video_webpage)
1858 if not smuggled_data.get('force_singlefeed
', False):
1859 if not self._downloader.params.get('noplaylist
'):
1860 multifeed_metadata_list = try_get(
1862 lambda x: x['multicamera
']['playerLegacyMulticameraRenderer
']['metadataList
'],
1863 compat_str) or try_get(
1864 video_info, lambda x: x['multifeed_metadata_list
'][0], compat_str)
1865 if multifeed_metadata_list:
1868 for feed in multifeed_metadata_list.split(','):
1869 # Unquote should take place before split on comma (,) since textual
1870 # fields may contain comma as well (see
1871 # https://github.com/ytdl-org/youtube-dl/issues/8536)
1872 feed_data = compat_parse_qs(compat_urllib_parse_unquote_plus(feed))
1874 def feed_entry(name):
1875 return try_get(feed_data, lambda x: x[name][0], compat_str)
1877 feed_id = feed_entry('id')
1880 feed_title = feed_entry('title
')
1883 title += ' (%s)' % feed_title
1885 '_type
': 'url_transparent
',
1886 'ie_key
': 'Youtube
',
1888 '%s://www
.youtube
.com
/watch?v
=%s' % (proto, feed_data['id'][0]),
1889 {'force_singlefeed': True}),
1892 feed_ids.append(feed_id)
1894 'Downloading multifeed
video (%s) - add
--no
-playlist to just download video
%s'
1895 % (', '.join(feed_ids), video_id))
1896 return self.playlist_result(entries, video_id, video_title, video_description)
1898 self.to_screen('Downloading just video
%s because of
--no
-playlist
' % video_id)
1900 if view_count is None:
1901 view_count = extract_view_count(video_info)
1902 if view_count is None and video_details:
1903 view_count = int_or_none(video_details.get('viewCount
'))
1904 if view_count is None and microformat:
1905 view_count = int_or_none(microformat.get('viewCount
'))
1908 is_live = bool_or_none(video_details.get('isLive
'))
1910 has_live_chat_replay = False
1912 yt_initial_data = self._get_yt_initial_data(video_id, video_webpage)
1914 yt_initial_data['contents
']['twoColumnWatchNextResults
']['conversationBar
']['liveChatRenderer
']['continuations
'][0]['reloadContinuationData
']['continuation
']
1915 has_live_chat_replay = True
1916 except (KeyError, IndexError, TypeError):
1919 # Check for "rental" videos
1920 if 'ypc_video_rental_bar_text
' in video_info and 'author
' not in video_info:
1921 raise ExtractorError('"rental" videos
not supported
. See https
://github
.com
/ytdl
-org
/youtube
-dl
/issues
/359 for more information
.', expected=True)
1923 def _extract_filesize(media_url):
1924 return int_or_none(self._search_regex(
1925 r'\bclen
[=/](\d
+)', media_url, 'filesize
', default=None))
1927 streaming_formats = try_get(player_response, lambda x: x['streamingData
']['formats
'], list) or []
1928 streaming_formats.extend(try_get(player_response, lambda x: x['streamingData
']['adaptiveFormats
'], list) or [])
1930 if 'conn
' in video_info and video_info['conn
'][0].startswith('rtmp
'):
1931 self.report_rtmp_download()
1933 'format_id
': '_rtmp
',
1935 'url
': video_info['conn
'][0],
1936 'player_url
': player_url,
1938 elif not is_live and (streaming_formats or len(video_info.get('url_encoded_fmt_stream_map
', [''])[0]) >= 1 or len(video_info.get('adaptive_fmts
', [''])[0]) >= 1):
1939 encoded_url_map = video_info.get('url_encoded_fmt_stream_map
', [''])[0] + ',' + video_info.get('adaptive_fmts
', [''])[0]
1940 if 'rtmpe
%3Dyes
' in encoded_url_map:
1941 raise ExtractorError('rtmpe downloads are
not supported
, see https
://github
.com
/ytdl
-org
/youtube
-dl
/issues
/343 for more information
.', expected=True)
1944 fmt_list = video_info.get('fmt_list
', [''])[0]
1946 for fmt in fmt_list.split(','):
1947 spec = fmt.split('/')
1949 width_height = spec[1].split('x
')
1950 if len(width_height) == 2:
1951 formats_spec[spec[0]] = {
1952 'resolution
': spec[1],
1953 'width
': int_or_none(width_height[0]),
1954 'height
': int_or_none(width_height[1]),
1956 for fmt in streaming_formats:
1957 itag = str_or_none(fmt.get('itag
'))
1960 quality = fmt.get('quality
')
1961 quality_label = fmt.get('qualityLabel
') or quality
1962 formats_spec[itag] = {
1963 'asr
': int_or_none(fmt.get('audioSampleRate
')),
1964 'filesize
': int_or_none(fmt.get('contentLength
')),
1965 'format_note
': quality_label,
1966 'fps
': int_or_none(fmt.get('fps
')),
1967 'height
': int_or_none(fmt.get('height
')),
1968 # bitrate for itag 43 is always 2147483647
1969 'tbr
': float_or_none(fmt.get('averageBitrate
') or fmt.get('bitrate
'), 1000) if itag != '43' else None,
1970 'width
': int_or_none(fmt.get('width
')),
1973 for fmt in streaming_formats:
1974 if fmt.get('drmFamilies
') or fmt.get('drm_families
'):
1976 url = url_or_none(fmt.get('url
'))
1979 cipher = fmt.get('cipher
') or fmt.get('signatureCipher
')
1982 url_data = compat_parse_qs(cipher)
1983 url = url_or_none(try_get(url_data, lambda x: x['url
'][0], compat_str))
1988 url_data = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
1990 stream_type = int_or_none(try_get(url_data, lambda x: x['stream_type
'][0]))
1991 # Unsupported FORMAT_STREAM_TYPE_OTF
1992 if stream_type == 3:
1995 format_id = fmt.get('itag
') or url_data['itag
'][0]
1998 format_id = compat_str(format_id)
2001 if 's
' in url_data or self._downloader.params.get('youtube_include_dash_manifest
', True):
2003 r'<script
[^
>]+\bsrc
=("[^"]+")[^>]+\bname=["\']player_ias
/base
',
2004 r'"jsUrl"\s
*:\s
*("[^"]+")',
2005 r'"assets
":.+?"js
":\s*("[^
"]+")')
2006 jsplayer_url_json = self._search_regex(
2008 embed_webpage if age_gate else video_webpage,
2009 'JS player
URL (1)', default=None)
2010 if not jsplayer_url_json and not age_gate:
2011 # We need the embed website after all
2012 if embed_webpage is None:
2013 embed_url = proto + '://www
.youtube
.com
/embed
/%s' % video_id
2014 embed_webpage = self._download_webpage(
2015 embed_url, video_id, 'Downloading embed webpage
')
2016 jsplayer_url_json = self._search_regex(
2017 ASSETS_RE, embed_webpage, 'JS player URL
')
2019 player_url = json.loads(jsplayer_url_json)
2020 if player_url is None:
2021 player_url_json = self._search_regex(
2022 r'ytplayer\
.config
.*?
"url"\s
*:\s
*("[^"]+")',
2023 video_webpage, 'age gate player URL')
2024 player_url = json.loads(player_url_json)
2026 if 'sig' in url_data:
2027 url += '&signature=' + url_data['sig'][0]
2028 elif 's' in url_data:
2029 encrypted_sig = url_data['s'][0]
2031 if self._downloader.params.get('verbose'):
2032 if player_url is None:
2033 player_desc = 'unknown'
2035 player_type, player_version = self._extract_player_info(player_url)
2036 player_desc = '%s player %s' % ('flash' if player_type == 'swf' else 'html5', player_version)
2037 parts_sizes = self._signature_cache_id(encrypted_sig)
2038 self.to_screen('{%s} signature length %s, %s' %
2039 (format_id, parts_sizes, player_desc))
2041 signature = self._decrypt_signature(
2042 encrypted_sig, video_id, player_url, age_gate)
2043 sp = try_get(url_data, lambda x: x['sp'][0], compat_str) or 'signature'
2044 url += '&%s=%s' % (sp, signature)
2045 if 'ratebypass' not in url:
2046 url += '&ratebypass=yes'
2049 'format_id': format_id,
2051 'player_url': player_url,
2053 if format_id in self._formats:
2054 dct.update(self._formats[format_id])
2055 if format_id in formats_spec:
2056 dct.update(formats_spec[format_id])
2058 # Some itags are not included in DASH manifest thus corresponding formats will
2059 # lack metadata (see https://github.com/ytdl-org/youtube-dl/pull/5993).
2060 # Trying to extract metadata from url_encoded_fmt_stream_map entry.
2061 mobj = re.search(r'^(?P<width>\d+)[xX](?P<height>\d+)$', url_data.get('size', [''])[0])
2062 width, height = (int(mobj.group('width')), int(mobj.group('height'))) if mobj else (None, None)
2065 width = int_or_none(fmt.get('width'))
2067 height = int_or_none(fmt.get('height'))
2069 filesize = int_or_none(url_data.get(
2070 'clen', [None])[0]) or _extract_filesize(url)
2072 quality = url_data.get('quality', [None])[0] or fmt.get('quality')
2073 quality_label = url_data.get('quality_label', [None])[0] or fmt.get('qualityLabel')
2075 tbr = (float_or_none(url_data.get('bitrate', [None])[0], 1000)
2076 or float_or_none(fmt.get('bitrate'), 1000)) if format_id != '43' else None
2077 fps = int_or_none(url_data.get('fps', [None])[0]) or int_or_none(fmt.get('fps'))
2080 'filesize': filesize,
2085 'format_note': quality_label or quality,
2087 for key, value in more_fields.items():
2090 type_ = url_data.get('type', [None])[0] or fmt.get('mimeType')
2092 type_split = type_.split(';')
2093 kind_ext = type_split[0].split('/')
2094 if len(kind_ext) == 2:
2096 dct['ext'] = mimetype2ext(type_split[0])
2097 if kind in ('audio', 'video'):
2099 for mobj in re.finditer(
2100 r'(?P<key>[a-zA-Z_-]+)=(?P<quote>["\']?
)(?P
<val
>.+?
)(?P
=quote
)(?
:;|$
)', type_):
2101 if mobj.group('key
') == 'codecs
':
2102 codecs = mobj.group('val
')
2105 dct.update(parse_codecs(codecs))
2106 if dct.get('acodec
') == 'none
' or dct.get('vcodec
') == 'none
':
2107 dct['downloader_options
'] = {
2108 # Youtube throttles chunks >~10M
2109 'http_chunk_size
': 10485760,
2114 url_or_none(try_get(
2116 lambda x: x['streamingData
']['hlsManifestUrl
'],
2118 or url_or_none(try_get(
2119 video_info, lambda x: x['hlsvp
'][0], compat_str)))
2122 m3u8_formats = self._extract_m3u8_formats(
2123 manifest_url, video_id, 'mp4
', fatal=False)
2124 for a_format in m3u8_formats:
2125 itag = self._search_regex(
2126 r'/itag
/(\d
+)/', a_format['url
'], 'itag
', default=None)
2128 a_format['format_id
'] = itag
2129 if itag in self._formats:
2130 dct = self._formats[itag].copy()
2131 dct.update(a_format)
2133 a_format['player_url
'] = player_url
2134 # Accept-Encoding header causes failures in live streams on Youtube and Youtube Gaming
2135 a_format.setdefault('http_headers
', {})['Youtubedl
-no
-compression
'] = 'True'
2136 if self._downloader.params.get('youtube_include_hls_manifest
', True):
2137 formats.append(a_format)
2139 error_message = extract_unavailable_message()
2140 if not error_message:
2141 reason_list = try_get(
2143 lambda x: x['playabilityStatus
']['errorScreen
']['playerErrorMessageRenderer
']['subreason
']['runs
'],
2145 for reason in reason_list:
2146 if not isinstance(reason, dict):
2148 reason_text = try_get(reason, lambda x: x['text
'], compat_str)
2150 if not error_message:
2152 error_message += reason_text
2154 error_message = clean_html(error_message)
2155 if not error_message:
2156 error_message = clean_html(try_get(
2157 player_response, lambda x: x['playabilityStatus
']['reason
'],
2159 if not error_message:
2160 error_message = clean_html(
2161 try_get(video_info, lambda x: x['reason
'][0], compat_str))
2163 raise ExtractorError(error_message, expected=True)
2164 raise ExtractorError('no conn
, hlsvp
, hlsManifestUrl
or url_encoded_fmt_stream_map information found
in video info
')
2167 video_uploader = try_get(
2168 video_info, lambda x: x['author
'][0],
2169 compat_str) or str_or_none(video_details.get('author
'))
2171 video_uploader = compat_urllib_parse_unquote_plus(video_uploader)
2173 self._downloader.report_warning('unable to extract uploader name
')
2176 video_uploader_id = None
2177 video_uploader_url = None
2179 r'<link itemprop
="url" href
="(?P<uploader_url>https?://www\.youtube\.com/(?:user|channel)/(?P<uploader_id>[^"]+))">',
2181 if mobj is not None:
2182 video_uploader_id = mobj.group('uploader_id')
2183 video_uploader_url = mobj.group('uploader_url')
2185 owner_profile_url = url_or_none(microformat.get('ownerProfileUrl'))
2186 if owner_profile_url:
2187 video_uploader_id = self._search_regex(
2188 r'(?:user|channel)/([^/]+)', owner_profile_url, 'uploader id',
2190 video_uploader_url = owner_profile_url
2193 str_or_none(video_details.get('channelId'))
2194 or self._html_search_meta(
2195 'channelId', video_webpage, 'channel id', default=None)
2196 or self._search_regex(
2197 r'data-channel-external-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
2198 video_webpage, 'channel
id', default=None, group='id'))
2199 channel_url = 'http
://www
.youtube
.com
/channel
/%s' % channel_id if channel_id else None
2202 thumbnails_list = try_get(
2203 video_details, lambda x: x['thumbnail
']['thumbnails
'], list) or []
2204 for t in thumbnails_list:
2205 if not isinstance(t, dict):
2207 thumbnail_url = url_or_none(t.get('url
'))
2208 if not thumbnail_url:
2211 'url
': thumbnail_url,
2212 'width
': int_or_none(t.get('width
')),
2213 'height
': int_or_none(t.get('height
')),
2217 video_thumbnail = None
2218 # We try first to get a high quality image:
2219 m_thumb = re.search(r'<span itemprop
="thumbnail".*?href
="(.*?)">',
2220 video_webpage, re.DOTALL)
2221 if m_thumb is not None:
2222 video_thumbnail = m_thumb.group(1)
2223 thumbnail_url = try_get(video_info, lambda x: x['thumbnail_url
'][0], compat_str)
2225 video_thumbnail = compat_urllib_parse_unquote_plus(thumbnail_url)
2227 thumbnails.append({'url': video_thumbnail})
2230 upload_date = self._html_search_meta(
2231 'datePublished
', video_webpage, 'upload date
', default=None)
2233 upload_date = self._search_regex(
2234 [r'(?s
)id="eow-date.*?>(.*?)</span>',
2235 r'(?:id="watch
-uploader
-info
".*?>.*?|["\']simpleText
["\']\s*:\s*["\'])(?
:Published|Uploaded|Streamed live|Started
) on (.+?
)[<"\']'],
2236 video_webpage, 'upload date', default=None)
2238 upload_date = microformat.get('publishDate') or microformat.get('uploadDate')
2239 upload_date = unified_strdate(upload_date)
2241 video_license = self._html_search_regex(
2242 r'<h4[^>]+class="title
"[^>]*>\s*License\s*</h4>\s*<ul[^>]*>\s*<li>(.+?)</li',
2243 video_webpage, 'license', default=None)
2245 m_music = re.search(
2247 <h4[^>]+class="title
"[^>]*>\s*Music\s*</h4>\s*
2255 \bhref=["\']/red
[^
>]*>|
# drop possible
2256 >\s
*Listen ad
-free
with YouTube Red
# YouTube Red ad
2263 video_alt_title = remove_quotes(unescapeHTML(m_music.group('title')))
2264 video_creator = clean_html(m_music.group('creator'))
2266 video_alt_title = video_creator = None
2268 def extract_meta(field):
2269 return self._html_search_regex(
2270 r'<h4[^>]+class="title"[^>]*>\s*%s\s*</h4>\s*<ul[^>]*>\s*<li>(.+?)</li>\s*' % field,
2271 video_webpage, field, default=None)
2273 track = extract_meta('Song')
2274 artist = extract_meta('Artist')
2275 album = extract_meta('Album')
2277 # Youtube Music Auto-generated description
2278 release_date = release_year = None
2279 if video_description:
2280 mobj = re.search(r'(?s)(?P<track>[^·\n]+)·(?P<artist>[^\n]+)\n+(?P<album>[^\n]+)(?:.+?℗\s*(?P<release_year>\d{4})(?!\d))?(?:.+?Released on\s*:\s*(?P<release_date>\d{4}-\d{2}-\d{2}))?(.+?\nArtist\s*:\s*(?P<clean_artist>[^\n]+))?.+\nAuto-generated by YouTube\.\s*$', video_description)
2283 track = mobj.group('track').strip()
2285 artist = mobj.group('clean_artist') or ', '.join(a.strip() for a in mobj.group('artist').split('·'))
2287 album = mobj.group('album'.strip())
2288 release_year = mobj.group('release_year')
2289 release_date = mobj.group('release_date')
2291 release_date = release_date.replace('-', '')
2292 if not release_year:
2293 release_year = int(release_date[:4])
2295 release_year = int(release_year)
2297 yt_initial_data = self._extract_yt_initial_data(video_id, video_webpage)
2298 contents = try_get(yt_initial_data, lambda x: x['contents']['twoColumnWatchNextResults']['results']['results']['contents'], list) or []
2299 for content in contents:
2300 rows = try_get(content, lambda x: x['videoSecondaryInfoRenderer']['metadataRowContainer']['metadataRowContainerRenderer']['rows'], list) or []
2301 multiple_songs = False
2303 if try_get(row, lambda x: x['metadataRowRenderer']['hasDividerLine']) is True:
2304 multiple_songs = True
2307 mrr = row.get('metadataRowRenderer') or {}
2308 mrr_title = try_get(
2309 mrr, lambda x: x['title']['simpleText'], compat_str)
2310 mrr_contents = try_get(
2311 mrr, lambda x: x['contents'][0], dict) or {}
2312 mrr_contents_text = try_get(mrr_contents, [lambda x: x['simpleText'], lambda x: x['runs'][0]['text']], compat_str)
2313 if not (mrr_title and mrr_contents_text):
2315 if mrr_title == 'License':
2316 video_license = mrr_contents_text
2317 elif not multiple_songs:
2318 if mrr_title == 'Album':
2319 album = mrr_contents_text
2320 elif mrr_title == 'Artist':
2321 artist = mrr_contents_text
2322 elif mrr_title == 'Song':
2323 track = mrr_contents_text
2325 m_episode = re.search(
2326 r'<div[^>]+id="watch7-headline"[^>]*>\s*<span[^>]*>.*?>(?P<series>[^<]+)</a></b>\s*S(?P<season>\d+)\s*•\s*E(?P<episode>\d+)</span>',
2329 series = unescapeHTML(m_episode.group('series'))
2330 season_number = int(m_episode.group('season'))
2331 episode_number = int(m_episode.group('episode'))
2333 series = season_number = episode_number = None
2335 m_cat_container = self._search_regex(
2336 r'(?s)<h4[^>]*>\s*Category\s*</h4>\s*<ul[^>]*>(.*?)</ul>',
2337 video_webpage, 'categories', default=None)
2340 category = self._html_search_regex(
2341 r'(?s)<a[^<]+>(.*?)</a>', m_cat_container, 'category',
2345 microformat, lambda x: x['category'], compat_str)
2346 video_categories = None if category is None else [category]
2349 unescapeHTML(m.group('content'))
2350 for m in re.finditer(self._meta_regex('og:video:tag'), video_webpage)]
2352 video_tags = try_get(video_details, lambda x: x['keywords'], list)
2354 def _extract_count(count_name):
2355 return str_to_int(self._search_regex(
2356 (r'-%s-button[^>]+><span[^>]+class="yt-uix-button-content"[^>]*>([\d,]+)</span>' % re.escape(count_name),
2357 r'["\']label["\']\s*:\s*["\']([\d,.]+)\s+%ss["\']' % re.escape(count_name)),
2358 video_webpage, count_name, default=None))
2360 like_count = _extract_count('like')
2361 dislike_count = _extract_count('dislike')
2363 if view_count is None:
2364 view_count = str_to_int(self._search_regex(
2365 r'<[^>]+class=["\']watch-view-count[^>]+>\s*([\d,\s]+)', video_webpage,
2366 'view count', default=None))
2369 float_or_none(video_details.get('averageRating'))
2370 or try_get(video_info, lambda x: float_or_none(x['avg_rating'][0])))
2373 video_subtitles = self.extract_subtitles(
2374 video_id, video_webpage, has_live_chat_replay)
2375 automatic_captions = self.extract_automatic_captions(video_id, player_response, ytplayer_config)
2377 video_duration = try_get(
2378 video_info, lambda x: int_or_none(x['length_seconds'][0]))
2379 if not video_duration:
2380 video_duration = int_or_none(video_details.get('lengthSeconds'))
2381 if not video_duration:
2382 video_duration = parse_duration(self._html_search_meta(
2383 'duration', video_webpage, 'video duration'))
2385 # Get Subscriber Count of channel
2386 subscriber_count = parse_count(self._search_regex(
2387 r'"text":"([\d\.]+\w?) subscribers"',
2394 video_annotations = None
2395 if self._downloader.params.get('writeannotations', False):
2397 ytcfg = self._extract_ytcfg(video_id, video_webpage)
2399 xsrf_token = try_get(ytcfg, lambda x: x['XSRF_TOKEN'], compat_str)
2401 xsrf_token = self._search_regex(
2402 r'([\'"])XSRF_TOKEN\1\s*:\s*([\'"])(?P<xsrf_token>(?:(?!\2).)+)\2',
2403 video_webpage, 'xsrf token', group='xsrf_token', fatal=False)
2404 invideo_url = try_get(
2405 player_response, lambda x: x['annotations'][0]['playerAnnotationsUrlsRenderer']['invideoUrl'], compat_str)
2406 if xsrf_token and invideo_url:
2407 xsrf_field_name = None
2409 xsrf_field_name = try_get(ytcfg, lambda x: x['XSRF_FIELD_NAME'], compat_str)
2410 if not xsrf_field_name:
2411 xsrf_field_name = self._search_regex(
2412 r'([\'"])XSRF_FIELD_NAME\1\s*:\s*([\'"])(?P<xsrf_field_name>\w+)\2',
2413 video_webpage, 'xsrf field name',
2414 group='xsrf_field_name', default='session_token')
2415 video_annotations = self._download_webpage(
2416 self._proto_relative_url(invideo_url),
2417 video_id, note='Downloading annotations',
2418 errnote='Unable to download video annotations', fatal=False,
2419 data=urlencode_postdata({xsrf_field_name: xsrf_token}))
2421 chapters = self._extract_chapters(video_webpage, description_original, video_id, video_duration)
2423 # Look for the DASH manifest
2424 if self._downloader.params.get('youtube_include_dash_manifest', True):
2425 dash_mpd_fatal = True
2426 for mpd_url in dash_mpds:
2429 def decrypt_sig(mobj):
2431 dec_s = self._decrypt_signature(s, video_id, player_url, age_gate)
2432 return '/signature/%s' % dec_s
2434 mpd_url = re.sub(r'/s/([a-fA-F0-9\.]+)', decrypt_sig, mpd_url)
2436 for df in self._extract_mpd_formats(
2437 mpd_url, video_id, fatal=dash_mpd_fatal,
2438 formats_dict=self._formats):
2439 if not df.get('filesize'):
2440 df['filesize'] = _extract_filesize(df['url'])
2441 # Do not overwrite DASH format found in some previous DASH manifest
2442 if df['format_id'] not in dash_formats:
2443 dash_formats[df['format_id']] = df
2444 # Additional DASH manifests may end up in HTTP Error 403 therefore
2445 # allow them to fail without bug report message if we already have
2446 # some DASH manifest succeeded. This is temporary workaround to reduce
2447 # burst of bug reports until we figure out the reason and whether it
2448 # can be fixed at all.
2449 dash_mpd_fatal = False
2450 except (ExtractorError, KeyError) as e:
2451 self.report_warning(
2452 'Skipping DASH manifest: %r' % e, video_id)
2454 # Remove the formats we found through non-DASH, they
2455 # contain less info and it can be wrong, because we use
2456 # fixed values (for example the resolution). See
2457 # https://github.com/ytdl-org/youtube-dl/issues/5774 for an
2459 formats = [f for f in formats if f['format_id'] not in dash_formats.keys()]
2460 formats.extend(dash_formats.values())
2462 # Check for malformed aspect ratio
2463 stretched_m = re.search(
2464 r'<meta\s+property="og:video:tag".*?content="yt:stretch=(?P<w>[0-9]+):(?P<h>[0-9]+)">',
2467 w = float(stretched_m.group('w'))
2468 h = float(stretched_m.group('h'))
2469 # yt:stretch may hold invalid ratio data (e.g. for Q39EVAstoRM ratio is 17:0).
2470 # We will only process correct ratios.
2474 if f.get('vcodec') != 'none':
2475 f['stretched_ratio'] = ratio
2478 if 'reason' in video_info:
2479 if 'The uploader has not made this video available in your country.' in video_info['reason']:
2480 regions_allowed = self._html_search_meta(
2481 'regionsAllowed', video_webpage, default=None)
2482 countries = regions_allowed.split(',') if regions_allowed else None
2483 self.raise_geo_restricted(
2484 msg=video_info['reason'][0], countries=countries)
2485 reason = video_info['reason'][0]
2486 if 'Invalid parameters' in reason:
2487 unavailable_message = extract_unavailable_message()
2488 if unavailable_message:
2489 reason = unavailable_message
2490 raise ExtractorError(
2491 'YouTube said: %s' % reason,
2492 expected=True, video_id=video_id)
2493 if video_info.get('license_info') or try_get(player_response, lambda x: x['streamingData']['licenseInfos']):
2494 raise ExtractorError('This video is DRM protected.', expected=True)
2496 self._sort_formats(formats)
2498 self.mark_watched(video_id, video_info, player_response)
2502 'uploader': video_uploader,
2503 'uploader_id': video_uploader_id,
2504 'uploader_url': video_uploader_url,
2505 'channel_id': channel_id,
2506 'channel_url': channel_url,
2507 'upload_date': upload_date,
2508 'license': video_license,
2509 'creator': video_creator or artist,
2510 'title': video_title,
2511 'alt_title': video_alt_title or track,
2512 'thumbnails': thumbnails,
2513 'description': video_description,
2514 'categories': video_categories,
2516 'subtitles': video_subtitles,
2517 'automatic_captions': automatic_captions,
2518 'duration': video_duration,
2519 'age_limit': 18 if age_gate else 0,
2520 'annotations': video_annotations,
2521 'chapters': chapters,
2522 'webpage_url': proto + '://www.youtube.com/watch?v=%s' % video_id,
2523 'view_count': view_count,
2524 'like_count': like_count,
2525 'dislike_count': dislike_count,
2526 'average_rating': average_rating,
2529 'start_time': start_time,
2530 'end_time': end_time,
2532 'season_number': season_number,
2533 'episode_number': episode_number,
2537 'release_date': release_date,
2538 'release_year': release_year,
2539 'subscriber_count': subscriber_count,
2543 class YoutubeTabIE(YoutubeBaseInfoExtractor):
2544 IE_DESC = 'YouTube.com tab'
2545 _VALID_URL = r'''(?x
)
2549 youtube(?
:kids
)?\
.com|
2553 (?
:channel|c|user
)/|
2556 (?
:playlist|watch
)\?.*?
\blist
=
2558 (?
!(?
:%s)\b) # Direct URLs
2561 ''' % YoutubeBaseInfoExtractor._RESERVED_NAMES
2562 IE_NAME = 'youtube:tab'
2565 # playlists, multipage
2566 'url': 'https://www.youtube.com/c/ИгорьКлейнер/playlists?view=1&flow=grid',
2567 'playlist_mincount': 94,
2569 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
2570 'title': 'Игорь Клейнер - Playlists',
2571 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
2574 # playlists, multipage, different order
2575 'url': 'https://www.youtube.com/user/igorkle1/playlists?view=1&sort=dd',
2576 'playlist_mincount': 94,
2578 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
2579 'title': 'Игорь Клейнер - Playlists',
2580 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
2583 # playlists, singlepage
2584 'url': 'https://www.youtube.com/user/ThirstForScience/playlists',
2585 'playlist_mincount': 4,
2587 'id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
2588 'title': 'ThirstForScience - Playlists',
2589 'description': 'md5:609399d937ea957b0f53cbffb747a14c',
2592 'url': 'https://www.youtube.com/c/ChristophLaimer/playlists',
2593 'only_matching': True,
2595 # basic, single video playlist
2596 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2598 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
2599 'uploader': 'Sergey M.',
2600 'id': 'PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2601 'title': 'youtube-dl public playlist',
2603 'playlist_count': 1,
2606 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
2608 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
2609 'uploader': 'Sergey M.',
2610 'id': 'PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
2611 'title': 'youtube-dl empty playlist',
2613 'playlist_count': 0,
2616 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/featured',
2618 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2619 'title': 'lex will - Home',
2620 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2622 'playlist_mincount': 2,
2625 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos',
2627 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2628 'title': 'lex will - Videos',
2629 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2631 'playlist_mincount': 975,
2633 # Videos tab, sorted by popular
2634 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos?view=0&sort=p&flow=grid',
2636 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2637 'title': 'lex will - Videos',
2638 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2640 'playlist_mincount': 199,
2643 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/playlists',
2645 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2646 'title': 'lex will - Playlists',
2647 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2649 'playlist_mincount': 17,
2652 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/community',
2654 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2655 'title': 'lex will - Community',
2656 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2658 'playlist_mincount': 18,
2661 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/channels',
2663 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2664 'title': 'lex will - Channels',
2665 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2667 'playlist_mincount': 138,
2669 'url': 'https://invidio.us/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2670 'only_matching': True,
2672 'url': 'https://www.youtubekids.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2673 'only_matching': True,
2675 'url': 'https://music.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2676 'only_matching': True,
2678 'note': 'Playlist with deleted videos (#651). As a bonus, the video #51 is also twice in this list.',
2679 'url': 'https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
2681 'title': '29C3: Not my department',
2682 'id': 'PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
2683 'uploader': 'Christiaan008',
2684 'uploader_id': 'UCEPzS1rYsrkqzSLNp76nrcg',
2686 'playlist_count': 96,
2688 'note': 'Large playlist',
2689 'url': 'https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q',
2691 'title': 'Uploads from Cauchemar',
2692 'id': 'UUBABnxM4Ar9ten8Mdjj1j0Q',
2693 'uploader': 'Cauchemar',
2694 'uploader_id': 'UCBABnxM4Ar9ten8Mdjj1j0Q',
2696 'playlist_mincount': 1123,
2698 # even larger playlist, 8832 videos
2699 'url': 'http://www.youtube.com/user/NASAgovVideo/videos',
2700 'only_matching': True,
2702 'note': 'Buggy playlist: the webpage has a "Load more" button but it doesn\'t have more videos',
2703 'url': 'https://www.youtube.com/playlist?list=UUXw-G3eDE9trcvY2sBMM_aA',
2705 'title': 'Uploads from Interstellar Movie',
2706 'id': 'UUXw-G3eDE9trcvY2sBMM_aA',
2707 'uploader': 'Interstellar Movie',
2708 'uploader_id': 'UCXw-G3eDE9trcvY2sBMM_aA',
2710 'playlist_mincount': 21,
2712 # https://github.com/ytdl-org/youtube-dl/issues/21844
2713 'url': 'https://www.youtube.com/playlist?list=PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
2715 'title': 'Data Analysis with Dr Mike Pound',
2716 'id': 'PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
2717 'uploader_id': 'UC9-y-6csu5WGm29I7JiwpnA',
2718 'uploader': 'Computerphile',
2720 'playlist_mincount': 11,
2722 'url': 'https://invidio.us/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2723 'only_matching': True,
2725 # Playlist URL that does not actually serve a playlist
2726 'url': 'https://www.youtube.com/watch?v=FqZTN594JQw&list=PLMYEtVRpaqY00V9W81Cwmzp6N6vZqfUKD4',
2728 'id': 'FqZTN594JQw',
2730 'title': "Smiley's People 01 detective, Adventure Series, Action",
2731 'uploader': 'STREEM',
2732 'uploader_id': 'UCyPhqAZgwYWZfxElWVbVJng',
2733 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCyPhqAZgwYWZfxElWVbVJng',
2734 'upload_date': '20150526',
2735 'license': 'Standard YouTube License',
2736 'description': 'md5:507cdcb5a49ac0da37a920ece610be80',
2737 'categories': ['People & Blogs'],
2741 'dislike_count': int,
2744 'skip_download': True,
2746 'skip': 'This video is not available.',
2747 'add_ie': [YoutubeIE.ie_key()],
2749 'url': 'https://www.youtubekids.com/watch?v=Agk7R8I8o5U&list=PUZ6jURNr1WQZCNHF0ao-c0g',
2750 'only_matching': True,
2752 'url': 'https://www.youtube.com/watch?v=MuAGGZNfUkU&list=RDMM',
2753 'only_matching': True,
2755 'url': 'https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live',
2757 'id': '9Auq9mYxFEE',
2759 'title': 'Watch Sky News live',
2760 'uploader': 'Sky News',
2761 'uploader_id': 'skynews',
2762 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/skynews',
2763 'upload_date': '20191102',
2764 'description': 'md5:78de4e1c2359d0ea3ed829678e38b662',
2765 'categories': ['News & Politics'],
2768 'dislike_count': int,
2771 'skip_download': True,
2774 'url': 'https://www.youtube.com/user/TheYoungTurks/live',
2776 'id': 'a48o2S1cPoo',
2778 'title': 'The Young Turks - Live Main Show',
2779 'uploader': 'The Young Turks',
2780 'uploader_id': 'TheYoungTurks',
2781 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/TheYoungTurks',
2782 'upload_date': '20150715',
2783 'license': 'Standard YouTube License',
2784 'description': 'md5:438179573adcdff3c97ebb1ee632b891',
2785 'categories': ['News & Politics'],
2786 'tags': ['Cenk Uygur (TV Program Creator)', 'The Young Turks (Award-Winning Work)', 'Talk Show (TV Genre)'],
2788 'dislike_count': int,
2791 'skip_download': True,
2793 'only_matching': True,
2795 'url': 'https://www.youtube.com/channel/UC1yBKRuGpC1tSM73A0ZjYjQ/live',
2796 'only_matching': True,
2798 'url': 'https://www.youtube.com/c/CommanderVideoHq/live',
2799 'only_matching': True,
2801 'url': 'https://www.youtube.com/feed/trending',
2802 'only_matching': True,
2805 'url': 'https://www.youtube.com/feed/library',
2806 'only_matching': True,
2809 'url': 'https://www.youtube.com/feed/history',
2810 'only_matching': True,
2813 'url': 'https://www.youtube.com/feed/subscriptions',
2814 'only_matching': True,
2817 'url': 'https://www.youtube.com/feed/watch_later',
2818 'only_matching': True,
2820 # no longer available?
2821 'url': 'https://www.youtube.com/feed/recommended',
2822 'only_matching': True,
2824 # inline playlist with not always working continuations
2825 'url': 'https://www.youtube.com/watch?v=UC6u0Tct-Fo&list=PL36D642111D65BE7C',
2826 'only_matching': True,
2828 'url': 'https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8',
2829 'only_matching': True,
2831 'url': 'https://www.youtube.com/course',
2832 'only_matching': True,
2834 'url': 'https://www.youtube.com/zsecurity',
2835 'only_matching': True,
2837 'url': 'http://www.youtube.com/NASAgovVideo/videos',
2838 'only_matching': True,
2840 'url': 'https://www.youtube.com/TheYoungTurks/live',
2841 'only_matching': True,
2845 def suitable(cls, url):
2846 return False if YoutubeIE.suitable(url) else super(
2847 YoutubeTabIE, cls).suitable(url)
2849 def _extract_channel_id(self, webpage):
2850 channel_id = self._html_search_meta(
2851 'channelId', webpage, 'channel id', default=None)
2854 channel_url = self._html_search_meta(
2855 ('og:url', 'al:ios:url', 'al:android:url', 'al:web:url',
2856 'twitter:url', 'twitter:app:url:iphone', 'twitter:app:url:ipad',
2857 'twitter:app:url:googleplay'), webpage, 'channel url')
2858 return self._search_regex(
2859 r'https?://(?:www\.)?youtube\.com/channel/([^/?#&])+',
2860 channel_url, 'channel id')
2863 def _extract_grid_item_renderer(item):
2864 for item_kind in ('Playlist', 'Video', 'Channel'):
2865 renderer = item.get('grid%sRenderer' % item_kind)
2869 def _extract_video(self, renderer):
2870 video_id = renderer.get('videoId')
2873 (lambda x: x['title']['runs'][0]['text'],
2874 lambda x: x['title']['simpleText']), compat_str)
2875 description = try_get(
2876 renderer, lambda x: x['descriptionSnippet']['runs'][0]['text'],
2878 duration = parse_duration(try_get(
2879 renderer, lambda x: x['lengthText']['simpleText'], compat_str))
2880 view_count_text = try_get(
2881 renderer, lambda x: x['viewCountText']['simpleText'], compat_str) or ''
2882 view_count = str_to_int(self._search_regex(
2883 r'^([\d,]+)', re.sub(r'\s', '', view_count_text),
2884 'view count', default=None))
2886 renderer, lambda x: x['ownerText']['runs'][0]['text'], compat_str)
2888 '_type': 'url_transparent',
2889 'ie_key': YoutubeIE.ie_key(),
2893 'description': description,
2894 'duration': duration,
2895 'view_count': view_count,
2896 'uploader': uploader,
2899 def _grid_entries(self, grid_renderer):
2900 for item in grid_renderer['items']:
2901 if not isinstance(item, dict):
2903 renderer = self._extract_grid_item_renderer(item)
2904 if not isinstance(renderer, dict):
2907 renderer, lambda x: x['title']['runs'][0]['text'], compat_str)
2909 playlist_id = renderer.get('playlistId')
2911 yield self.url_result(
2912 'https://www.youtube.com/playlist?list=%s' % playlist_id,
2913 ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
2916 video_id = renderer.get('videoId')
2918 yield self._extract_video(renderer)
2920 channel_id = renderer.get('channelId')
2923 renderer, lambda x: x['title']['simpleText'], compat_str)
2924 yield self.url_result(
2925 'https://www.youtube.com/channel/%s' % channel_id,
2926 ie=YoutubeTabIE.ie_key(), video_title=title)
2928 def _shelf_entries_from_content(self, shelf_renderer):
2929 content = shelf_renderer.get('content')
2930 if not isinstance(content, dict):
2932 renderer = content.get('gridRenderer')
2934 # TODO: add support for nested playlists so each shelf is processed
2935 # as separate playlist
2936 # TODO: this includes only first N items
2937 for entry in self._grid_entries(renderer):
2939 renderer = content.get('horizontalListRenderer')
2944 def _shelf_entries(self, shelf_renderer, skip_channels=False):
2946 shelf_renderer, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
2948 shelf_url = urljoin('https://www.youtube.com', ep)
2950 # Skipping links to another channels, note that checking for
2951 # endpoint.commandMetadata.webCommandMetadata.webPageTypwebPageType == WEB_PAGE_TYPE_CHANNEL
2953 if skip_channels and '/channels?' in shelf_url:
2956 shelf_renderer, lambda x: x['title']['runs'][0]['text'], compat_str)
2957 yield self.url_result(shelf_url, video_title=title)
2958 # Shelf may not contain shelf URL, fallback to extraction from content
2959 for entry in self._shelf_entries_from_content(shelf_renderer):
2962 def _playlist_entries(self, video_list_renderer):
2963 for content in video_list_renderer['contents']:
2964 if not isinstance(content, dict):
2966 renderer = content.get('playlistVideoRenderer') or content.get('playlistPanelVideoRenderer')
2967 if not isinstance(renderer, dict):
2969 video_id = renderer.get('videoId')
2972 yield self._extract_video(renderer)
2974 r""" # Not needed in the new implementation
2975 def _itemSection_entries(self, item_sect_renderer):
2976 for content in item_sect_renderer['contents']:
2977 if not isinstance(content, dict):
2979 renderer = content.get('videoRenderer', {})
2980 if not isinstance(renderer, dict):
2982 video_id = renderer.get('videoId')
2985 yield self._extract_video(renderer)
2988 def _rich_entries(self, rich_grid_renderer):
2990 rich_grid_renderer, lambda x: x['content']['videoRenderer'], dict) or {}
2991 video_id = renderer.get('videoId')
2994 yield self._extract_video(renderer)
2996 def _video_entry(self, video_renderer):
2997 video_id = video_renderer.get('videoId')
2999 return self._extract_video(video_renderer)
3001 def _post_thread_entries(self, post_thread_renderer):
3002 post_renderer = try_get(
3003 post_thread_renderer, lambda x: x['post']['backstagePostRenderer'], dict)
3004 if not post_renderer:
3007 video_renderer = try_get(
3008 post_renderer, lambda x: x['backstageAttachment']['videoRenderer'], dict)
3011 entry = self._video_entry(video_renderer)
3014 # inline video links
3015 runs = try_get(post_renderer, lambda x: x['contentText']['runs'], list) or []
3017 if not isinstance(run, dict):
3020 run, lambda x: x['navigationEndpoint']['urlEndpoint']['url'], compat_str)
3023 if not YoutubeIE.suitable(ep_url):
3025 ep_video_id = YoutubeIE._match_id(ep_url)
3026 if video_id == ep_video_id:
3028 yield self.url_result(ep_url, ie=YoutubeIE.ie_key(), video_id=video_id)
3030 def _post_thread_continuation_entries(self, post_thread_continuation):
3031 contents = post_thread_continuation.get('contents')
3032 if not isinstance(contents, list):
3034 for content in contents:
3035 renderer = content.get('backstagePostThreadRenderer')
3036 if not isinstance(renderer, dict):
3038 for entry in self._post_thread_entries(renderer):
3042 def _build_continuation_query(continuation, ctp=None):
3044 'ctoken': continuation,
3045 'continuation': continuation,
3052 def _extract_next_continuation_data(renderer):
3053 next_continuation = try_get(
3054 renderer, lambda x: x['continuations'][0]['nextContinuationData'], dict)
3055 if not next_continuation:
3057 continuation = next_continuation.get('continuation')
3058 if not continuation:
3060 ctp = next_continuation.get('clickTrackingParams')
3061 return YoutubeTabIE._build_continuation_query(continuation, ctp)
3064 def _extract_continuation(cls, renderer):
3065 next_continuation = cls._extract_next_continuation_data(renderer)
3066 if next_continuation:
3067 return next_continuation
3068 contents = renderer.get('contents')
3069 if not isinstance(contents, list):
3071 for content in contents:
3072 if not isinstance(content, dict):
3074 continuation_ep = try_get(
3075 content, lambda x: x['continuationItemRenderer']['continuationEndpoint'],
3077 if not continuation_ep:
3079 continuation = try_get(
3080 continuation_ep, lambda x: x['continuationCommand']['token'], compat_str)
3081 if not continuation:
3083 ctp = continuation_ep.get('clickTrackingParams')
3084 return YoutubeTabIE._build_continuation_query(continuation, ctp)
3086 def _entries(self, tab, identity_token):
3088 def extract_entries(parent_renderer): # this needs to called again for continuation to work with feeds
3089 contents = try_get(parent_renderer, lambda x: x['contents'], list) or []
3090 for content in contents:
3091 if not isinstance(content, dict):
3093 is_renderer = try_get(content, lambda x: x['itemSectionRenderer'], dict)
3095 renderer = content.get('richItemRenderer')
3097 for entry in self._rich_entries(renderer):
3099 continuation_list[0] = self._extract_continuation(parent_renderer)
3101 isr_contents = try_get(is_renderer, lambda x: x['contents'], list) or []
3102 for isr_content in isr_contents:
3103 if not isinstance(isr_content, dict):
3105 renderer = isr_content.get('playlistVideoListRenderer')
3107 for entry in self._playlist_entries(renderer):
3109 continuation_list[0] = self._extract_continuation(renderer)
3111 renderer = isr_content.get('gridRenderer')
3113 for entry in self._grid_entries(renderer):
3115 continuation_list[0] = self._extract_continuation(renderer)
3117 renderer = isr_content.get('shelfRenderer')
3119 is_channels_tab = tab.get('title') == 'Channels'
3120 for entry in self._shelf_entries(renderer, not is_channels_tab):
3123 renderer = isr_content.get('backstagePostThreadRenderer')
3125 for entry in self._post_thread_entries(renderer):
3127 continuation_list[0] = self._extract_continuation(renderer)
3129 renderer = isr_content.get('videoRenderer')
3131 entry = self._video_entry(renderer)
3135 if not continuation_list[0]:
3136 continuation_list[0] = self._extract_continuation(is_renderer)
3138 if not continuation_list[0]:
3139 continuation_list[0] = self._extract_continuation(parent_renderer)
3141 continuation_list = [None] # Python 2 doesnot support nonlocal
3142 tab_content = try_get(tab, lambda x: x['content'], dict)
3146 try_get(tab_content, lambda x: x['sectionListRenderer'], dict)
3147 or try_get(tab_content, lambda x: x['richGridRenderer'], dict) or {})
3148 for entry in extract_entries(parent_renderer):
3150 continuation = continuation_list[0]
3153 'x-youtube-client-name': '1',
3154 'x-youtube-client-version': '2.20201112.04.01',
3157 headers['x-youtube-identity-token'] = identity_token
3159 for page_num in itertools.count(1):
3160 if not continuation:
3164 while count <= retries:
3166 # Downloading page may result in intermittent 5xx HTTP error
3167 # that is usually worked around with a retry
3168 browse = self._download_json(
3169 'https://www.youtube.com/browse_ajax', None,
3170 'Downloading page %d%s'
3171 % (page_num, ' (retry #%d)' % count if count else ''),
3172 headers=headers, query=continuation)
3174 except ExtractorError as e:
3175 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (500, 503):
3177 if count <= retries:
3182 response = try_get(browse, lambda x: x[1]['response'], dict)
3186 continuation_contents = try_get(
3187 response, lambda x: x['continuationContents'], dict)
3188 if continuation_contents:
3189 continuation_renderer = continuation_contents.get('playlistVideoListContinuation')
3190 if continuation_renderer:
3191 for entry in self._playlist_entries(continuation_renderer):
3193 continuation = self._extract_continuation(continuation_renderer)
3195 continuation_renderer = continuation_contents.get('gridContinuation')
3196 if continuation_renderer:
3197 for entry in self._grid_entries(continuation_renderer):
3199 continuation = self._extract_continuation(continuation_renderer)
3201 continuation_renderer = continuation_contents.get('itemSectionContinuation')
3202 if continuation_renderer:
3203 for entry in self._post_thread_continuation_entries(continuation_renderer):
3205 continuation = self._extract_continuation(continuation_renderer)
3207 continuation_renderer = continuation_contents.get('sectionListContinuation') # for feeds
3208 if continuation_renderer:
3209 continuation_list = [None]
3210 for entry in extract_entries(continuation_renderer):
3212 continuation = continuation_list[0]
3215 continuation_items = try_get(
3216 response, lambda x: x['onResponseReceivedActions'][0]['appendContinuationItemsAction']['continuationItems'], list)
3217 if continuation_items:
3218 continuation_item = continuation_items[0]
3219 if not isinstance(continuation_item, dict):
3221 renderer = continuation_item.get('playlistVideoRenderer') or continuation_item.get('itemSectionRenderer')
3223 video_list_renderer = {'contents': continuation_items}
3224 for entry in self._playlist_entries(video_list_renderer):
3226 continuation = self._extract_continuation(video_list_renderer)
3231 def _extract_selected_tab(tabs):
3233 if try_get(tab, lambda x: x['tabRenderer']['selected'], bool):
3234 return tab['tabRenderer']
3236 raise ExtractorError('Unable to find selected tab')
3239 def _extract_uploader(data):
3241 sidebar_renderer = try_get(
3242 data, lambda x: x['sidebar']['playlistSidebarRenderer']['items'], list)
3243 if sidebar_renderer:
3244 for item in sidebar_renderer:
3245 if not isinstance(item, dict):
3247 renderer = item.get('playlistSidebarSecondaryInfoRenderer')
3248 if not isinstance(renderer, dict):
3251 renderer, lambda x: x['videoOwner']['videoOwnerRenderer']['title']['runs'][0], dict)
3253 uploader['uploader'] = owner.get('text')
3254 uploader['uploader_id'] = try_get(
3255 owner, lambda x: x['navigationEndpoint']['browseEndpoint']['browseId'], compat_str)
3256 uploader['uploader_url'] = urljoin(
3257 'https://www.youtube.com/',
3258 try_get(owner, lambda x: x['navigationEndpoint']['browseEndpoint']['canonicalBaseUrl'], compat_str))
3261 def _extract_from_tabs(self, item_id, webpage, data, tabs, identity_token):
3262 selected_tab = self._extract_selected_tab(tabs)
3264 data, lambda x: x['metadata']['channelMetadataRenderer'], dict)
3265 playlist_id = title = description = None
3267 channel_title = renderer.get('title') or item_id
3268 tab_title = selected_tab.get('title')
3269 title = channel_title or item_id
3271 title += ' - %s' % tab_title
3272 description = renderer.get('description')
3273 playlist_id = renderer.get('externalId')
3275 data, lambda x: x['metadata']['playlistMetadataRenderer'], dict)
3277 title = renderer.get('title')
3279 playlist_id = item_id
3280 if playlist_id is None:
3281 playlist_id = item_id
3283 title = "Youtube " + playlist_id.title()
3284 playlist = self.playlist_result(
3285 self._entries(selected_tab, identity_token),
3286 playlist_id=playlist_id, playlist_title=title,
3287 playlist_description=description)
3288 playlist.update(self._extract_uploader(data))
3291 def _extract_from_playlist(self, item_id, url, data, playlist):
3292 title = playlist.get('title') or try_get(
3293 data, lambda x: x['titleText']['simpleText'], compat_str)
3294 playlist_id = playlist.get('playlistId') or item_id
3295 # Inline playlist rendition continuation does not always work
3296 # at Youtube side, so delegating regular tab-based playlist URL
3297 # processing whenever possible.
3298 playlist_url = urljoin(url, try_get(
3299 playlist, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
3301 if playlist_url and playlist_url != url:
3302 return self.url_result(
3303 playlist_url, ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
3305 return self.playlist_result(
3306 self._playlist_entries(playlist), playlist_id=playlist_id,
3307 playlist_title=title)
3310 def _extract_alerts(data):
3311 for alert_dict in try_get(data, lambda x: x['alerts'], list) or []:
3312 if not isinstance(alert_dict, dict):
3314 for renderer in alert_dict:
3315 alert = alert_dict[renderer]
3316 alert_type = alert.get('type')
3319 message = try_get(alert, lambda x: x['text']['simpleText'], compat_str)
3321 yield alert_type, message
3322 for run in try_get(alert, lambda x: x['text']['runs'], list) or []:
3323 message = try_get(run, lambda x: x['text'], compat_str)
3325 yield alert_type, message
3327 def _extract_identity_token(self, webpage, item_id):
3328 ytcfg = self._extract_ytcfg(item_id, webpage)
3330 token = try_get(ytcfg, lambda x: x['ID_TOKEN'], compat_str)
3333 return self._search_regex(
3334 r'\bID_TOKEN["\']\s*:\s*["\'](.+?)["\']', webpage,
3335 'identity token', default=None)
3337 def _real_extract(self, url):
3338 item_id = self._match_id(url)
3339 url = compat_urlparse.urlunparse(
3340 compat_urlparse.urlparse(url)._replace(netloc='www.youtube.com'))
3341 is_home = re.match(r'(?P<pre>%s)(?P<post>/?(?![^#?]).*$)' % self._VALID_URL, url)
3342 if is_home is not None and is_home.group('not_channel') is None and item_id != 'feed':
3343 self._downloader.report_warning(
3344 'A channel/user page was given. All the channel\'s videos will be downloaded. '
3345 'To download only the videos in the home page, add a "/home" to the URL')
3346 url = '%s/videos%s' % (is_home.group('pre'), is_home.group('post') or '')
3348 # Handle both video/playlist URLs
3349 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
3350 video_id = qs.get('v', [None])[0]
3351 playlist_id = qs.get('list', [None])[0]
3353 if is_home is not None and is_home.group('not_channel') is not None and is_home.group('not_channel').startswith('watch') and not video_id:
3355 self._downloader.report_warning('%s is not a valid Youtube URL. Trying to download playlist %s' % (url, playlist_id))
3356 url = 'https://www.youtube.com/playlist?list=%s' % playlist_id
3357 # return self.url_result(playlist_id, ie=YoutubePlaylistIE.ie_key())
3359 raise ExtractorError('Unable to recognize tab page')
3360 if video_id and playlist_id:
3361 if self._downloader.params.get('noplaylist'):
3362 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
3363 return self.url_result(video_id, ie=YoutubeIE.ie_key(), video_id=video_id)
3364 self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (playlist_id, video_id))
3366 webpage = self._download_webpage(url, item_id)
3367 identity_token = self._extract_identity_token(webpage, item_id)
3368 data = self._extract_yt_initial_data(item_id, webpage)
3369 for alert_type, alert_message in self._extract_alerts(data):
3370 self._downloader.report_warning('YouTube said: %s - %s' % (alert_type, alert_message))
3372 data, lambda x: x['contents']['twoColumnBrowseResultsRenderer']['tabs'], list)
3374 return self._extract_from_tabs(item_id, webpage, data, tabs, identity_token)
3376 data, lambda x: x['contents']['twoColumnWatchNextResults']['playlist']['playlist'], dict)
3378 return self._extract_from_playlist(item_id, url, data, playlist)
3379 # Fallback to video extraction if no playlist alike page is recognized.
3380 # First check for the current video then try the v attribute of URL query.
3382 data, lambda x: x['currentVideoEndpoint']['watchEndpoint']['videoId'],
3383 compat_str) or video_id
3385 return self.url_result(video_id, ie=YoutubeIE.ie_key(), video_id=video_id)
3386 # Failed to recognize
3387 raise ExtractorError('Unable to recognize tab page')
3390 class YoutubePlaylistIE(InfoExtractor):
3391 IE_DESC = 'YouTube.com playlists'
3392 _VALID_URL = r'''(?x
)(?
:
3397 youtube(?
:kids
)?\
.com|
3402 (?P
<id>%(playlist_id)s)
3403 )''' % {'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE}
3404 IE_NAME = 'youtube:playlist'
3406 'note': 'issue #673',
3407 'url': 'PLBB231211A4F62143',
3409 'title': '[OLD]Team Fortress 2 (Class-based LP)',
3410 'id': 'PLBB231211A4F62143',
3411 'uploader': 'Wickydoo',
3412 'uploader_id': 'UCKSpbfbl5kRQpTdL7kMc-1Q',
3414 'playlist_mincount': 29,
3416 'url': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
3418 'title': 'YDL_safe_search',
3419 'id': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
3421 'playlist_count': 2,
3422 'skip': 'This playlist is private',
3425 'url': 'https://www.youtube.com/embed/videoseries?list=PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
3426 'playlist_count': 4,
3429 'id': 'PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
3430 'uploader': 'milan',
3431 'uploader_id': 'UCEI1-PVPcYXjB73Hfelbmaw',
3434 'url': 'http://www.youtube.com/embed/_xDOZElKyNU?list=PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
3435 'playlist_mincount': 982,
3437 'title': '2018 Chinese New Singles (11/6 updated)',
3438 'id': 'PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
3440 'uploader_id': 'UC21nz3_MesPLqtDqwdvnoxA',
3443 'url': 'TLGGrESM50VT6acwMjAyMjAxNw',
3444 'only_matching': True,
3446 # music album playlist
3447 'url': 'OLAK5uy_m4xAFdmMC5rX3Ji3g93pQe3hqLZw_9LhM',
3448 'only_matching': True,
3452 def suitable(cls, url):
3453 return False if YoutubeTabIE.suitable(url) else super(
3454 YoutubePlaylistIE, cls).suitable(url)
3456 def _real_extract(self, url):
3457 playlist_id = self._match_id(url)
3458 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
3460 qs = {'list': playlist_id}
3461 return self.url_result(
3462 update_url_query('https://www.youtube.com/playlist', qs),
3463 ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
3466 class YoutubeYtBeIE(InfoExtractor):
3467 _VALID_URL = r'https?://youtu\.be/(?P<id>[0-9A-Za-z_-]{11})/*?.*?\blist=(?P<playlist_id>%(playlist_id)s)' % {'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE}
3469 'url': 'https://youtu.be/yeWKywCrFtk?list=PL2qgrgXsNUG5ig9cat4ohreBjYLAPC0J5',
3471 'id': 'yeWKywCrFtk',
3473 'title': 'Small Scale Baler and Braiding Rugs',
3474 'uploader': 'Backus-Page House Museum',
3475 'uploader_id': 'backuspagemuseum',
3476 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/backuspagemuseum',
3477 'upload_date': '20161008',
3478 'description': 'md5:800c0c78d5eb128500bffd4f0b4f2e8a',
3479 'categories': ['Nonprofits & Activism'],
3482 'dislike_count': int,
3486 'skip_download': True,
3489 'url': 'https://youtu.be/uWyaPkt-VOI?list=PL9D9FC436B881BA21',
3490 'only_matching': True,
3493 def _real_extract(self, url):
3494 mobj = re.match(self._VALID_URL, url)
3495 video_id = mobj.group('id')
3496 playlist_id = mobj.group('playlist_id')
3497 return self.url_result(
3498 update_url_query('https://www.youtube.com/watch', {
3500 'list': playlist_id,
3501 'feature': 'youtu.be',
3502 }), ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
3505 class YoutubeYtUserIE(InfoExtractor):
3506 _VALID_URL = r'ytuser:(?P<id>.+)'
3508 'url': 'ytuser:phihag',
3509 'only_matching': True,
3512 def _real_extract(self, url):
3513 user_id = self._match_id(url)
3514 return self.url_result(
3515 'https://www.youtube.com/user/%s' % user_id,
3516 ie=YoutubeTabIE.ie_key(), video_id=user_id)
3519 class YoutubeFavouritesIE(YoutubeBaseInfoExtractor):
3520 IE_NAME = 'youtube:favorites'
3521 IE_DESC = 'YouTube.com liked videos, ":ytfav" for short (requires authentication)'
3522 _VALID_URL = r':ytfav(?:ou?rite)?s?'
3523 _LOGIN_REQUIRED = True
3526 'only_matching': True,
3528 'url': ':ytfavorites',
3529 'only_matching': True,
3532 def _real_extract(self, url):
3533 return self.url_result(
3534 'https://www.youtube.com/playlist?list=LL',
3535 ie=YoutubeTabIE.ie_key())
3538 class YoutubeSearchIE(SearchInfoExtractor, YoutubeBaseInfoExtractor):
3539 IE_DESC = 'YouTube.com searches'
3540 # there doesn't appear to be a real limit, for example if you search for
3541 # 'python' you get more than 8.000.000 results
3542 _MAX_RESULTS = float('inf')
3543 IE_NAME = 'youtube:search'
3544 _SEARCH_KEY = 'ytsearch'
3545 _SEARCH_PARAMS = None
3548 def _entries(self, query, n):
3552 'clientName': 'WEB',
3553 'clientVersion': '2.20201021.03.00',
3558 if self._SEARCH_PARAMS:
3559 data['params'] = self._SEARCH_PARAMS
3561 for page_num in itertools.count(1):
3562 search = self._download_json(
3563 'https://www.youtube.com/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
3564 video_id='query "%s"' % query,
3565 note='Downloading page %s' % page_num,
3566 errnote='Unable to download API page', fatal=False,
3567 data=json.dumps(data).encode('utf8'),
3568 headers={'content-type': 'application/json'})
3571 slr_contents = try_get(
3573 (lambda x: x['contents']['twoColumnSearchResultsRenderer']['primaryContents']['sectionListRenderer']['contents'],
3574 lambda x: x['onResponseReceivedCommands'][0]['appendContinuationItemsAction']['continuationItems']),
3576 if not slr_contents:
3580 continuation_token = None
3581 # Youtube sometimes adds promoted content to searches,
3582 # changing the index location of videos and token.
3583 # So we search through all entries till we find them.
3584 for index, isr in enumerate(slr_contents):
3585 if not isr_contents:
3586 isr_contents = try_get(
3588 (lambda x: x[index]['itemSectionRenderer']['contents']),
3590 for content in isr_contents:
3591 if content.get('videoRenderer') is not None:
3596 if continuation_token is None:
3597 continuation_token = try_get(
3599 lambda x: x[index]['continuationItemRenderer']['continuationEndpoint']['continuationCommand'][
3602 if continuation_token is not None and isr_contents:
3605 if not isr_contents:
3607 for content in isr_contents:
3608 if not isinstance(content, dict):
3610 video = content.get('videoRenderer')
3611 if not isinstance(video, dict):
3613 video_id = video.get('videoId')
3616 title = try_get(video, lambda x: x['title']['runs'][0]['text'], compat_str)
3617 description = try_get(video, lambda x: x['descriptionSnippet']['runs'][0]['text'], compat_str)
3618 duration = parse_duration(try_get(video, lambda x: x['lengthText']['simpleText'], compat_str))
3619 view_count_text = try_get(video, lambda x: x['viewCountText']['simpleText'], compat_str) or ''
3620 view_count = int_or_none(self._search_regex(
3621 r'^(\d+)', re.sub(r'\s', '', view_count_text),
3622 'view count', default=None))
3623 uploader = try_get(video, lambda x: x['ownerText']['runs'][0]['text'], compat_str)
3626 '_type': 'url_transparent',
3627 'ie_key': YoutubeIE.ie_key(),
3631 'description': description,
3632 'duration': duration,
3633 'view_count': view_count,
3634 'uploader': uploader,
3638 if not continuation_token:
3640 data['continuation'] = continuation_token
3642 def _get_n_results(self, query, n):
3643 """Get a specified number of results for a query"""
3644 return self.playlist_result(self._entries(query, n), query)
3647 class YoutubeSearchDateIE(YoutubeSearchIE):
3648 IE_NAME = YoutubeSearchIE.IE_NAME + ':date'
3649 _SEARCH_KEY = 'ytsearchdate'
3650 IE_DESC = 'YouTube.com searches, newest videos first'
3651 _SEARCH_PARAMS = 'CAI%3D'
3654 class YoutubeSearchURLIE(YoutubeSearchIE):
3655 IE_DESC = 'YouTube.com search URLs'
3656 IE_NAME = YoutubeSearchIE.IE_NAME + '_url'
3657 _VALID_URL = r'https?://(?:www\.)?youtube\.com/results\?(.*?&)?(?:search_query|q)=(?:[^&]+)(?:[&]|$)'
3658 # _MAX_RESULTS = 100
3660 'url': 'https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video',
3661 'playlist_mincount': 5,
3663 'title': 'youtube-dl test video',
3666 'url': 'https://www.youtube.com/results?q=test&sp=EgQIBBgB',
3667 'only_matching': True,
3671 def _make_valid_url(cls):
3672 return cls._VALID_URL
3674 def _real_extract(self, url):
3675 qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
3676 query = (qs.get('search_query') or qs.get('q'))[0]
3677 self._SEARCH_PARAMS = qs.get('sp', ('',))[0]
3678 return self._get_n_results(query, self._MAX_RESULTS)
3681 class YoutubeFeedsInfoExtractor(YoutubeTabIE):
3683 Base class for feed extractors
3684 Subclasses must define the _FEED_NAME property.
3686 _LOGIN_REQUIRED = True
3692 return 'youtube:%s' % self._FEED_NAME
3694 def _real_initialize(self):
3697 def _real_extract(self, url):
3698 return self.url_result(
3699 'https://www.youtube.com/feed/%s' % self._FEED_NAME,
3700 ie=YoutubeTabIE.ie_key())
3703 class YoutubeWatchLaterIE(InfoExtractor):
3704 IE_NAME = 'youtube:watchlater'
3705 IE_DESC = 'Youtube watch later list, ":ytwatchlater" for short (requires authentication)'
3706 _VALID_URL = r':ytwatchlater'
3708 'url': ':ytwatchlater',
3709 'only_matching': True,
3712 def _real_extract(self, url):
3713 return self.url_result(
3714 'https://www.youtube.com/playlist?list=WL', ie=YoutubeTabIE.ie_key())
3717 class YoutubeRecommendedIE(YoutubeFeedsInfoExtractor):
3718 IE_DESC = 'YouTube.com recommended videos, ":ytrec" for short (requires authentication)'
3719 _VALID_URL = r'https?://(?:www\.)?youtube\.com/?(?:[?#]|$)|:ytrec(?:ommended)?'
3720 _FEED_NAME = 'recommended'
3723 'only_matching': True,
3725 'url': ':ytrecommended',
3726 'only_matching': True,
3728 'url': 'https://youtube.com',
3729 'only_matching': True,
3733 class YoutubeSubscriptionsIE(YoutubeFeedsInfoExtractor):
3734 IE_DESC = 'YouTube.com subscriptions feed, ":ytsubs" for short (requires authentication)'
3735 _VALID_URL = r':ytsub(?:scription)?s?'
3736 _FEED_NAME = 'subscriptions'
3739 'only_matching': True,
3741 'url': ':ytsubscriptions',
3742 'only_matching': True,
3746 class YoutubeHistoryIE(YoutubeFeedsInfoExtractor):
3747 IE_DESC = 'Youtube watch history, ":ythistory" for short (requires authentication)'
3748 _VALID_URL = r':ythistory'
3749 _FEED_NAME = 'history'
3751 'url': ':ythistory',
3752 'only_matching': True,
3756 class YoutubeTruncatedURLIE(InfoExtractor):
3757 IE_NAME = 'youtube:truncated_url'
3758 IE_DESC = False # Do not list
3759 _VALID_URL = r'''(?x
)
3761 (?
:\w
+\
.)?
[yY
][oO
][uU
][tT
][uU
][bB
][eE
](?
:-nocookie
)?\
.com
/
3764 annotation_id
=annotation_
[^
&]+|
3770 attribution_link
\?a
=[^
&]+
3776 'url': 'https://www.youtube.com/watch?annotation_id=annotation_3951667041',
3777 'only_matching': True,
3779 'url': 'https://www.youtube.com/watch?',
3780 'only_matching': True,
3782 'url': 'https://www.youtube.com/watch?x-yt-cl=84503534',
3783 'only_matching': True,
3785 'url': 'https://www.youtube.com/watch?feature=foo',
3786 'only_matching': True,
3788 'url': 'https://www.youtube.com/watch?hl=en-GB',
3789 'only_matching': True,
3791 'url': 'https://www.youtube.com/watch?t=2372',
3792 'only_matching': True,
3795 def _real_extract(self, url):
3796 raise ExtractorError(
3797 'Did you forget to quote the URL? Remember that & is a meta '
3798 'character in most shells, so you want to put the URL in quotes, '
3800 '"https://www.youtube.com/watch?feature=foo&v=BaW_jenozKc" '
3801 ' or simply youtube-dl BaW_jenozKc .',
3805 class YoutubeTruncatedIDIE(InfoExtractor):
3806 IE_NAME = 'youtube:truncated_id'
3807 IE_DESC = False # Do not list
3808 _VALID_URL = r'https?://(?:www\.)?youtube\.com/watch\?v=(?P<id>[0-9A-Za-z_-]{1,10})$'
3811 'url': 'https://www.youtube.com/watch?v=N_708QY7Ob',
3812 'only_matching': True,
3815 def _real_extract(self, url):
3816 video_id = self._match_id(url)
3817 raise ExtractorError(
3818 'Incomplete YouTube ID %s. URL %s looks truncated.' % (video_id, url),
3822 # Do Youtube show urls even exist anymore? I couldn't find any
3824 class YoutubeShowIE(YoutubeTabIE
):
3825 IE_DESC
= 'YouTube.com (multi-season) shows'
3826 _VALID_URL
= r
'https?://(?:www\.)?youtube\.com/show/(?P<id>[^?#]*)'
3827 IE_NAME
= 'youtube:show'
3829 'url': 'https://www.youtube.com/show/airdisasters',
3830 'playlist_mincount': 5,
3832 'id': 'airdisasters',
3833 'title': 'Air Disasters',
3837 def _real_extract(self
, url
):
3838 playlist_id
= self
._match
_id
(url
)
3839 return super(YoutubeShowIE
, self
)._real
_extract
(
3840 'https://www.youtube.com/show/%s/playlists' % playlist_id
)