3 from __future__
import unicode_literals
13 from .common
import InfoExtractor
, SearchInfoExtractor
14 from ..compat
import (
20 compat_urllib_parse_unquote_plus
,
21 compat_urllib_parse_urlencode
,
22 compat_urllib_parse_urlparse
,
25 from ..jsinterp
import JSInterpreter
35 # qualities, # TODO: Enable this after fixing formatSort
51 class YoutubeBaseInfoExtractor(InfoExtractor
):
52 """Provide base functions for Youtube extractors"""
53 _LOGIN_URL
= 'https://accounts.google.com/ServiceLogin'
54 _TWOFACTOR_URL
= 'https://accounts.google.com/signin/challenge'
56 _LOOKUP_URL
= 'https://accounts.google.com/_/signin/sl/lookup'
57 _CHALLENGE_URL
= 'https://accounts.google.com/_/signin/sl/challenge'
58 _TFA_URL
= 'https://accounts.google.com/_/signin/challenge?hl=en&TL={0}'
61 r
'embed|e|watch_popup|channel|c|user|playlist|watch|w|v|movies|results|shared|hashtag|'
62 r
'storefront|oops|index|account|reporthistory|t/terms|about|upload|signin|logout|'
63 r
'feed/(?:watch_later|history|subscriptions|library|trending|recommended)')
65 _NETRC_MACHINE
= 'youtube'
66 # If True it will raise an error if no login info is provided
67 _LOGIN_REQUIRED
= False
69 _PLAYLIST_ID_RE
= r
'(?:(?:PL|LL|EC|UU|FL|RD|UL|TL|PU|OLAK5uy_)[0-9A-Za-z-_]{10,}|RDMM|WL|LL|LM)'
71 def _ids_to_results(self
, ids
):
73 self
.url_result(vid_id
, 'Youtube', video_id
=vid_id
)
78 Attempt to log in to YouTube.
79 True is returned if successful or skipped.
80 False is returned if login failed.
82 If _LOGIN_REQUIRED is set and no authentication was provided, an error is raised.
84 username
, password
= self
._get
_login
_info
()
85 # No authentication to be performed
87 if self
._LOGIN
_REQUIRED
and self
._downloader
.params
.get('cookiefile') is None:
88 raise ExtractorError('No login info available, needed for using %s.' % self
.IE_NAME
, expected
=True)
89 # if self._downloader.params.get('cookiefile'): # TODO remove 'and False' later - too many people using outdated cookies and open issues, remind them.
90 # self.to_screen('[Cookies] Reminder - Make sure to always use up to date cookies!')
93 login_page
= self
._download
_webpage
(
94 self
._LOGIN
_URL
, None,
95 note
='Downloading login page',
96 errnote
='unable to fetch login page', fatal
=False)
97 if login_page
is False:
100 login_form
= self
._hidden
_inputs
(login_page
)
102 def req(url
, f_req
, note
, errnote
):
103 data
= login_form
.copy()
106 'checkConnection': 'youtube',
107 'checkedDomains': 'youtube',
109 'deviceinfo': '[null,null,null,[],null,"US",null,null,[],"GlifWebSignIn",null,[null,null,[]]]',
110 'f.req': json
.dumps(f_req
),
111 'flowName': 'GlifWebSignIn',
112 'flowEntry': 'ServiceLogin',
113 # TODO: reverse actual botguard identifier generation algo
114 'bgRequest': '["identifier",""]',
116 return self
._download
_json
(
117 url
, None, note
=note
, errnote
=errnote
,
118 transform_source
=lambda s
: re
.sub(r
'^[^[]*', '', s
),
120 data
=urlencode_postdata(data
), headers
={
121 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
122 'Google-Accounts-XSRF': 1,
126 self
._downloader
.report_warning(message
)
130 None, [], None, 'US', None, None, 2, False, True,
134 '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',
136 1, [None, None, []], None, None, None, True
141 lookup_results
= req(
142 self
._LOOKUP
_URL
, lookup_req
,
143 'Looking up account info', 'Unable to look up account info')
145 if lookup_results
is False:
148 user_hash
= try_get(lookup_results
, lambda x
: x
[0][2], compat_str
)
150 warn('Unable to extract user hash')
155 None, 1, None, [1, None, None, None, [password
, None, True]],
157 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],
158 1, [None, None, []], None, None, None, True
161 challenge_results
= req(
162 self
._CHALLENGE
_URL
, challenge_req
,
163 'Logging in', 'Unable to log in')
165 if challenge_results
is False:
168 login_res
= try_get(challenge_results
, lambda x
: x
[0][5], list)
170 login_msg
= try_get(login_res
, lambda x
: x
[5], compat_str
)
172 'Unable to login: %s' % 'Invalid password'
173 if login_msg
== 'INCORRECT_ANSWER_ENTERED' else login_msg
)
176 res
= try_get(challenge_results
, lambda x
: x
[0][-1], list)
178 warn('Unable to extract result entry')
181 login_challenge
= try_get(res
, lambda x
: x
[0][0], list)
183 challenge_str
= try_get(login_challenge
, lambda x
: x
[2], compat_str
)
184 if challenge_str
== 'TWO_STEP_VERIFICATION':
185 # SEND_SUCCESS - TFA code has been successfully sent to phone
186 # QUOTA_EXCEEDED - reached the limit of TFA codes
187 status
= try_get(login_challenge
, lambda x
: x
[5], compat_str
)
188 if status
== 'QUOTA_EXCEEDED':
189 warn('Exceeded the limit of TFA codes, try later')
192 tl
= try_get(challenge_results
, lambda x
: x
[1][2], compat_str
)
194 warn('Unable to extract TL')
197 tfa_code
= self
._get
_tfa
_info
('2-step verification code')
201 'Two-factor authentication required. Provide it either interactively or with --twofactor <code>'
202 '(Note that only TOTP (Google Authenticator App) codes work at this time.)')
205 tfa_code
= remove_start(tfa_code
, 'G-')
208 user_hash
, None, 2, None,
210 9, None, None, None, None, None, None, None,
211 [None, tfa_code
, True, 2]
215 self
._TFA
_URL
.format(tl
), tfa_req
,
216 'Submitting TFA code', 'Unable to submit TFA code')
218 if tfa_results
is False:
221 tfa_res
= try_get(tfa_results
, lambda x
: x
[0][5], list)
223 tfa_msg
= try_get(tfa_res
, lambda x
: x
[5], compat_str
)
225 'Unable to finish TFA: %s' % 'Invalid TFA code'
226 if tfa_msg
== 'INCORRECT_ANSWER_ENTERED' else tfa_msg
)
229 check_cookie_url
= try_get(
230 tfa_results
, lambda x
: x
[0][-1][2], compat_str
)
233 'LOGIN_CHALLENGE': "This device isn't recognized. For your security, Google wants to make sure it's really you.",
234 'USERNAME_RECOVERY': 'Please provide additional information to aid in the recovery process.',
235 'REAUTH': "There is something unusual about your activity. For your security, Google wants to make sure it's really you.",
237 challenge
= CHALLENGES
.get(
239 '%s returned error %s.' % (self
.IE_NAME
, challenge_str
))
240 warn('%s\nGo to https://accounts.google.com/, login and solve a challenge.' % challenge
)
243 check_cookie_url
= try_get(res
, lambda x
: x
[2], compat_str
)
245 if not check_cookie_url
:
246 warn('Unable to extract CheckCookie URL')
249 check_cookie_results
= self
._download
_webpage
(
250 check_cookie_url
, None, 'Checking cookie', fatal
=False)
252 if check_cookie_results
is False:
255 if 'https://myaccount.google.com/' not in check_cookie_results
:
256 warn('Unable to log in')
261 def _download_webpage_handle(self
, *args
, **kwargs
):
262 query
= kwargs
.get('query', {}).copy()
263 kwargs
['query'] = query
264 return super(YoutubeBaseInfoExtractor
, self
)._download
_webpage
_handle
(
265 *args
, **compat_kwargs(kwargs
))
267 def _real_initialize(self
):
268 if self
._downloader
is None:
270 if not self
._login
():
273 _DEFAULT_API_DATA
= {
277 'clientVersion': '2.20201021.03.00',
282 _YT_INITIAL_DATA_RE
= r
'(?:window\s*\[\s*["\']ytInitialData
["\']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;'
283 _YT_INITIAL_PLAYER_RESPONSE_RE = r'ytInitialPlayerResponse\s*=\s*({.+?})\s*;'
284 _YT_INITIAL_BOUNDARY_RE = r'(?:var\s+meta|</script|\n)'
286 def _call_api(self, ep, query, video_id, fatal=True):
287 data = self._DEFAULT_API_DATA.copy()
290 return self._download_json(
291 'https://www.youtube.com/youtubei/v1/%s' % ep, video_id=video_id,
292 note='Downloading API JSON', errnote='Unable to download API page',
293 data=json.dumps(data).encode('utf8'), fatal=fatal,
294 headers={'content-type': 'application/json'},
295 query={'key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'})
297 def _extract_yt_initial_data(self, video_id, webpage):
298 return self._parse_json(
300 (r'%s\s*%s' % (self._YT_INITIAL_DATA_RE, self._YT_INITIAL_BOUNDARY_RE),
301 self._YT_INITIAL_DATA_RE), webpage, 'yt initial data'),
304 def _extract_ytcfg(self, video_id, webpage):
305 return self._parse_json(
307 r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', webpage, 'ytcfg',
308 default='{}'), video_id, fatal=False)
310 def _extract_video(self, renderer):
311 video_id = renderer.get('videoId')
314 (lambda x: x['title']['runs'][0]['text'],
315 lambda x: x['title']['simpleText']), compat_str)
316 description = try_get(
317 renderer, lambda x: x['descriptionSnippet']['runs'][0]['text'],
319 duration = parse_duration(try_get(
320 renderer, lambda x: x['lengthText']['simpleText'], compat_str))
321 view_count_text = try_get(
322 renderer, lambda x: x['viewCountText']['simpleText'], compat_str) or ''
323 view_count = str_to_int(self._search_regex(
324 r'^([\d,]+)', re.sub(r'\s', '', view_count_text),
325 'view count', default=None))
327 renderer, lambda x: x['ownerText']['runs'][0]['text'], compat_str)
329 '_type': 'url_transparent',
330 'ie_key': YoutubeIE.ie_key(),
334 'description': description,
335 'duration': duration,
336 'view_count': view_count,
337 'uploader': uploader,
341 class YoutubeIE(YoutubeBaseInfoExtractor):
342 IE_DESC = 'YouTube.com'
343 _VALID_URL = r"""(?x)^
345 (?:https?://|//) # http(s):// or protocol-independent URL
346 (?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie|kids)?\.com/|
347 (?:www\.)?deturl\.com/www\.youtube\.com/|
348 (?:www\.)?pwnyoutube\.com/|
349 (?:www\.)?hooktube\.com/|
350 (?:www\.)?yourepeat\.com/|
351 tube\.majestyc\.net/|
352 # Invidious instances taken from https://github.com/omarroth/invidious/wiki/Invidious-Instances
353 (?:www\.)?invidious\.pussthecat\.org/|
354 (?:www\.)?invidious\.048596\.xyz/|
355 (?:www\.)?invidious\.zee\.li/|
356 (?:www\.)?vid\.puffyan\.us/|
357 (?:(?:www|au)\.)?ytprivate\.com/|
358 (?:www\.)?invidious\.namazso\.eu/|
359 (?:www\.)?invidious\.ethibox\.fr/|
360 (?:www\.)?inv\.skyn3t\.in/|
361 (?:www\.)?invidious\.himiko\.cloud/|
362 (?:www\.)?w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd\.onion/|
363 (?:www\.)?kbjggqkzv65ivcqj6bumvp337z6264huv5kpkwuv6gu5yjiskvan7fad\.onion/|
364 (?:www\.)?invidious\.3o7z6yfxhbw7n3za4rss6l434kmv55cgw2vuziwuigpwegswvwzqipyd\.onion/|
365 (?:www\.)?grwp24hodrefzvjjuccrkw3mjq4tzhaaq32amf33dzpmuxe7ilepcmad\.onion/|
366 (?:(?:www|dev)\.)?invidio\.us/|
367 (?:(?:www|no)\.)?invidiou\.sh/|
368 (?:(?:www|fi)\.)?invidious\.snopyta\.org/|
369 (?:www\.)?invidious\.kabi\.tk/|
370 (?:www\.)?invidious\.13ad\.de/|
371 (?:www\.)?invidious\.mastodon\.host/|
372 (?:www\.)?invidious\.zapashcanon\.fr/|
373 (?:www\.)?invidious\.kavin\.rocks/|
374 (?:www\.)?invidious\.tube/|
375 (?:www\.)?invidiou\.site/|
376 (?:www\.)?invidious\.site/|
377 (?:www\.)?invidious\.xyz/|
378 (?:www\.)?invidious\.nixnet\.xyz/|
379 (?:www\.)?invidious\.drycat\.fr/|
380 (?:www\.)?tube\.poal\.co/|
381 (?:www\.)?tube\.connect\.cafe/|
382 (?:www\.)?vid\.wxzm\.sx/|
383 (?:www\.)?vid\.mint\.lgbt/|
384 (?:www\.)?yewtu\.be/|
385 (?:www\.)?yt\.elukerio\.org/|
386 (?:www\.)?yt\.lelux\.fi/|
387 (?:www\.)?invidious\.ggc-project\.de/|
388 (?:www\.)?yt\.maisputain\.ovh/|
389 (?:www\.)?invidious\.toot\.koeln/|
390 (?:www\.)?invidious\.fdn\.fr/|
391 (?:www\.)?watch\.nettohikari\.com/|
392 (?:www\.)?kgg2m7yk5aybusll\.onion/|
393 (?:www\.)?qklhadlycap4cnod\.onion/|
394 (?:www\.)?axqzx4s6s54s32yentfqojs3x5i7faxza6xo3ehd4bzzsg2ii4fv2iid\.onion/|
395 (?:www\.)?c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid\.onion/|
396 (?:www\.)?fz253lmuao3strwbfbmx46yu7acac2jz27iwtorgmbqlkurlclmancad\.onion/|
397 (?:www\.)?invidious\.l4qlywnpwqsluw65ts7md3khrivpirse744un3x7mlskqauz5pyuzgqd\.onion/|
398 (?:www\.)?owxfohz4kjyv25fvlqilyxast7inivgiktls3th44jhk3ej3i7ya\.b32\.i2p/|
399 (?:www\.)?4l2dgddgsrkf2ous66i6seeyi6etzfgrue332grh2n7madpwopotugyd\.onion/|
400 youtube\.googleapis\.com/) # the various hostnames, with wildcard subdomains
401 (?:.*?\#/)? # handle anchor (#/) redirect urls
402 (?: # the various things that can precede the ID:
403 (?:(?:v|embed|e)/(?!videoseries)) # v/ or embed/ or e/
404 |(?: # or the v= param in all its forms
405 (?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
406 (?:\?|\#!?) # the params delimiter ? or # or #!
407 (?:.*?[&;])?? # any other preceding param (like /?s=tuff&v=xxxx or ?s=tuff&v=V36LpHqtcDY)
412 youtu\.be| # just youtu.be/xxxx
413 vid\.plus| # or vid.plus/xxxx
414 zwearz\.com/watch| # or zwearz.com/watch/xxxx
416 |(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=
418 )? # all until now is optional -> you can pass the naked ID
419 (?P<id>[0-9A-Za-z_-]{11}) # here is it! the YouTube video ID
422 %(playlist_id)s| # combined list/video URLs are handled by the playlist IE
423 WL # WL are handled by the watch later IE
426 (?(1).+)? # if we found the ID, everything can follow
427 $""" % {'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE}
429 r'/s/player/(?P<id>[a-zA-Z0-9_-]{8,})/player',
430 r'/(?P<id>[a-zA-Z0-9_-]{8,})/player(?:_ias\.vflset(?:/[a-zA-Z]{2,3}_[a-zA-Z]{2,3})?|-plasma-ias-(?:phone|tablet)-[a-z]{2}_[A-Z]{2}\.vflset)/base\.js$',
431 r'\b(?P<id>vfl[a-zA-Z0-9_-]+)\b.*?\.js$',
434 '5': {'ext': 'flv', 'width': 400, 'height': 240, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
435 '6': {'ext': 'flv', 'width': 450, 'height': 270, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
436 '13': {'ext': '3gp', 'acodec': 'aac', 'vcodec': 'mp4v'},
437 '17': {'ext': '3gp', 'width': 176, 'height': 144, 'acodec': 'aac', 'abr': 24, 'vcodec': 'mp4v'},
438 '18': {'ext': 'mp4', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 96, 'vcodec': 'h264'},
439 '22': {'ext': 'mp4', 'width': 1280, 'height': 720, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
440 '34': {'ext': 'flv', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
441 '35': {'ext': 'flv', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
442 # itag 36 videos are either 320x180 (BaW_jenozKc) or 320x240 (__2ABJjxzNo), abr varies as well
443 '36': {'ext': '3gp', 'width': 320, 'acodec': 'aac', 'vcodec': 'mp4v'},
444 '37': {'ext': 'mp4', 'width': 1920, 'height': 1080, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
445 '38': {'ext': 'mp4', 'width': 4096, 'height': 3072, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
446 '43': {'ext': 'webm', 'width': 640, 'height': 360, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
447 '44': {'ext': 'webm', 'width': 854, 'height': 480, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
448 '45': {'ext': 'webm', 'width': 1280, 'height': 720, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
449 '46': {'ext': 'webm', 'width': 1920, 'height': 1080, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
450 '59': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
451 '78': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
455 '82': {'ext': 'mp4', 'height': 360, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
456 '83': {'ext': 'mp4', 'height': 480, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
457 '84': {'ext': 'mp4', 'height': 720, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
458 '85': {'ext': 'mp4', 'height': 1080, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
459 '100': {'ext': 'webm', 'height': 360, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8', 'preference': -20},
460 '101': {'ext': 'webm', 'height': 480, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
461 '102': {'ext': 'webm', 'height': 720, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
463 # Apple HTTP Live Streaming
464 '91': {'ext': 'mp4', 'height': 144, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
465 '92': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
466 '93': {'ext': 'mp4', 'height': 360, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
467 '94': {'ext': 'mp4', 'height': 480, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
468 '95': {'ext': 'mp4', 'height': 720, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
469 '96': {'ext': 'mp4', 'height': 1080, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
470 '132': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
471 '151': {'ext': 'mp4', 'height': 72, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 24, 'vcodec': 'h264', 'preference': -10},
474 '133': {'ext': 'mp4', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'h264'},
475 '134': {'ext': 'mp4', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'h264'},
476 '135': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
477 '136': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264'},
478 '137': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264'},
479 '138': {'ext': 'mp4', 'format_note': 'DASH video', 'vcodec': 'h264'}, # Height can vary (https://github.com/ytdl-org/youtube-dl/issues/4559)
480 '160': {'ext': 'mp4', 'height': 144, 'format_note': 'DASH video', 'vcodec': 'h264'},
481 '212': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
482 '264': {'ext': 'mp4', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'h264'},
483 '298': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
484 '299': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
485 '266': {'ext': 'mp4', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'h264'},
488 '139': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 48, 'container': 'm4a_dash'},
489 '140': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 128, 'container': 'm4a_dash'},
490 '141': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 256, 'container': 'm4a_dash'},
491 '256': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
492 '258': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
493 '325': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'dtse', 'container': 'm4a_dash'},
494 '328': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'ec-3', 'container': 'm4a_dash'},
497 '167': {'ext': 'webm', 'height': 360, 'width': 640, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
498 '168': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
499 '169': {'ext': 'webm', 'height': 720, 'width': 1280, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
500 '170': {'ext': 'webm', 'height': 1080, 'width': 1920, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
501 '218': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
502 '219': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
503 '278': {'ext': 'webm', 'height': 144, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp9'},
504 '242': {'ext': 'webm', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'vp9'},
505 '243': {'ext': 'webm', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'vp9'},
506 '244': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
507 '245': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
508 '246': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
509 '247': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9'},
510 '248': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9'},
511 '271': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9'},
512 # itag 272 videos are either 3840x2160 (e.g. RtoitU2A-3E) or 7680x4320 (sLprVF6d7Ug)
513 '272': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
514 '302': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
515 '303': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
516 '308': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
517 '313': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
518 '315': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
521 '171': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 128},
522 '172': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 256},
524 # Dash webm audio with opus inside
525 '249': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 50},
526 '250': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 70},
527 '251': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 160},
530 '_rtmp': {'protocol': 'rtmp'},
532 # av01 video only formats sometimes served with "unknown
" codecs
533 '394': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
534 '395': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
535 '396': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
536 '397': {'acodec': 'none', 'vcodec': 'av01.0.05M.08'},
538 _SUBTITLE_FORMATS = ('json3', 'srv1', 'srv2', 'srv3', 'ttml', 'vtt')
545 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&t=1s&end=9',
549 'title': 'youtube-dl test video "\'/\\ä↭𝕐
',
550 'uploader
': 'Philipp Hagemeister
',
551 'uploader_id
': 'phihag
',
552 'uploader_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/user
/phihag
',
553 'channel_id
': 'UCLqxVugv74EIW3VWh2NOa3Q
',
554 'channel_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/channel
/UCLqxVugv74EIW3VWh2NOa3Q
',
555 'upload_date
': '20121002',
556 '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 .',
557 'categories': ['Science & Technology'],
558 'tags': ['youtube-dl'],
562 'dislike_count': int,
568 'url': '//www.YouTube.com/watch?v=yZIXLfi8CZQ',
569 'note': 'Embed-only video (#1746)',
573 'upload_date': '20120608',
574 'title': 'Principal Sexually Assaults A Teacher - Episode 117 - 8th June 2012',
575 'description': 'md5:09b78bd971f1e3e289601dfba15ca4f7',
576 'uploader': 'SET India',
577 'uploader_id': 'setindia',
578 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/setindia',
581 'skip': 'Private video',
584 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&v=yZIXLfi8CZQ',
585 'note': 'Use the first video ID in the URL',
589 'title': 'youtube-dl test video "\'/\\ä↭𝕐
',
590 'uploader
': 'Philipp Hagemeister
',
591 'uploader_id
': 'phihag
',
592 'uploader_url
': r're
:https?
://(?
:www\
.)?youtube\
.com
/user
/phihag
',
593 'upload_date
': '20121002',
594 '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 .',
595 'categories': ['Science & Technology'],
596 'tags': ['youtube-dl'],
600 'dislike_count': int,
603 'skip_download': True,
607 'url': 'https://www.youtube.com/watch?v=a9LDPn-MO4I',
608 'note': '256k DASH audio (format 141) via DASH manifest',
612 'upload_date': '20121002',
613 'uploader_id': '8KVIDEO',
614 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/8KVIDEO',
616 'uploader': '8KVIDEO',
617 'title': 'UHDTV TEST 8K VIDEO.mp4'
620 'youtube_include_dash_manifest': True,
623 'skip': 'format 141 not served anymore',
625 # DASH manifest with encrypted signature
627 'url': 'https://www.youtube.com/watch?v=IB3lcPjvWLA',
631 'title': 'Afrojack, Spree Wilson - The Spark (Official Music Video) ft. Spree Wilson',
632 'description': 'md5:8f5e2b82460520b619ccac1f509d43bf',
634 'uploader': 'AfrojackVEVO',
635 'uploader_id': 'AfrojackVEVO',
636 'upload_date': '20131011',
640 'youtube_include_dash_manifest': True,
641 'format': '141/bestaudio[ext=m4a]',
646 'url': 'https://www.youtube.com/watch?v=T4XJQO3qol8',
651 'upload_date': '20100909',
652 'uploader': 'Amazing Atheist',
653 'uploader_id': 'TheAmazingAtheist',
654 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/TheAmazingAtheist',
655 'title': 'Burning Everyone\'s Koran',
656 'description': 'SUBSCRIBE: http://www.youtube.com/saturninefilms \r\n\r\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',
659 # Normal age-gate video (embed allowed)
661 'url': 'https://youtube.com/watch?v=HtVdAasjOgU',
665 'title': 'The Witcher 3: Wild Hunt - The Sword Of Destiny Trailer',
666 'description': r're:(?s).{100,}About the Game\n.*?The Witcher 3: Wild Hunt.{100,}',
668 'uploader': 'The Witcher',
669 'uploader_id': 'WitcherGame',
670 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/WitcherGame',
671 'upload_date': '20140605',
675 # video_info is None (https://github.com/ytdl-org/youtube-dl/issues/4421)
676 # YouTube Red ad is not captured for creator
678 'url': '__2ABJjxzNo',
683 'upload_date': '20100430',
684 'uploader_id': 'deadmau5',
685 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/deadmau5',
686 'creator': 'deadmau5',
687 'description': 'md5:6cbcd3a92ce1bc676fc4d6ab4ace2336',
688 'uploader': 'deadmau5',
689 'title': 'Deadmau5 - Some Chords (HD)',
690 'alt_title': 'Some Chords',
692 'expected_warnings': [
693 'DASH manifest missing',
696 # Olympics (https://github.com/ytdl-org/youtube-dl/issues/4431)
698 'url': 'lqQg6PlCWgI',
703 'upload_date': '20150827',
704 'uploader_id': 'olympic',
705 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/olympic',
706 'description': 'HO09 - Women - GER-AUS - Hockey - 31 July 2012 - London 2012 Olympic Games',
707 'uploader': 'Olympic',
708 'title': 'Hockey - Women - GER-AUS - London 2012 Olympic Games',
711 'skip_download': 'requires avconv',
716 'url': 'https://www.youtube.com/watch?v=_b-2C3KPAM0',
720 'stretched_ratio': 16 / 9.,
722 'upload_date': '20110310',
723 'uploader_id': 'AllenMeow',
724 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/AllenMeow',
725 'description': 'made by Wacom from Korea | 字幕&加油添醋 by TY\'s Allen | 感謝heylisa00cavey1001同學熱情提供梗及翻譯',
727 'title': '[A-made] 變態妍字幕版 太妍 我就是這樣的人',
730 # url_encoded_fmt_stream_map is empty string
732 'url': 'qEJwOuvDf7I',
736 'title': 'Обсуждение судебной практики по выборам 14 сентября 2014 года в Санкт-Петербурге',
738 'upload_date': '20150404',
739 'uploader_id': 'spbelect',
740 'uploader': 'Наблюдатели Петербурга',
743 'skip_download': 'requires avconv',
745 'skip': 'This live event has ended.',
747 # Extraction from multiple DASH manifests (https://github.com/ytdl-org/youtube-dl/pull/6097)
749 'url': 'https://www.youtube.com/watch?v=FIl7x6_3R5Y',
753 'title': 'md5:7b81415841e02ecd4313668cde88737a',
754 'description': 'md5:116377fd2963b81ec4ce64b542173306',
756 'upload_date': '20150625',
757 'uploader_id': 'dorappi2000',
758 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/dorappi2000',
759 'uploader': 'dorappi2000',
760 'formats': 'mincount:31',
762 'skip': 'not actual anymore',
764 # DASH manifest with segment_list
766 'url': 'https://www.youtube.com/embed/CsmdDsKjzN8',
767 'md5': '8ce563a1d667b599d21064e982ab9e31',
771 'upload_date': '20150501', # According to '<meta itemprop="datePublished
"', but in other places it's 20150510
772 'uploader': 'Airtek',
773 'description': 'Retransmisión en directo de la XVIII media maratón de Zaragoza.',
774 'uploader_id': 'UCzTzUmjXxxacNnL8I3m4LnQ',
775 'title': 'Retransmisión XVIII Media maratón Zaragoza 2015',
778 'youtube_include_dash_manifest': True,
779 'format': '135', # bestvideo
781 'skip': 'This live event has ended.',
784 # Multifeed videos (multiple cameras), URL is for Main Camera
785 'url': 'https://www.youtube.com/watch?v=jvGDaLqkpTg',
788 'title': 'Tom Clancy Free Weekend Rainbow Whatever',
789 'description': 'md5:e03b909557865076822aa169218d6a5d',
795 'title': 'Tom Clancy Free Weekend Rainbow Whatever (Main Camera)',
796 'description': 'md5:e03b909557865076822aa169218d6a5d',
798 'upload_date': '20161111',
799 'uploader': 'Team PGP',
800 'uploader_id': 'UChORY56LMMETTuGjXaJXvLg',
801 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UChORY56LMMETTuGjXaJXvLg',
807 'title': 'Tom Clancy Free Weekend Rainbow Whatever (Camera 2)',
808 'description': 'md5:e03b909557865076822aa169218d6a5d',
810 'upload_date': '20161111',
811 'uploader': 'Team PGP',
812 'uploader_id': 'UChORY56LMMETTuGjXaJXvLg',
813 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UChORY56LMMETTuGjXaJXvLg',
819 'title': 'Tom Clancy Free Weekend Rainbow Whatever (Camera 3)',
820 'description': 'md5:e03b909557865076822aa169218d6a5d',
822 'upload_date': '20161111',
823 'uploader': 'Team PGP',
824 'uploader_id': 'UChORY56LMMETTuGjXaJXvLg',
825 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UChORY56LMMETTuGjXaJXvLg',
831 'title': 'Tom Clancy Free Weekend Rainbow Whatever (Camera 4)',
832 'description': 'md5:e03b909557865076822aa169218d6a5d',
834 'upload_date': '20161111',
835 'uploader': 'Team PGP',
836 'uploader_id': 'UChORY56LMMETTuGjXaJXvLg',
837 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UChORY56LMMETTuGjXaJXvLg',
841 'skip_download': True,
845 # Multifeed video with comma in title (see https://github.com/ytdl-org/youtube-dl/issues/8536)
846 'url': 'https://www.youtube.com/watch?v=gVfLd0zydlo',
849 'title': 'DevConf.cz 2016 Day 2 Workshops 1 14:00 - 15:30',
852 'skip': 'Not multifeed anymore',
855 'url': 'https://vid.plus/FlRa-iH7PGw',
856 'only_matching': True,
859 'url': 'https://zwearz.com/watch/9lWxNJF-ufM/electra-woman-dyna-girl-official-trailer-grace-helbig.html',
860 'only_matching': True,
863 # Title with JS-like syntax "};" (see https://github.com/ytdl-org/youtube-dl/issues/7468)
864 # Also tests cut-off URL expansion in video description (see
865 # https://github.com/ytdl-org/youtube-dl/issues/1892,
866 # https://github.com/ytdl-org/youtube-dl/issues/8164)
867 'url': 'https://www.youtube.com/watch?v=lsguqyKfVQg',
871 'title': '{dark walk}; Loki/AC/Dishonored; collab w/Elflover21',
872 'alt_title': 'Dark Walk - Position Music',
873 'description': 'md5:8085699c11dc3f597ce0410b0dcbb34a',
875 'upload_date': '20151119',
876 'uploader_id': 'IronSoulElf',
877 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/IronSoulElf',
878 'uploader': 'IronSoulElf',
879 'creator': 'Todd Haberman, Daniel Law Heath and Aaron Kaplan',
880 'track': 'Dark Walk - Position Music',
881 'artist': 'Todd Haberman, Daniel Law Heath and Aaron Kaplan',
882 'album': 'Position Music - Production Music Vol. 143 - Dark Walk',
885 'skip_download': True,
889 # Tags with '};' (see https://github.com/ytdl-org/youtube-dl/issues/7468)
890 'url': 'https://www.youtube.com/watch?v=Ms7iBXnlUO8',
891 'only_matching': True,
894 # Video with yt:stretch=17:0
895 'url': 'https://www.youtube.com/watch?v=Q39EVAstoRM',
899 'title': 'Clash Of Clans#14 Dicas De Ataque Para CV 4',
900 'description': 'md5:ee18a25c350637c8faff806845bddee9',
901 'upload_date': '20151107',
902 'uploader_id': 'UCCr7TALkRbo3EtFzETQF1LA',
903 'uploader': 'CH GAMER DROID',
906 'skip_download': True,
908 'skip': 'This video does not exist.',
911 # Video licensed under Creative Commons
912 'url': 'https://www.youtube.com/watch?v=M4gD1WSo5mA',
916 'title': 'md5:e41008789470fc2533a3252216f1c1d1',
917 'description': 'md5:a677553cf0840649b731a3024aeff4cc',
919 'upload_date': '20150127',
920 'uploader_id': 'BerkmanCenter',
921 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/BerkmanCenter',
922 'uploader': 'The Berkman Klein Center for Internet & Society',
923 'license': 'Creative Commons Attribution license (reuse allowed)',
926 'skip_download': True,
930 # Channel-like uploader_url
931 'url': 'https://www.youtube.com/watch?v=eQcmzGIKrzg',
935 'title': 'Democratic Socialism and Foreign Policy | Bernie Sanders',
936 'description': 'md5:13a2503d7b5904ef4b223aa101628f39',
938 'upload_date': '20151119',
939 'uploader': 'Bernie Sanders',
940 'uploader_id': 'UCH1dpzjCEiGAt8CXkryhkZg',
941 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCH1dpzjCEiGAt8CXkryhkZg',
942 'license': 'Creative Commons Attribution license (reuse allowed)',
945 'skip_download': True,
949 'url': 'https://www.youtube.com/watch?feature=player_embedded&amp;v=V36LpHqtcDY',
950 'only_matching': True,
953 # YouTube Red paid video (https://github.com/ytdl-org/youtube-dl/issues/10059)
954 'url': 'https://www.youtube.com/watch?v=i1Ko8UG-Tdo',
955 'only_matching': True,
958 # Rental video preview
959 'url': 'https://www.youtube.com/watch?v=yYr8q0y5Jfg',
963 'title': 'Piku - Trailer',
964 'description': 'md5:c36bd60c3fd6f1954086c083c72092eb',
965 'upload_date': '20150811',
966 'uploader': 'FlixMatrix',
967 'uploader_id': 'FlixMatrixKaravan',
968 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/FlixMatrixKaravan',
969 'license': 'Standard YouTube License',
972 'skip_download': True,
974 'skip': 'This video is not available.',
977 # YouTube Red video with episode data
978 'url': 'https://www.youtube.com/watch?v=iqKdEhx-dD4',
982 'title': 'Isolation - Mind Field (Ep 1)',
983 'description': 'md5:f540112edec5d09fc8cc752d3d4ba3cd',
985 'upload_date': '20170118',
986 'uploader': 'Vsauce',
987 'uploader_id': 'Vsauce',
988 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Vsauce',
989 'series': 'Mind Field',
994 'skip_download': True,
996 'expected_warnings': [
997 'Skipping DASH manifest',
1001 # The following content has been identified by the YouTube community
1002 # as inappropriate or offensive to some audiences.
1003 'url': 'https://www.youtube.com/watch?v=6SJNVb0GnPI',
1005 'id': '6SJNVb0GnPI',
1007 'title': 'Race Differences in Intelligence',
1008 'description': 'md5:5d161533167390427a1f8ee89a1fc6f1',
1010 'upload_date': '20140124',
1011 'uploader': 'New Century Foundation',
1012 'uploader_id': 'UCEJYpZGqgUob0zVVEaLhvVg',
1013 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCEJYpZGqgUob0zVVEaLhvVg',
1016 'skip_download': True,
1018 'skip': 'This video has been removed for violating YouTube\'s policy on hate speech.',
1022 'url': '1t24XAntNCY',
1023 'only_matching': True,
1026 # geo restricted to JP
1027 'url': 'sJL6WA-aGkQ',
1028 'only_matching': True,
1031 'url': 'https://invidio.us/watch?v=BaW_jenozKc',
1032 'only_matching': True,
1036 'url': 'https://www.youtube.com/watch?v=s7_qI6_mIXc',
1037 'only_matching': True,
1040 # Video with unsupported adaptive stream type formats
1041 'url': 'https://www.youtube.com/watch?v=Z4Vy8R84T1U',
1043 'id': 'Z4Vy8R84T1U',
1045 'title': 'saman SMAN 53 Jakarta(Sancety) opening COFFEE4th at SMAN 53 Jakarta',
1046 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
1048 'upload_date': '20130923',
1049 'uploader': 'Amelia Putri Harwita',
1050 'uploader_id': 'UCpOxM49HJxmC1qCalXyB3_Q',
1051 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCpOxM49HJxmC1qCalXyB3_Q',
1052 'formats': 'maxcount:10',
1055 'skip_download': True,
1056 'youtube_include_dash_manifest': False,
1058 'skip': 'not actual anymore',
1061 # Youtube Music Auto-generated description
1062 'url': 'https://music.youtube.com/watch?v=MgNrAu2pzNs',
1064 'id': 'MgNrAu2pzNs',
1066 'title': 'Voyeur Girl',
1067 'description': 'md5:7ae382a65843d6df2685993e90a8628f',
1068 'upload_date': '20190312',
1069 'uploader': 'Stephen - Topic',
1070 'uploader_id': 'UC-pWHpBjdGG69N9mM2auIAA',
1071 'artist': 'Stephen',
1072 'track': 'Voyeur Girl',
1073 'album': 'it\'s too much love to know my dear',
1074 'release_date': '20190313',
1075 'release_year': 2019,
1078 'skip_download': True,
1082 'url': 'https://www.youtubekids.com/watch?v=3b8nCWDgZ6Q',
1083 'only_matching': True,
1086 # invalid -> valid video id redirection
1087 'url': 'DJztXj2GPfl',
1089 'id': 'DJztXj2GPfk',
1091 'title': 'Panjabi MC - Mundian To Bach Ke (The Dictator Soundtrack)',
1092 'description': 'md5:bf577a41da97918e94fa9798d9228825',
1093 'upload_date': '20090125',
1094 'uploader': 'Prochorowka',
1095 'uploader_id': 'Prochorowka',
1096 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Prochorowka',
1097 'artist': 'Panjabi MC',
1098 'track': 'Beware of the Boys (Mundian to Bach Ke) - Motivo Hi-Lectro Remix',
1099 'album': 'Beware of the Boys (Mundian To Bach Ke)',
1102 'skip_download': True,
1104 'skip': 'Video unavailable',
1107 # empty description results in an empty string
1108 'url': 'https://www.youtube.com/watch?v=x41yOUIvK2k',
1110 'id': 'x41yOUIvK2k',
1112 'title': 'IMG 3456',
1114 'upload_date': '20170613',
1115 'uploader_id': 'ElevageOrVert',
1116 'uploader': 'ElevageOrVert',
1119 'skip_download': True,
1123 # with '};' inside yt initial data (see [1])
1124 # see [2] for an example with '};' inside ytInitialPlayerResponse
1125 # 1. https://github.com/ytdl-org/youtube-dl/issues/27093
1126 # 2. https://github.com/ytdl-org/youtube-dl/issues/27216
1127 'url': 'https://www.youtube.com/watch?v=CHqg6qOn4no',
1129 'id': 'CHqg6qOn4no',
1131 'title': 'Part 77 Sort a list of simple types in c#',
1132 'description': 'md5:b8746fa52e10cdbf47997903f13b20dc',
1133 'upload_date': '20130831',
1134 'uploader_id': 'kudvenkat',
1135 'uploader': 'kudvenkat',
1138 'skip_download': True,
1142 # another example of '};' in ytInitialData
1143 'url': 'https://www.youtube.com/watch?v=gVfgbahppCY',
1144 'only_matching': True,
1147 'url': 'https://www.youtube.com/watch_popup?v=63RmMXCd_bQ',
1148 'only_matching': True,
1151 # https://github.com/ytdl-org/youtube-dl/pull/28094
1152 'url': 'OtqTfy26tG0',
1154 'id': 'OtqTfy26tG0',
1156 'title': 'Burn Out',
1157 'description': 'md5:8d07b84dcbcbfb34bc12a56d968b6131',
1158 'upload_date': '20141120',
1159 'uploader': 'The Cinematic Orchestra - Topic',
1160 'uploader_id': 'UCIzsJBIyo8hhpFm1NK0uLgw',
1161 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCIzsJBIyo8hhpFm1NK0uLgw',
1162 'artist': 'The Cinematic Orchestra',
1163 'track': 'Burn Out',
1164 'album': 'Every Day',
1165 'release_data': None,
1166 'release_year': None,
1169 'skip_download': True,
1174 def __init__(self, *args, **kwargs):
1175 super(YoutubeIE, self).__init__(*args, **kwargs)
1176 self._code_cache = {}
1177 self._player_cache = {}
1179 def _signature_cache_id(self, example_sig):
1180 """ Return a string representation of a signature """
1181 return '.'.join(compat_str(len(part)) for part in example_sig.split('.'))
1184 def _extract_player_info(cls, player_url):
1185 for player_re in cls._PLAYER_INFO_RE:
1186 id_m = re.search(player_re, player_url)
1190 raise ExtractorError('Cannot identify player %r' % player_url)
1191 return id_m.group('id')
1193 def _extract_signature_function(self, video_id, player_url, example_sig):
1194 player_id = self._extract_player_info(player_url)
1196 # Read from filesystem cache
1197 func_id = 'js_%s_%s' % (
1198 player_id, self._signature_cache_id(example_sig))
1199 assert os.path.basename(func_id) == func_id
1201 cache_spec = self._downloader.cache.load('youtube-sigfuncs', func_id)
1202 if cache_spec is not None:
1203 return lambda s: ''.join(s[i] for i in cache_spec)
1205 if player_id not in self._code_cache:
1206 self._code_cache[player_id] = self._download_webpage(
1207 player_url, video_id,
1208 note='Downloading player ' + player_id,
1209 errnote='Download of %s failed' % player_url)
1210 code = self._code_cache[player_id]
1211 res = self._parse_sig_js(code)
1213 test_string = ''.join(map(compat_chr, range(len(example_sig))))
1214 cache_res = res(test_string)
1215 cache_spec = [ord(c) for c in cache_res]
1217 self._downloader.cache.store('youtube-sigfuncs', func_id, cache_spec)
1220 def _print_sig_code(self, func, example_sig):
1221 def gen_sig_code(idxs):
1222 def _genslice(start, end, step):
1223 starts = '' if start == 0 else str(start)
1224 ends = (':%d' % (end + step)) if end + step >= 0 else ':'
1225 steps = '' if step == 1 else (':%d' % step)
1226 return 's[%s%s%s]' % (starts, ends, steps)
1229 # Quelch pyflakes warnings - start will be set when step is set
1230 start = '(Never used)'
1231 for i, prev in zip(idxs[1:], idxs[:-1]):
1232 if step is not None:
1233 if i - prev == step:
1235 yield _genslice(start, prev, step)
1238 if i - prev in [-1, 1]:
1243 yield 's[%d]' % prev
1247 yield _genslice(start, i, step)
1249 test_string = ''.join(map(compat_chr, range(len(example_sig))))
1250 cache_res = func(test_string)
1251 cache_spec = [ord(c) for c in cache_res]
1252 expr_code = ' + '.join(gen_sig_code(cache_spec))
1253 signature_id_tuple = '(%s)' % (
1254 ', '.join(compat_str(len(p)) for p in example_sig.split('.')))
1255 code = ('if tuple(len(p) for p in s.split(\'.\')) == %s:\n'
1256 ' return %s\n') % (signature_id_tuple, expr_code)
1257 self.to_screen('Extracted signature function:\n' + code)
1259 def _parse_sig_js(self, jscode):
1260 funcname = self._search_regex(
1261 (r'\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
1262 r'\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
1263 r'\bm=(?P<sig>[a-zA-Z0-9$]{2})\(decodeURIComponent\(h\.s\)\)',
1264 r'\bc&&\(c=(?P<sig>[a-zA-Z0-9$]{2})\(decodeURIComponent\(c\)\)',
1265 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*\);[a-zA-Z0-9$]{2}\.[a-zA-Z0-9$]{2}\(a,\d+\)',
1266 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*\)',
1267 r'(?P<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)',
1269 r'(["\'])signature\
1\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1270 r'\
.sig\|\|
(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1271 r'yt\
.akamaized\
.net
/\
)\s
*\|\|\s
*.*?\s
*[cs
]\s
*&&\s
*[adf
]\
.set\
([^
,]+\s
*,\s
*(?
:encodeURIComponent\s
*\
()?\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1272 r'\b[cs
]\s
*&&\s
*[adf
]\
.set\
([^
,]+\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1273 r'\b[a
-zA
-Z0
-9]+\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1274 r'\bc\s
*&&\s
*a\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1275 r'\bc\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
(',
1276 r'\bc\s
*&&\s
*[a
-zA
-Z0
-9]+\
.set\
([^
,]+\s
*,\s
*\
([^
)]*\
)\s
*\
(\s
*(?P
<sig
>[a
-zA
-Z0
-9$
]+)\
('),
1277 jscode, 'Initial JS player signature function name
', group='sig
')
1279 jsi = JSInterpreter(jscode)
1280 initial_function = jsi.extract_function(funcname)
1281 return lambda s: initial_function([s])
1283 def _decrypt_signature(self, s, video_id, player_url):
1284 """Turn the encrypted s field into a working signature"""
1286 if player_url is None:
1287 raise ExtractorError('Cannot decrypt signature without player_url
')
1289 if player_url.startswith('//'):
1290 player_url = 'https
:' + player_url
1291 elif not re.match(r'https?
://', player_url):
1292 player_url = compat_urlparse.urljoin(
1293 'https
://www
.youtube
.com
', player_url)
1295 player_id = (player_url, self._signature_cache_id(s))
1296 if player_id not in self._player_cache:
1297 func = self._extract_signature_function(
1298 video_id, player_url, s
1300 self._player_cache[player_id] = func
1301 func = self._player_cache[player_id]
1302 if self._downloader.params.get('youtube_print_sig_code
'):
1303 self._print_sig_code(func, s)
1305 except Exception as e:
1306 tb = traceback.format_exc()
1307 raise ExtractorError(
1308 'Signature extraction failed
: ' + tb, cause=e)
1310 def _mark_watched(self, video_id, player_response):
1311 playback_url = url_or_none(try_get(
1313 lambda x: x['playbackTracking
']['videostatsPlaybackUrl
']['baseUrl
']))
1314 if not playback_url:
1316 parsed_playback_url = compat_urlparse.urlparse(playback_url)
1317 qs = compat_urlparse.parse_qs(parsed_playback_url.query)
1319 # cpn generation algorithm is reverse engineered from base.js.
1320 # In fact it works even with dummy cpn.
1321 CPN_ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
-_
'
1322 cpn = ''.join((CPN_ALPHABET[random.randint(0, 256) & 63] for _ in range(0, 16)))
1328 playback_url = compat_urlparse.urlunparse(
1329 parsed_playback_url._replace(query=compat_urllib_parse_urlencode(qs, True)))
1331 self._download_webpage(
1332 playback_url, video_id, 'Marking watched
',
1333 'Unable to mark watched
', fatal=False)
1336 def _extract_urls(webpage):
1337 # Embedded YouTube player
1339 unescapeHTML(mobj.group('url
'))
1340 for mobj in re.finditer(r'''(?x)
1350 (?P<url>(?:https?:)?//(?:www\.)?youtube(?:-nocookie)?\.com/
1351 (?:embed|v|p)/[0-9A-Za-z_-]{11}.*?)
1354 # lazyYT YouTube embed
1355 entries.extend(list(map(
1357 re.findall(r'class="lazyYT" data
-youtube
-id="([^"]+)"', webpage))))
1359 # Wordpress "YouTube Video Importer
" plugin
1360 matches = re.findall(r'''(?x)<div[^>]+
1361 class=(?P<q1>[\'"])[^
\'"]*\byvii_single_video_player\b[^\'"]*(?P
=q1
)[^
>]+
1362 data
-video_id
=(?P
<q2
>[\'"])([^\'"]+)(?P
=q2
)''', webpage)
1363 entries.extend(m[-1] for m in matches)
1368 def _extract_url(webpage):
1369 urls = YoutubeIE._extract_urls(webpage)
1370 return urls[0] if urls else None
1373 def extract_id(cls, url):
1374 mobj = re.match(cls._VALID_URL, url, re.VERBOSE)
1376 raise ExtractorError('Invalid URL: %s' % url)
1377 video_id = mobj.group(2)
1380 def _extract_chapters_from_json(self, data, video_id, duration):
1381 chapters_list = try_get(
1383 lambda x: x['playerOverlays']
1384 ['playerOverlayRenderer']
1385 ['decoratedPlayerBarRenderer']
1386 ['decoratedPlayerBarRenderer']
1388 ['chapteredPlayerBarRenderer']
1391 if not chapters_list:
1394 def chapter_time(chapter):
1395 return float_or_none(
1398 lambda x: x['chapterRenderer']['timeRangeStartMillis'],
1402 for next_num, chapter in enumerate(chapters_list, start=1):
1403 start_time = chapter_time(chapter)
1404 if start_time is None:
1406 end_time = (chapter_time(chapters_list[next_num])
1407 if next_num < len(chapters_list) else duration)
1408 if end_time is None:
1411 chapter, lambda x: x['chapterRenderer']['title']['simpleText'],
1414 'start_time': start_time,
1415 'end_time': end_time,
1420 def _extract_yt_initial_variable(self, webpage, regex, video_id, name):
1421 return self._parse_json(self._search_regex(
1422 (r'%s\s*%s' % (regex, self._YT_INITIAL_BOUNDARY_RE),
1423 regex), webpage, name, default='{}'), video_id, fatal=False)
1425 def _real_extract(self, url):
1426 url, smuggled_data = unsmuggle_url(url, {})
1427 video_id = self._match_id(url)
1428 base_url = self.http_scheme() + '//www.youtube.com/'
1429 webpage_url = base_url + 'watch?v=' + video_id + '&has_verified=1'
1430 webpage = self._download_webpage(webpage_url, video_id, fatal=False)
1432 player_response = None
1434 player_response = self._extract_yt_initial_variable(
1435 webpage, self._YT_INITIAL_PLAYER_RESPONSE_RE,
1436 video_id, 'initial player response')
1437 if not player_response:
1438 player_response = self._call_api(
1439 'player', {'videoId': video_id}, video_id)
1441 playability_status = player_response.get('playabilityStatus') or {}
1442 if playability_status.get('reason') == 'Sign in to confirm your age':
1443 pr = self._parse_json(try_get(compat_parse_qs(
1444 self._download_webpage(
1445 base_url + 'get_video_info', video_id,
1446 'Refetching age-gated info webpage',
1447 'unable to download video info webpage', query={
1448 'video_id': video_id,
1449 'eurl': 'https://www.youtube.com/embed/' + video_id,
1451 lambda x: x['player_response'][0],
1452 compat_str) or '{}', video_id)
1454 player_response = pr
1456 trailer_video_id = try_get(
1458 lambda x: x['errorScreen']['playerLegacyDesktopYpcTrailerRenderer']['trailerVideoId'],
1460 if trailer_video_id:
1461 return self.url_result(
1462 trailer_video_id, self.ie_key(), trailer_video_id)
1467 return x.get('simpleText') or ''.join([r['text'] for r in x['runs']])
1470 lambda x: self._html_search_meta(x, webpage, default=None)) \
1471 if webpage else lambda x: None
1473 video_details = player_response.get('videoDetails') or {}
1474 microformat = try_get(
1476 lambda x: x['microformat']['playerMicroformatRenderer'],
1478 video_title = video_details.get('title') \
1479 or get_text(microformat.get('title')) \
1480 or search_meta(['og:title', 'twitter:title', 'title'])
1481 video_description = video_details.get('shortDescription')
1483 if not smuggled_data.get('force_singlefeed', False):
1484 if not self._downloader.params.get('noplaylist'):
1485 multifeed_metadata_list = try_get(
1487 lambda x: x['multicamera']['playerLegacyMulticameraRenderer']['metadataList'],
1489 if multifeed_metadata_list:
1492 for feed in multifeed_metadata_list.split(','):
1493 # Unquote should take place before split on comma (,) since textual
1494 # fields may contain comma as well (see
1495 # https://github.com/ytdl-org/youtube-dl/issues/8536)
1496 feed_data = compat_parse_qs(
1497 compat_urllib_parse_unquote_plus(feed))
1499 def feed_entry(name):
1501 feed_data, lambda x: x[name][0], compat_str)
1503 feed_id = feed_entry('id')
1506 feed_title = feed_entry('title')
1509 title += ' (%s)' % feed_title
1511 '_type': 'url_transparent',
1512 'ie_key': 'Youtube',
1514 base_url + 'watch?v=' + feed_data['id'][0],
1515 {'force_singlefeed': True}),
1518 feed_ids.append(feed_id)
1520 'Downloading multifeed video (%s) - add --no-playlist to just download video %s'
1521 % (', '.join(feed_ids), video_id))
1522 return self.playlist_result(
1523 entries, video_id, video_title, video_description)
1525 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
1531 # TODO: Enable this after fixing formatSort
1532 # q = qualities(['tiny', 'small', 'medium', 'large', 'hd720', 'hd1080', 'hd1440', 'hd2160', 'hd2880', 'highres'])
1533 streaming_data = player_response.get('streamingData') or {}
1534 streaming_formats = streaming_data.get('formats') or []
1535 streaming_formats.extend(streaming_data.get('adaptiveFormats') or [])
1536 for fmt in streaming_formats:
1537 if fmt.get('targetDurationSec') or fmt.get('drmFamilies'):
1540 itag = str_or_none(fmt.get('itag'))
1541 quality = fmt.get('quality')
1542 if itag and quality:
1543 itag_qualities[itag] = quality
1544 # FORMAT_STREAM_TYPE_OTF(otf=1) requires downloading the init fragment
1545 # (adding `&sq=0` to the URL) and parsing emsg box to determine the
1546 # number of fragment that would subsequently requested with (`&sq=N`)
1547 if fmt.get('type') == 'FORMAT_STREAM_TYPE_OTF':
1550 fmt_url = fmt.get('url')
1552 sc = compat_parse_qs(fmt.get('signatureCipher'))
1553 fmt_url = url_or_none(try_get(sc, lambda x: x['url'][0]))
1554 encrypted_sig = try_get(sc, lambda x: x['s'][0])
1555 if not (sc and fmt_url and encrypted_sig):
1560 player_url = self._search_regex(
1561 r'"(?:PLAYER_JS_URL|jsUrl)"\s*:\s*"([^"]+)"',
1562 webpage, 'player URL', fatal=False)
1565 signature = self._decrypt_signature(sc['s'][0], video_id, player_url)
1566 sp = try_get(sc, lambda x: x['sp'][0]) or 'signature'
1567 fmt_url += '&' + sp + '=' + signature
1571 tbr = float_or_none(
1572 fmt.get('averageBitrate') or fmt.get('bitrate'), 1000)
1574 'asr': int_or_none(fmt.get('audioSampleRate')),
1575 'filesize': int_or_none(fmt.get('contentLength')),
1577 'format_note': fmt.get('qualityLabel') or quality,
1578 'fps': int_or_none(fmt.get('fps')),
1579 'height': int_or_none(fmt.get('height')),
1580 # 'quality': q(quality), # TODO: Enable this after fixing formatSort
1583 'width': fmt.get('width'),
1585 mimetype = fmt.get('mimeType')
1588 r'((?:[^/]+)/(?:[^;]+))(?:;\s*codecs="([^"]+)")?', mimetype)
1590 dct['ext'] = mimetype2ext(mobj.group(1))
1591 dct.update(parse_codecs(mobj.group(2)))
1592 no_audio = dct.get('acodec') == 'none'
1593 no_video = dct.get('vcodec') == 'none'
1598 if no_audio or no_video:
1599 dct['downloader_options'] = {
1600 # Youtube throttles chunks >~10M
1601 'http_chunk_size': 10485760,
1605 hls_manifest_url = streaming_data.get('hlsManifestUrl')
1606 if hls_manifest_url:
1607 for f in self._extract_m3u8_formats(
1608 hls_manifest_url, video_id, 'mp4', fatal=False):
1609 itag = self._search_regex(
1610 r'/itag/(\d+)', f['url'], 'itag', default=None)
1612 f['format_id'] = itag
1615 if self._downloader.params.get('youtube_include_dash_manifest'):
1616 dash_manifest_url = streaming_data.get('dashManifestUrl')
1617 if dash_manifest_url:
1618 for f in self._extract_mpd_formats(
1619 dash_manifest_url, video_id, fatal=False):
1620 itag = f['format_id']
1623 # if itag in itag_qualities: # TODO: Enable this after fixing formatSort
1624 # f['quality'] = q(itag_qualities[itag])
1625 filesize = int_or_none(self._search_regex(
1626 r'/clen/(\d+)', f.get('fragment_base_url')
1627 or f['url'], 'file size', default=None))
1629 f['filesize'] = filesize
1633 if not self._downloader.params.get('allow_unplayable_formats') and streaming_data.get('licenseInfos'):
1634 raise ExtractorError(
1635 'This video is DRM protected.', expected=True)
1638 lambda x: x['errorScreen']['playerErrorMessageRenderer'],
1640 reason = get_text(pemr.get('reason')) or playability_status.get('reason')
1641 subreason = pemr.get('subreason')
1643 subreason = clean_html(get_text(subreason))
1644 if subreason == 'The uploader has not made this video available in your country.':
1645 countries = microformat.get('availableCountries')
1647 regions_allowed = search_meta('regionsAllowed')
1648 countries = regions_allowed.split(',') if regions_allowed else None
1649 self.raise_geo_restricted(
1650 subreason, countries)
1651 reason += '\n' + subreason
1653 raise ExtractorError(reason, expected=True)
1655 self._sort_formats(formats)
1657 keywords = video_details.get('keywords') or []
1658 if not keywords and webpage:
1660 unescapeHTML(m.group('content'))
1661 for m in re.finditer(self._meta_regex('og:video:tag'), webpage)]
1662 for keyword in keywords:
1663 if keyword.startswith('yt:stretch='):
1664 w, h = keyword.split('=')[1].split(':')
1665 w, h = int(w), int(h)
1669 if f.get('vcodec') != 'none':
1670 f['stretched_ratio'] = ratio
1673 for container in (video_details, microformat):
1674 for thumbnail in (try_get(
1676 lambda x: x['thumbnail']['thumbnails'], list) or []):
1677 thumbnail_url = thumbnail.get('url')
1678 if not thumbnail_url:
1681 'height': int_or_none(thumbnail.get('height')),
1682 'url': thumbnail_url,
1683 'width': int_or_none(thumbnail.get('width')),
1688 thumbnail = search_meta(['og:image', 'twitter:image'])
1690 thumbnails = [{'url': thumbnail}]
1692 category = microformat.get('category') or search_meta('genre')
1693 channel_id = video_details.get('channelId') \
1694 or microformat.get('externalChannelId') \
1695 or search_meta('channelId')
1696 duration = int_or_none(
1697 video_details.get('lengthSeconds')
1698 or microformat.get('lengthSeconds')) \
1699 or parse_duration(search_meta('duration'))
1700 is_live = video_details.get('isLive')
1701 owner_profile_url = microformat.get('ownerProfileUrl')
1705 'title': self._live_title(video_title) if is_live else video_title,
1707 'thumbnails': thumbnails,
1708 'description': video_description,
1709 'upload_date': unified_strdate(
1710 microformat.get('uploadDate')
1711 or search_meta('uploadDate')),
1712 'uploader': video_details['author'],
1713 'uploader_id': self._search_regex(r'/(?:channel|user)/([^/?&#]+)', owner_profile_url, 'uploader id') if owner_profile_url else None,
1714 'uploader_url': owner_profile_url,
1715 'channel_id': channel_id,
1716 'channel_url': 'https://www.youtube.com/channel/' + channel_id if channel_id else None,
1717 'duration': duration,
1718 'view_count': int_or_none(
1719 video_details.get('viewCount')
1720 or microformat.get('viewCount')
1721 or search_meta('interactionCount')),
1722 'average_rating': float_or_none(video_details.get('averageRating')),
1723 'age_limit': 18 if (
1724 microformat.get('isFamilySafe') is False
1725 or search_meta('isFamilyFriendly') == 'false'
1726 or search_meta('og:restrictions:age') == '18+') else 0,
1727 'webpage_url': webpage_url,
1728 'categories': [category] if category else None,
1731 'playable_in_embed': playability_status.get('playableInEmbed'),
1736 lambda x: x['captions']['playerCaptionsTracklistRenderer'], dict)
1739 def process_language(container, base_url, lang_code, query):
1741 for fmt in self._SUBTITLE_FORMATS:
1747 'url': update_url_query(base_url, query),
1749 container[lang_code] = lang_subs
1751 for caption_track in (pctr.get('captionTracks') or []):
1752 base_url = caption_track.get('baseUrl')
1755 if caption_track.get('kind') != 'asr':
1756 lang_code = caption_track.get('languageCode')
1760 subtitles, base_url, lang_code, {})
1762 automatic_captions = {}
1763 for translation_language in (pctr.get('translationLanguages') or []):
1764 translation_language_code = translation_language.get('languageCode')
1765 if not translation_language_code:
1768 automatic_captions, base_url, translation_language_code,
1769 {'tlang': translation_language_code})
1770 info['automatic_captions'] = automatic_captions
1771 info['subtitles'] = subtitles
1773 parsed_url = compat_urllib_parse_urlparse(url)
1774 for component in [parsed_url.fragment, parsed_url.query]:
1775 query = compat_parse_qs(component)
1776 for k, v in query.items():
1777 for d_k, s_ks in [('start', ('start', 't')), ('end', ('end',))]:
1779 if d_k not in info and k in s_ks:
1780 info[d_k] = parse_duration(query[k][0])
1782 # Youtube Music Auto-generated description
1783 if video_description:
1784 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)
1786 release_year = mobj.group('release_year')
1787 release_date = mobj.group('release_date')
1789 release_date = release_date.replace('-', '')
1790 if not release_year:
1791 release_year = release_date[:4]
1793 'album': mobj.group('album'.strip()),
1794 'artist': mobj.group('clean_artist') or ', '.join(a.strip() for a in mobj.group('artist').split('·')),
1795 'track': mobj.group('track').strip(),
1796 'release_date': release_date,
1797 'release_year': int_or_none(release_year),
1802 initial_data = self._extract_yt_initial_variable(
1803 webpage, self._YT_INITIAL_DATA_RE, video_id,
1805 if not initial_data:
1806 initial_data = self._call_api(
1807 'next', {'videoId': video_id}, video_id, fatal=False)
1811 # This will error if there is no livechat
1812 initial_data['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation']
1813 info['subtitles']['live_chat'] = [{
1814 'video_id': video_id,
1816 'protocol': 'youtube_live_chat_replay',
1818 except (KeyError, IndexError, TypeError):
1822 chapters = self._extract_chapters_from_json(
1823 initial_data, video_id, duration)
1825 for engagment_pannel in (initial_data.get('engagementPanels') or []):
1827 engagment_pannel, lambda x: x['engagementPanelSectionListRenderer']['content']['macroMarkersListRenderer']['contents'],
1832 def chapter_time(mmlir):
1833 return parse_duration(
1834 get_text(mmlir.get('timeDescription')))
1837 for next_num, content in enumerate(contents, start=1):
1838 mmlir = content.get('macroMarkersListItemRenderer') or {}
1839 start_time = chapter_time(mmlir)
1840 end_time = chapter_time(try_get(
1841 contents, lambda x: x[next_num]['macroMarkersListItemRenderer'])) \
1842 if next_num < len(contents) else duration
1843 if start_time is None or end_time is None:
1846 'start_time': start_time,
1847 'end_time': end_time,
1848 'title': get_text(mmlir.get('title')),
1853 info['chapters'] = chapters
1857 lambda x: x['contents']['twoColumnWatchNextResults']['results']['results']['contents'],
1859 for content in contents:
1860 vpir = content.get('videoPrimaryInfoRenderer')
1862 stl = vpir.get('superTitleLink')
1867 lambda x: x['superTitleIcon']['iconType']) == 'LOCATION_PIN':
1868 info['location'] = stl
1870 mobj = re.search(r'(.+?)\s*S(\d+)\s*•\s*E(\d+)', stl)
1873 'series': mobj.group(1),
1874 'season_number': int(mobj.group(2)),
1875 'episode_number': int(mobj.group(3)),
1877 for tlb in (try_get(
1879 lambda x: x['videoActions']['menuRenderer']['topLevelButtons'],
1881 tbr = tlb.get('toggleButtonRenderer') or {}
1882 for getter, regex in [(
1883 lambda x: x['defaultText']['accessibility']['accessibilityData'],
1884 r'(?P<count>[\d,]+)\s*(?P<type>(?:dis)?like)'), ([
1885 lambda x: x['accessibility'],
1886 lambda x: x['accessibilityData']['accessibilityData'],
1887 ], r'(?P<type>(?:dis)?like) this video along with (?P<count>[\d,]+) other people')]:
1888 label = (try_get(tbr, getter, dict) or {}).get('label')
1890 mobj = re.match(regex, label)
1892 info[mobj.group('type') + '_count'] = str_to_int(mobj.group('count'))
1894 sbr_tooltip = try_get(
1895 vpir, lambda x: x['sentimentBar']['sentimentBarRenderer']['tooltip'])
1897 like_count, dislike_count = sbr_tooltip.split(' / ')
1899 'like_count': str_to_int(like_count),
1900 'dislike_count': str_to_int(dislike_count),
1902 vsir = content.get('videoSecondaryInfoRenderer')
1904 info['channel'] = get_text(try_get(
1906 lambda x: x['owner']['videoOwnerRenderer']['title'],
1910 lambda x: x['metadataRowContainer']['metadataRowContainerRenderer']['rows'],
1912 multiple_songs = False
1914 if try_get(row, lambda x: x['metadataRowRenderer']['hasDividerLine']) is True:
1915 multiple_songs = True
1918 mrr = row.get('metadataRowRenderer') or {}
1919 mrr_title = mrr.get('title')
1922 mrr_title = get_text(mrr['title'])
1923 mrr_contents_text = get_text(mrr['contents'][0])
1924 if mrr_title == 'License':
1925 info['license'] = mrr_contents_text
1926 elif not multiple_songs:
1927 if mrr_title == 'Album':
1928 info['album'] = mrr_contents_text
1929 elif mrr_title == 'Artist':
1930 info['artist'] = mrr_contents_text
1931 elif mrr_title == 'Song':
1932 info['track'] = mrr_contents_text
1935 'channel': 'uploader',
1936 'channel_id': 'uploader_id',
1937 'channel_url': 'uploader_url',
1939 for to, frm in fallbacks.items():
1940 if not info.get(to):
1941 info[to] = info.get(frm)
1943 for s_k, d_k in [('artist', 'creator'), ('track', 'alt_title')]:
1948 # get xsrf for annotations or comments
1949 get_annotations = self._downloader.params.get('writeannotations', False)
1950 get_comments = self._downloader.params.get('getcomments', False)
1951 if get_annotations or get_comments:
1953 ytcfg = self._extract_ytcfg(video_id, webpage)
1955 xsrf_token = try_get(ytcfg, lambda x: x['XSRF_TOKEN'], compat_str)
1957 xsrf_token = self._search_regex(
1958 r'([\'"])XSRF_TOKEN\1\s*:\s*([\'"])(?P<xsrf_token>(?:(?!\2).)+)\2',
1959 webpage, 'xsrf token', group='xsrf_token', fatal=False)
1963 invideo_url = try_get(
1964 player_response, lambda x: x['annotations'][0]['playerAnnotationsUrlsRenderer']['invideoUrl'], compat_str)
1965 if xsrf_token and invideo_url:
1966 xsrf_field_name = None
1968 xsrf_field_name = try_get(ytcfg, lambda x: x['XSRF_FIELD_NAME'], compat_str)
1969 if not xsrf_field_name:
1970 xsrf_field_name = self._search_regex(
1971 r'([\'"])XSRF_FIELD_NAME\1\s*:\s*([\'"])(?P<xsrf_field_name>\w+)\2',
1972 webpage, 'xsrf field name',
1973 group='xsrf_field_name', default='session_token')
1974 info['annotations'] = self._download_webpage(
1975 self._proto_relative_url(invideo_url),
1976 video_id, note='Downloading annotations',
1977 errnote='Unable to download video annotations', fatal=False,
1978 data=urlencode_postdata({xsrf_field_name: xsrf_token}))
1981 # TODO: Refactor and move to seperate function
1983 expected_video_comment_count = 0
1986 def find_value(html, key, num_chars=2, separator='"'):
1987 pos_begin = html.find(key) + len(key) + num_chars
1988 pos_end = html.find(separator, pos_begin)
1989 return html[pos_begin: pos_end]
1991 def search_dict(partial, key):
1992 if isinstance(partial, dict):
1993 for k, v in partial.items():
1997 for o in search_dict(v, key):
1999 elif isinstance(partial, list):
2001 for o in search_dict(i, key):
2007 ncd = next(search_dict(initial_data, 'nextContinuationData'))
2008 continuations = [ncd['continuation']]
2009 # Handle videos where comments have been disabled entirely
2010 except StopIteration:
2013 def get_continuation(continuation, session_token, replies=False):
2016 'ctoken': continuation,
2019 query['action_get_comment_replies'] = 1
2021 query['action_get_comments'] = 1
2024 content, handle = self._download_webpage_handle(
2025 'https://www.youtube.com/comment_service_ajax',
2028 expected_status=[413],
2029 data=urlencode_postdata({
2030 'session_token': session_token
2035 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0',
2036 'X-YouTube-Client-Name': '1',
2037 'X-YouTube-Client-Version': '2.20201202.06.01'
2041 response_code = handle.getcode()
2042 if (response_code == 200):
2043 return self._parse_json(content, video_id)
2044 if (response_code == 413):
2046 raise ExtractorError('Unexpected HTTP error code: %s' % response_code)
2048 first_continuation = True
2050 self.to_screen('Downloading comments')
2051 while continuations:
2052 continuation = continuations.pop()
2053 comment_response = get_continuation(continuation, xsrf_token)
2054 if not comment_response:
2056 if list(search_dict(comment_response, 'externalErrorMessage')):
2057 raise ExtractorError('Error returned from server: ' + next(search_dict(comment_response, 'externalErrorMessage')))
2059 if 'continuationContents' not in comment_response['response']:
2060 # Something is wrong here. Youtube won't accept this continuation token for some reason and responds with a user satisfaction dialog (error?)
2062 # not sure if this actually helps
2063 if 'xsrf_token' in comment_response:
2064 xsrf_token = comment_response['xsrf_token']
2066 item_section = comment_response['response']['continuationContents']['itemSectionContinuation']
2067 if first_continuation:
2068 expected_video_comment_count = int(item_section['header']['commentsHeaderRenderer']['countText']['runs'][0]['text'].replace(' Comments', '').replace('1 Comment', '1').replace(',', ''))
2069 first_continuation = False
2070 if 'contents' not in item_section:
2071 # continuation returned no comments?
2072 # set an empty array as to not break the for loop
2073 item_section['contents'] = []
2075 for meta_comment in item_section['contents']:
2076 comment = meta_comment['commentThreadRenderer']['comment']['commentRenderer']
2077 video_comments.append({
2078 'id': comment['commentId'],
2079 'text': ''.join([c['text'] for c in comment['contentText']['runs']]),
2080 'time_text': ''.join([c['text'] for c in comment['publishedTimeText']['runs']]),
2081 'author': comment.get('authorText', {}).get('simpleText', ''),
2082 'votes': comment.get('voteCount', {}).get('simpleText', '0'),
2083 'author_thumbnail': comment['authorThumbnail']['thumbnails'][-1]['url'],
2086 if 'replies' not in meta_comment['commentThreadRenderer']:
2089 reply_continuations = [rcn['nextContinuationData']['continuation'] for rcn in meta_comment['commentThreadRenderer']['replies']['commentRepliesRenderer']['continuations']]
2090 while reply_continuations:
2092 continuation = reply_continuations.pop()
2093 replies_data = get_continuation(continuation, xsrf_token, True)
2094 if not replies_data or 'continuationContents' not in replies_data[1]['response']:
2097 if self._downloader.params.get('verbose', False):
2098 chain_msg = ' (chain %s)' % comment['commentId']
2099 self.to_screen('Comments downloaded: %d of ~%d%s' % (len(video_comments), expected_video_comment_count, chain_msg))
2100 reply_comment_meta = replies_data[1]['response']['continuationContents']['commentRepliesContinuation']
2101 for reply_meta in reply_comment_meta.get('contents', {}):
2102 reply_comment = reply_meta['commentRenderer']
2103 video_comments.append({
2104 'id': reply_comment['commentId'],
2105 'text': ''.join([c['text'] for c in reply_comment['contentText']['runs']]),
2106 'time_text': ''.join([c['text'] for c in reply_comment['publishedTimeText']['runs']]),
2107 'author': reply_comment.get('authorText', {}).get('simpleText', ''),
2108 'votes': reply_comment.get('voteCount', {}).get('simpleText', '0'),
2109 'author_thumbnail': reply_comment['authorThumbnail']['thumbnails'][-1]['url'],
2110 'parent': comment['commentId']
2112 if 'continuations' not in reply_comment_meta or len(reply_comment_meta['continuations']) == 0:
2114 reply_continuations += [rcn['nextContinuationData']['continuation'] for rcn in reply_comment_meta['continuations']]
2116 self.to_screen('Comments downloaded: %d of ~%d' % (len(video_comments), expected_video_comment_count))
2117 if 'continuations' in item_section:
2118 continuations += [ncd['nextContinuationData']['continuation'] for ncd in item_section['continuations']]
2121 self.to_screen('Total comments downloaded: %d of ~%d' % (len(video_comments), expected_video_comment_count))
2123 'comments': video_comments,
2124 'comment_count': expected_video_comment_count
2127 self.mark_watched(video_id, player_response)
2132 class YoutubeTabIE(YoutubeBaseInfoExtractor):
2133 IE_DESC = 'YouTube.com tab'
2134 _VALID_URL = r'''(?x
)
2138 youtube(?
:kids
)?\
.com|
2142 (?
:channel|c|user
)/|
2145 (?
:playlist|watch
)\?.*?
\blist
=
2147 (?
!(?
:%s)\b) # Direct URLs
2150 ''' % YoutubeBaseInfoExtractor._RESERVED_NAMES
2151 IE_NAME = 'youtube:tab'
2154 # playlists, multipage
2155 'url': 'https://www.youtube.com/c/ИгорьКлейнер/playlists?view=1&flow=grid',
2156 'playlist_mincount': 94,
2158 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
2159 'title': 'Игорь Клейнер - Playlists',
2160 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
2161 'uploader': 'Игорь Клейнер',
2162 'uploader_id': 'UCqj7Cz7revf5maW9g5pgNcg',
2165 # playlists, multipage, different order
2166 'url': 'https://www.youtube.com/user/igorkle1/playlists?view=1&sort=dd',
2167 'playlist_mincount': 94,
2169 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
2170 'title': 'Игорь Клейнер - Playlists',
2171 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
2172 'uploader_id': 'UCqj7Cz7revf5maW9g5pgNcg',
2173 'uploader': 'Игорь Клейнер',
2176 # playlists, singlepage
2177 'url': 'https://www.youtube.com/user/ThirstForScience/playlists',
2178 'playlist_mincount': 4,
2180 'id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
2181 'title': 'ThirstForScience - Playlists',
2182 'description': 'md5:609399d937ea957b0f53cbffb747a14c',
2183 'uploader': 'ThirstForScience',
2184 'uploader_id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
2187 'url': 'https://www.youtube.com/c/ChristophLaimer/playlists',
2188 'only_matching': True,
2190 # basic, single video playlist
2191 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2193 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
2194 'uploader': 'Sergey M.',
2195 'id': 'PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2196 'title': 'youtube-dl public playlist',
2198 'playlist_count': 1,
2201 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
2203 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
2204 'uploader': 'Sergey M.',
2205 'id': 'PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
2206 'title': 'youtube-dl empty playlist',
2208 'playlist_count': 0,
2211 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/featured',
2213 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2214 'title': 'lex will - Home',
2215 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2216 'uploader': 'lex will',
2217 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2219 'playlist_mincount': 2,
2222 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos',
2224 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2225 'title': 'lex will - Videos',
2226 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2227 'uploader': 'lex will',
2228 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2230 'playlist_mincount': 975,
2232 # Videos tab, sorted by popular
2233 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos?view=0&sort=p&flow=grid',
2235 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2236 'title': 'lex will - Videos',
2237 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2238 'uploader': 'lex will',
2239 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2241 'playlist_mincount': 199,
2244 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/playlists',
2246 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2247 'title': 'lex will - Playlists',
2248 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2249 'uploader': 'lex will',
2250 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2252 'playlist_mincount': 17,
2255 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/community',
2257 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2258 'title': 'lex will - Community',
2259 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2260 'uploader': 'lex will',
2261 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2263 'playlist_mincount': 18,
2266 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/channels',
2268 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2269 'title': 'lex will - Channels',
2270 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
2271 'uploader': 'lex will',
2272 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
2274 'playlist_mincount': 12,
2276 'url': 'https://invidio.us/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2277 'only_matching': True,
2279 'url': 'https://www.youtubekids.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2280 'only_matching': True,
2282 'url': 'https://music.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
2283 'only_matching': True,
2285 'note': 'Playlist with deleted videos (#651). As a bonus, the video #51 is also twice in this list.',
2286 'url': 'https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
2288 'title': '29C3: Not my department',
2289 'id': 'PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
2290 'uploader': 'Christiaan008',
2291 'uploader_id': 'UCEPzS1rYsrkqzSLNp76nrcg',
2292 'description': 'md5:a14dc1a8ef8307a9807fe136a0660268',
2294 'playlist_count': 96,
2296 'note': 'Large playlist',
2297 'url': 'https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q',
2299 'title': 'Uploads from Cauchemar',
2300 'id': 'UUBABnxM4Ar9ten8Mdjj1j0Q',
2301 'uploader': 'Cauchemar',
2302 'uploader_id': 'UCBABnxM4Ar9ten8Mdjj1j0Q',
2304 'playlist_mincount': 1123,
2306 # even larger playlist, 8832 videos
2307 'url': 'http://www.youtube.com/user/NASAgovVideo/videos',
2308 'only_matching': True,
2310 'note': 'Buggy playlist: the webpage has a "Load more" button but it doesn\'t have more videos',
2311 'url': 'https://www.youtube.com/playlist?list=UUXw-G3eDE9trcvY2sBMM_aA',
2313 'title': 'Uploads from Interstellar Movie',
2314 'id': 'UUXw-G3eDE9trcvY2sBMM_aA',
2315 'uploader': 'Interstellar Movie',
2316 'uploader_id': 'UCXw-G3eDE9trcvY2sBMM_aA',
2318 'playlist_mincount': 21,
2320 # https://github.com/ytdl-org/youtube-dl/issues/21844
2321 'url': 'https://www.youtube.com/playlist?list=PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
2323 'title': 'Data Analysis with Dr Mike Pound',
2324 'id': 'PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
2325 'uploader_id': 'UC9-y-6csu5WGm29I7JiwpnA',
2326 'uploader': 'Computerphile',
2327 'description': 'md5:7f567c574d13d3f8c0954d9ffee4e487',
2329 'playlist_mincount': 11,
2331 'url': 'https://invidio.us/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
2332 'only_matching': True,
2334 # Playlist URL that does not actually serve a playlist
2335 'url': 'https://www.youtube.com/watch?v=FqZTN594JQw&list=PLMYEtVRpaqY00V9W81Cwmzp6N6vZqfUKD4',
2337 'id': 'FqZTN594JQw',
2339 'title': "Smiley's People 01 detective, Adventure Series, Action",
2340 'uploader': 'STREEM',
2341 'uploader_id': 'UCyPhqAZgwYWZfxElWVbVJng',
2342 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCyPhqAZgwYWZfxElWVbVJng',
2343 'upload_date': '20150526',
2344 'license': 'Standard YouTube License',
2345 'description': 'md5:507cdcb5a49ac0da37a920ece610be80',
2346 'categories': ['People & Blogs'],
2350 'dislike_count': int,
2353 'skip_download': True,
2355 'skip': 'This video is not available.',
2356 'add_ie': [YoutubeIE.ie_key()],
2358 'url': 'https://www.youtubekids.com/watch?v=Agk7R8I8o5U&list=PUZ6jURNr1WQZCNHF0ao-c0g',
2359 'only_matching': True,
2361 'url': 'https://www.youtube.com/watch?v=MuAGGZNfUkU&list=RDMM',
2362 'only_matching': True,
2364 'url': 'https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live',
2366 'id': '9Auq9mYxFEE',
2368 'title': compat_str,
2369 'uploader': 'Sky News',
2370 'uploader_id': 'skynews',
2371 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/skynews',
2372 'upload_date': '20191102',
2373 'description': 'md5:85ddd75d888674631aaf9599a9a0b0ae',
2374 'categories': ['News & Politics'],
2377 'dislike_count': int,
2380 'skip_download': True,
2383 'url': 'https://www.youtube.com/user/TheYoungTurks/live',
2385 'id': 'a48o2S1cPoo',
2387 'title': 'The Young Turks - Live Main Show',
2388 'uploader': 'The Young Turks',
2389 'uploader_id': 'TheYoungTurks',
2390 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/TheYoungTurks',
2391 'upload_date': '20150715',
2392 'license': 'Standard YouTube License',
2393 'description': 'md5:438179573adcdff3c97ebb1ee632b891',
2394 'categories': ['News & Politics'],
2395 'tags': ['Cenk Uygur (TV Program Creator)', 'The Young Turks (Award-Winning Work)', 'Talk Show (TV Genre)'],
2397 'dislike_count': int,
2400 'skip_download': True,
2402 'only_matching': True,
2404 'url': 'https://www.youtube.com/channel/UC1yBKRuGpC1tSM73A0ZjYjQ/live',
2405 'only_matching': True,
2407 'url': 'https://www.youtube.com/c/CommanderVideoHq/live',
2408 'only_matching': True,
2410 'url': 'https://www.youtube.com/feed/trending',
2411 'only_matching': True,
2414 'url': 'https://www.youtube.com/feed/library',
2415 'only_matching': True,
2418 'url': 'https://www.youtube.com/feed/history',
2419 'only_matching': True,
2422 'url': 'https://www.youtube.com/feed/subscriptions',
2423 'only_matching': True,
2426 'url': 'https://www.youtube.com/feed/watch_later',
2427 'only_matching': True,
2429 # no longer available?
2430 'url': 'https://www.youtube.com/feed/recommended',
2431 'only_matching': True,
2433 # inline playlist with not always working continuations
2434 'url': 'https://www.youtube.com/watch?v=UC6u0Tct-Fo&list=PL36D642111D65BE7C',
2435 'only_matching': True,
2437 'url': 'https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8',
2438 'only_matching': True,
2440 'url': 'https://www.youtube.com/course',
2441 'only_matching': True,
2443 'url': 'https://www.youtube.com/zsecurity',
2444 'only_matching': True,
2446 'url': 'http://www.youtube.com/NASAgovVideo/videos',
2447 'only_matching': True,
2449 'url': 'https://www.youtube.com/TheYoungTurks/live',
2450 'only_matching': True,
2454 def suitable(cls, url):
2455 return False if YoutubeIE.suitable(url) else super(
2456 YoutubeTabIE, cls).suitable(url)
2458 def _extract_channel_id(self, webpage):
2459 channel_id = self._html_search_meta(
2460 'channelId', webpage, 'channel id', default=None)
2463 channel_url = self._html_search_meta(
2464 ('og:url', 'al:ios:url', 'al:android:url', 'al:web:url',
2465 'twitter:url', 'twitter:app:url:iphone', 'twitter:app:url:ipad',
2466 'twitter:app:url:googleplay'), webpage, 'channel url')
2467 return self._search_regex(
2468 r'https?://(?:www\.)?youtube\.com/channel/([^/?#&])+',
2469 channel_url, 'channel id')
2472 def _extract_grid_item_renderer(item):
2473 for item_kind in ('Playlist', 'Video', 'Channel'):
2474 renderer = item.get('grid%sRenderer' % item_kind)
2478 def _grid_entries(self, grid_renderer):
2479 for item in grid_renderer['items']:
2480 if not isinstance(item, dict):
2482 renderer = self._extract_grid_item_renderer(item)
2483 if not isinstance(renderer, dict):
2486 renderer, lambda x: x['title']['runs'][0]['text'], compat_str)
2488 playlist_id = renderer.get('playlistId')
2490 yield self.url_result(
2491 'https://www.youtube.com/playlist?list=%s' % playlist_id,
2492 ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
2495 video_id = renderer.get('videoId')
2497 yield self._extract_video(renderer)
2499 channel_id = renderer.get('channelId')
2502 renderer, lambda x: x['title']['simpleText'], compat_str)
2503 yield self.url_result(
2504 'https://www.youtube.com/channel/%s' % channel_id,
2505 ie=YoutubeTabIE.ie_key(), video_title=title)
2507 def _shelf_entries_from_content(self, shelf_renderer):
2508 content = shelf_renderer.get('content')
2509 if not isinstance(content, dict):
2511 renderer = content.get('gridRenderer')
2513 # TODO: add support for nested playlists so each shelf is processed
2514 # as separate playlist
2515 # TODO: this includes only first N items
2516 for entry in self._grid_entries(renderer):
2518 renderer = content.get('horizontalListRenderer')
2523 def _shelf_entries(self, shelf_renderer, skip_channels=False):
2525 shelf_renderer, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
2527 shelf_url = urljoin('https://www.youtube.com', ep)
2529 # Skipping links to another channels, note that checking for
2530 # endpoint.commandMetadata.webCommandMetadata.webPageTypwebPageType == WEB_PAGE_TYPE_CHANNEL
2532 if skip_channels and '/channels?' in shelf_url:
2535 shelf_renderer, lambda x: x['title']['runs'][0]['text'], compat_str)
2536 yield self.url_result(shelf_url, video_title=title)
2537 # Shelf may not contain shelf URL, fallback to extraction from content
2538 for entry in self._shelf_entries_from_content(shelf_renderer):
2541 def _playlist_entries(self, video_list_renderer):
2542 for content in video_list_renderer['contents']:
2543 if not isinstance(content, dict):
2545 renderer = content.get('playlistVideoRenderer') or content.get('playlistPanelVideoRenderer')
2546 if not isinstance(renderer, dict):
2548 video_id = renderer.get('videoId')
2551 yield self._extract_video(renderer)
2553 r""" # Not needed in the new implementation
2554 def _itemSection_entries(self, item_sect_renderer):
2555 for content in item_sect_renderer['contents']:
2556 if not isinstance(content, dict):
2558 renderer = content.get('videoRenderer', {})
2559 if not isinstance(renderer, dict):
2561 video_id = renderer.get('videoId')
2564 yield self._extract_video(renderer)
2567 def _rich_entries(self, rich_grid_renderer):
2569 rich_grid_renderer, lambda x: x['content']['videoRenderer'], dict) or {}
2570 video_id = renderer.get('videoId')
2573 yield self._extract_video(renderer)
2575 def _video_entry(self, video_renderer):
2576 video_id = video_renderer.get('videoId')
2578 return self._extract_video(video_renderer)
2580 def _post_thread_entries(self, post_thread_renderer):
2581 post_renderer = try_get(
2582 post_thread_renderer, lambda x: x['post']['backstagePostRenderer'], dict)
2583 if not post_renderer:
2586 video_renderer = try_get(
2587 post_renderer, lambda x: x['backstageAttachment']['videoRenderer'], dict)
2590 entry = self._video_entry(video_renderer)
2593 # inline video links
2594 runs = try_get(post_renderer, lambda x: x['contentText']['runs'], list) or []
2596 if not isinstance(run, dict):
2599 run, lambda x: x['navigationEndpoint']['urlEndpoint']['url'], compat_str)
2602 if not YoutubeIE.suitable(ep_url):
2604 ep_video_id = YoutubeIE._match_id(ep_url)
2605 if video_id == ep_video_id:
2607 yield self.url_result(ep_url, ie=YoutubeIE.ie_key(), video_id=video_id)
2609 def _post_thread_continuation_entries(self, post_thread_continuation):
2610 contents = post_thread_continuation.get('contents')
2611 if not isinstance(contents, list):
2613 for content in contents:
2614 renderer = content.get('backstagePostThreadRenderer')
2615 if not isinstance(renderer, dict):
2617 for entry in self._post_thread_entries(renderer):
2621 def _build_continuation_query(continuation, ctp=None):
2623 'ctoken': continuation,
2624 'continuation': continuation,
2631 def _extract_next_continuation_data(renderer):
2632 next_continuation = try_get(
2633 renderer, lambda x: x['continuations'][0]['nextContinuationData'], dict)
2634 if not next_continuation:
2636 continuation = next_continuation.get('continuation')
2637 if not continuation:
2639 ctp = next_continuation.get('clickTrackingParams')
2640 return YoutubeTabIE._build_continuation_query(continuation, ctp)
2643 def _extract_continuation(cls, renderer):
2644 next_continuation = cls._extract_next_continuation_data(renderer)
2645 if next_continuation:
2646 return next_continuation
2648 for key in ('contents', 'items'):
2649 contents.extend(try_get(renderer, lambda x: x[key], list) or [])
2650 for content in contents:
2651 if not isinstance(content, dict):
2653 continuation_ep = try_get(
2654 content, lambda x: x['continuationItemRenderer']['continuationEndpoint'],
2656 if not continuation_ep:
2658 continuation = try_get(
2659 continuation_ep, lambda x: x['continuationCommand']['token'], compat_str)
2660 if not continuation:
2662 ctp = continuation_ep.get('clickTrackingParams')
2663 return YoutubeTabIE._build_continuation_query(continuation, ctp)
2665 def _entries(self, tab, identity_token):
2667 def extract_entries(parent_renderer): # this needs to called again for continuation to work with feeds
2668 contents = try_get(parent_renderer, lambda x: x['contents'], list) or []
2669 for content in contents:
2670 if not isinstance(content, dict):
2672 is_renderer = try_get(content, lambda x: x['itemSectionRenderer'], dict)
2674 renderer = content.get('richItemRenderer')
2676 for entry in self._rich_entries(renderer):
2678 continuation_list[0] = self._extract_continuation(parent_renderer)
2680 isr_contents = try_get(is_renderer, lambda x: x['contents'], list) or []
2681 for isr_content in isr_contents:
2682 if not isinstance(isr_content, dict):
2686 'playlistVideoListRenderer': self._playlist_entries,
2687 'gridRenderer': self._grid_entries,
2688 'shelfRenderer': lambda x: self._shelf_entries(x, tab.get('title') != 'Channels'),
2689 'backstagePostThreadRenderer': self._post_thread_entries,
2690 'videoRenderer': lambda x: [self._video_entry(x)],
2692 for key, renderer in isr_content.items():
2693 if key not in known_renderers:
2695 for entry in known_renderers[key](renderer):
2698 continuation_list[0] = self._extract_continuation(renderer)
2701 if not continuation_list[0]:
2702 continuation_list[0] = self._extract_continuation(is_renderer)
2704 if not continuation_list[0]:
2705 continuation_list[0] = self._extract_continuation(parent_renderer)
2707 continuation_list = [None] # Python 2 doesnot support nonlocal
2708 tab_content = try_get(tab, lambda x: x['content'], dict)
2712 try_get(tab_content, lambda x: x['sectionListRenderer'], dict)
2713 or try_get(tab_content, lambda x: x['richGridRenderer'], dict) or {})
2714 for entry in extract_entries(parent_renderer):
2716 continuation = continuation_list[0]
2719 'x-youtube-client-name': '1',
2720 'x-youtube-client-version': '2.20201112.04.01',
2723 headers['x-youtube-identity-token'] = identity_token
2725 for page_num in itertools.count(1):
2726 if not continuation:
2730 while count <= retries:
2732 # Downloading page may result in intermittent 5xx HTTP error
2733 # that is usually worked around with a retry
2734 browse = self._download_json(
2735 'https://www.youtube.com/browse_ajax', None,
2736 'Downloading page %d%s'
2737 % (page_num, ' (retry #%d)' % count if count else ''),
2738 headers=headers, query=continuation)
2740 except ExtractorError as e:
2741 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (500, 503):
2743 if count <= retries:
2748 response = try_get(browse, lambda x: x[1]['response'], dict)
2752 known_continuation_renderers = {
2753 'playlistVideoListContinuation': self._playlist_entries,
2754 'gridContinuation': self._grid_entries,
2755 'itemSectionContinuation': self._post_thread_continuation_entries,
2756 'sectionListContinuation': extract_entries, # for feeds
2758 continuation_contents = try_get(
2759 response, lambda x: x['continuationContents'], dict) or {}
2760 continuation_renderer = None
2761 for key, value in continuation_contents.items():
2762 if key not in known_continuation_renderers:
2764 continuation_renderer = value
2765 continuation_list = [None]
2766 for entry in known_continuation_renderers[key](continuation_renderer):
2768 continuation = continuation_list[0] or self._extract_continuation(continuation_renderer)
2770 if continuation_renderer:
2774 'gridPlaylistRenderer': (self._grid_entries, 'items'),
2775 'gridVideoRenderer': (self._grid_entries, 'items'),
2776 'playlistVideoRenderer': (self._playlist_entries, 'contents'),
2777 'itemSectionRenderer': (self._playlist_entries, 'contents'),
2778 'richItemRenderer': (extract_entries, 'contents'), # for hashtag
2780 continuation_items = try_get(
2781 response, lambda x: x['onResponseReceivedActions'][0]['appendContinuationItemsAction']['continuationItems'], list)
2782 continuation_item = try_get(continuation_items, lambda x: x[0], dict) or {}
2783 video_items_renderer = None
2784 for key, value in continuation_item.items():
2785 if key not in known_renderers:
2787 video_items_renderer = {known_renderers[key][1]: continuation_items}
2788 continuation_list = [None]
2789 for entry in known_renderers[key][0](video_items_renderer):
2791 continuation = continuation_list[0] or self._extract_continuation(video_items_renderer)
2793 if video_items_renderer:
2798 def _extract_selected_tab(tabs):
2800 if try_get(tab, lambda x: x['tabRenderer']['selected'], bool):
2801 return tab['tabRenderer']
2803 raise ExtractorError('Unable to find selected tab')
2806 def _extract_uploader(data):
2808 sidebar_renderer = try_get(
2809 data, lambda x: x['sidebar']['playlistSidebarRenderer']['items'], list)
2810 if sidebar_renderer:
2811 for item in sidebar_renderer:
2812 if not isinstance(item, dict):
2814 renderer = item.get('playlistSidebarSecondaryInfoRenderer')
2815 if not isinstance(renderer, dict):
2818 renderer, lambda x: x['videoOwner']['videoOwnerRenderer']['title']['runs'][0], dict)
2820 uploader['uploader'] = owner.get('text')
2821 uploader['uploader_id'] = try_get(
2822 owner, lambda x: x['navigationEndpoint']['browseEndpoint']['browseId'], compat_str)
2823 uploader['uploader_url'] = urljoin(
2824 'https://www.youtube.com/',
2825 try_get(owner, lambda x: x['navigationEndpoint']['browseEndpoint']['canonicalBaseUrl'], compat_str))
2826 return {k: v for k, v in uploader.items() if v is not None}
2828 def _extract_from_tabs(self, item_id, webpage, data, tabs, identity_token):
2829 playlist_id = title = description = channel_url = channel_name = channel_id = None
2830 thumbnails_list = tags = []
2832 selected_tab = self._extract_selected_tab(tabs)
2834 data, lambda x: x['metadata']['channelMetadataRenderer'], dict)
2836 channel_name = renderer.get('title')
2837 channel_url = renderer.get('channelUrl')
2838 channel_id = renderer.get('externalId')
2842 data, lambda x: x['metadata']['playlistMetadataRenderer'], dict)
2844 title = renderer.get('title')
2845 description = renderer.get('description', '')
2846 playlist_id = channel_id
2847 tags = renderer.get('keywords', '').split()
2849 try_get(renderer, lambda x: x['avatar']['thumbnails'], list)
2852 lambda x: x['sidebar']['playlistSidebarRenderer']['items'][0]['playlistSidebarPrimaryInfoRenderer']['thumbnailRenderer']['playlistVideoThumbnailRenderer']['thumbnail']['thumbnails'],
2857 for t in thumbnails_list:
2858 if not isinstance(t, dict):
2860 thumbnail_url = url_or_none(t.get('url'))
2861 if not thumbnail_url:
2864 'url': thumbnail_url,
2865 'width': int_or_none(t.get('width')),
2866 'height': int_or_none(t.get('height')),
2869 if playlist_id is None:
2870 playlist_id = item_id
2873 title += format_field(selected_tab, 'title', ' - %s')
2876 'playlist_id': playlist_id,
2877 'playlist_title': title,
2878 'playlist_description': description,
2879 'uploader': channel_name,
2880 'uploader_id': channel_id,
2881 'uploader_url': channel_url,
2882 'thumbnails': thumbnails,
2886 metadata.update(self._extract_uploader(data))
2888 'channel': metadata['uploader'],
2889 'channel_id': metadata['uploader_id'],
2890 'channel_url': metadata['uploader_url']})
2891 return self.playlist_result(
2892 self._entries(selected_tab, identity_token),
2895 def _extract_from_playlist(self, item_id, url, data, playlist):
2896 title = playlist.get('title') or try_get(
2897 data, lambda x: x['titleText']['simpleText'], compat_str)
2898 playlist_id = playlist.get('playlistId') or item_id
2899 # Inline playlist rendition continuation does not always work
2900 # at Youtube side, so delegating regular tab-based playlist URL
2901 # processing whenever possible.
2902 playlist_url = urljoin(url, try_get(
2903 playlist, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
2905 if playlist_url and playlist_url != url:
2906 return self.url_result(
2907 playlist_url, ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
2909 return self.playlist_result(
2910 self._playlist_entries(playlist), playlist_id=playlist_id,
2911 playlist_title=title)
2914 def _extract_alerts(data):
2915 for alert_dict in try_get(data, lambda x: x['alerts'], list) or []:
2916 if not isinstance(alert_dict, dict):
2918 for renderer in alert_dict:
2919 alert = alert_dict[renderer]
2920 alert_type = alert.get('type')
2923 message = try_get(alert, lambda x: x['text']['simpleText'], compat_str)
2925 yield alert_type, message
2926 for run in try_get(alert, lambda x: x['text']['runs'], list) or []:
2927 message = try_get(run, lambda x: x['text'], compat_str)
2929 yield alert_type, message
2931 def _extract_identity_token(self, webpage, item_id):
2932 ytcfg = self._extract_ytcfg(item_id, webpage)
2934 token = try_get(ytcfg, lambda x: x['ID_TOKEN'], compat_str)
2937 return self._search_regex(
2938 r'\bID_TOKEN["\']\s*:\s*["\'](.+?)["\']', webpage,
2939 'identity token', default=None)
2941 def _real_extract(self, url):
2942 item_id = self._match_id(url)
2943 url = compat_urlparse.urlunparse(
2944 compat_urlparse.urlparse(url)._replace(netloc='www.youtube.com'))
2945 is_home = re.match(r'(?P<pre>%s)(?P<post>/?(?![^#?]).*$)' % self._VALID_URL, url)
2946 if is_home is not None and is_home.group('not_channel') is None and item_id != 'feed':
2947 self._downloader.report_warning(
2948 'A channel/user page was given. All the channel\'s videos will be downloaded. '
2949 'To download only the videos in the home page, add a "/featured" to the URL')
2950 url = '%s/videos%s' % (is_home.group('pre'), is_home.group('post') or '')
2952 # Handle both video/playlist URLs
2953 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
2954 video_id = qs.get('v', [None])[0]
2955 playlist_id = qs.get('list', [None])[0]
2957 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:
2959 self._downloader.report_warning('%s is not a valid Youtube URL. Trying to download playlist %s' % (url, playlist_id))
2960 url = 'https://www.youtube.com/playlist?list=%s' % playlist_id
2961 # return self.url_result(playlist_id, ie=YoutubePlaylistIE.ie_key())
2963 raise ExtractorError('Unable to recognize tab page')
2964 if video_id and playlist_id:
2965 if self._downloader.params.get('noplaylist'):
2966 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
2967 return self.url_result(video_id, ie=YoutubeIE.ie_key(), video_id=video_id)
2968 self.to_screen('Downloading playlist %s - add --no-playlist to just download video %s' % (playlist_id, video_id))
2970 webpage = self._download_webpage(url, item_id)
2971 identity_token = self._extract_identity_token(webpage, item_id)
2972 data = self._extract_yt_initial_data(item_id, webpage)
2974 for alert_type, alert_message in self._extract_alerts(data):
2975 if alert_type.lower() == 'error':
2977 self._downloader.report_warning('YouTube said: %s - %s' % ('ERROR', err_msg))
2978 err_msg = alert_message
2980 self._downloader.report_warning('YouTube said: %s - %s' % (alert_type, alert_message))
2982 raise ExtractorError('YouTube said: %s' % err_msg, expected=True)
2984 data, lambda x: x['contents']['twoColumnBrowseResultsRenderer']['tabs'], list)
2986 return self._extract_from_tabs(item_id, webpage, data, tabs, identity_token)
2988 data, lambda x: x['contents']['twoColumnWatchNextResults']['playlist']['playlist'], dict)
2990 return self._extract_from_playlist(item_id, url, data, playlist)
2991 # Fallback to video extraction if no playlist alike page is recognized.
2992 # First check for the current video then try the v attribute of URL query.
2994 data, lambda x: x['currentVideoEndpoint']['watchEndpoint']['videoId'],
2995 compat_str) or video_id
2997 return self.url_result(video_id, ie=YoutubeIE.ie_key(), video_id=video_id)
2998 # Failed to recognize
2999 raise ExtractorError('Unable to recognize tab page')
3002 class YoutubePlaylistIE(InfoExtractor):
3003 IE_DESC = 'YouTube.com playlists'
3004 _VALID_URL = r'''(?x
)(?
:
3009 youtube(?
:kids
)?\
.com|
3014 (?P
<id>%(playlist_id)s)
3015 )''' % {'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE}
3016 IE_NAME = 'youtube:playlist'
3018 'note': 'issue #673',
3019 'url': 'PLBB231211A4F62143',
3021 'title': '[OLD]Team Fortress 2 (Class-based LP)',
3022 'id': 'PLBB231211A4F62143',
3023 'uploader': 'Wickydoo',
3024 'uploader_id': 'UCKSpbfbl5kRQpTdL7kMc-1Q',
3026 'playlist_mincount': 29,
3028 'url': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
3030 'title': 'YDL_safe_search',
3031 'id': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
3033 'playlist_count': 2,
3034 'skip': 'This playlist is private',
3037 'url': 'https://www.youtube.com/embed/videoseries?list=PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
3038 'playlist_count': 4,
3041 'id': 'PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
3042 'uploader': 'milan',
3043 'uploader_id': 'UCEI1-PVPcYXjB73Hfelbmaw',
3046 'url': 'http://www.youtube.com/embed/_xDOZElKyNU?list=PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
3047 'playlist_mincount': 982,
3049 'title': '2018 Chinese New Singles (11/6 updated)',
3050 'id': 'PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
3052 'uploader_id': 'UC21nz3_MesPLqtDqwdvnoxA',
3055 'url': 'TLGGrESM50VT6acwMjAyMjAxNw',
3056 'only_matching': True,
3058 # music album playlist
3059 'url': 'OLAK5uy_m4xAFdmMC5rX3Ji3g93pQe3hqLZw_9LhM',
3060 'only_matching': True,
3064 def suitable(cls, url):
3065 return False if YoutubeTabIE.suitable(url) else super(
3066 YoutubePlaylistIE, cls).suitable(url)
3068 def _real_extract(self, url):
3069 playlist_id = self._match_id(url)
3070 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
3072 qs = {'list': playlist_id}
3073 return self.url_result(
3074 update_url_query('https://www.youtube.com/playlist', qs),
3075 ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
3078 class YoutubeYtBeIE(InfoExtractor):
3079 IE_DESC = 'youtu.be'
3080 _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}
3082 'url': 'https://youtu.be/yeWKywCrFtk?list=PL2qgrgXsNUG5ig9cat4ohreBjYLAPC0J5',
3084 'id': 'yeWKywCrFtk',
3086 'title': 'Small Scale Baler and Braiding Rugs',
3087 'uploader': 'Backus-Page House Museum',
3088 'uploader_id': 'backuspagemuseum',
3089 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/backuspagemuseum',
3090 'upload_date': '20161008',
3091 'description': 'md5:800c0c78d5eb128500bffd4f0b4f2e8a',
3092 'categories': ['Nonprofits & Activism'],
3095 'dislike_count': int,
3099 'skip_download': True,
3102 'url': 'https://youtu.be/uWyaPkt-VOI?list=PL9D9FC436B881BA21',
3103 'only_matching': True,
3106 def _real_extract(self, url):
3107 mobj = re.match(self._VALID_URL, url)
3108 video_id = mobj.group('id')
3109 playlist_id = mobj.group('playlist_id')
3110 return self.url_result(
3111 update_url_query('https://www.youtube.com/watch', {
3113 'list': playlist_id,
3114 'feature': 'youtu.be',
3115 }), ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
3118 class YoutubeYtUserIE(InfoExtractor):
3119 IE_DESC = 'YouTube.com user videos, URL or "ytuser" keyword'
3120 _VALID_URL = r'ytuser:(?P<id>.+)'
3122 'url': 'ytuser:phihag',
3123 'only_matching': True,
3126 def _real_extract(self, url):
3127 user_id = self._match_id(url)
3128 return self.url_result(
3129 'https://www.youtube.com/user/%s' % user_id,
3130 ie=YoutubeTabIE.ie_key(), video_id=user_id)
3133 class YoutubeFavouritesIE(YoutubeBaseInfoExtractor):
3134 IE_NAME = 'youtube:favorites'
3135 IE_DESC = 'YouTube.com liked videos, ":ytfav" for short (requires authentication)'
3136 _VALID_URL = r':ytfav(?:ou?rite)?s?'
3137 _LOGIN_REQUIRED = True
3140 'only_matching': True,
3142 'url': ':ytfavorites',
3143 'only_matching': True,
3146 def _real_extract(self, url):
3147 return self.url_result(
3148 'https://www.youtube.com/playlist?list=LL',
3149 ie=YoutubeTabIE.ie_key())
3152 class YoutubeSearchIE(SearchInfoExtractor, YoutubeBaseInfoExtractor):
3153 IE_DESC = 'YouTube.com searches, "ytsearch" keyword'
3154 # there doesn't appear to be a real limit, for example if you search for
3155 # 'python' you get more than 8.000.000 results
3156 _MAX_RESULTS = float('inf')
3157 IE_NAME = 'youtube:search'
3158 _SEARCH_KEY = 'ytsearch'
3159 _SEARCH_PARAMS = None
3162 def _entries(self, query, n):
3166 'clientName': 'WEB',
3167 'clientVersion': '2.20201021.03.00',
3172 if self._SEARCH_PARAMS:
3173 data['params'] = self._SEARCH_PARAMS
3175 for page_num in itertools.count(1):
3176 search = self._download_json(
3177 'https://www.youtube.com/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
3178 video_id='query "%s"' % query,
3179 note='Downloading page %s' % page_num,
3180 errnote='Unable to download API page', fatal=False,
3181 data=json.dumps(data).encode('utf8'),
3182 headers={'content-type': 'application/json'})
3185 slr_contents = try_get(
3187 (lambda x: x['contents']['twoColumnSearchResultsRenderer']['primaryContents']['sectionListRenderer']['contents'],
3188 lambda x: x['onResponseReceivedCommands'][0]['appendContinuationItemsAction']['continuationItems']),
3190 if not slr_contents:
3193 # Youtube sometimes adds promoted content to searches,
3194 # changing the index location of videos and token.
3195 # So we search through all entries till we find them.
3196 continuation_token = None
3197 for slr_content in slr_contents:
3198 if continuation_token is None:
3199 continuation_token = try_get(
3201 lambda x: x['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'],
3204 isr_contents = try_get(
3206 lambda x: x['itemSectionRenderer']['contents'],
3208 if not isr_contents:
3210 for content in isr_contents:
3211 if not isinstance(content, dict):
3213 video = content.get('videoRenderer')
3214 if not isinstance(video, dict):
3216 video_id = video.get('videoId')
3220 yield self._extract_video(video)
3225 if not continuation_token:
3227 data['continuation'] = continuation_token
3229 def _get_n_results(self, query, n):
3230 """Get a specified number of results for a query"""
3231 return self.playlist_result(self._entries(query, n), query)
3234 class YoutubeSearchDateIE(YoutubeSearchIE):
3235 IE_NAME = YoutubeSearchIE.IE_NAME + ':date'
3236 _SEARCH_KEY = 'ytsearchdate'
3237 IE_DESC = 'YouTube.com searches, newest videos first, "ytsearchdate" keyword'
3238 _SEARCH_PARAMS = 'CAI%3D'
3241 class YoutubeSearchURLIE(YoutubeSearchIE):
3242 IE_DESC = 'YouTube.com search URLs'
3243 IE_NAME = YoutubeSearchIE.IE_NAME + '_url'
3244 _VALID_URL = r'https?://(?:www\.)?youtube\.com/results\?(.*?&)?(?:search_query|q)=(?:[^&]+)(?:[&]|$)'
3245 # _MAX_RESULTS = 100
3247 'url': 'https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video',
3248 'playlist_mincount': 5,
3250 'title': 'youtube-dl test video',
3253 'url': 'https://www.youtube.com/results?q=test&sp=EgQIBBgB',
3254 'only_matching': True,
3258 def _make_valid_url(cls):
3259 return cls._VALID_URL
3261 def _real_extract(self, url):
3262 qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
3263 query = (qs.get('search_query') or qs.get('q'))[0]
3264 self._SEARCH_PARAMS = qs.get('sp', ('',))[0]
3265 return self._get_n_results(query, self._MAX_RESULTS)
3268 class YoutubeFeedsInfoExtractor(YoutubeTabIE):
3270 Base class for feed extractors
3271 Subclasses must define the _FEED_NAME property.
3273 _LOGIN_REQUIRED = True
3279 return 'youtube:%s' % self._FEED_NAME
3281 def _real_initialize(self):
3284 def _real_extract(self, url):
3285 return self.url_result(
3286 'https://www.youtube.com/feed/%s' % self._FEED_NAME,
3287 ie=YoutubeTabIE.ie_key())
3290 class YoutubeWatchLaterIE(InfoExtractor):
3291 IE_NAME = 'youtube:watchlater'
3292 IE_DESC = 'Youtube watch later list, ":ytwatchlater" for short (requires authentication)'
3293 _VALID_URL = r':ytwatchlater'
3295 'url': ':ytwatchlater',
3296 'only_matching': True,
3299 def _real_extract(self, url):
3300 return self.url_result(
3301 'https://www.youtube.com/playlist?list=WL', ie=YoutubeTabIE.ie_key())
3304 class YoutubeRecommendedIE(YoutubeFeedsInfoExtractor):
3305 IE_DESC = 'YouTube.com recommended videos, ":ytrec" for short (requires authentication)'
3306 _VALID_URL = r'https?://(?:www\.)?youtube\.com/?(?:[?#]|$)|:ytrec(?:ommended)?'
3307 _FEED_NAME = 'recommended'
3310 'only_matching': True,
3312 'url': ':ytrecommended',
3313 'only_matching': True,
3315 'url': 'https://youtube.com',
3316 'only_matching': True,
3320 class YoutubeSubscriptionsIE(YoutubeFeedsInfoExtractor):
3321 IE_DESC = 'YouTube.com subscriptions feed, ":ytsubs" for short (requires authentication)'
3322 _VALID_URL = r':ytsub(?:scription)?s?'
3323 _FEED_NAME = 'subscriptions'
3326 'only_matching': True,
3328 'url': ':ytsubscriptions',
3329 'only_matching': True,
3333 class YoutubeHistoryIE(YoutubeFeedsInfoExtractor):
3334 IE_DESC = 'Youtube watch history, ":ythistory" for short (requires authentication)'
3335 _VALID_URL = r':ythistory'
3336 _FEED_NAME = 'history'
3338 'url': ':ythistory',
3339 'only_matching': True,
3343 class YoutubeTruncatedURLIE(InfoExtractor):
3344 IE_NAME = 'youtube:truncated_url'
3345 IE_DESC = False # Do not list
3346 _VALID_URL = r'''(?x
)
3348 (?
:\w
+\
.)?
[yY
][oO
][uU
][tT
][uU
][bB
][eE
](?
:-nocookie
)?\
.com
/
3351 annotation_id
=annotation_
[^
&]+|
3357 attribution_link
\?a
=[^
&]+
3363 'url': 'https://www.youtube.com/watch?annotation_id=annotation_3951667041',
3364 'only_matching': True,
3366 'url': 'https://www.youtube.com/watch?',
3367 'only_matching': True,
3369 'url': 'https://www.youtube.com/watch?x-yt-cl=84503534',
3370 'only_matching': True,
3372 'url': 'https://www.youtube.com/watch?feature=foo',
3373 'only_matching': True,
3375 'url': 'https://www.youtube.com/watch?hl=en-GB',
3376 'only_matching': True,
3378 'url': 'https://www.youtube.com/watch?t=2372',
3379 'only_matching': True,
3382 def _real_extract(self, url):
3383 raise ExtractorError(
3384 'Did you forget to quote the URL? Remember that & is a meta '
3385 'character in most shells, so you want to put the URL in quotes, '
3387 '"https://www.youtube.com/watch?feature=foo&v=BaW_jenozKc" '
3388 ' or simply youtube-dl BaW_jenozKc .',
3392 class YoutubeTruncatedIDIE(InfoExtractor):
3393 IE_NAME = 'youtube:truncated_id'
3394 IE_DESC = False # Do not list
3395 _VALID_URL = r'https?://(?:www\.)?youtube\.com/watch\?v=(?P<id>[0-9A-Za-z_-]{1,10})$'
3398 'url': 'https://www.youtube.com/watch?v=N_708QY7Ob',
3399 'only_matching': True,
3402 def _real_extract(self, url):
3403 video_id = self._match_id(url)
3404 raise ExtractorError(
3405 'Incomplete YouTube ID %s. URL %s looks truncated.' % (video_id, url),