]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/youtube.py
8a2dd728c2cb78fe07a5fa92591cd7edb94bfbcc
[yt-dlp.git] / yt_dlp / extractor / youtube.py
1 import base64
2 import calendar
3 import collections
4 import copy
5 import datetime
6 import enum
7 import hashlib
8 import itertools
9 import json
10 import math
11 import os.path
12 import random
13 import re
14 import sys
15 import threading
16 import time
17 import traceback
18 import urllib.error
19 import urllib.parse
20
21 from .common import InfoExtractor, SearchInfoExtractor
22 from .openload import PhantomJSwrapper
23 from ..compat import functools
24 from ..jsinterp import JSInterpreter
25 from ..utils import (
26 NO_DEFAULT,
27 ExtractorError,
28 LazyList,
29 UserNotLive,
30 bug_reports_message,
31 classproperty,
32 clean_html,
33 datetime_from_str,
34 dict_get,
35 filter_dict,
36 float_or_none,
37 format_field,
38 get_first,
39 int_or_none,
40 is_html,
41 join_nonempty,
42 js_to_json,
43 mimetype2ext,
44 network_exceptions,
45 orderedSet,
46 parse_codecs,
47 parse_count,
48 parse_duration,
49 parse_iso8601,
50 parse_qs,
51 qualities,
52 remove_start,
53 smuggle_url,
54 str_or_none,
55 str_to_int,
56 strftime_or_none,
57 traverse_obj,
58 try_get,
59 unescapeHTML,
60 unified_strdate,
61 unified_timestamp,
62 unsmuggle_url,
63 update_url_query,
64 url_or_none,
65 urljoin,
66 variadic,
67 )
68
69 # any clients starting with _ cannot be explicitly requested by the user
70 INNERTUBE_CLIENTS = {
71 'web': {
72 'INNERTUBE_API_KEY': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
73 'INNERTUBE_CONTEXT': {
74 'client': {
75 'clientName': 'WEB',
76 'clientVersion': '2.20220801.00.00',
77 }
78 },
79 'INNERTUBE_CONTEXT_CLIENT_NAME': 1
80 },
81 'web_embedded': {
82 'INNERTUBE_API_KEY': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
83 'INNERTUBE_CONTEXT': {
84 'client': {
85 'clientName': 'WEB_EMBEDDED_PLAYER',
86 'clientVersion': '1.20220731.00.00',
87 },
88 },
89 'INNERTUBE_CONTEXT_CLIENT_NAME': 56
90 },
91 'web_music': {
92 'INNERTUBE_API_KEY': 'AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30',
93 'INNERTUBE_HOST': 'music.youtube.com',
94 'INNERTUBE_CONTEXT': {
95 'client': {
96 'clientName': 'WEB_REMIX',
97 'clientVersion': '1.20220727.01.00',
98 }
99 },
100 'INNERTUBE_CONTEXT_CLIENT_NAME': 67,
101 },
102 'web_creator': {
103 'INNERTUBE_API_KEY': 'AIzaSyBUPetSUmoZL-OhlxA7wSac5XinrygCqMo',
104 'INNERTUBE_CONTEXT': {
105 'client': {
106 'clientName': 'WEB_CREATOR',
107 'clientVersion': '1.20220726.00.00',
108 }
109 },
110 'INNERTUBE_CONTEXT_CLIENT_NAME': 62,
111 },
112 'android': {
113 'INNERTUBE_API_KEY': 'AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w',
114 'INNERTUBE_CONTEXT': {
115 'client': {
116 'clientName': 'ANDROID',
117 'clientVersion': '17.31.35',
118 'androidSdkVersion': 30,
119 'userAgent': 'com.google.android.youtube/17.31.35 (Linux; U; Android 11) gzip'
120 }
121 },
122 'INNERTUBE_CONTEXT_CLIENT_NAME': 3,
123 'REQUIRE_JS_PLAYER': False
124 },
125 'android_embedded': {
126 'INNERTUBE_API_KEY': 'AIzaSyCjc_pVEDi4qsv5MtC2dMXzpIaDoRFLsxw',
127 'INNERTUBE_CONTEXT': {
128 'client': {
129 'clientName': 'ANDROID_EMBEDDED_PLAYER',
130 'clientVersion': '17.31.35',
131 'androidSdkVersion': 30,
132 'userAgent': 'com.google.android.youtube/17.31.35 (Linux; U; Android 11) gzip'
133 },
134 },
135 'INNERTUBE_CONTEXT_CLIENT_NAME': 55,
136 'REQUIRE_JS_PLAYER': False
137 },
138 'android_music': {
139 'INNERTUBE_API_KEY': 'AIzaSyAOghZGza2MQSZkY_zfZ370N-PUdXEo8AI',
140 'INNERTUBE_CONTEXT': {
141 'client': {
142 'clientName': 'ANDROID_MUSIC',
143 'clientVersion': '5.16.51',
144 'androidSdkVersion': 30,
145 'userAgent': 'com.google.android.apps.youtube.music/5.16.51 (Linux; U; Android 11) gzip'
146 }
147 },
148 'INNERTUBE_CONTEXT_CLIENT_NAME': 21,
149 'REQUIRE_JS_PLAYER': False
150 },
151 'android_creator': {
152 'INNERTUBE_API_KEY': 'AIzaSyD_qjV8zaaUMehtLkrKFgVeSX_Iqbtyws8',
153 'INNERTUBE_CONTEXT': {
154 'client': {
155 'clientName': 'ANDROID_CREATOR',
156 'clientVersion': '22.30.100',
157 'androidSdkVersion': 30,
158 'userAgent': 'com.google.android.apps.youtube.creator/22.30.100 (Linux; U; Android 11) gzip'
159 },
160 },
161 'INNERTUBE_CONTEXT_CLIENT_NAME': 14,
162 'REQUIRE_JS_PLAYER': False
163 },
164 # iOS clients have HLS live streams. Setting device model to get 60fps formats.
165 # See: https://github.com/TeamNewPipe/NewPipeExtractor/issues/680#issuecomment-1002724558
166 'ios': {
167 'INNERTUBE_API_KEY': 'AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc',
168 'INNERTUBE_CONTEXT': {
169 'client': {
170 'clientName': 'IOS',
171 'clientVersion': '17.33.2',
172 'deviceModel': 'iPhone14,3',
173 'userAgent': 'com.google.ios.youtube/17.33.2 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)'
174 }
175 },
176 'INNERTUBE_CONTEXT_CLIENT_NAME': 5,
177 'REQUIRE_JS_PLAYER': False
178 },
179 'ios_embedded': {
180 'INNERTUBE_CONTEXT': {
181 'client': {
182 'clientName': 'IOS_MESSAGES_EXTENSION',
183 'clientVersion': '17.33.2',
184 'deviceModel': 'iPhone14,3',
185 'userAgent': 'com.google.ios.youtube/17.33.2 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)'
186 },
187 },
188 'INNERTUBE_CONTEXT_CLIENT_NAME': 66,
189 'REQUIRE_JS_PLAYER': False
190 },
191 'ios_music': {
192 'INNERTUBE_API_KEY': 'AIzaSyBAETezhkwP0ZWA02RsqT1zu78Fpt0bC_s',
193 'INNERTUBE_CONTEXT': {
194 'client': {
195 'clientName': 'IOS_MUSIC',
196 'clientVersion': '5.21',
197 'deviceModel': 'iPhone14,3',
198 'userAgent': 'com.google.ios.youtubemusic/5.21 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)'
199 },
200 },
201 'INNERTUBE_CONTEXT_CLIENT_NAME': 26,
202 'REQUIRE_JS_PLAYER': False
203 },
204 'ios_creator': {
205 'INNERTUBE_CONTEXT': {
206 'client': {
207 'clientName': 'IOS_CREATOR',
208 'clientVersion': '22.33.101',
209 'deviceModel': 'iPhone14,3',
210 'userAgent': 'com.google.ios.ytcreator/22.33.101 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)'
211 },
212 },
213 'INNERTUBE_CONTEXT_CLIENT_NAME': 15,
214 'REQUIRE_JS_PLAYER': False
215 },
216 # mweb has 'ultralow' formats
217 # See: https://github.com/yt-dlp/yt-dlp/pull/557
218 'mweb': {
219 'INNERTUBE_API_KEY': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
220 'INNERTUBE_CONTEXT': {
221 'client': {
222 'clientName': 'MWEB',
223 'clientVersion': '2.20220801.00.00',
224 }
225 },
226 'INNERTUBE_CONTEXT_CLIENT_NAME': 2
227 },
228 # This client can access age restricted videos (unless the uploader has disabled the 'allow embedding' option)
229 # See: https://github.com/zerodytrash/YouTube-Internal-Clients
230 'tv_embedded': {
231 'INNERTUBE_API_KEY': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
232 'INNERTUBE_CONTEXT': {
233 'client': {
234 'clientName': 'TVHTML5_SIMPLY_EMBEDDED_PLAYER',
235 'clientVersion': '2.0',
236 },
237 },
238 'INNERTUBE_CONTEXT_CLIENT_NAME': 85
239 },
240 }
241
242
243 def _split_innertube_client(client_name):
244 variant, *base = client_name.rsplit('.', 1)
245 if base:
246 return variant, base[0], variant
247 base, *variant = client_name.split('_', 1)
248 return client_name, base, variant[0] if variant else None
249
250
251 def build_innertube_clients():
252 THIRD_PARTY = {
253 'embedUrl': 'https://www.youtube.com/', # Can be any valid URL
254 }
255 BASE_CLIENTS = ('android', 'web', 'tv', 'ios', 'mweb')
256 priority = qualities(BASE_CLIENTS[::-1])
257
258 for client, ytcfg in tuple(INNERTUBE_CLIENTS.items()):
259 ytcfg.setdefault('INNERTUBE_API_KEY', 'AIzaSyDCU8hByM-4DrUqRUYnGn-3llEO78bcxq8')
260 ytcfg.setdefault('INNERTUBE_HOST', 'www.youtube.com')
261 ytcfg.setdefault('REQUIRE_JS_PLAYER', True)
262 ytcfg['INNERTUBE_CONTEXT']['client'].setdefault('hl', 'en')
263
264 _, base_client, variant = _split_innertube_client(client)
265 ytcfg['priority'] = 10 * priority(base_client)
266
267 if not variant:
268 INNERTUBE_CLIENTS[f'{client}_embedscreen'] = embedscreen = copy.deepcopy(ytcfg)
269 embedscreen['INNERTUBE_CONTEXT']['client']['clientScreen'] = 'EMBED'
270 embedscreen['INNERTUBE_CONTEXT']['thirdParty'] = THIRD_PARTY
271 embedscreen['priority'] -= 3
272 elif variant == 'embedded':
273 ytcfg['INNERTUBE_CONTEXT']['thirdParty'] = THIRD_PARTY
274 ytcfg['priority'] -= 2
275 else:
276 ytcfg['priority'] -= 3
277
278
279 build_innertube_clients()
280
281
282 class BadgeType(enum.Enum):
283 AVAILABILITY_UNLISTED = enum.auto()
284 AVAILABILITY_PRIVATE = enum.auto()
285 AVAILABILITY_PUBLIC = enum.auto()
286 AVAILABILITY_PREMIUM = enum.auto()
287 AVAILABILITY_SUBSCRIPTION = enum.auto()
288 LIVE_NOW = enum.auto()
289
290
291 class YoutubeBaseInfoExtractor(InfoExtractor):
292 """Provide base functions for Youtube extractors"""
293
294 _RESERVED_NAMES = (
295 r'channel|c|user|playlist|watch|w|v|embed|e|watch_popup|clip|'
296 r'shorts|movies|results|search|shared|hashtag|trending|explore|feed|feeds|'
297 r'browse|oembed|get_video_info|iframe_api|s/player|source|'
298 r'storefront|oops|index|account|t/terms|about|upload|signin|logout')
299
300 _PLAYLIST_ID_RE = r'(?:(?:PL|LL|EC|UU|FL|RD|UL|TL|PU|OLAK5uy_)[0-9A-Za-z-_]{10,}|RDMM|WL|LL|LM)'
301
302 # _NETRC_MACHINE = 'youtube'
303
304 # If True it will raise an error if no login info is provided
305 _LOGIN_REQUIRED = False
306
307 _INVIDIOUS_SITES = (
308 # invidious-redirect websites
309 r'(?:www\.)?redirect\.invidious\.io',
310 r'(?:(?:www|dev)\.)?invidio\.us',
311 # Invidious instances taken from https://github.com/iv-org/documentation/blob/master/docs/instances.md
312 r'(?:www\.)?invidious\.pussthecat\.org',
313 r'(?:www\.)?invidious\.zee\.li',
314 r'(?:www\.)?invidious\.ethibox\.fr',
315 r'(?:www\.)?invidious\.3o7z6yfxhbw7n3za4rss6l434kmv55cgw2vuziwuigpwegswvwzqipyd\.onion',
316 r'(?:www\.)?osbivz6guyeahrwp2lnwyjk2xos342h4ocsxyqrlaopqjuhwn2djiiyd\.onion',
317 r'(?:www\.)?u2cvlit75owumwpy4dj2hsmvkq7nvrclkpht7xgyye2pyoxhpmclkrad\.onion',
318 # youtube-dl invidious instances list
319 r'(?:(?:www|no)\.)?invidiou\.sh',
320 r'(?:(?:www|fi)\.)?invidious\.snopyta\.org',
321 r'(?:www\.)?invidious\.kabi\.tk',
322 r'(?:www\.)?invidious\.mastodon\.host',
323 r'(?:www\.)?invidious\.zapashcanon\.fr',
324 r'(?:www\.)?(?:invidious(?:-us)?|piped)\.kavin\.rocks',
325 r'(?:www\.)?invidious\.tinfoil-hat\.net',
326 r'(?:www\.)?invidious\.himiko\.cloud',
327 r'(?:www\.)?invidious\.reallyancient\.tech',
328 r'(?:www\.)?invidious\.tube',
329 r'(?:www\.)?invidiou\.site',
330 r'(?:www\.)?invidious\.site',
331 r'(?:www\.)?invidious\.xyz',
332 r'(?:www\.)?invidious\.nixnet\.xyz',
333 r'(?:www\.)?invidious\.048596\.xyz',
334 r'(?:www\.)?invidious\.drycat\.fr',
335 r'(?:www\.)?inv\.skyn3t\.in',
336 r'(?:www\.)?tube\.poal\.co',
337 r'(?:www\.)?tube\.connect\.cafe',
338 r'(?:www\.)?vid\.wxzm\.sx',
339 r'(?:www\.)?vid\.mint\.lgbt',
340 r'(?:www\.)?vid\.puffyan\.us',
341 r'(?:www\.)?yewtu\.be',
342 r'(?:www\.)?yt\.elukerio\.org',
343 r'(?:www\.)?yt\.lelux\.fi',
344 r'(?:www\.)?invidious\.ggc-project\.de',
345 r'(?:www\.)?yt\.maisputain\.ovh',
346 r'(?:www\.)?ytprivate\.com',
347 r'(?:www\.)?invidious\.13ad\.de',
348 r'(?:www\.)?invidious\.toot\.koeln',
349 r'(?:www\.)?invidious\.fdn\.fr',
350 r'(?:www\.)?watch\.nettohikari\.com',
351 r'(?:www\.)?invidious\.namazso\.eu',
352 r'(?:www\.)?invidious\.silkky\.cloud',
353 r'(?:www\.)?invidious\.exonip\.de',
354 r'(?:www\.)?invidious\.riverside\.rocks',
355 r'(?:www\.)?invidious\.blamefran\.net',
356 r'(?:www\.)?invidious\.moomoo\.de',
357 r'(?:www\.)?ytb\.trom\.tf',
358 r'(?:www\.)?yt\.cyberhost\.uk',
359 r'(?:www\.)?kgg2m7yk5aybusll\.onion',
360 r'(?:www\.)?qklhadlycap4cnod\.onion',
361 r'(?:www\.)?axqzx4s6s54s32yentfqojs3x5i7faxza6xo3ehd4bzzsg2ii4fv2iid\.onion',
362 r'(?:www\.)?c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid\.onion',
363 r'(?:www\.)?fz253lmuao3strwbfbmx46yu7acac2jz27iwtorgmbqlkurlclmancad\.onion',
364 r'(?:www\.)?invidious\.l4qlywnpwqsluw65ts7md3khrivpirse744un3x7mlskqauz5pyuzgqd\.onion',
365 r'(?:www\.)?owxfohz4kjyv25fvlqilyxast7inivgiktls3th44jhk3ej3i7ya\.b32\.i2p',
366 r'(?:www\.)?4l2dgddgsrkf2ous66i6seeyi6etzfgrue332grh2n7madpwopotugyd\.onion',
367 r'(?:www\.)?w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd\.onion',
368 r'(?:www\.)?kbjggqkzv65ivcqj6bumvp337z6264huv5kpkwuv6gu5yjiskvan7fad\.onion',
369 r'(?:www\.)?grwp24hodrefzvjjuccrkw3mjq4tzhaaq32amf33dzpmuxe7ilepcmad\.onion',
370 r'(?:www\.)?hpniueoejy4opn7bc4ftgazyqjoeqwlvh2uiku2xqku6zpoa4bf5ruid\.onion',
371 # piped instances from https://github.com/TeamPiped/Piped/wiki/Instances
372 r'(?:www\.)?piped\.kavin\.rocks',
373 r'(?:www\.)?piped\.tokhmi\.xyz',
374 r'(?:www\.)?piped\.syncpundit\.io',
375 r'(?:www\.)?piped\.mha\.fi',
376 r'(?:www\.)?watch\.whatever\.social',
377 r'(?:www\.)?piped\.garudalinux\.org',
378 r'(?:www\.)?piped\.rivo\.lol',
379 r'(?:www\.)?piped-libre\.kavin\.rocks',
380 r'(?:www\.)?yt\.jae\.fi',
381 r'(?:www\.)?piped\.mint\.lgbt',
382 r'(?:www\.)?il\.ax',
383 r'(?:www\.)?piped\.esmailelbob\.xyz',
384 r'(?:www\.)?piped\.projectsegfau\.lt',
385 r'(?:www\.)?piped\.privacydev\.net',
386 r'(?:www\.)?piped\.palveluntarjoaja\.eu',
387 r'(?:www\.)?piped\.smnz\.de',
388 r'(?:www\.)?piped\.adminforge\.de',
389 r'(?:www\.)?watch\.whatevertinfoil\.de',
390 r'(?:www\.)?piped\.qdi\.fi',
391 )
392
393 # extracted from account/account_menu ep
394 # XXX: These are the supported YouTube UI and API languages,
395 # which is slightly different from languages supported for translation in YouTube studio
396 _SUPPORTED_LANG_CODES = [
397 'af', 'az', 'id', 'ms', 'bs', 'ca', 'cs', 'da', 'de', 'et', 'en-IN', 'en-GB', 'en', 'es',
398 'es-419', 'es-US', 'eu', 'fil', 'fr', 'fr-CA', 'gl', 'hr', 'zu', 'is', 'it', 'sw', 'lv',
399 'lt', 'hu', 'nl', 'no', 'uz', 'pl', 'pt-PT', 'pt', 'ro', 'sq', 'sk', 'sl', 'sr-Latn', 'fi',
400 'sv', 'vi', 'tr', 'be', 'bg', 'ky', 'kk', 'mk', 'mn', 'ru', 'sr', 'uk', 'el', 'hy', 'iw',
401 'ur', 'ar', 'fa', 'ne', 'mr', 'hi', 'as', 'bn', 'pa', 'gu', 'or', 'ta', 'te', 'kn', 'ml',
402 'si', 'th', 'lo', 'my', 'ka', 'am', 'km', 'zh-CN', 'zh-TW', 'zh-HK', 'ja', 'ko'
403 ]
404
405 _IGNORED_WARNINGS = {'Unavailable videos will be hidden during playback'}
406
407 @functools.cached_property
408 def _preferred_lang(self):
409 """
410 Returns a language code supported by YouTube for the user preferred language.
411 Returns None if no preferred language set.
412 """
413 preferred_lang = self._configuration_arg('lang', ie_key='Youtube', casesense=True, default=[''])[0]
414 if not preferred_lang:
415 return
416 if preferred_lang not in self._SUPPORTED_LANG_CODES:
417 raise ExtractorError(
418 f'Unsupported language code: {preferred_lang}. Supported language codes (case-sensitive): {join_nonempty(*self._SUPPORTED_LANG_CODES, delim=", ")}.',
419 expected=True)
420 elif preferred_lang != 'en':
421 self.report_warning(
422 f'Preferring "{preferred_lang}" translated fields. Note that some metadata extraction may fail or be incorrect.')
423 return preferred_lang
424
425 def _initialize_consent(self):
426 cookies = self._get_cookies('https://www.youtube.com/')
427 if cookies.get('__Secure-3PSID'):
428 return
429 consent_id = None
430 consent = cookies.get('CONSENT')
431 if consent:
432 if 'YES' in consent.value:
433 return
434 consent_id = self._search_regex(
435 r'PENDING\+(\d+)', consent.value, 'consent', default=None)
436 if not consent_id:
437 consent_id = random.randint(100, 999)
438 self._set_cookie('.youtube.com', 'CONSENT', 'YES+cb.20210328-17-p0.en+FX+%s' % consent_id)
439
440 def _initialize_pref(self):
441 cookies = self._get_cookies('https://www.youtube.com/')
442 pref_cookie = cookies.get('PREF')
443 pref = {}
444 if pref_cookie:
445 try:
446 pref = dict(urllib.parse.parse_qsl(pref_cookie.value))
447 except ValueError:
448 self.report_warning('Failed to parse user PREF cookie' + bug_reports_message())
449 pref.update({'hl': self._preferred_lang or 'en', 'tz': 'UTC'})
450 self._set_cookie('.youtube.com', name='PREF', value=urllib.parse.urlencode(pref))
451
452 def _real_initialize(self):
453 self._initialize_pref()
454 self._initialize_consent()
455 self._check_login_required()
456
457 def _check_login_required(self):
458 if self._LOGIN_REQUIRED and not self._cookies_passed:
459 self.raise_login_required('Login details are needed to download this content', method='cookies')
460
461 _YT_INITIAL_DATA_RE = r'(?:window\s*\[\s*["\']ytInitialData["\']\s*\]|ytInitialData)\s*='
462 _YT_INITIAL_PLAYER_RESPONSE_RE = r'ytInitialPlayerResponse\s*='
463
464 def _get_default_ytcfg(self, client='web'):
465 return copy.deepcopy(INNERTUBE_CLIENTS[client])
466
467 def _get_innertube_host(self, client='web'):
468 return INNERTUBE_CLIENTS[client]['INNERTUBE_HOST']
469
470 def _ytcfg_get_safe(self, ytcfg, getter, expected_type=None, default_client='web'):
471 # try_get but with fallback to default ytcfg client values when present
472 _func = lambda y: try_get(y, getter, expected_type)
473 return _func(ytcfg) or _func(self._get_default_ytcfg(default_client))
474
475 def _extract_client_name(self, ytcfg, default_client='web'):
476 return self._ytcfg_get_safe(
477 ytcfg, (lambda x: x['INNERTUBE_CLIENT_NAME'],
478 lambda x: x['INNERTUBE_CONTEXT']['client']['clientName']), str, default_client)
479
480 def _extract_client_version(self, ytcfg, default_client='web'):
481 return self._ytcfg_get_safe(
482 ytcfg, (lambda x: x['INNERTUBE_CLIENT_VERSION'],
483 lambda x: x['INNERTUBE_CONTEXT']['client']['clientVersion']), str, default_client)
484
485 def _select_api_hostname(self, req_api_hostname, default_client=None):
486 return (self._configuration_arg('innertube_host', [''], ie_key=YoutubeIE.ie_key())[0]
487 or req_api_hostname or self._get_innertube_host(default_client or 'web'))
488
489 def _extract_api_key(self, ytcfg=None, default_client='web'):
490 return self._ytcfg_get_safe(ytcfg, lambda x: x['INNERTUBE_API_KEY'], str, default_client)
491
492 def _extract_context(self, ytcfg=None, default_client='web'):
493 context = get_first(
494 (ytcfg, self._get_default_ytcfg(default_client)), 'INNERTUBE_CONTEXT', expected_type=dict)
495 # Enforce language and tz for extraction
496 client_context = traverse_obj(context, 'client', expected_type=dict, default={})
497 client_context.update({'hl': self._preferred_lang or 'en', 'timeZone': 'UTC', 'utcOffsetMinutes': 0})
498 return context
499
500 _SAPISID = None
501
502 def _generate_sapisidhash_header(self, origin='https://www.youtube.com'):
503 time_now = round(time.time())
504 if self._SAPISID is None:
505 yt_cookies = self._get_cookies('https://www.youtube.com')
506 # Sometimes SAPISID cookie isn't present but __Secure-3PAPISID is.
507 # See: https://github.com/yt-dlp/yt-dlp/issues/393
508 sapisid_cookie = dict_get(
509 yt_cookies, ('__Secure-3PAPISID', 'SAPISID'))
510 if sapisid_cookie and sapisid_cookie.value:
511 self._SAPISID = sapisid_cookie.value
512 self.write_debug('Extracted SAPISID cookie')
513 # SAPISID cookie is required if not already present
514 if not yt_cookies.get('SAPISID'):
515 self.write_debug('Copying __Secure-3PAPISID cookie to SAPISID cookie')
516 self._set_cookie(
517 '.youtube.com', 'SAPISID', self._SAPISID, secure=True, expire_time=time_now + 3600)
518 else:
519 self._SAPISID = False
520 if not self._SAPISID:
521 return None
522 # SAPISIDHASH algorithm from https://stackoverflow.com/a/32065323
523 sapisidhash = hashlib.sha1(
524 f'{time_now} {self._SAPISID} {origin}'.encode()).hexdigest()
525 return f'SAPISIDHASH {time_now}_{sapisidhash}'
526
527 def _call_api(self, ep, query, video_id, fatal=True, headers=None,
528 note='Downloading API JSON', errnote='Unable to download API page',
529 context=None, api_key=None, api_hostname=None, default_client='web'):
530
531 data = {'context': context} if context else {'context': self._extract_context(default_client=default_client)}
532 data.update(query)
533 real_headers = self.generate_api_headers(default_client=default_client)
534 real_headers.update({'content-type': 'application/json'})
535 if headers:
536 real_headers.update(headers)
537 api_key = (self._configuration_arg('innertube_key', [''], ie_key=YoutubeIE.ie_key(), casesense=True)[0]
538 or api_key or self._extract_api_key(default_client=default_client))
539 return self._download_json(
540 f'https://{self._select_api_hostname(api_hostname, default_client)}/youtubei/v1/{ep}',
541 video_id=video_id, fatal=fatal, note=note, errnote=errnote,
542 data=json.dumps(data).encode('utf8'), headers=real_headers,
543 query={'key': api_key, 'prettyPrint': 'false'})
544
545 def extract_yt_initial_data(self, item_id, webpage, fatal=True):
546 return self._search_json(self._YT_INITIAL_DATA_RE, webpage, 'yt initial data', item_id, fatal=fatal)
547
548 @staticmethod
549 def _extract_session_index(*data):
550 """
551 Index of current account in account list.
552 See: https://github.com/yt-dlp/yt-dlp/pull/519
553 """
554 for ytcfg in data:
555 session_index = int_or_none(try_get(ytcfg, lambda x: x['SESSION_INDEX']))
556 if session_index is not None:
557 return session_index
558
559 # Deprecated?
560 def _extract_identity_token(self, ytcfg=None, webpage=None):
561 if ytcfg:
562 token = try_get(ytcfg, lambda x: x['ID_TOKEN'], str)
563 if token:
564 return token
565 if webpage:
566 return self._search_regex(
567 r'\bID_TOKEN["\']\s*:\s*["\'](.+?)["\']', webpage,
568 'identity token', default=None, fatal=False)
569
570 @staticmethod
571 def _extract_account_syncid(*args):
572 """
573 Extract syncId required to download private playlists of secondary channels
574 @params response and/or ytcfg
575 """
576 for data in args:
577 # ytcfg includes channel_syncid if on secondary channel
578 delegated_sid = try_get(data, lambda x: x['DELEGATED_SESSION_ID'], str)
579 if delegated_sid:
580 return delegated_sid
581 sync_ids = (try_get(
582 data, (lambda x: x['responseContext']['mainAppWebResponseContext']['datasyncId'],
583 lambda x: x['DATASYNC_ID']), str) or '').split('||')
584 if len(sync_ids) >= 2 and sync_ids[1]:
585 # datasyncid is of the form "channel_syncid||user_syncid" for secondary channel
586 # and just "user_syncid||" for primary channel. We only want the channel_syncid
587 return sync_ids[0]
588
589 @staticmethod
590 def _extract_visitor_data(*args):
591 """
592 Extracts visitorData from an API response or ytcfg
593 Appears to be used to track session state
594 """
595 return get_first(
596 args, [('VISITOR_DATA', ('INNERTUBE_CONTEXT', 'client', 'visitorData'), ('responseContext', 'visitorData'))],
597 expected_type=str)
598
599 @functools.cached_property
600 def is_authenticated(self):
601 return bool(self._generate_sapisidhash_header())
602
603 def extract_ytcfg(self, video_id, webpage):
604 if not webpage:
605 return {}
606 return self._parse_json(
607 self._search_regex(
608 r'ytcfg\.set\s*\(\s*({.+?})\s*\)\s*;', webpage, 'ytcfg',
609 default='{}'), video_id, fatal=False) or {}
610
611 def generate_api_headers(
612 self, *, ytcfg=None, account_syncid=None, session_index=None,
613 visitor_data=None, identity_token=None, api_hostname=None, default_client='web'):
614
615 origin = 'https://' + (self._select_api_hostname(api_hostname, default_client))
616 headers = {
617 'X-YouTube-Client-Name': str(
618 self._ytcfg_get_safe(ytcfg, lambda x: x['INNERTUBE_CONTEXT_CLIENT_NAME'], default_client=default_client)),
619 'X-YouTube-Client-Version': self._extract_client_version(ytcfg, default_client),
620 'Origin': origin,
621 'X-Youtube-Identity-Token': identity_token or self._extract_identity_token(ytcfg),
622 'X-Goog-PageId': account_syncid or self._extract_account_syncid(ytcfg),
623 'X-Goog-Visitor-Id': visitor_data or self._extract_visitor_data(ytcfg),
624 'User-Agent': self._ytcfg_get_safe(ytcfg, lambda x: x['INNERTUBE_CONTEXT']['client']['userAgent'], default_client=default_client)
625 }
626 if session_index is None:
627 session_index = self._extract_session_index(ytcfg)
628 if account_syncid or session_index is not None:
629 headers['X-Goog-AuthUser'] = session_index if session_index is not None else 0
630
631 auth = self._generate_sapisidhash_header(origin)
632 if auth is not None:
633 headers['Authorization'] = auth
634 headers['X-Origin'] = origin
635 return filter_dict(headers)
636
637 def _download_ytcfg(self, client, video_id):
638 url = {
639 'web': 'https://www.youtube.com',
640 'web_music': 'https://music.youtube.com',
641 'web_embedded': f'https://www.youtube.com/embed/{video_id}?html5=1'
642 }.get(client)
643 if not url:
644 return {}
645 webpage = self._download_webpage(
646 url, video_id, fatal=False, note=f'Downloading {client.replace("_", " ").strip()} client config')
647 return self.extract_ytcfg(video_id, webpage) or {}
648
649 @staticmethod
650 def _build_api_continuation_query(continuation, ctp=None):
651 query = {
652 'continuation': continuation
653 }
654 # TODO: Inconsistency with clickTrackingParams.
655 # Currently we have a fixed ctp contained within context (from ytcfg)
656 # and a ctp in root query for continuation.
657 if ctp:
658 query['clickTracking'] = {'clickTrackingParams': ctp}
659 return query
660
661 @classmethod
662 def _extract_next_continuation_data(cls, renderer):
663 next_continuation = try_get(
664 renderer, (lambda x: x['continuations'][0]['nextContinuationData'],
665 lambda x: x['continuation']['reloadContinuationData']), dict)
666 if not next_continuation:
667 return
668 continuation = next_continuation.get('continuation')
669 if not continuation:
670 return
671 ctp = next_continuation.get('clickTrackingParams')
672 return cls._build_api_continuation_query(continuation, ctp)
673
674 @classmethod
675 def _extract_continuation_ep_data(cls, continuation_ep: dict):
676 if isinstance(continuation_ep, dict):
677 continuation = try_get(
678 continuation_ep, lambda x: x['continuationCommand']['token'], str)
679 if not continuation:
680 return
681 ctp = continuation_ep.get('clickTrackingParams')
682 return cls._build_api_continuation_query(continuation, ctp)
683
684 @classmethod
685 def _extract_continuation(cls, renderer):
686 next_continuation = cls._extract_next_continuation_data(renderer)
687 if next_continuation:
688 return next_continuation
689
690 return traverse_obj(renderer, (
691 ('contents', 'items', 'rows'), ..., 'continuationItemRenderer',
692 ('continuationEndpoint', ('button', 'buttonRenderer', 'command'))
693 ), get_all=False, expected_type=cls._extract_continuation_ep_data)
694
695 @classmethod
696 def _extract_alerts(cls, data):
697 for alert_dict in try_get(data, lambda x: x['alerts'], list) or []:
698 if not isinstance(alert_dict, dict):
699 continue
700 for alert in alert_dict.values():
701 alert_type = alert.get('type')
702 if not alert_type:
703 continue
704 message = cls._get_text(alert, 'text')
705 if message:
706 yield alert_type, message
707
708 def _report_alerts(self, alerts, expected=True, fatal=True, only_once=False):
709 errors, warnings = [], []
710 for alert_type, alert_message in alerts:
711 if alert_type.lower() == 'error' and fatal:
712 errors.append([alert_type, alert_message])
713 elif alert_message not in self._IGNORED_WARNINGS:
714 warnings.append([alert_type, alert_message])
715
716 for alert_type, alert_message in (warnings + errors[:-1]):
717 self.report_warning(f'YouTube said: {alert_type} - {alert_message}', only_once=only_once)
718 if errors:
719 raise ExtractorError('YouTube said: %s' % errors[-1][1], expected=expected)
720
721 def _extract_and_report_alerts(self, data, *args, **kwargs):
722 return self._report_alerts(self._extract_alerts(data), *args, **kwargs)
723
724 def _extract_badges(self, renderer: dict):
725 privacy_icon_map = {
726 'PRIVACY_UNLISTED': BadgeType.AVAILABILITY_UNLISTED,
727 'PRIVACY_PRIVATE': BadgeType.AVAILABILITY_PRIVATE,
728 'PRIVACY_PUBLIC': BadgeType.AVAILABILITY_PUBLIC
729 }
730
731 badge_style_map = {
732 'BADGE_STYLE_TYPE_MEMBERS_ONLY': BadgeType.AVAILABILITY_SUBSCRIPTION,
733 'BADGE_STYLE_TYPE_PREMIUM': BadgeType.AVAILABILITY_PREMIUM,
734 'BADGE_STYLE_TYPE_LIVE_NOW': BadgeType.LIVE_NOW
735 }
736
737 label_map = {
738 'unlisted': BadgeType.AVAILABILITY_UNLISTED,
739 'private': BadgeType.AVAILABILITY_PRIVATE,
740 'members only': BadgeType.AVAILABILITY_SUBSCRIPTION,
741 'live': BadgeType.LIVE_NOW,
742 'premium': BadgeType.AVAILABILITY_PREMIUM
743 }
744
745 badges = []
746 for badge in traverse_obj(renderer, ('badges', ..., 'metadataBadgeRenderer'), default=[]):
747 badge_type = (
748 privacy_icon_map.get(traverse_obj(badge, ('icon', 'iconType'), expected_type=str))
749 or badge_style_map.get(traverse_obj(badge, 'style'))
750 )
751 if badge_type:
752 badges.append({'type': badge_type})
753 continue
754
755 # fallback, won't work in some languages
756 label = traverse_obj(badge, 'label', expected_type=str, default='')
757 for match, label_badge_type in label_map.items():
758 if match in label.lower():
759 badges.append({'type': badge_type})
760 continue
761
762 return badges
763
764 @staticmethod
765 def _has_badge(badges, badge_type):
766 return bool(traverse_obj(badges, lambda _, v: v['type'] == badge_type))
767
768 @staticmethod
769 def _get_text(data, *path_list, max_runs=None):
770 for path in path_list or [None]:
771 if path is None:
772 obj = [data]
773 else:
774 obj = traverse_obj(data, path, default=[])
775 if not any(key is ... or isinstance(key, (list, tuple)) for key in variadic(path)):
776 obj = [obj]
777 for item in obj:
778 text = try_get(item, lambda x: x['simpleText'], str)
779 if text:
780 return text
781 runs = try_get(item, lambda x: x['runs'], list) or []
782 if not runs and isinstance(item, list):
783 runs = item
784
785 runs = runs[:min(len(runs), max_runs or len(runs))]
786 text = ''.join(traverse_obj(runs, (..., 'text'), expected_type=str, default=[]))
787 if text:
788 return text
789
790 def _get_count(self, data, *path_list):
791 count_text = self._get_text(data, *path_list) or ''
792 count = parse_count(count_text)
793 if count is None:
794 count = str_to_int(
795 self._search_regex(r'^([\d,]+)', re.sub(r'\s', '', count_text), 'count', default=None))
796 return count
797
798 @staticmethod
799 def _extract_thumbnails(data, *path_list):
800 """
801 Extract thumbnails from thumbnails dict
802 @param path_list: path list to level that contains 'thumbnails' key
803 """
804 thumbnails = []
805 for path in path_list or [()]:
806 for thumbnail in traverse_obj(data, (*variadic(path), 'thumbnails', ...), default=[]):
807 thumbnail_url = url_or_none(thumbnail.get('url'))
808 if not thumbnail_url:
809 continue
810 # Sometimes youtube gives a wrong thumbnail URL. See:
811 # https://github.com/yt-dlp/yt-dlp/issues/233
812 # https://github.com/ytdl-org/youtube-dl/issues/28023
813 if 'maxresdefault' in thumbnail_url:
814 thumbnail_url = thumbnail_url.split('?')[0]
815 thumbnails.append({
816 'url': thumbnail_url,
817 'height': int_or_none(thumbnail.get('height')),
818 'width': int_or_none(thumbnail.get('width')),
819 })
820 return thumbnails
821
822 @staticmethod
823 def extract_relative_time(relative_time_text):
824 """
825 Extracts a relative time from string and converts to dt object
826 e.g. 'streamed 6 days ago', '5 seconds ago (edited)', 'updated today'
827 """
828 mobj = re.search(r'(?P<start>today|yesterday|now)|(?P<time>\d+)\s*(?P<unit>microsecond|second|minute|hour|day|week|month|year)s?\s*ago', relative_time_text)
829 if mobj:
830 start = mobj.group('start')
831 if start:
832 return datetime_from_str(start)
833 try:
834 return datetime_from_str('now-%s%s' % (mobj.group('time'), mobj.group('unit')))
835 except ValueError:
836 return None
837
838 def _parse_time_text(self, text):
839 if not text:
840 return
841 dt = self.extract_relative_time(text)
842 timestamp = None
843 if isinstance(dt, datetime.datetime):
844 timestamp = calendar.timegm(dt.timetuple())
845
846 if timestamp is None:
847 timestamp = (
848 unified_timestamp(text) or unified_timestamp(
849 self._search_regex(
850 (r'([a-z]+\s*\d{1,2},?\s*20\d{2})', r'(?:.+|^)(?:live|premieres|ed|ing)(?:\s*(?:on|for))?\s*(.+\d)'),
851 text.lower(), 'time text', default=None)))
852
853 if text and timestamp is None and self._preferred_lang in (None, 'en'):
854 self.report_warning(
855 f'Cannot parse localized time text "{text}"', only_once=True)
856 return timestamp
857
858 def _extract_response(self, item_id, query, note='Downloading API JSON', headers=None,
859 ytcfg=None, check_get_keys=None, ep='browse', fatal=True, api_hostname=None,
860 default_client='web'):
861 for retry in self.RetryManager():
862 try:
863 response = self._call_api(
864 ep=ep, fatal=True, headers=headers,
865 video_id=item_id, query=query, note=note,
866 context=self._extract_context(ytcfg, default_client),
867 api_key=self._extract_api_key(ytcfg, default_client),
868 api_hostname=api_hostname, default_client=default_client)
869 except ExtractorError as e:
870 if not isinstance(e.cause, network_exceptions):
871 return self._error_or_warning(e, fatal=fatal)
872 elif not isinstance(e.cause, urllib.error.HTTPError):
873 retry.error = e
874 continue
875
876 first_bytes = e.cause.read(512)
877 if not is_html(first_bytes):
878 yt_error = try_get(
879 self._parse_json(
880 self._webpage_read_content(e.cause, None, item_id, prefix=first_bytes) or '{}', item_id, fatal=False),
881 lambda x: x['error']['message'], str)
882 if yt_error:
883 self._report_alerts([('ERROR', yt_error)], fatal=False)
884 # Downloading page may result in intermittent 5xx HTTP error
885 # Sometimes a 404 is also recieved. See: https://github.com/ytdl-org/youtube-dl/issues/28289
886 # We also want to catch all other network exceptions since errors in later pages can be troublesome
887 # See https://github.com/yt-dlp/yt-dlp/issues/507#issuecomment-880188210
888 if e.cause.code not in (403, 429):
889 retry.error = e
890 continue
891 return self._error_or_warning(e, fatal=fatal)
892
893 try:
894 self._extract_and_report_alerts(response, only_once=True)
895 except ExtractorError as e:
896 # YouTube servers may return errors we want to retry on in a 200 OK response
897 # See: https://github.com/yt-dlp/yt-dlp/issues/839
898 if 'unknown error' in e.msg.lower():
899 retry.error = e
900 continue
901 return self._error_or_warning(e, fatal=fatal)
902 # Youtube sometimes sends incomplete data
903 # See: https://github.com/ytdl-org/youtube-dl/issues/28194
904 if not traverse_obj(response, *variadic(check_get_keys)):
905 retry.error = ExtractorError('Incomplete data received', expected=True)
906 continue
907
908 return response
909
910 @staticmethod
911 def is_music_url(url):
912 return re.match(r'https?://music\.youtube\.com/', url) is not None
913
914 def _extract_video(self, renderer):
915 video_id = renderer.get('videoId')
916
917 reel_header_renderer = traverse_obj(renderer, (
918 'navigationEndpoint', 'reelWatchEndpoint', 'overlay', 'reelPlayerOverlayRenderer',
919 'reelPlayerHeaderSupportedRenderers', 'reelPlayerHeaderRenderer'))
920
921 title = self._get_text(renderer, 'title', 'headline') or self._get_text(reel_header_renderer, 'reelTitleText')
922 description = self._get_text(renderer, 'descriptionSnippet')
923
924 duration = int_or_none(renderer.get('lengthSeconds'))
925 if duration is None:
926 duration = parse_duration(self._get_text(
927 renderer, 'lengthText', ('thumbnailOverlays', ..., 'thumbnailOverlayTimeStatusRenderer', 'text')))
928 if duration is None:
929 # XXX: should write a parser to be more general to support more cases (e.g. shorts in shorts tab)
930 duration = parse_duration(self._search_regex(
931 r'(?i)(ago)(?!.*\1)\s+(?P<duration>[a-z0-9 ,]+?)(?:\s+[\d,]+\s+views)?(?:\s+-\s+play\s+short)?$',
932 traverse_obj(renderer, ('title', 'accessibility', 'accessibilityData', 'label'), default='', expected_type=str),
933 video_id, default=None, group='duration'))
934
935 channel_id = traverse_obj(
936 renderer, ('shortBylineText', 'runs', ..., 'navigationEndpoint', 'browseEndpoint', 'browseId'),
937 expected_type=str, get_all=False)
938 if not channel_id:
939 channel_id = traverse_obj(reel_header_renderer, ('channelNavigationEndpoint', 'browseEndpoint', 'browseId'))
940
941 overlay_style = traverse_obj(
942 renderer, ('thumbnailOverlays', ..., 'thumbnailOverlayTimeStatusRenderer', 'style'),
943 get_all=False, expected_type=str)
944 badges = self._extract_badges(renderer)
945
946 navigation_url = urljoin('https://www.youtube.com/', traverse_obj(
947 renderer, ('navigationEndpoint', 'commandMetadata', 'webCommandMetadata', 'url'),
948 expected_type=str)) or ''
949 url = f'https://www.youtube.com/watch?v={video_id}'
950 if overlay_style == 'SHORTS' or '/shorts/' in navigation_url:
951 url = f'https://www.youtube.com/shorts/{video_id}'
952
953 time_text = (self._get_text(renderer, 'publishedTimeText', 'videoInfo')
954 or self._get_text(reel_header_renderer, 'timestampText') or '')
955 scheduled_timestamp = str_to_int(traverse_obj(renderer, ('upcomingEventData', 'startTime'), get_all=False))
956
957 live_status = (
958 'is_upcoming' if scheduled_timestamp is not None
959 else 'was_live' if 'streamed' in time_text.lower()
960 else 'is_live' if overlay_style == 'LIVE' or self._has_badge(badges, BadgeType.LIVE_NOW)
961 else None)
962
963 # videoInfo is a string like '50K views • 10 years ago'.
964 view_count_text = self._get_text(renderer, 'viewCountText', 'shortViewCountText', 'videoInfo') or ''
965 view_count = (0 if 'no views' in view_count_text.lower()
966 else self._get_count({'simpleText': view_count_text}))
967 view_count_field = 'concurrent_view_count' if live_status in ('is_live', 'is_upcoming') else 'view_count'
968
969 return {
970 '_type': 'url',
971 'ie_key': YoutubeIE.ie_key(),
972 'id': video_id,
973 'url': url,
974 'title': title,
975 'description': description,
976 'duration': duration,
977 'channel_id': channel_id,
978 'channel': (self._get_text(renderer, 'ownerText', 'shortBylineText')
979 or self._get_text(reel_header_renderer, 'channelTitleText')),
980 'channel_url': f'https://www.youtube.com/channel/{channel_id}' if channel_id else None,
981 'thumbnails': self._extract_thumbnails(renderer, 'thumbnail'),
982 'timestamp': (self._parse_time_text(time_text)
983 if self._configuration_arg('approximate_date', ie_key=YoutubeTabIE)
984 else None),
985 'release_timestamp': scheduled_timestamp,
986 'availability':
987 'public' if self._has_badge(badges, BadgeType.AVAILABILITY_PUBLIC)
988 else self._availability(
989 is_private=self._has_badge(badges, BadgeType.AVAILABILITY_PRIVATE) or None,
990 needs_premium=self._has_badge(badges, BadgeType.AVAILABILITY_PREMIUM) or None,
991 needs_subscription=self._has_badge(badges, BadgeType.AVAILABILITY_SUBSCRIPTION) or None,
992 is_unlisted=self._has_badge(badges, BadgeType.AVAILABILITY_UNLISTED) or None),
993 view_count_field: view_count,
994 'live_status': live_status
995 }
996
997
998 class YoutubeIE(YoutubeBaseInfoExtractor):
999 IE_DESC = 'YouTube'
1000 _VALID_URL = r"""(?x)^
1001 (
1002 (?:https?://|//) # http(s):// or protocol-independent URL
1003 (?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie|kids)?\.com|
1004 (?:www\.)?deturl\.com/www\.youtube\.com|
1005 (?:www\.)?pwnyoutube\.com|
1006 (?:www\.)?hooktube\.com|
1007 (?:www\.)?yourepeat\.com|
1008 tube\.majestyc\.net|
1009 %(invidious)s|
1010 youtube\.googleapis\.com)/ # the various hostnames, with wildcard subdomains
1011 (?:.*?\#/)? # handle anchor (#/) redirect urls
1012 (?: # the various things that can precede the ID:
1013 (?:(?:v|embed|e|shorts)/(?!videoseries|live_stream)) # v/ or embed/ or e/ or shorts/
1014 |(?: # or the v= param in all its forms
1015 (?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
1016 (?:\?|\#!?) # the params delimiter ? or # or #!
1017 (?:.*?[&;])?? # any other preceding param (like /?s=tuff&v=xxxx or ?s=tuff&amp;v=V36LpHqtcDY)
1018 v=
1019 )
1020 ))
1021 |(?:
1022 youtu\.be| # just youtu.be/xxxx
1023 vid\.plus| # or vid.plus/xxxx
1024 zwearz\.com/watch| # or zwearz.com/watch/xxxx
1025 %(invidious)s
1026 )/
1027 |(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=
1028 )
1029 )? # all until now is optional -> you can pass the naked ID
1030 (?P<id>[0-9A-Za-z_-]{11}) # here is it! the YouTube video ID
1031 (?(1).+)? # if we found the ID, everything can follow
1032 (?:\#|$)""" % {
1033 'invidious': '|'.join(YoutubeBaseInfoExtractor._INVIDIOUS_SITES),
1034 }
1035 _EMBED_REGEX = [
1036 r'''(?x)
1037 (?:
1038 <(?:[0-9A-Za-z-]+?)?iframe[^>]+?src=|
1039 data-video-url=|
1040 <embed[^>]+?src=|
1041 embedSWF\(?:\s*|
1042 <object[^>]+data=|
1043 new\s+SWFObject\(
1044 )
1045 (["\'])
1046 (?P<url>(?:https?:)?//(?:www\.)?youtube(?:-nocookie)?\.com/
1047 (?:embed|v|p)/[0-9A-Za-z_-]{11}.*?)
1048 \1''',
1049 # https://wordpress.org/plugins/lazy-load-for-videos/
1050 r'''(?xs)
1051 <a\s[^>]*\bhref="(?P<url>https://www\.youtube\.com/watch\?v=[0-9A-Za-z_-]{11})"
1052 \s[^>]*\bclass="[^"]*\blazy-load-youtube''',
1053 ]
1054 _RETURN_TYPE = 'video' # XXX: How to handle multifeed?
1055
1056 _PLAYER_INFO_RE = (
1057 r'/s/player/(?P<id>[a-zA-Z0-9_-]{8,})/player',
1058 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$',
1059 r'\b(?P<id>vfl[a-zA-Z0-9_-]+)\b.*?\.js$',
1060 )
1061 _formats = {
1062 '5': {'ext': 'flv', 'width': 400, 'height': 240, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
1063 '6': {'ext': 'flv', 'width': 450, 'height': 270, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
1064 '13': {'ext': '3gp', 'acodec': 'aac', 'vcodec': 'mp4v'},
1065 '17': {'ext': '3gp', 'width': 176, 'height': 144, 'acodec': 'aac', 'abr': 24, 'vcodec': 'mp4v'},
1066 '18': {'ext': 'mp4', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 96, 'vcodec': 'h264'},
1067 '22': {'ext': 'mp4', 'width': 1280, 'height': 720, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
1068 '34': {'ext': 'flv', 'width': 640, 'height': 360, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
1069 '35': {'ext': 'flv', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
1070 # itag 36 videos are either 320x180 (BaW_jenozKc) or 320x240 (__2ABJjxzNo), abr varies as well
1071 '36': {'ext': '3gp', 'width': 320, 'acodec': 'aac', 'vcodec': 'mp4v'},
1072 '37': {'ext': 'mp4', 'width': 1920, 'height': 1080, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
1073 '38': {'ext': 'mp4', 'width': 4096, 'height': 3072, 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264'},
1074 '43': {'ext': 'webm', 'width': 640, 'height': 360, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
1075 '44': {'ext': 'webm', 'width': 854, 'height': 480, 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8'},
1076 '45': {'ext': 'webm', 'width': 1280, 'height': 720, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
1077 '46': {'ext': 'webm', 'width': 1920, 'height': 1080, 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8'},
1078 '59': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
1079 '78': {'ext': 'mp4', 'width': 854, 'height': 480, 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264'},
1080
1081
1082 # 3D videos
1083 '82': {'ext': 'mp4', 'height': 360, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
1084 '83': {'ext': 'mp4', 'height': 480, 'format_note': '3D', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -20},
1085 '84': {'ext': 'mp4', 'height': 720, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
1086 '85': {'ext': 'mp4', 'height': 1080, 'format_note': '3D', 'acodec': 'aac', 'abr': 192, 'vcodec': 'h264', 'preference': -20},
1087 '100': {'ext': 'webm', 'height': 360, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 128, 'vcodec': 'vp8', 'preference': -20},
1088 '101': {'ext': 'webm', 'height': 480, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
1089 '102': {'ext': 'webm', 'height': 720, 'format_note': '3D', 'acodec': 'vorbis', 'abr': 192, 'vcodec': 'vp8', 'preference': -20},
1090
1091 # Apple HTTP Live Streaming
1092 '91': {'ext': 'mp4', 'height': 144, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
1093 '92': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
1094 '93': {'ext': 'mp4', 'height': 360, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
1095 '94': {'ext': 'mp4', 'height': 480, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 128, 'vcodec': 'h264', 'preference': -10},
1096 '95': {'ext': 'mp4', 'height': 720, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
1097 '96': {'ext': 'mp4', 'height': 1080, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 256, 'vcodec': 'h264', 'preference': -10},
1098 '132': {'ext': 'mp4', 'height': 240, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 48, 'vcodec': 'h264', 'preference': -10},
1099 '151': {'ext': 'mp4', 'height': 72, 'format_note': 'HLS', 'acodec': 'aac', 'abr': 24, 'vcodec': 'h264', 'preference': -10},
1100
1101 # DASH mp4 video
1102 '133': {'ext': 'mp4', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'h264'},
1103 '134': {'ext': 'mp4', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'h264'},
1104 '135': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
1105 '136': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264'},
1106 '137': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264'},
1107 '138': {'ext': 'mp4', 'format_note': 'DASH video', 'vcodec': 'h264'}, # Height can vary (https://github.com/ytdl-org/youtube-dl/issues/4559)
1108 '160': {'ext': 'mp4', 'height': 144, 'format_note': 'DASH video', 'vcodec': 'h264'},
1109 '212': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'h264'},
1110 '264': {'ext': 'mp4', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'h264'},
1111 '298': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
1112 '299': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60},
1113 '266': {'ext': 'mp4', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'h264'},
1114
1115 # Dash mp4 audio
1116 '139': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 48, 'container': 'm4a_dash'},
1117 '140': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 128, 'container': 'm4a_dash'},
1118 '141': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'abr': 256, 'container': 'm4a_dash'},
1119 '256': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
1120 '258': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'aac', 'container': 'm4a_dash'},
1121 '325': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'dtse', 'container': 'm4a_dash'},
1122 '328': {'ext': 'm4a', 'format_note': 'DASH audio', 'acodec': 'ec-3', 'container': 'm4a_dash'},
1123
1124 # Dash webm
1125 '167': {'ext': 'webm', 'height': 360, 'width': 640, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1126 '168': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1127 '169': {'ext': 'webm', 'height': 720, 'width': 1280, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1128 '170': {'ext': 'webm', 'height': 1080, 'width': 1920, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1129 '218': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1130 '219': {'ext': 'webm', 'height': 480, 'width': 854, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp8'},
1131 '278': {'ext': 'webm', 'height': 144, 'format_note': 'DASH video', 'container': 'webm', 'vcodec': 'vp9'},
1132 '242': {'ext': 'webm', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1133 '243': {'ext': 'webm', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1134 '244': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1135 '245': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1136 '246': {'ext': 'webm', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1137 '247': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1138 '248': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1139 '271': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1140 # itag 272 videos are either 3840x2160 (e.g. RtoitU2A-3E) or 7680x4320 (sLprVF6d7Ug)
1141 '272': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1142 '302': {'ext': 'webm', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
1143 '303': {'ext': 'webm', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
1144 '308': {'ext': 'webm', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
1145 '313': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9'},
1146 '315': {'ext': 'webm', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'vp9', 'fps': 60},
1147
1148 # Dash webm audio
1149 '171': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 128},
1150 '172': {'ext': 'webm', 'acodec': 'vorbis', 'format_note': 'DASH audio', 'abr': 256},
1151
1152 # Dash webm audio with opus inside
1153 '249': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 50},
1154 '250': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 70},
1155 '251': {'ext': 'webm', 'format_note': 'DASH audio', 'acodec': 'opus', 'abr': 160},
1156
1157 # RTMP (unnamed)
1158 '_rtmp': {'protocol': 'rtmp'},
1159
1160 # av01 video only formats sometimes served with "unknown" codecs
1161 '394': {'ext': 'mp4', 'height': 144, 'format_note': 'DASH video', 'vcodec': 'av01.0.00M.08'},
1162 '395': {'ext': 'mp4', 'height': 240, 'format_note': 'DASH video', 'vcodec': 'av01.0.00M.08'},
1163 '396': {'ext': 'mp4', 'height': 360, 'format_note': 'DASH video', 'vcodec': 'av01.0.01M.08'},
1164 '397': {'ext': 'mp4', 'height': 480, 'format_note': 'DASH video', 'vcodec': 'av01.0.04M.08'},
1165 '398': {'ext': 'mp4', 'height': 720, 'format_note': 'DASH video', 'vcodec': 'av01.0.05M.08'},
1166 '399': {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'av01.0.08M.08'},
1167 '400': {'ext': 'mp4', 'height': 1440, 'format_note': 'DASH video', 'vcodec': 'av01.0.12M.08'},
1168 '401': {'ext': 'mp4', 'height': 2160, 'format_note': 'DASH video', 'vcodec': 'av01.0.12M.08'},
1169 }
1170 _SUBTITLE_FORMATS = ('json3', 'srv1', 'srv2', 'srv3', 'ttml', 'vtt')
1171
1172 _GEO_BYPASS = False
1173
1174 IE_NAME = 'youtube'
1175 _TESTS = [
1176 {
1177 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&t=1s&end=9',
1178 'info_dict': {
1179 'id': 'BaW_jenozKc',
1180 'ext': 'mp4',
1181 'title': 'youtube-dl test video "\'/\\ä↭𝕐',
1182 'uploader': 'Philipp Hagemeister',
1183 'uploader_id': 'phihag',
1184 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/phihag',
1185 'channel': 'Philipp Hagemeister',
1186 'channel_id': 'UCLqxVugv74EIW3VWh2NOa3Q',
1187 'channel_url': r're:https?://(?:www\.)?youtube\.com/channel/UCLqxVugv74EIW3VWh2NOa3Q',
1188 'upload_date': '20121002',
1189 'description': 'md5:8fb536f4877b8a7455c2ec23794dbc22',
1190 'categories': ['Science & Technology'],
1191 'tags': ['youtube-dl'],
1192 'duration': 10,
1193 'view_count': int,
1194 'like_count': int,
1195 'availability': 'public',
1196 'playable_in_embed': True,
1197 'thumbnail': 'https://i.ytimg.com/vi/BaW_jenozKc/maxresdefault.jpg',
1198 'live_status': 'not_live',
1199 'age_limit': 0,
1200 'start_time': 1,
1201 'end_time': 9,
1202 'comment_count': int,
1203 'channel_follower_count': int
1204 }
1205 },
1206 {
1207 'url': '//www.YouTube.com/watch?v=yZIXLfi8CZQ',
1208 'note': 'Embed-only video (#1746)',
1209 'info_dict': {
1210 'id': 'yZIXLfi8CZQ',
1211 'ext': 'mp4',
1212 'upload_date': '20120608',
1213 'title': 'Principal Sexually Assaults A Teacher - Episode 117 - 8th June 2012',
1214 'description': 'md5:09b78bd971f1e3e289601dfba15ca4f7',
1215 'uploader': 'SET India',
1216 'uploader_id': 'setindia',
1217 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/setindia',
1218 'age_limit': 18,
1219 },
1220 'skip': 'Private video',
1221 },
1222 {
1223 'url': 'https://www.youtube.com/watch?v=BaW_jenozKc&v=yZIXLfi8CZQ',
1224 'note': 'Use the first video ID in the URL',
1225 'info_dict': {
1226 'id': 'BaW_jenozKc',
1227 'ext': 'mp4',
1228 'title': 'youtube-dl test video "\'/\\ä↭𝕐',
1229 'uploader': 'Philipp Hagemeister',
1230 'uploader_id': 'phihag',
1231 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/phihag',
1232 'channel': 'Philipp Hagemeister',
1233 'channel_id': 'UCLqxVugv74EIW3VWh2NOa3Q',
1234 'channel_url': r're:https?://(?:www\.)?youtube\.com/channel/UCLqxVugv74EIW3VWh2NOa3Q',
1235 'upload_date': '20121002',
1236 'description': 'md5:8fb536f4877b8a7455c2ec23794dbc22',
1237 'categories': ['Science & Technology'],
1238 'tags': ['youtube-dl'],
1239 'duration': 10,
1240 'view_count': int,
1241 'like_count': int,
1242 'availability': 'public',
1243 'playable_in_embed': True,
1244 'thumbnail': 'https://i.ytimg.com/vi/BaW_jenozKc/maxresdefault.jpg',
1245 'live_status': 'not_live',
1246 'age_limit': 0,
1247 'comment_count': int,
1248 'channel_follower_count': int
1249 },
1250 'params': {
1251 'skip_download': True,
1252 },
1253 },
1254 {
1255 'url': 'https://www.youtube.com/watch?v=a9LDPn-MO4I',
1256 'note': '256k DASH audio (format 141) via DASH manifest',
1257 'info_dict': {
1258 'id': 'a9LDPn-MO4I',
1259 'ext': 'm4a',
1260 'upload_date': '20121002',
1261 'uploader_id': '8KVIDEO',
1262 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/8KVIDEO',
1263 'description': '',
1264 'uploader': '8KVIDEO',
1265 'title': 'UHDTV TEST 8K VIDEO.mp4'
1266 },
1267 'params': {
1268 'youtube_include_dash_manifest': True,
1269 'format': '141',
1270 },
1271 'skip': 'format 141 not served anymore',
1272 },
1273 # DASH manifest with encrypted signature
1274 {
1275 'url': 'https://www.youtube.com/watch?v=IB3lcPjvWLA',
1276 'info_dict': {
1277 'id': 'IB3lcPjvWLA',
1278 'ext': 'm4a',
1279 'title': 'Afrojack, Spree Wilson - The Spark (Official Music Video) ft. Spree Wilson',
1280 'description': 'md5:8f5e2b82460520b619ccac1f509d43bf',
1281 'duration': 244,
1282 'uploader': 'AfrojackVEVO',
1283 'uploader_id': 'AfrojackVEVO',
1284 'upload_date': '20131011',
1285 'abr': 129.495,
1286 'like_count': int,
1287 'channel_id': 'UChuZAo1RKL85gev3Eal9_zg',
1288 'playable_in_embed': True,
1289 'channel_url': 'https://www.youtube.com/channel/UChuZAo1RKL85gev3Eal9_zg',
1290 'view_count': int,
1291 'track': 'The Spark',
1292 'live_status': 'not_live',
1293 'thumbnail': 'https://i.ytimg.com/vi_webp/IB3lcPjvWLA/maxresdefault.webp',
1294 'channel': 'Afrojack',
1295 'uploader_url': 'http://www.youtube.com/user/AfrojackVEVO',
1296 'tags': 'count:19',
1297 'availability': 'public',
1298 'categories': ['Music'],
1299 'age_limit': 0,
1300 'alt_title': 'The Spark',
1301 'channel_follower_count': int
1302 },
1303 'params': {
1304 'youtube_include_dash_manifest': True,
1305 'format': '141/bestaudio[ext=m4a]',
1306 },
1307 },
1308 # Age-gate videos. See https://github.com/yt-dlp/yt-dlp/pull/575#issuecomment-888837000
1309 {
1310 'note': 'Embed allowed age-gate video',
1311 'url': 'https://youtube.com/watch?v=HtVdAasjOgU',
1312 'info_dict': {
1313 'id': 'HtVdAasjOgU',
1314 'ext': 'mp4',
1315 'title': 'The Witcher 3: Wild Hunt - The Sword Of Destiny Trailer',
1316 'description': r're:(?s).{100,}About the Game\n.*?The Witcher 3: Wild Hunt.{100,}',
1317 'duration': 142,
1318 'uploader': 'The Witcher',
1319 'uploader_id': 'WitcherGame',
1320 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/WitcherGame',
1321 'upload_date': '20140605',
1322 'age_limit': 18,
1323 'categories': ['Gaming'],
1324 'thumbnail': 'https://i.ytimg.com/vi_webp/HtVdAasjOgU/maxresdefault.webp',
1325 'availability': 'needs_auth',
1326 'channel_url': 'https://www.youtube.com/channel/UCzybXLxv08IApdjdN0mJhEg',
1327 'like_count': int,
1328 'channel': 'The Witcher',
1329 'live_status': 'not_live',
1330 'tags': 'count:17',
1331 'channel_id': 'UCzybXLxv08IApdjdN0mJhEg',
1332 'playable_in_embed': True,
1333 'view_count': int,
1334 'channel_follower_count': int
1335 },
1336 },
1337 {
1338 'note': 'Age-gate video with embed allowed in public site',
1339 'url': 'https://youtube.com/watch?v=HsUATh_Nc2U',
1340 'info_dict': {
1341 'id': 'HsUATh_Nc2U',
1342 'ext': 'mp4',
1343 'title': 'Godzilla 2 (Official Video)',
1344 'description': 'md5:bf77e03fcae5529475e500129b05668a',
1345 'upload_date': '20200408',
1346 'uploader_id': 'FlyingKitty900',
1347 'uploader': 'FlyingKitty',
1348 'age_limit': 18,
1349 'availability': 'needs_auth',
1350 'channel_id': 'UCYQT13AtrJC0gsM1far_zJg',
1351 'uploader_url': 'http://www.youtube.com/user/FlyingKitty900',
1352 'channel': 'FlyingKitty',
1353 'channel_url': 'https://www.youtube.com/channel/UCYQT13AtrJC0gsM1far_zJg',
1354 'view_count': int,
1355 'categories': ['Entertainment'],
1356 'live_status': 'not_live',
1357 'tags': ['Flyingkitty', 'godzilla 2'],
1358 'thumbnail': 'https://i.ytimg.com/vi/HsUATh_Nc2U/maxresdefault.jpg',
1359 'like_count': int,
1360 'duration': 177,
1361 'playable_in_embed': True,
1362 'channel_follower_count': int
1363 },
1364 },
1365 {
1366 'note': 'Age-gate video embedable only with clientScreen=EMBED',
1367 'url': 'https://youtube.com/watch?v=Tq92D6wQ1mg',
1368 'info_dict': {
1369 'id': 'Tq92D6wQ1mg',
1370 'title': '[MMD] Adios - EVERGLOW [+Motion DL]',
1371 'ext': 'mp4',
1372 'upload_date': '20191228',
1373 'uploader_id': 'UC1yoRdFoFJaCY-AGfD9W0wQ',
1374 'uploader': 'Projekt Melody',
1375 'description': 'md5:17eccca93a786d51bc67646756894066',
1376 'age_limit': 18,
1377 'like_count': int,
1378 'availability': 'needs_auth',
1379 'uploader_url': 'http://www.youtube.com/channel/UC1yoRdFoFJaCY-AGfD9W0wQ',
1380 'channel_id': 'UC1yoRdFoFJaCY-AGfD9W0wQ',
1381 'view_count': int,
1382 'thumbnail': 'https://i.ytimg.com/vi_webp/Tq92D6wQ1mg/sddefault.webp',
1383 'channel': 'Projekt Melody',
1384 'live_status': 'not_live',
1385 'tags': ['mmd', 'dance', 'mikumikudance', 'kpop', 'vtuber'],
1386 'playable_in_embed': True,
1387 'categories': ['Entertainment'],
1388 'duration': 106,
1389 'channel_url': 'https://www.youtube.com/channel/UC1yoRdFoFJaCY-AGfD9W0wQ',
1390 'comment_count': int,
1391 'channel_follower_count': int
1392 },
1393 },
1394 {
1395 'note': 'Non-Agegated non-embeddable video',
1396 'url': 'https://youtube.com/watch?v=MeJVWBSsPAY',
1397 'info_dict': {
1398 'id': 'MeJVWBSsPAY',
1399 'ext': 'mp4',
1400 'title': 'OOMPH! - Such Mich Find Mich (Lyrics)',
1401 'uploader': 'Herr Lurik',
1402 'uploader_id': 'st3in234',
1403 'description': 'Fan Video. Music & Lyrics by OOMPH!.',
1404 'upload_date': '20130730',
1405 'track': 'Such mich find mich',
1406 'age_limit': 0,
1407 'tags': ['oomph', 'such mich find mich', 'lyrics', 'german industrial', 'musica industrial'],
1408 'like_count': int,
1409 'playable_in_embed': False,
1410 'creator': 'OOMPH!',
1411 'thumbnail': 'https://i.ytimg.com/vi/MeJVWBSsPAY/sddefault.jpg',
1412 'view_count': int,
1413 'alt_title': 'Such mich find mich',
1414 'duration': 210,
1415 'channel': 'Herr Lurik',
1416 'channel_id': 'UCdR3RSDPqub28LjZx0v9-aA',
1417 'categories': ['Music'],
1418 'availability': 'public',
1419 'uploader_url': 'http://www.youtube.com/user/st3in234',
1420 'channel_url': 'https://www.youtube.com/channel/UCdR3RSDPqub28LjZx0v9-aA',
1421 'live_status': 'not_live',
1422 'artist': 'OOMPH!',
1423 'channel_follower_count': int
1424 },
1425 },
1426 {
1427 'note': 'Non-bypassable age-gated video',
1428 'url': 'https://youtube.com/watch?v=Cr381pDsSsA',
1429 'only_matching': True,
1430 },
1431 # video_info is None (https://github.com/ytdl-org/youtube-dl/issues/4421)
1432 # YouTube Red ad is not captured for creator
1433 {
1434 'url': '__2ABJjxzNo',
1435 'info_dict': {
1436 'id': '__2ABJjxzNo',
1437 'ext': 'mp4',
1438 'duration': 266,
1439 'upload_date': '20100430',
1440 'uploader_id': 'deadmau5',
1441 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/deadmau5',
1442 'creator': 'deadmau5',
1443 'description': 'md5:6cbcd3a92ce1bc676fc4d6ab4ace2336',
1444 'uploader': 'deadmau5',
1445 'title': 'Deadmau5 - Some Chords (HD)',
1446 'alt_title': 'Some Chords',
1447 'availability': 'public',
1448 'tags': 'count:14',
1449 'channel_id': 'UCYEK6xds6eo-3tr4xRdflmQ',
1450 'view_count': int,
1451 'live_status': 'not_live',
1452 'channel': 'deadmau5',
1453 'thumbnail': 'https://i.ytimg.com/vi_webp/__2ABJjxzNo/maxresdefault.webp',
1454 'like_count': int,
1455 'track': 'Some Chords',
1456 'artist': 'deadmau5',
1457 'playable_in_embed': True,
1458 'age_limit': 0,
1459 'channel_url': 'https://www.youtube.com/channel/UCYEK6xds6eo-3tr4xRdflmQ',
1460 'categories': ['Music'],
1461 'album': 'Some Chords',
1462 'channel_follower_count': int
1463 },
1464 'expected_warnings': [
1465 'DASH manifest missing',
1466 ]
1467 },
1468 # Olympics (https://github.com/ytdl-org/youtube-dl/issues/4431)
1469 {
1470 'url': 'lqQg6PlCWgI',
1471 'info_dict': {
1472 'id': 'lqQg6PlCWgI',
1473 'ext': 'mp4',
1474 'duration': 6085,
1475 'upload_date': '20150827',
1476 'uploader_id': 'olympic',
1477 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/olympic',
1478 'description': 'md5:04bbbf3ccceb6795947572ca36f45904',
1479 'uploader': 'Olympics',
1480 'title': 'Hockey - Women - GER-AUS - London 2012 Olympic Games',
1481 'like_count': int,
1482 'release_timestamp': 1343767800,
1483 'playable_in_embed': True,
1484 'categories': ['Sports'],
1485 'release_date': '20120731',
1486 'channel': 'Olympics',
1487 'tags': ['Hockey', '2012-07-31', '31 July 2012', 'Riverbank Arena', 'Session', 'Olympics', 'Olympic Games', 'London 2012', '2012 Summer Olympics', 'Summer Games'],
1488 'channel_id': 'UCTl3QQTvqHFjurroKxexy2Q',
1489 'thumbnail': 'https://i.ytimg.com/vi/lqQg6PlCWgI/maxresdefault.jpg',
1490 'age_limit': 0,
1491 'availability': 'public',
1492 'live_status': 'was_live',
1493 'view_count': int,
1494 'channel_url': 'https://www.youtube.com/channel/UCTl3QQTvqHFjurroKxexy2Q',
1495 'channel_follower_count': int
1496 },
1497 'params': {
1498 'skip_download': 'requires avconv',
1499 }
1500 },
1501 # Non-square pixels
1502 {
1503 'url': 'https://www.youtube.com/watch?v=_b-2C3KPAM0',
1504 'info_dict': {
1505 'id': '_b-2C3KPAM0',
1506 'ext': 'mp4',
1507 'stretched_ratio': 16 / 9.,
1508 'duration': 85,
1509 'upload_date': '20110310',
1510 'uploader_id': 'AllenMeow',
1511 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/AllenMeow',
1512 'description': 'made by Wacom from Korea | 字幕&加油添醋 by TY\'s Allen | 感謝heylisa00cavey1001同學熱情提供梗及翻譯',
1513 'uploader': '孫ᄋᄅ',
1514 'title': '[A-made] 變態妍字幕版 太妍 我就是這樣的人',
1515 'playable_in_embed': True,
1516 'channel': '孫ᄋᄅ',
1517 'age_limit': 0,
1518 'tags': 'count:11',
1519 'channel_url': 'https://www.youtube.com/channel/UCS-xxCmRaA6BFdmgDPA_BIw',
1520 'channel_id': 'UCS-xxCmRaA6BFdmgDPA_BIw',
1521 'thumbnail': 'https://i.ytimg.com/vi/_b-2C3KPAM0/maxresdefault.jpg',
1522 'view_count': int,
1523 'categories': ['People & Blogs'],
1524 'like_count': int,
1525 'live_status': 'not_live',
1526 'availability': 'unlisted',
1527 'comment_count': int,
1528 'channel_follower_count': int
1529 },
1530 },
1531 # url_encoded_fmt_stream_map is empty string
1532 {
1533 'url': 'qEJwOuvDf7I',
1534 'info_dict': {
1535 'id': 'qEJwOuvDf7I',
1536 'ext': 'webm',
1537 'title': 'Обсуждение судебной практики по выборам 14 сентября 2014 года в Санкт-Петербурге',
1538 'description': '',
1539 'upload_date': '20150404',
1540 'uploader_id': 'spbelect',
1541 'uploader': 'Наблюдатели Петербурга',
1542 },
1543 'params': {
1544 'skip_download': 'requires avconv',
1545 },
1546 'skip': 'This live event has ended.',
1547 },
1548 # Extraction from multiple DASH manifests (https://github.com/ytdl-org/youtube-dl/pull/6097)
1549 {
1550 'url': 'https://www.youtube.com/watch?v=FIl7x6_3R5Y',
1551 'info_dict': {
1552 'id': 'FIl7x6_3R5Y',
1553 'ext': 'webm',
1554 'title': 'md5:7b81415841e02ecd4313668cde88737a',
1555 'description': 'md5:116377fd2963b81ec4ce64b542173306',
1556 'duration': 220,
1557 'upload_date': '20150625',
1558 'uploader_id': 'dorappi2000',
1559 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/dorappi2000',
1560 'uploader': 'dorappi2000',
1561 'formats': 'mincount:31',
1562 },
1563 'skip': 'not actual anymore',
1564 },
1565 # DASH manifest with segment_list
1566 {
1567 'url': 'https://www.youtube.com/embed/CsmdDsKjzN8',
1568 'md5': '8ce563a1d667b599d21064e982ab9e31',
1569 'info_dict': {
1570 'id': 'CsmdDsKjzN8',
1571 'ext': 'mp4',
1572 'upload_date': '20150501', # According to '<meta itemprop="datePublished"', but in other places it's 20150510
1573 'uploader': 'Airtek',
1574 'description': 'Retransmisión en directo de la XVIII media maratón de Zaragoza.',
1575 'uploader_id': 'UCzTzUmjXxxacNnL8I3m4LnQ',
1576 'title': 'Retransmisión XVIII Media maratón Zaragoza 2015',
1577 },
1578 'params': {
1579 'youtube_include_dash_manifest': True,
1580 'format': '135', # bestvideo
1581 },
1582 'skip': 'This live event has ended.',
1583 },
1584 {
1585 # Multifeed videos (multiple cameras), URL can be of any Camera
1586 'url': 'https://www.youtube.com/watch?v=zaPI8MvL8pg',
1587 'info_dict': {
1588 'id': 'zaPI8MvL8pg',
1589 'title': 'Terraria 1.2 Live Stream | Let\'s Play - Part 04',
1590 'description': 'md5:563ccbc698b39298481ca3c571169519',
1591 },
1592 'playlist': [{
1593 'info_dict': {
1594 'id': 'j5yGuxZ8lLU',
1595 'ext': 'mp4',
1596 'title': 'Terraria 1.2 Live Stream | Let\'s Play - Part 04 (Chris)',
1597 'uploader': 'WiiLikeToPlay',
1598 'description': 'md5:563ccbc698b39298481ca3c571169519',
1599 'uploader_url': 'http://www.youtube.com/user/WiiRikeToPray',
1600 'duration': 10120,
1601 'channel_follower_count': int,
1602 'channel_url': 'https://www.youtube.com/channel/UCN2XePorRokPB9TEgRZpddg',
1603 'availability': 'public',
1604 'playable_in_embed': True,
1605 'upload_date': '20131105',
1606 'uploader_id': 'WiiRikeToPray',
1607 'categories': ['Gaming'],
1608 'live_status': 'was_live',
1609 'tags': 'count:24',
1610 'release_timestamp': 1383701910,
1611 'thumbnail': 'https://i.ytimg.com/vi/j5yGuxZ8lLU/maxresdefault.jpg',
1612 'comment_count': int,
1613 'age_limit': 0,
1614 'like_count': int,
1615 'channel_id': 'UCN2XePorRokPB9TEgRZpddg',
1616 'channel': 'WiiLikeToPlay',
1617 'view_count': int,
1618 'release_date': '20131106',
1619 },
1620 }, {
1621 'info_dict': {
1622 'id': 'zaPI8MvL8pg',
1623 'ext': 'mp4',
1624 'title': 'Terraria 1.2 Live Stream | Let\'s Play - Part 04 (Tyson)',
1625 'uploader_id': 'WiiRikeToPray',
1626 'availability': 'public',
1627 'channel_url': 'https://www.youtube.com/channel/UCN2XePorRokPB9TEgRZpddg',
1628 'channel': 'WiiLikeToPlay',
1629 'uploader_url': 'http://www.youtube.com/user/WiiRikeToPray',
1630 'channel_follower_count': int,
1631 'description': 'md5:563ccbc698b39298481ca3c571169519',
1632 'duration': 10108,
1633 'age_limit': 0,
1634 'like_count': int,
1635 'tags': 'count:24',
1636 'channel_id': 'UCN2XePorRokPB9TEgRZpddg',
1637 'uploader': 'WiiLikeToPlay',
1638 'release_timestamp': 1383701915,
1639 'comment_count': int,
1640 'upload_date': '20131105',
1641 'thumbnail': 'https://i.ytimg.com/vi/zaPI8MvL8pg/maxresdefault.jpg',
1642 'release_date': '20131106',
1643 'playable_in_embed': True,
1644 'live_status': 'was_live',
1645 'categories': ['Gaming'],
1646 'view_count': int,
1647 },
1648 }, {
1649 'info_dict': {
1650 'id': 'R7r3vfO7Hao',
1651 'ext': 'mp4',
1652 'title': 'Terraria 1.2 Live Stream | Let\'s Play - Part 04 (Spencer)',
1653 'thumbnail': 'https://i.ytimg.com/vi/R7r3vfO7Hao/maxresdefault.jpg',
1654 'channel_id': 'UCN2XePorRokPB9TEgRZpddg',
1655 'like_count': int,
1656 'availability': 'public',
1657 'playable_in_embed': True,
1658 'upload_date': '20131105',
1659 'description': 'md5:563ccbc698b39298481ca3c571169519',
1660 'uploader_id': 'WiiRikeToPray',
1661 'uploader_url': 'http://www.youtube.com/user/WiiRikeToPray',
1662 'channel_follower_count': int,
1663 'tags': 'count:24',
1664 'release_date': '20131106',
1665 'uploader': 'WiiLikeToPlay',
1666 'comment_count': int,
1667 'channel_url': 'https://www.youtube.com/channel/UCN2XePorRokPB9TEgRZpddg',
1668 'channel': 'WiiLikeToPlay',
1669 'categories': ['Gaming'],
1670 'release_timestamp': 1383701914,
1671 'live_status': 'was_live',
1672 'age_limit': 0,
1673 'duration': 10128,
1674 'view_count': int,
1675 },
1676 }],
1677 'params': {'skip_download': True},
1678 },
1679 {
1680 # Multifeed video with comma in title (see https://github.com/ytdl-org/youtube-dl/issues/8536)
1681 'url': 'https://www.youtube.com/watch?v=gVfLd0zydlo',
1682 'info_dict': {
1683 'id': 'gVfLd0zydlo',
1684 'title': 'DevConf.cz 2016 Day 2 Workshops 1 14:00 - 15:30',
1685 },
1686 'playlist_count': 2,
1687 'skip': 'Not multifeed anymore',
1688 },
1689 {
1690 'url': 'https://vid.plus/FlRa-iH7PGw',
1691 'only_matching': True,
1692 },
1693 {
1694 'url': 'https://zwearz.com/watch/9lWxNJF-ufM/electra-woman-dyna-girl-official-trailer-grace-helbig.html',
1695 'only_matching': True,
1696 },
1697 {
1698 # Title with JS-like syntax "};" (see https://github.com/ytdl-org/youtube-dl/issues/7468)
1699 # Also tests cut-off URL expansion in video description (see
1700 # https://github.com/ytdl-org/youtube-dl/issues/1892,
1701 # https://github.com/ytdl-org/youtube-dl/issues/8164)
1702 'url': 'https://www.youtube.com/watch?v=lsguqyKfVQg',
1703 'info_dict': {
1704 'id': 'lsguqyKfVQg',
1705 'ext': 'mp4',
1706 'title': '{dark walk}; Loki/AC/Dishonored; collab w/Elflover21',
1707 'alt_title': 'Dark Walk',
1708 'description': 'md5:8085699c11dc3f597ce0410b0dcbb34a',
1709 'duration': 133,
1710 'upload_date': '20151119',
1711 'uploader_id': 'IronSoulElf',
1712 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/IronSoulElf',
1713 'uploader': 'IronSoulElf',
1714 'creator': 'Todd Haberman;\nDaniel Law Heath and Aaron Kaplan',
1715 'track': 'Dark Walk',
1716 'artist': 'Todd Haberman;\nDaniel Law Heath and Aaron Kaplan',
1717 'album': 'Position Music - Production Music Vol. 143 - Dark Walk',
1718 'thumbnail': 'https://i.ytimg.com/vi_webp/lsguqyKfVQg/maxresdefault.webp',
1719 'categories': ['Film & Animation'],
1720 'view_count': int,
1721 'live_status': 'not_live',
1722 'channel_url': 'https://www.youtube.com/channel/UCTSRgz5jylBvFt_S7wnsqLQ',
1723 'channel_id': 'UCTSRgz5jylBvFt_S7wnsqLQ',
1724 'tags': 'count:13',
1725 'availability': 'public',
1726 'channel': 'IronSoulElf',
1727 'playable_in_embed': True,
1728 'like_count': int,
1729 'age_limit': 0,
1730 'channel_follower_count': int
1731 },
1732 'params': {
1733 'skip_download': True,
1734 },
1735 },
1736 {
1737 # Tags with '};' (see https://github.com/ytdl-org/youtube-dl/issues/7468)
1738 'url': 'https://www.youtube.com/watch?v=Ms7iBXnlUO8',
1739 'only_matching': True,
1740 },
1741 {
1742 # Video with yt:stretch=17:0
1743 'url': 'https://www.youtube.com/watch?v=Q39EVAstoRM',
1744 'info_dict': {
1745 'id': 'Q39EVAstoRM',
1746 'ext': 'mp4',
1747 'title': 'Clash Of Clans#14 Dicas De Ataque Para CV 4',
1748 'description': 'md5:ee18a25c350637c8faff806845bddee9',
1749 'upload_date': '20151107',
1750 'uploader_id': 'UCCr7TALkRbo3EtFzETQF1LA',
1751 'uploader': 'CH GAMER DROID',
1752 },
1753 'params': {
1754 'skip_download': True,
1755 },
1756 'skip': 'This video does not exist.',
1757 },
1758 {
1759 # Video with incomplete 'yt:stretch=16:'
1760 'url': 'https://www.youtube.com/watch?v=FRhJzUSJbGI',
1761 'only_matching': True,
1762 },
1763 {
1764 # Video licensed under Creative Commons
1765 'url': 'https://www.youtube.com/watch?v=M4gD1WSo5mA',
1766 'info_dict': {
1767 'id': 'M4gD1WSo5mA',
1768 'ext': 'mp4',
1769 'title': 'md5:e41008789470fc2533a3252216f1c1d1',
1770 'description': 'md5:a677553cf0840649b731a3024aeff4cc',
1771 'duration': 721,
1772 'upload_date': '20150128',
1773 'uploader_id': 'BerkmanCenter',
1774 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/BerkmanCenter',
1775 'uploader': 'The Berkman Klein Center for Internet & Society',
1776 'license': 'Creative Commons Attribution license (reuse allowed)',
1777 'channel_id': 'UCuLGmD72gJDBwmLw06X58SA',
1778 'channel_url': 'https://www.youtube.com/channel/UCuLGmD72gJDBwmLw06X58SA',
1779 'like_count': int,
1780 'age_limit': 0,
1781 'tags': ['Copyright (Legal Subject)', 'Law (Industry)', 'William W. Fisher (Author)'],
1782 'channel': 'The Berkman Klein Center for Internet & Society',
1783 'availability': 'public',
1784 'view_count': int,
1785 'categories': ['Education'],
1786 'thumbnail': 'https://i.ytimg.com/vi_webp/M4gD1WSo5mA/maxresdefault.webp',
1787 'live_status': 'not_live',
1788 'playable_in_embed': True,
1789 'comment_count': int,
1790 'channel_follower_count': int,
1791 'chapters': list,
1792 },
1793 'params': {
1794 'skip_download': True,
1795 },
1796 },
1797 {
1798 # Channel-like uploader_url
1799 'url': 'https://www.youtube.com/watch?v=eQcmzGIKrzg',
1800 'info_dict': {
1801 'id': 'eQcmzGIKrzg',
1802 'ext': 'mp4',
1803 'title': 'Democratic Socialism and Foreign Policy | Bernie Sanders',
1804 'description': 'md5:13a2503d7b5904ef4b223aa101628f39',
1805 'duration': 4060,
1806 'upload_date': '20151120',
1807 'uploader': 'Bernie Sanders',
1808 'uploader_id': 'UCH1dpzjCEiGAt8CXkryhkZg',
1809 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCH1dpzjCEiGAt8CXkryhkZg',
1810 'license': 'Creative Commons Attribution license (reuse allowed)',
1811 'playable_in_embed': True,
1812 'tags': 'count:12',
1813 'like_count': int,
1814 'channel_id': 'UCH1dpzjCEiGAt8CXkryhkZg',
1815 'age_limit': 0,
1816 'availability': 'public',
1817 'categories': ['News & Politics'],
1818 'channel': 'Bernie Sanders',
1819 'thumbnail': 'https://i.ytimg.com/vi_webp/eQcmzGIKrzg/maxresdefault.webp',
1820 'view_count': int,
1821 'live_status': 'not_live',
1822 'channel_url': 'https://www.youtube.com/channel/UCH1dpzjCEiGAt8CXkryhkZg',
1823 'comment_count': int,
1824 'channel_follower_count': int,
1825 'chapters': list,
1826 },
1827 'params': {
1828 'skip_download': True,
1829 },
1830 },
1831 {
1832 'url': 'https://www.youtube.com/watch?feature=player_embedded&amp;amp;v=V36LpHqtcDY',
1833 'only_matching': True,
1834 },
1835 {
1836 # YouTube Red paid video (https://github.com/ytdl-org/youtube-dl/issues/10059)
1837 'url': 'https://www.youtube.com/watch?v=i1Ko8UG-Tdo',
1838 'only_matching': True,
1839 },
1840 {
1841 # Rental video preview
1842 'url': 'https://www.youtube.com/watch?v=yYr8q0y5Jfg',
1843 'info_dict': {
1844 'id': 'uGpuVWrhIzE',
1845 'ext': 'mp4',
1846 'title': 'Piku - Trailer',
1847 'description': 'md5:c36bd60c3fd6f1954086c083c72092eb',
1848 'upload_date': '20150811',
1849 'uploader': 'FlixMatrix',
1850 'uploader_id': 'FlixMatrixKaravan',
1851 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/FlixMatrixKaravan',
1852 'license': 'Standard YouTube License',
1853 },
1854 'params': {
1855 'skip_download': True,
1856 },
1857 'skip': 'This video is not available.',
1858 },
1859 {
1860 # YouTube Red video with episode data
1861 'url': 'https://www.youtube.com/watch?v=iqKdEhx-dD4',
1862 'info_dict': {
1863 'id': 'iqKdEhx-dD4',
1864 'ext': 'mp4',
1865 'title': 'Isolation - Mind Field (Ep 1)',
1866 'description': 'md5:f540112edec5d09fc8cc752d3d4ba3cd',
1867 'duration': 2085,
1868 'upload_date': '20170118',
1869 'uploader': 'Vsauce',
1870 'uploader_id': 'Vsauce',
1871 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Vsauce',
1872 'series': 'Mind Field',
1873 'season_number': 1,
1874 'episode_number': 1,
1875 'thumbnail': 'https://i.ytimg.com/vi_webp/iqKdEhx-dD4/maxresdefault.webp',
1876 'tags': 'count:12',
1877 'view_count': int,
1878 'availability': 'public',
1879 'age_limit': 0,
1880 'channel': 'Vsauce',
1881 'episode': 'Episode 1',
1882 'categories': ['Entertainment'],
1883 'season': 'Season 1',
1884 'channel_id': 'UC6nSFpj9HTCZ5t-N3Rm3-HA',
1885 'channel_url': 'https://www.youtube.com/channel/UC6nSFpj9HTCZ5t-N3Rm3-HA',
1886 'like_count': int,
1887 'playable_in_embed': True,
1888 'live_status': 'not_live',
1889 'channel_follower_count': int
1890 },
1891 'params': {
1892 'skip_download': True,
1893 },
1894 'expected_warnings': [
1895 'Skipping DASH manifest',
1896 ],
1897 },
1898 {
1899 # The following content has been identified by the YouTube community
1900 # as inappropriate or offensive to some audiences.
1901 'url': 'https://www.youtube.com/watch?v=6SJNVb0GnPI',
1902 'info_dict': {
1903 'id': '6SJNVb0GnPI',
1904 'ext': 'mp4',
1905 'title': 'Race Differences in Intelligence',
1906 'description': 'md5:5d161533167390427a1f8ee89a1fc6f1',
1907 'duration': 965,
1908 'upload_date': '20140124',
1909 'uploader': 'New Century Foundation',
1910 'uploader_id': 'UCEJYpZGqgUob0zVVEaLhvVg',
1911 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCEJYpZGqgUob0zVVEaLhvVg',
1912 },
1913 'params': {
1914 'skip_download': True,
1915 },
1916 'skip': 'This video has been removed for violating YouTube\'s policy on hate speech.',
1917 },
1918 {
1919 # itag 212
1920 'url': '1t24XAntNCY',
1921 'only_matching': True,
1922 },
1923 {
1924 # geo restricted to JP
1925 'url': 'sJL6WA-aGkQ',
1926 'only_matching': True,
1927 },
1928 {
1929 'url': 'https://invidio.us/watch?v=BaW_jenozKc',
1930 'only_matching': True,
1931 },
1932 {
1933 'url': 'https://redirect.invidious.io/watch?v=BaW_jenozKc',
1934 'only_matching': True,
1935 },
1936 {
1937 # from https://nitter.pussthecat.org/YouTube/status/1360363141947944964#m
1938 'url': 'https://redirect.invidious.io/Yh0AhrY9GjA',
1939 'only_matching': True,
1940 },
1941 {
1942 # DRM protected
1943 'url': 'https://www.youtube.com/watch?v=s7_qI6_mIXc',
1944 'only_matching': True,
1945 },
1946 {
1947 # Video with unsupported adaptive stream type formats
1948 'url': 'https://www.youtube.com/watch?v=Z4Vy8R84T1U',
1949 'info_dict': {
1950 'id': 'Z4Vy8R84T1U',
1951 'ext': 'mp4',
1952 'title': 'saman SMAN 53 Jakarta(Sancety) opening COFFEE4th at SMAN 53 Jakarta',
1953 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
1954 'duration': 433,
1955 'upload_date': '20130923',
1956 'uploader': 'Amelia Putri Harwita',
1957 'uploader_id': 'UCpOxM49HJxmC1qCalXyB3_Q',
1958 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCpOxM49HJxmC1qCalXyB3_Q',
1959 'formats': 'maxcount:10',
1960 },
1961 'params': {
1962 'skip_download': True,
1963 'youtube_include_dash_manifest': False,
1964 },
1965 'skip': 'not actual anymore',
1966 },
1967 {
1968 # Youtube Music Auto-generated description
1969 'url': 'https://music.youtube.com/watch?v=MgNrAu2pzNs',
1970 'info_dict': {
1971 'id': 'MgNrAu2pzNs',
1972 'ext': 'mp4',
1973 'title': 'Voyeur Girl',
1974 'description': 'md5:7ae382a65843d6df2685993e90a8628f',
1975 'upload_date': '20190312',
1976 'uploader': 'Stephen - Topic',
1977 'uploader_id': 'UC-pWHpBjdGG69N9mM2auIAA',
1978 'artist': 'Stephen',
1979 'track': 'Voyeur Girl',
1980 'album': 'it\'s too much love to know my dear',
1981 'release_date': '20190313',
1982 'release_year': 2019,
1983 'alt_title': 'Voyeur Girl',
1984 'view_count': int,
1985 'uploader_url': 'http://www.youtube.com/channel/UC-pWHpBjdGG69N9mM2auIAA',
1986 'playable_in_embed': True,
1987 'like_count': int,
1988 'categories': ['Music'],
1989 'channel_url': 'https://www.youtube.com/channel/UC-pWHpBjdGG69N9mM2auIAA',
1990 'channel': 'Stephen',
1991 'availability': 'public',
1992 'creator': 'Stephen',
1993 'duration': 169,
1994 'thumbnail': 'https://i.ytimg.com/vi_webp/MgNrAu2pzNs/maxresdefault.webp',
1995 'age_limit': 0,
1996 'channel_id': 'UC-pWHpBjdGG69N9mM2auIAA',
1997 'tags': 'count:11',
1998 'live_status': 'not_live',
1999 'channel_follower_count': int
2000 },
2001 'params': {
2002 'skip_download': True,
2003 },
2004 },
2005 {
2006 'url': 'https://www.youtubekids.com/watch?v=3b8nCWDgZ6Q',
2007 'only_matching': True,
2008 },
2009 {
2010 # invalid -> valid video id redirection
2011 'url': 'DJztXj2GPfl',
2012 'info_dict': {
2013 'id': 'DJztXj2GPfk',
2014 'ext': 'mp4',
2015 'title': 'Panjabi MC - Mundian To Bach Ke (The Dictator Soundtrack)',
2016 'description': 'md5:bf577a41da97918e94fa9798d9228825',
2017 'upload_date': '20090125',
2018 'uploader': 'Prochorowka',
2019 'uploader_id': 'Prochorowka',
2020 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/Prochorowka',
2021 'artist': 'Panjabi MC',
2022 'track': 'Beware of the Boys (Mundian to Bach Ke) - Motivo Hi-Lectro Remix',
2023 'album': 'Beware of the Boys (Mundian To Bach Ke)',
2024 },
2025 'params': {
2026 'skip_download': True,
2027 },
2028 'skip': 'Video unavailable',
2029 },
2030 {
2031 # empty description results in an empty string
2032 'url': 'https://www.youtube.com/watch?v=x41yOUIvK2k',
2033 'info_dict': {
2034 'id': 'x41yOUIvK2k',
2035 'ext': 'mp4',
2036 'title': 'IMG 3456',
2037 'description': '',
2038 'upload_date': '20170613',
2039 'uploader_id': 'ElevageOrVert',
2040 'uploader': 'ElevageOrVert',
2041 'view_count': int,
2042 'thumbnail': 'https://i.ytimg.com/vi_webp/x41yOUIvK2k/maxresdefault.webp',
2043 'uploader_url': 'http://www.youtube.com/user/ElevageOrVert',
2044 'like_count': int,
2045 'channel_id': 'UCo03ZQPBW5U4UC3regpt1nw',
2046 'tags': [],
2047 'channel_url': 'https://www.youtube.com/channel/UCo03ZQPBW5U4UC3regpt1nw',
2048 'availability': 'public',
2049 'age_limit': 0,
2050 'categories': ['Pets & Animals'],
2051 'duration': 7,
2052 'playable_in_embed': True,
2053 'live_status': 'not_live',
2054 'channel': 'ElevageOrVert',
2055 'channel_follower_count': int
2056 },
2057 'params': {
2058 'skip_download': True,
2059 },
2060 },
2061 {
2062 # with '};' inside yt initial data (see [1])
2063 # see [2] for an example with '};' inside ytInitialPlayerResponse
2064 # 1. https://github.com/ytdl-org/youtube-dl/issues/27093
2065 # 2. https://github.com/ytdl-org/youtube-dl/issues/27216
2066 'url': 'https://www.youtube.com/watch?v=CHqg6qOn4no',
2067 'info_dict': {
2068 'id': 'CHqg6qOn4no',
2069 'ext': 'mp4',
2070 'title': 'Part 77 Sort a list of simple types in c#',
2071 'description': 'md5:b8746fa52e10cdbf47997903f13b20dc',
2072 'upload_date': '20130831',
2073 'uploader_id': 'kudvenkat',
2074 'uploader': 'kudvenkat',
2075 'channel_id': 'UCCTVrRB5KpIiK6V2GGVsR1Q',
2076 'like_count': int,
2077 'uploader_url': 'http://www.youtube.com/user/kudvenkat',
2078 'channel_url': 'https://www.youtube.com/channel/UCCTVrRB5KpIiK6V2GGVsR1Q',
2079 'live_status': 'not_live',
2080 'categories': ['Education'],
2081 'availability': 'public',
2082 'thumbnail': 'https://i.ytimg.com/vi/CHqg6qOn4no/sddefault.jpg',
2083 'tags': 'count:12',
2084 'playable_in_embed': True,
2085 'age_limit': 0,
2086 'view_count': int,
2087 'duration': 522,
2088 'channel': 'kudvenkat',
2089 'comment_count': int,
2090 'channel_follower_count': int,
2091 'chapters': list,
2092 },
2093 'params': {
2094 'skip_download': True,
2095 },
2096 },
2097 {
2098 # another example of '};' in ytInitialData
2099 'url': 'https://www.youtube.com/watch?v=gVfgbahppCY',
2100 'only_matching': True,
2101 },
2102 {
2103 'url': 'https://www.youtube.com/watch_popup?v=63RmMXCd_bQ',
2104 'only_matching': True,
2105 },
2106 {
2107 # https://github.com/ytdl-org/youtube-dl/pull/28094
2108 'url': 'OtqTfy26tG0',
2109 'info_dict': {
2110 'id': 'OtqTfy26tG0',
2111 'ext': 'mp4',
2112 'title': 'Burn Out',
2113 'description': 'md5:8d07b84dcbcbfb34bc12a56d968b6131',
2114 'upload_date': '20141120',
2115 'uploader': 'The Cinematic Orchestra - Topic',
2116 'uploader_id': 'UCIzsJBIyo8hhpFm1NK0uLgw',
2117 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCIzsJBIyo8hhpFm1NK0uLgw',
2118 'artist': 'The Cinematic Orchestra',
2119 'track': 'Burn Out',
2120 'album': 'Every Day',
2121 'like_count': int,
2122 'live_status': 'not_live',
2123 'alt_title': 'Burn Out',
2124 'duration': 614,
2125 'age_limit': 0,
2126 'view_count': int,
2127 'channel_url': 'https://www.youtube.com/channel/UCIzsJBIyo8hhpFm1NK0uLgw',
2128 'creator': 'The Cinematic Orchestra',
2129 'channel': 'The Cinematic Orchestra',
2130 'tags': ['The Cinematic Orchestra', 'Every Day', 'Burn Out'],
2131 'channel_id': 'UCIzsJBIyo8hhpFm1NK0uLgw',
2132 'availability': 'public',
2133 'thumbnail': 'https://i.ytimg.com/vi/OtqTfy26tG0/maxresdefault.jpg',
2134 'categories': ['Music'],
2135 'playable_in_embed': True,
2136 'channel_follower_count': int
2137 },
2138 'params': {
2139 'skip_download': True,
2140 },
2141 },
2142 {
2143 # controversial video, only works with bpctr when authenticated with cookies
2144 'url': 'https://www.youtube.com/watch?v=nGC3D_FkCmg',
2145 'only_matching': True,
2146 },
2147 {
2148 # controversial video, requires bpctr/contentCheckOk
2149 'url': 'https://www.youtube.com/watch?v=SZJvDhaSDnc',
2150 'info_dict': {
2151 'id': 'SZJvDhaSDnc',
2152 'ext': 'mp4',
2153 'title': 'San Diego teen commits suicide after bullying over embarrassing video',
2154 'channel_id': 'UC-SJ6nODDmufqBzPBwCvYvQ',
2155 'uploader': 'CBS Mornings',
2156 'uploader_id': 'CBSThisMorning',
2157 'upload_date': '20140716',
2158 'description': 'md5:acde3a73d3f133fc97e837a9f76b53b7',
2159 'duration': 170,
2160 'categories': ['News & Politics'],
2161 'uploader_url': 'http://www.youtube.com/user/CBSThisMorning',
2162 'view_count': int,
2163 'channel': 'CBS Mornings',
2164 'tags': ['suicide', 'bullying', 'video', 'cbs', 'news'],
2165 'thumbnail': 'https://i.ytimg.com/vi/SZJvDhaSDnc/hqdefault.jpg',
2166 'age_limit': 18,
2167 'availability': 'needs_auth',
2168 'channel_url': 'https://www.youtube.com/channel/UC-SJ6nODDmufqBzPBwCvYvQ',
2169 'like_count': int,
2170 'live_status': 'not_live',
2171 'playable_in_embed': True,
2172 'channel_follower_count': int
2173 }
2174 },
2175 {
2176 # restricted location, https://github.com/ytdl-org/youtube-dl/issues/28685
2177 'url': 'cBvYw8_A0vQ',
2178 'info_dict': {
2179 'id': 'cBvYw8_A0vQ',
2180 'ext': 'mp4',
2181 'title': '4K Ueno Okachimachi Street Scenes 上野御徒町歩き',
2182 'description': 'md5:ea770e474b7cd6722b4c95b833c03630',
2183 'upload_date': '20201120',
2184 'uploader': 'Walk around Japan',
2185 'uploader_id': 'UC3o_t8PzBmXf5S9b7GLx1Mw',
2186 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UC3o_t8PzBmXf5S9b7GLx1Mw',
2187 'duration': 1456,
2188 'categories': ['Travel & Events'],
2189 'channel_id': 'UC3o_t8PzBmXf5S9b7GLx1Mw',
2190 'view_count': int,
2191 'channel': 'Walk around Japan',
2192 'tags': ['Ueno Tokyo', 'Okachimachi Tokyo', 'Ameyoko Street', 'Tokyo attraction', 'Travel in Tokyo'],
2193 'thumbnail': 'https://i.ytimg.com/vi_webp/cBvYw8_A0vQ/hqdefault.webp',
2194 'age_limit': 0,
2195 'availability': 'public',
2196 'channel_url': 'https://www.youtube.com/channel/UC3o_t8PzBmXf5S9b7GLx1Mw',
2197 'live_status': 'not_live',
2198 'playable_in_embed': True,
2199 'channel_follower_count': int
2200 },
2201 'params': {
2202 'skip_download': True,
2203 },
2204 }, {
2205 # Has multiple audio streams
2206 'url': 'WaOKSUlf4TM',
2207 'only_matching': True
2208 }, {
2209 # Requires Premium: has format 141 when requested using YTM url
2210 'url': 'https://music.youtube.com/watch?v=XclachpHxis',
2211 'only_matching': True
2212 }, {
2213 # multiple subtitles with same lang_code
2214 'url': 'https://www.youtube.com/watch?v=wsQiKKfKxug',
2215 'only_matching': True,
2216 }, {
2217 # Force use android client fallback
2218 'url': 'https://www.youtube.com/watch?v=YOelRv7fMxY',
2219 'info_dict': {
2220 'id': 'YOelRv7fMxY',
2221 'title': 'DIGGING A SECRET TUNNEL Part 1',
2222 'ext': '3gp',
2223 'upload_date': '20210624',
2224 'channel_id': 'UCp68_FLety0O-n9QU6phsgw',
2225 'uploader': 'colinfurze',
2226 'uploader_id': 'colinfurze',
2227 'channel_url': r're:https?://(?:www\.)?youtube\.com/channel/UCp68_FLety0O-n9QU6phsgw',
2228 'description': 'md5:5d5991195d599b56cd0c4148907eec50',
2229 'duration': 596,
2230 'categories': ['Entertainment'],
2231 'uploader_url': 'http://www.youtube.com/user/colinfurze',
2232 'view_count': int,
2233 'channel': 'colinfurze',
2234 'tags': ['Colin', 'furze', 'Terry', 'tunnel', 'underground', 'bunker'],
2235 'thumbnail': 'https://i.ytimg.com/vi/YOelRv7fMxY/maxresdefault.jpg',
2236 'age_limit': 0,
2237 'availability': 'public',
2238 'like_count': int,
2239 'live_status': 'not_live',
2240 'playable_in_embed': True,
2241 'channel_follower_count': int,
2242 'chapters': list,
2243 },
2244 'params': {
2245 'format': '17', # 3gp format available on android
2246 'extractor_args': {'youtube': {'player_client': ['android']}},
2247 },
2248 },
2249 {
2250 # Skip download of additional client configs (remix client config in this case)
2251 'url': 'https://music.youtube.com/watch?v=MgNrAu2pzNs',
2252 'only_matching': True,
2253 'params': {
2254 'extractor_args': {'youtube': {'player_skip': ['configs']}},
2255 },
2256 }, {
2257 # shorts
2258 'url': 'https://www.youtube.com/shorts/BGQWPY4IigY',
2259 'only_matching': True,
2260 }, {
2261 'note': 'Storyboards',
2262 'url': 'https://www.youtube.com/watch?v=5KLPxDtMqe8',
2263 'info_dict': {
2264 'id': '5KLPxDtMqe8',
2265 'ext': 'mhtml',
2266 'format_id': 'sb0',
2267 'title': 'Your Brain is Plastic',
2268 'uploader_id': 'scishow',
2269 'description': 'md5:89cd86034bdb5466cd87c6ba206cd2bc',
2270 'upload_date': '20140324',
2271 'uploader': 'SciShow',
2272 'like_count': int,
2273 'channel_id': 'UCZYTClx2T1of7BRZ86-8fow',
2274 'channel_url': 'https://www.youtube.com/channel/UCZYTClx2T1of7BRZ86-8fow',
2275 'view_count': int,
2276 'thumbnail': 'https://i.ytimg.com/vi/5KLPxDtMqe8/maxresdefault.jpg',
2277 'playable_in_embed': True,
2278 'tags': 'count:12',
2279 'uploader_url': 'http://www.youtube.com/user/scishow',
2280 'availability': 'public',
2281 'channel': 'SciShow',
2282 'live_status': 'not_live',
2283 'duration': 248,
2284 'categories': ['Education'],
2285 'age_limit': 0,
2286 'channel_follower_count': int,
2287 'chapters': list,
2288 }, 'params': {'format': 'mhtml', 'skip_download': True}
2289 }, {
2290 # Ensure video upload_date is in UTC timezone (video was uploaded 1641170939)
2291 'url': 'https://www.youtube.com/watch?v=2NUZ8W2llS4',
2292 'info_dict': {
2293 'id': '2NUZ8W2llS4',
2294 'ext': 'mp4',
2295 'title': 'The NP that test your phone performance 🙂',
2296 'description': 'md5:144494b24d4f9dfacb97c1bbef5de84d',
2297 'uploader': 'Leon Nguyen',
2298 'uploader_id': 'VNSXIII',
2299 'uploader_url': 'http://www.youtube.com/user/VNSXIII',
2300 'channel_id': 'UCRqNBSOHgilHfAczlUmlWHA',
2301 'channel_url': 'https://www.youtube.com/channel/UCRqNBSOHgilHfAczlUmlWHA',
2302 'duration': 21,
2303 'view_count': int,
2304 'age_limit': 0,
2305 'categories': ['Gaming'],
2306 'tags': 'count:23',
2307 'playable_in_embed': True,
2308 'live_status': 'not_live',
2309 'upload_date': '20220103',
2310 'like_count': int,
2311 'availability': 'public',
2312 'channel': 'Leon Nguyen',
2313 'thumbnail': 'https://i.ytimg.com/vi_webp/2NUZ8W2llS4/maxresdefault.webp',
2314 'comment_count': int,
2315 'channel_follower_count': int
2316 }
2317 }, {
2318 # Same video as above, but with --compat-opt no-youtube-prefer-utc-upload-date
2319 'url': 'https://www.youtube.com/watch?v=2NUZ8W2llS4',
2320 'info_dict': {
2321 'id': '2NUZ8W2llS4',
2322 'ext': 'mp4',
2323 'title': 'The NP that test your phone performance 🙂',
2324 'description': 'md5:144494b24d4f9dfacb97c1bbef5de84d',
2325 'uploader': 'Leon Nguyen',
2326 'uploader_id': 'VNSXIII',
2327 'uploader_url': 'http://www.youtube.com/user/VNSXIII',
2328 'channel_id': 'UCRqNBSOHgilHfAczlUmlWHA',
2329 'channel_url': 'https://www.youtube.com/channel/UCRqNBSOHgilHfAczlUmlWHA',
2330 'duration': 21,
2331 'view_count': int,
2332 'age_limit': 0,
2333 'categories': ['Gaming'],
2334 'tags': 'count:23',
2335 'playable_in_embed': True,
2336 'live_status': 'not_live',
2337 'upload_date': '20220102',
2338 'like_count': int,
2339 'availability': 'public',
2340 'channel': 'Leon Nguyen',
2341 'thumbnail': 'https://i.ytimg.com/vi_webp/2NUZ8W2llS4/maxresdefault.webp',
2342 'comment_count': int,
2343 'channel_follower_count': int
2344 },
2345 'params': {'compat_opts': ['no-youtube-prefer-utc-upload-date']}
2346 }, {
2347 # date text is premiered video, ensure upload date in UTC (published 1641172509)
2348 'url': 'https://www.youtube.com/watch?v=mzZzzBU6lrM',
2349 'info_dict': {
2350 'id': 'mzZzzBU6lrM',
2351 'ext': 'mp4',
2352 'title': 'I Met GeorgeNotFound In Real Life...',
2353 'description': 'md5:cca98a355c7184e750f711f3a1b22c84',
2354 'uploader': 'Quackity',
2355 'uploader_id': 'QuackityHQ',
2356 'uploader_url': 'http://www.youtube.com/user/QuackityHQ',
2357 'channel_id': 'UC_8NknAFiyhOUaZqHR3lq3Q',
2358 'channel_url': 'https://www.youtube.com/channel/UC_8NknAFiyhOUaZqHR3lq3Q',
2359 'duration': 955,
2360 'view_count': int,
2361 'age_limit': 0,
2362 'categories': ['Entertainment'],
2363 'tags': 'count:26',
2364 'playable_in_embed': True,
2365 'live_status': 'not_live',
2366 'release_timestamp': 1641172509,
2367 'release_date': '20220103',
2368 'upload_date': '20220103',
2369 'like_count': int,
2370 'availability': 'public',
2371 'channel': 'Quackity',
2372 'thumbnail': 'https://i.ytimg.com/vi/mzZzzBU6lrM/maxresdefault.jpg',
2373 'channel_follower_count': int
2374 }
2375 },
2376 { # continuous livestream. Microformat upload date should be preferred.
2377 # Upload date was 2021-06-19 (not UTC), while stream start is 2021-11-27
2378 'url': 'https://www.youtube.com/watch?v=kgx4WGK0oNU',
2379 'info_dict': {
2380 'id': 'kgx4WGK0oNU',
2381 'title': r're:jazz\/lofi hip hop radio🌱chill beats to relax\/study to \[LIVE 24\/7\] \d{4}-\d{2}-\d{2} \d{2}:\d{2}',
2382 'ext': 'mp4',
2383 'channel_id': 'UC84whx2xxsiA1gXHXXqKGOA',
2384 'availability': 'public',
2385 'age_limit': 0,
2386 'release_timestamp': 1637975704,
2387 'upload_date': '20210619',
2388 'channel_url': 'https://www.youtube.com/channel/UC84whx2xxsiA1gXHXXqKGOA',
2389 'live_status': 'is_live',
2390 'thumbnail': 'https://i.ytimg.com/vi/kgx4WGK0oNU/maxresdefault.jpg',
2391 'uploader': '阿鲍Abao',
2392 'uploader_url': 'http://www.youtube.com/channel/UC84whx2xxsiA1gXHXXqKGOA',
2393 'channel': 'Abao in Tokyo',
2394 'channel_follower_count': int,
2395 'release_date': '20211127',
2396 'tags': 'count:39',
2397 'categories': ['People & Blogs'],
2398 'like_count': int,
2399 'uploader_id': 'UC84whx2xxsiA1gXHXXqKGOA',
2400 'view_count': int,
2401 'playable_in_embed': True,
2402 'description': 'md5:2ef1d002cad520f65825346e2084e49d',
2403 'concurrent_view_count': int,
2404 },
2405 'params': {'skip_download': True}
2406 }, {
2407 # Story. Requires specific player params to work.
2408 'url': 'https://www.youtube.com/watch?v=vv8qTUWmulI',
2409 'info_dict': {
2410 'id': 'vv8qTUWmulI',
2411 'ext': 'mp4',
2412 'availability': 'unlisted',
2413 'view_count': int,
2414 'channel_id': 'UCzIZ8HrzDgc-pNQDUG6avBA',
2415 'upload_date': '20220526',
2416 'categories': ['Education'],
2417 'title': 'Story',
2418 'channel': 'IT\'S HISTORY',
2419 'description': '',
2420 'uploader_id': 'BlastfromthePast',
2421 'duration': 12,
2422 'uploader': 'IT\'S HISTORY',
2423 'playable_in_embed': True,
2424 'age_limit': 0,
2425 'live_status': 'not_live',
2426 'tags': [],
2427 'thumbnail': 'https://i.ytimg.com/vi_webp/vv8qTUWmulI/maxresdefault.webp',
2428 'uploader_url': 'http://www.youtube.com/user/BlastfromthePast',
2429 'channel_url': 'https://www.youtube.com/channel/UCzIZ8HrzDgc-pNQDUG6avBA',
2430 },
2431 'skip': 'stories get removed after some period of time',
2432 }, {
2433 'url': 'https://www.youtube.com/watch?v=tjjjtzRLHvA',
2434 'info_dict': {
2435 'id': 'tjjjtzRLHvA',
2436 'ext': 'mp4',
2437 'title': 'ハッシュタグ無し };if window.ytcsi',
2438 'upload_date': '20220323',
2439 'like_count': int,
2440 'availability': 'unlisted',
2441 'channel': 'nao20010128nao',
2442 'thumbnail': 'https://i.ytimg.com/vi_webp/tjjjtzRLHvA/maxresdefault.webp',
2443 'age_limit': 0,
2444 'uploader': 'nao20010128nao',
2445 'uploader_id': 'nao20010128nao',
2446 'categories': ['Music'],
2447 'view_count': int,
2448 'description': '',
2449 'channel_url': 'https://www.youtube.com/channel/UCdqltm_7iv1Vs6kp6Syke5A',
2450 'channel_id': 'UCdqltm_7iv1Vs6kp6Syke5A',
2451 'live_status': 'not_live',
2452 'playable_in_embed': True,
2453 'channel_follower_count': int,
2454 'duration': 6,
2455 'tags': [],
2456 'uploader_url': 'http://www.youtube.com/user/nao20010128nao',
2457 }
2458 }, {
2459 # Prefer primary title+description language metadata by default
2460 # Do not prefer translated description if primary is empty
2461 'url': 'https://www.youtube.com/watch?v=el3E4MbxRqQ',
2462 'info_dict': {
2463 'id': 'el3E4MbxRqQ',
2464 'ext': 'mp4',
2465 'title': 'dlp test video 2 - primary sv no desc',
2466 'description': '',
2467 'channel': 'cole-dlp-test-acc',
2468 'tags': [],
2469 'view_count': int,
2470 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
2471 'like_count': int,
2472 'playable_in_embed': True,
2473 'availability': 'unlisted',
2474 'thumbnail': 'https://i.ytimg.com/vi_webp/el3E4MbxRqQ/maxresdefault.webp',
2475 'age_limit': 0,
2476 'duration': 5,
2477 'uploader_id': 'UCiu-3thuViMebBjw_5nWYrA',
2478 'uploader_url': 'http://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
2479 'live_status': 'not_live',
2480 'upload_date': '20220908',
2481 'categories': ['People & Blogs'],
2482 'uploader': 'cole-dlp-test-acc',
2483 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
2484 },
2485 'params': {'skip_download': True}
2486 }, {
2487 # Extractor argument: prefer translated title+description
2488 'url': 'https://www.youtube.com/watch?v=gHKT4uU8Zng',
2489 'info_dict': {
2490 'id': 'gHKT4uU8Zng',
2491 'ext': 'mp4',
2492 'channel': 'cole-dlp-test-acc',
2493 'tags': [],
2494 'duration': 5,
2495 'live_status': 'not_live',
2496 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
2497 'upload_date': '20220728',
2498 'uploader_id': 'UCiu-3thuViMebBjw_5nWYrA',
2499 'view_count': int,
2500 'categories': ['People & Blogs'],
2501 'thumbnail': 'https://i.ytimg.com/vi_webp/gHKT4uU8Zng/maxresdefault.webp',
2502 'title': 'dlp test video title translated (fr)',
2503 'availability': 'public',
2504 'uploader': 'cole-dlp-test-acc',
2505 'age_limit': 0,
2506 'description': 'dlp test video description translated (fr)',
2507 'playable_in_embed': True,
2508 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
2509 'uploader_url': 'http://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
2510 },
2511 'params': {'skip_download': True, 'extractor_args': {'youtube': {'lang': ['fr']}}},
2512 'expected_warnings': [r'Preferring "fr" translated fields'],
2513 }, {
2514 'note': '6 channel audio',
2515 'url': 'https://www.youtube.com/watch?v=zgdo7-RRjgo',
2516 'only_matching': True,
2517 }, {
2518 'note': 'Multiple HLS formats with same itag',
2519 'url': 'https://www.youtube.com/watch?v=kX3nB4PpJko',
2520 'info_dict': {
2521 'id': 'kX3nB4PpJko',
2522 'ext': 'mp4',
2523 'categories': ['Entertainment'],
2524 'description': 'md5:e8031ff6e426cdb6a77670c9b81f6fa6',
2525 'uploader_url': 'http://www.youtube.com/user/MrBeast6000',
2526 'live_status': 'not_live',
2527 'duration': 937,
2528 'channel_follower_count': int,
2529 'thumbnail': 'https://i.ytimg.com/vi_webp/kX3nB4PpJko/maxresdefault.webp',
2530 'title': 'Last To Take Hand Off Jet, Keeps It!',
2531 'channel': 'MrBeast',
2532 'playable_in_embed': True,
2533 'view_count': int,
2534 'upload_date': '20221112',
2535 'uploader': 'MrBeast',
2536 'uploader_id': 'MrBeast6000',
2537 'channel_url': 'https://www.youtube.com/channel/UCX6OQ3DkcsbYNE6H8uQQuVA',
2538 'age_limit': 0,
2539 'availability': 'public',
2540 'channel_id': 'UCX6OQ3DkcsbYNE6H8uQQuVA',
2541 'like_count': int,
2542 'tags': [],
2543 },
2544 'params': {'extractor_args': {'youtube': {'player_client': ['ios']}}, 'format': '233-1'},
2545 }
2546 ]
2547
2548 _WEBPAGE_TESTS = [
2549 # YouTube <object> embed
2550 {
2551 'url': 'http://www.improbable.com/2017/04/03/untrained-modern-youths-and-ancient-masters-in-selfie-portraits/',
2552 'md5': '873c81d308b979f0e23ee7e620b312a3',
2553 'info_dict': {
2554 'id': 'msN87y-iEx0',
2555 'ext': 'mp4',
2556 'title': 'Feynman: Mirrors FUN TO IMAGINE 6',
2557 'upload_date': '20080526',
2558 'description': 'md5:873c81d308b979f0e23ee7e620b312a3',
2559 'uploader': 'Christopher Sykes',
2560 'uploader_id': 'ChristopherJSykes',
2561 'age_limit': 0,
2562 'tags': ['feynman', 'mirror', 'science', 'physics', 'imagination', 'fun', 'cool', 'puzzle'],
2563 'channel_id': 'UCCeo--lls1vna5YJABWAcVA',
2564 'playable_in_embed': True,
2565 'thumbnail': 'https://i.ytimg.com/vi/msN87y-iEx0/hqdefault.jpg',
2566 'like_count': int,
2567 'comment_count': int,
2568 'channel': 'Christopher Sykes',
2569 'live_status': 'not_live',
2570 'channel_url': 'https://www.youtube.com/channel/UCCeo--lls1vna5YJABWAcVA',
2571 'availability': 'public',
2572 'duration': 195,
2573 'view_count': int,
2574 'categories': ['Science & Technology'],
2575 'channel_follower_count': int,
2576 'uploader_url': 'http://www.youtube.com/user/ChristopherJSykes',
2577 },
2578 'params': {
2579 'skip_download': True,
2580 }
2581 },
2582 ]
2583
2584 @classmethod
2585 def suitable(cls, url):
2586 from ..utils import parse_qs
2587
2588 qs = parse_qs(url)
2589 if qs.get('list', [None])[0]:
2590 return False
2591 return super().suitable(url)
2592
2593 def __init__(self, *args, **kwargs):
2594 super().__init__(*args, **kwargs)
2595 self._code_cache = {}
2596 self._player_cache = {}
2597
2598 def _prepare_live_from_start_formats(self, formats, video_id, live_start_time, url, webpage_url, smuggled_data, is_live):
2599 lock = threading.Lock()
2600 start_time = time.time()
2601 formats = [f for f in formats if f.get('is_from_start')]
2602
2603 def refetch_manifest(format_id, delay):
2604 nonlocal formats, start_time, is_live
2605 if time.time() <= start_time + delay:
2606 return
2607
2608 _, _, prs, player_url = self._download_player_responses(url, smuggled_data, video_id, webpage_url)
2609 video_details = traverse_obj(
2610 prs, (..., 'videoDetails'), expected_type=dict, default=[])
2611 microformats = traverse_obj(
2612 prs, (..., 'microformat', 'playerMicroformatRenderer'),
2613 expected_type=dict, default=[])
2614 _, live_status, _, formats, _ = self._list_formats(video_id, microformats, video_details, prs, player_url)
2615 is_live = live_status == 'is_live'
2616 start_time = time.time()
2617
2618 def mpd_feed(format_id, delay):
2619 """
2620 @returns (manifest_url, manifest_stream_number, is_live) or None
2621 """
2622 with lock:
2623 refetch_manifest(format_id, delay)
2624
2625 f = next((f for f in formats if f['format_id'] == format_id), None)
2626 if not f:
2627 if not is_live:
2628 self.to_screen(f'{video_id}: Video is no longer live')
2629 else:
2630 self.report_warning(
2631 f'Cannot find refreshed manifest for format {format_id}{bug_reports_message()}')
2632 return None
2633 return f['manifest_url'], f['manifest_stream_number'], is_live
2634
2635 for f in formats:
2636 f['is_live'] = is_live
2637 gen = functools.partial(self._live_dash_fragments, video_id, f['format_id'],
2638 live_start_time, mpd_feed, not is_live and f.copy())
2639 if is_live:
2640 f['fragments'] = gen
2641 f['protocol'] = 'http_dash_segments_generator'
2642 else:
2643 f['fragments'] = LazyList(gen({}))
2644 del f['is_from_start']
2645
2646 def _live_dash_fragments(self, video_id, format_id, live_start_time, mpd_feed, manifestless_orig_fmt, ctx):
2647 FETCH_SPAN, MAX_DURATION = 5, 432000
2648
2649 mpd_url, stream_number, is_live = None, None, True
2650
2651 begin_index = 0
2652 download_start_time = ctx.get('start') or time.time()
2653
2654 lack_early_segments = download_start_time - (live_start_time or download_start_time) > MAX_DURATION
2655 if lack_early_segments:
2656 self.report_warning(bug_reports_message(
2657 'Starting download from the last 120 hours of the live stream since '
2658 'YouTube does not have data before that. If you think this is wrong,'), only_once=True)
2659 lack_early_segments = True
2660
2661 known_idx, no_fragment_score, last_segment_url = begin_index, 0, None
2662 fragments, fragment_base_url = None, None
2663
2664 def _extract_sequence_from_mpd(refresh_sequence, immediate):
2665 nonlocal mpd_url, stream_number, is_live, no_fragment_score, fragments, fragment_base_url
2666 # Obtain from MPD's maximum seq value
2667 old_mpd_url = mpd_url
2668 last_error = ctx.pop('last_error', None)
2669 expire_fast = immediate or last_error and isinstance(last_error, urllib.error.HTTPError) and last_error.code == 403
2670 mpd_url, stream_number, is_live = (mpd_feed(format_id, 5 if expire_fast else 18000)
2671 or (mpd_url, stream_number, False))
2672 if not refresh_sequence:
2673 if expire_fast and not is_live:
2674 return False, last_seq
2675 elif old_mpd_url == mpd_url:
2676 return True, last_seq
2677 if manifestless_orig_fmt:
2678 fmt_info = manifestless_orig_fmt
2679 else:
2680 try:
2681 fmts, _ = self._extract_mpd_formats_and_subtitles(
2682 mpd_url, None, note=False, errnote=False, fatal=False)
2683 except ExtractorError:
2684 fmts = None
2685 if not fmts:
2686 no_fragment_score += 2
2687 return False, last_seq
2688 fmt_info = next(x for x in fmts if x['manifest_stream_number'] == stream_number)
2689 fragments = fmt_info['fragments']
2690 fragment_base_url = fmt_info['fragment_base_url']
2691 assert fragment_base_url
2692
2693 _last_seq = int(re.search(r'(?:/|^)sq/(\d+)', fragments[-1]['path']).group(1))
2694 return True, _last_seq
2695
2696 self.write_debug(f'[{video_id}] Generating fragments for format {format_id}')
2697 while is_live:
2698 fetch_time = time.time()
2699 if no_fragment_score > 30:
2700 return
2701 if last_segment_url:
2702 # Obtain from "X-Head-Seqnum" header value from each segment
2703 try:
2704 urlh = self._request_webpage(
2705 last_segment_url, None, note=False, errnote=False, fatal=False)
2706 except ExtractorError:
2707 urlh = None
2708 last_seq = try_get(urlh, lambda x: int_or_none(x.headers['X-Head-Seqnum']))
2709 if last_seq is None:
2710 no_fragment_score += 2
2711 last_segment_url = None
2712 continue
2713 else:
2714 should_continue, last_seq = _extract_sequence_from_mpd(True, no_fragment_score > 15)
2715 no_fragment_score += 2
2716 if not should_continue:
2717 continue
2718
2719 if known_idx > last_seq:
2720 last_segment_url = None
2721 continue
2722
2723 last_seq += 1
2724
2725 if begin_index < 0 and known_idx < 0:
2726 # skip from the start when it's negative value
2727 known_idx = last_seq + begin_index
2728 if lack_early_segments:
2729 known_idx = max(known_idx, last_seq - int(MAX_DURATION // fragments[-1]['duration']))
2730 try:
2731 for idx in range(known_idx, last_seq):
2732 # do not update sequence here or you'll get skipped some part of it
2733 should_continue, _ = _extract_sequence_from_mpd(False, False)
2734 if not should_continue:
2735 known_idx = idx - 1
2736 raise ExtractorError('breaking out of outer loop')
2737 last_segment_url = urljoin(fragment_base_url, 'sq/%d' % idx)
2738 yield {
2739 'url': last_segment_url,
2740 'fragment_count': last_seq,
2741 }
2742 if known_idx == last_seq:
2743 no_fragment_score += 5
2744 else:
2745 no_fragment_score = 0
2746 known_idx = last_seq
2747 except ExtractorError:
2748 continue
2749
2750 if manifestless_orig_fmt:
2751 # Stop at the first iteration if running for post-live manifestless;
2752 # fragment count no longer increase since it starts
2753 break
2754
2755 time.sleep(max(0, FETCH_SPAN + fetch_time - time.time()))
2756
2757 def _extract_player_url(self, *ytcfgs, webpage=None):
2758 player_url = traverse_obj(
2759 ytcfgs, (..., 'PLAYER_JS_URL'), (..., 'WEB_PLAYER_CONTEXT_CONFIGS', ..., 'jsUrl'),
2760 get_all=False, expected_type=str)
2761 if not player_url:
2762 return
2763 return urljoin('https://www.youtube.com', player_url)
2764
2765 def _download_player_url(self, video_id, fatal=False):
2766 res = self._download_webpage(
2767 'https://www.youtube.com/iframe_api',
2768 note='Downloading iframe API JS', video_id=video_id, fatal=fatal)
2769 if res:
2770 player_version = self._search_regex(
2771 r'player\\?/([0-9a-fA-F]{8})\\?/', res, 'player version', fatal=fatal)
2772 if player_version:
2773 return f'https://www.youtube.com/s/player/{player_version}/player_ias.vflset/en_US/base.js'
2774
2775 def _signature_cache_id(self, example_sig):
2776 """ Return a string representation of a signature """
2777 return '.'.join(str(len(part)) for part in example_sig.split('.'))
2778
2779 @classmethod
2780 def _extract_player_info(cls, player_url):
2781 for player_re in cls._PLAYER_INFO_RE:
2782 id_m = re.search(player_re, player_url)
2783 if id_m:
2784 break
2785 else:
2786 raise ExtractorError('Cannot identify player %r' % player_url)
2787 return id_m.group('id')
2788
2789 def _load_player(self, video_id, player_url, fatal=True):
2790 player_id = self._extract_player_info(player_url)
2791 if player_id not in self._code_cache:
2792 code = self._download_webpage(
2793 player_url, video_id, fatal=fatal,
2794 note='Downloading player ' + player_id,
2795 errnote='Download of %s failed' % player_url)
2796 if code:
2797 self._code_cache[player_id] = code
2798 return self._code_cache.get(player_id)
2799
2800 def _extract_signature_function(self, video_id, player_url, example_sig):
2801 player_id = self._extract_player_info(player_url)
2802
2803 # Read from filesystem cache
2804 func_id = f'js_{player_id}_{self._signature_cache_id(example_sig)}'
2805 assert os.path.basename(func_id) == func_id
2806
2807 self.write_debug(f'Extracting signature function {func_id}')
2808 cache_spec, code = self.cache.load('youtube-sigfuncs', func_id), None
2809
2810 if not cache_spec:
2811 code = self._load_player(video_id, player_url)
2812 if code:
2813 res = self._parse_sig_js(code)
2814 test_string = ''.join(map(chr, range(len(example_sig))))
2815 cache_spec = [ord(c) for c in res(test_string)]
2816 self.cache.store('youtube-sigfuncs', func_id, cache_spec)
2817
2818 return lambda s: ''.join(s[i] for i in cache_spec)
2819
2820 def _print_sig_code(self, func, example_sig):
2821 if not self.get_param('youtube_print_sig_code'):
2822 return
2823
2824 def gen_sig_code(idxs):
2825 def _genslice(start, end, step):
2826 starts = '' if start == 0 else str(start)
2827 ends = (':%d' % (end + step)) if end + step >= 0 else ':'
2828 steps = '' if step == 1 else (':%d' % step)
2829 return f's[{starts}{ends}{steps}]'
2830
2831 step = None
2832 # Quelch pyflakes warnings - start will be set when step is set
2833 start = '(Never used)'
2834 for i, prev in zip(idxs[1:], idxs[:-1]):
2835 if step is not None:
2836 if i - prev == step:
2837 continue
2838 yield _genslice(start, prev, step)
2839 step = None
2840 continue
2841 if i - prev in [-1, 1]:
2842 step = i - prev
2843 start = prev
2844 continue
2845 else:
2846 yield 's[%d]' % prev
2847 if step is None:
2848 yield 's[%d]' % i
2849 else:
2850 yield _genslice(start, i, step)
2851
2852 test_string = ''.join(map(chr, range(len(example_sig))))
2853 cache_res = func(test_string)
2854 cache_spec = [ord(c) for c in cache_res]
2855 expr_code = ' + '.join(gen_sig_code(cache_spec))
2856 signature_id_tuple = '(%s)' % (
2857 ', '.join(str(len(p)) for p in example_sig.split('.')))
2858 code = ('if tuple(len(p) for p in s.split(\'.\')) == %s:\n'
2859 ' return %s\n') % (signature_id_tuple, expr_code)
2860 self.to_screen('Extracted signature function:\n' + code)
2861
2862 def _parse_sig_js(self, jscode):
2863 funcname = self._search_regex(
2864 (r'\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2865 r'\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2866 r'\bm=(?P<sig>[a-zA-Z0-9$]{2,})\(decodeURIComponent\(h\.s\)\)',
2867 r'\bc&&\(c=(?P<sig>[a-zA-Z0-9$]{2,})\(decodeURIComponent\(c\)\)',
2868 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+\)',
2869 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*\)',
2870 r'(?P<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)',
2871 # Obsolete patterns
2872 r'(["\'])signature\1\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2873 r'\.sig\|\|(?P<sig>[a-zA-Z0-9$]+)\(',
2874 r'yt\.akamaized\.net/\)\s*\|\|\s*.*?\s*[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*(?:encodeURIComponent\s*\()?\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2875 r'\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2876 r'\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2877 r'\bc\s*&&\s*a\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2878 r'\bc\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
2879 r'\bc\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\('),
2880 jscode, 'Initial JS player signature function name', group='sig')
2881
2882 jsi = JSInterpreter(jscode)
2883 initial_function = jsi.extract_function(funcname)
2884 return lambda s: initial_function([s])
2885
2886 def _cached(self, func, *cache_id):
2887 def inner(*args, **kwargs):
2888 if cache_id not in self._player_cache:
2889 try:
2890 self._player_cache[cache_id] = func(*args, **kwargs)
2891 except ExtractorError as e:
2892 self._player_cache[cache_id] = e
2893 except Exception as e:
2894 self._player_cache[cache_id] = ExtractorError(traceback.format_exc(), cause=e)
2895
2896 ret = self._player_cache[cache_id]
2897 if isinstance(ret, Exception):
2898 raise ret
2899 return ret
2900 return inner
2901
2902 def _decrypt_signature(self, s, video_id, player_url):
2903 """Turn the encrypted s field into a working signature"""
2904 extract_sig = self._cached(
2905 self._extract_signature_function, 'sig', player_url, self._signature_cache_id(s))
2906 func = extract_sig(video_id, player_url, s)
2907 self._print_sig_code(func, s)
2908 return func(s)
2909
2910 def _decrypt_nsig(self, s, video_id, player_url):
2911 """Turn the encrypted n field into a working signature"""
2912 if player_url is None:
2913 raise ExtractorError('Cannot decrypt nsig without player_url')
2914 player_url = urljoin('https://www.youtube.com', player_url)
2915
2916 try:
2917 jsi, player_id, func_code = self._extract_n_function_code(video_id, player_url)
2918 except ExtractorError as e:
2919 raise ExtractorError('Unable to extract nsig function code', cause=e)
2920 if self.get_param('youtube_print_sig_code'):
2921 self.to_screen(f'Extracted nsig function from {player_id}:\n{func_code[1]}\n')
2922
2923 try:
2924 extract_nsig = self._cached(self._extract_n_function_from_code, 'nsig func', player_url)
2925 ret = extract_nsig(jsi, func_code)(s)
2926 except JSInterpreter.Exception as e:
2927 try:
2928 jsi = PhantomJSwrapper(self, timeout=5000)
2929 except ExtractorError:
2930 raise e
2931 self.report_warning(
2932 f'Native nsig extraction failed: Trying with PhantomJS\n'
2933 f' n = {s} ; player = {player_url}', video_id)
2934 self.write_debug(e, only_once=True)
2935
2936 args, func_body = func_code
2937 ret = jsi.execute(
2938 f'console.log(function({", ".join(args)}) {{ {func_body} }}({s!r}));',
2939 video_id=video_id, note='Executing signature code').strip()
2940
2941 self.write_debug(f'Decrypted nsig {s} => {ret}')
2942 return ret
2943
2944 def _extract_n_function_name(self, jscode):
2945 funcname, idx = self._search_regex(
2946 r'\.get\("n"\)\)&&\(b=(?P<nfunc>[a-zA-Z0-9$]+)(?:\[(?P<idx>\d+)\])?\([a-zA-Z0-9]\)',
2947 jscode, 'Initial JS player n function name', group=('nfunc', 'idx'))
2948 if not idx:
2949 return funcname
2950
2951 return json.loads(js_to_json(self._search_regex(
2952 rf'var {re.escape(funcname)}\s*=\s*(\[.+?\]);', jscode,
2953 f'Initial JS player n function list ({funcname}.{idx})')))[int(idx)]
2954
2955 def _extract_n_function_code(self, video_id, player_url):
2956 player_id = self._extract_player_info(player_url)
2957 func_code = self.cache.load('youtube-nsig', player_id, min_ver='2022.09.1')
2958 jscode = func_code or self._load_player(video_id, player_url)
2959 jsi = JSInterpreter(jscode)
2960
2961 if func_code:
2962 return jsi, player_id, func_code
2963
2964 func_name = self._extract_n_function_name(jscode)
2965
2966 # For redundancy
2967 func_code = self._search_regex(
2968 r'''(?xs)%s\s*=\s*function\s*\((?P<var>[\w$]+)\)\s*
2969 # NB: The end of the regex is intentionally kept strict
2970 {(?P<code>.+?}\s*return\ [\w$]+.join\(""\))};''' % func_name,
2971 jscode, 'nsig function', group=('var', 'code'), default=None)
2972 if func_code:
2973 func_code = ([func_code[0]], func_code[1])
2974 else:
2975 self.write_debug('Extracting nsig function with jsinterp')
2976 func_code = jsi.extract_function_code(func_name)
2977
2978 self.cache.store('youtube-nsig', player_id, func_code)
2979 return jsi, player_id, func_code
2980
2981 def _extract_n_function_from_code(self, jsi, func_code):
2982 func = jsi.extract_function_from_code(*func_code)
2983
2984 def extract_nsig(s):
2985 try:
2986 ret = func([s])
2987 except JSInterpreter.Exception:
2988 raise
2989 except Exception as e:
2990 raise JSInterpreter.Exception(traceback.format_exc(), cause=e)
2991
2992 if ret.startswith('enhanced_except_'):
2993 raise JSInterpreter.Exception('Signature function returned an exception')
2994 return ret
2995
2996 return extract_nsig
2997
2998 def _extract_signature_timestamp(self, video_id, player_url, ytcfg=None, fatal=False):
2999 """
3000 Extract signatureTimestamp (sts)
3001 Required to tell API what sig/player version is in use.
3002 """
3003 sts = None
3004 if isinstance(ytcfg, dict):
3005 sts = int_or_none(ytcfg.get('STS'))
3006
3007 if not sts:
3008 # Attempt to extract from player
3009 if player_url is None:
3010 error_msg = 'Cannot extract signature timestamp without player_url.'
3011 if fatal:
3012 raise ExtractorError(error_msg)
3013 self.report_warning(error_msg)
3014 return
3015 code = self._load_player(video_id, player_url, fatal=fatal)
3016 if code:
3017 sts = int_or_none(self._search_regex(
3018 r'(?:signatureTimestamp|sts)\s*:\s*(?P<sts>[0-9]{5})', code,
3019 'JS player signature timestamp', group='sts', fatal=fatal))
3020 return sts
3021
3022 def _mark_watched(self, video_id, player_responses):
3023 for is_full, key in enumerate(('videostatsPlaybackUrl', 'videostatsWatchtimeUrl')):
3024 label = 'fully ' if is_full else ''
3025 url = get_first(player_responses, ('playbackTracking', key, 'baseUrl'),
3026 expected_type=url_or_none)
3027 if not url:
3028 self.report_warning(f'Unable to mark {label}watched')
3029 return
3030 parsed_url = urllib.parse.urlparse(url)
3031 qs = urllib.parse.parse_qs(parsed_url.query)
3032
3033 # cpn generation algorithm is reverse engineered from base.js.
3034 # In fact it works even with dummy cpn.
3035 CPN_ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
3036 cpn = ''.join(CPN_ALPHABET[random.randint(0, 256) & 63] for _ in range(0, 16))
3037
3038 # # more consistent results setting it to right before the end
3039 video_length = [str(float((qs.get('len') or ['1.5'])[0]) - 1)]
3040
3041 qs.update({
3042 'ver': ['2'],
3043 'cpn': [cpn],
3044 'cmt': video_length,
3045 'el': 'detailpage', # otherwise defaults to "shorts"
3046 })
3047
3048 if is_full:
3049 # these seem to mark watchtime "history" in the real world
3050 # they're required, so send in a single value
3051 qs.update({
3052 'st': 0,
3053 'et': video_length,
3054 })
3055
3056 url = urllib.parse.urlunparse(
3057 parsed_url._replace(query=urllib.parse.urlencode(qs, True)))
3058
3059 self._download_webpage(
3060 url, video_id, f'Marking {label}watched',
3061 'Unable to mark watched', fatal=False)
3062
3063 @classmethod
3064 def _extract_from_webpage(cls, url, webpage):
3065 # Invidious Instances
3066 # https://github.com/yt-dlp/yt-dlp/issues/195
3067 # https://github.com/iv-org/invidious/pull/1730
3068 mobj = re.search(
3069 r'<link rel="alternate" href="(?P<url>https://www\.youtube\.com/watch\?v=[0-9A-Za-z_-]{11})"',
3070 webpage)
3071 if mobj:
3072 yield cls.url_result(mobj.group('url'), cls)
3073 raise cls.StopExtraction()
3074
3075 yield from super()._extract_from_webpage(url, webpage)
3076
3077 # lazyYT YouTube embed
3078 for id_ in re.findall(r'class="lazyYT" data-youtube-id="([^"]+)"', webpage):
3079 yield cls.url_result(unescapeHTML(id_), cls, id_)
3080
3081 # Wordpress "YouTube Video Importer" plugin
3082 for m in re.findall(r'''(?x)<div[^>]+
3083 class=(?P<q1>[\'"])[^\'"]*\byvii_single_video_player\b[^\'"]*(?P=q1)[^>]+
3084 data-video_id=(?P<q2>[\'"])([^\'"]+)(?P=q2)''', webpage):
3085 yield cls.url_result(m[-1], cls, m[-1])
3086
3087 @classmethod
3088 def extract_id(cls, url):
3089 video_id = cls.get_temp_id(url)
3090 if not video_id:
3091 raise ExtractorError(f'Invalid URL: {url}')
3092 return video_id
3093
3094 def _extract_chapters_from_json(self, data, duration):
3095 chapter_list = traverse_obj(
3096 data, (
3097 'playerOverlays', 'playerOverlayRenderer', 'decoratedPlayerBarRenderer',
3098 'decoratedPlayerBarRenderer', 'playerBar', 'chapteredPlayerBarRenderer', 'chapters'
3099 ), expected_type=list)
3100
3101 return self._extract_chapters(
3102 chapter_list,
3103 chapter_time=lambda chapter: float_or_none(
3104 traverse_obj(chapter, ('chapterRenderer', 'timeRangeStartMillis')), scale=1000),
3105 chapter_title=lambda chapter: traverse_obj(
3106 chapter, ('chapterRenderer', 'title', 'simpleText'), expected_type=str),
3107 duration=duration)
3108
3109 def _extract_chapters_from_engagement_panel(self, data, duration):
3110 content_list = traverse_obj(
3111 data,
3112 ('engagementPanels', ..., 'engagementPanelSectionListRenderer', 'content', 'macroMarkersListRenderer', 'contents'),
3113 expected_type=list, default=[])
3114 chapter_time = lambda chapter: parse_duration(self._get_text(chapter, 'timeDescription'))
3115 chapter_title = lambda chapter: self._get_text(chapter, 'title')
3116
3117 return next(filter(None, (
3118 self._extract_chapters(traverse_obj(contents, (..., 'macroMarkersListItemRenderer')),
3119 chapter_time, chapter_title, duration)
3120 for contents in content_list)), [])
3121
3122 def _extract_chapters_from_description(self, description, duration):
3123 duration_re = r'(?:\d+:)?\d{1,2}:\d{2}'
3124 sep_re = r'(?m)^\s*(%s)\b\W*\s(%s)\s*$'
3125 return self._extract_chapters(
3126 re.findall(sep_re % (duration_re, r'.+?'), description or ''),
3127 chapter_time=lambda x: parse_duration(x[0]), chapter_title=lambda x: x[1],
3128 duration=duration, strict=False) or self._extract_chapters(
3129 re.findall(sep_re % (r'.+?', duration_re), description or ''),
3130 chapter_time=lambda x: parse_duration(x[1]), chapter_title=lambda x: x[0],
3131 duration=duration, strict=False)
3132
3133 def _extract_chapters(self, chapter_list, chapter_time, chapter_title, duration, strict=True):
3134 if not duration:
3135 return
3136 chapter_list = [{
3137 'start_time': chapter_time(chapter),
3138 'title': chapter_title(chapter),
3139 } for chapter in chapter_list or []]
3140 if not strict:
3141 chapter_list.sort(key=lambda c: c['start_time'] or 0)
3142
3143 chapters = [{'start_time': 0}]
3144 for idx, chapter in enumerate(chapter_list):
3145 if chapter['start_time'] is None:
3146 self.report_warning(f'Incomplete chapter {idx}')
3147 elif chapters[-1]['start_time'] <= chapter['start_time'] <= duration:
3148 chapters.append(chapter)
3149 elif chapter not in chapters:
3150 self.report_warning(
3151 f'Invalid start time ({chapter["start_time"]} < {chapters[-1]["start_time"]}) for chapter "{chapter["title"]}"')
3152 return chapters[1:]
3153
3154 def _extract_comment(self, comment_renderer, parent=None):
3155 comment_id = comment_renderer.get('commentId')
3156 if not comment_id:
3157 return
3158
3159 text = self._get_text(comment_renderer, 'contentText')
3160
3161 # Timestamp is an estimate calculated from the current time and time_text
3162 time_text = self._get_text(comment_renderer, 'publishedTimeText') or ''
3163 timestamp = self._parse_time_text(time_text)
3164
3165 author = self._get_text(comment_renderer, 'authorText')
3166 author_id = try_get(comment_renderer,
3167 lambda x: x['authorEndpoint']['browseEndpoint']['browseId'], str)
3168
3169 votes = parse_count(try_get(comment_renderer, (lambda x: x['voteCount']['simpleText'],
3170 lambda x: x['likeCount']), str)) or 0
3171 author_thumbnail = try_get(comment_renderer,
3172 lambda x: x['authorThumbnail']['thumbnails'][-1]['url'], str)
3173
3174 author_is_uploader = try_get(comment_renderer, lambda x: x['authorIsChannelOwner'], bool)
3175 is_favorited = 'creatorHeart' in (try_get(
3176 comment_renderer, lambda x: x['actionButtons']['commentActionButtonsRenderer'], dict) or {})
3177 return {
3178 'id': comment_id,
3179 'text': text,
3180 'timestamp': timestamp,
3181 'time_text': time_text,
3182 'like_count': votes,
3183 'is_favorited': is_favorited,
3184 'author': author,
3185 'author_id': author_id,
3186 'author_thumbnail': author_thumbnail,
3187 'author_is_uploader': author_is_uploader,
3188 'parent': parent or 'root'
3189 }
3190
3191 def _comment_entries(self, root_continuation_data, ytcfg, video_id, parent=None, tracker=None):
3192
3193 get_single_config_arg = lambda c: self._configuration_arg(c, [''])[0]
3194
3195 def extract_header(contents):
3196 _continuation = None
3197 for content in contents:
3198 comments_header_renderer = traverse_obj(content, 'commentsHeaderRenderer')
3199 expected_comment_count = self._get_count(
3200 comments_header_renderer, 'countText', 'commentsCount')
3201
3202 if expected_comment_count:
3203 tracker['est_total'] = expected_comment_count
3204 self.to_screen(f'Downloading ~{expected_comment_count} comments')
3205 comment_sort_index = int(get_single_config_arg('comment_sort') != 'top') # 1 = new, 0 = top
3206
3207 sort_menu_item = try_get(
3208 comments_header_renderer,
3209 lambda x: x['sortMenu']['sortFilterSubMenuRenderer']['subMenuItems'][comment_sort_index], dict) or {}
3210 sort_continuation_ep = sort_menu_item.get('serviceEndpoint') or {}
3211
3212 _continuation = self._extract_continuation_ep_data(sort_continuation_ep) or self._extract_continuation(sort_menu_item)
3213 if not _continuation:
3214 continue
3215
3216 sort_text = str_or_none(sort_menu_item.get('title'))
3217 if not sort_text:
3218 sort_text = 'top comments' if comment_sort_index == 0 else 'newest first'
3219 self.to_screen('Sorting comments by %s' % sort_text.lower())
3220 break
3221 return _continuation
3222
3223 def extract_thread(contents):
3224 if not parent:
3225 tracker['current_page_thread'] = 0
3226 for content in contents:
3227 if not parent and tracker['total_parent_comments'] >= max_parents:
3228 yield
3229 comment_thread_renderer = try_get(content, lambda x: x['commentThreadRenderer'])
3230 comment_renderer = get_first(
3231 (comment_thread_renderer, content), [['commentRenderer', ('comment', 'commentRenderer')]],
3232 expected_type=dict, default={})
3233
3234 comment = self._extract_comment(comment_renderer, parent)
3235 if not comment:
3236 continue
3237
3238 tracker['running_total'] += 1
3239 tracker['total_reply_comments' if parent else 'total_parent_comments'] += 1
3240 yield comment
3241
3242 # Attempt to get the replies
3243 comment_replies_renderer = try_get(
3244 comment_thread_renderer, lambda x: x['replies']['commentRepliesRenderer'], dict)
3245
3246 if comment_replies_renderer:
3247 tracker['current_page_thread'] += 1
3248 comment_entries_iter = self._comment_entries(
3249 comment_replies_renderer, ytcfg, video_id,
3250 parent=comment.get('id'), tracker=tracker)
3251 yield from itertools.islice(comment_entries_iter, min(
3252 max_replies_per_thread, max(0, max_replies - tracker['total_reply_comments'])))
3253
3254 # Keeps track of counts across recursive calls
3255 if not tracker:
3256 tracker = dict(
3257 running_total=0,
3258 est_total=0,
3259 current_page_thread=0,
3260 total_parent_comments=0,
3261 total_reply_comments=0)
3262
3263 # TODO: Deprecated
3264 # YouTube comments have a max depth of 2
3265 max_depth = int_or_none(get_single_config_arg('max_comment_depth'))
3266 if max_depth:
3267 self._downloader.deprecated_feature('[youtube] max_comment_depth extractor argument is deprecated. '
3268 'Set max replies in the max-comments extractor argument instead')
3269 if max_depth == 1 and parent:
3270 return
3271
3272 max_comments, max_parents, max_replies, max_replies_per_thread, *_ = map(
3273 lambda p: int_or_none(p, default=sys.maxsize), self._configuration_arg('max_comments', ) + [''] * 4)
3274
3275 continuation = self._extract_continuation(root_continuation_data)
3276
3277 response = None
3278 is_forced_continuation = False
3279 is_first_continuation = parent is None
3280 if is_first_continuation and not continuation:
3281 # Sometimes you can get comments by generating the continuation yourself,
3282 # even if YouTube initially reports them being disabled - e.g. stories comments.
3283 # Note: if the comment section is actually disabled, YouTube may return a response with
3284 # required check_get_keys missing. So we will disable that check initially in this case.
3285 continuation = self._build_api_continuation_query(self._generate_comment_continuation(video_id))
3286 is_forced_continuation = True
3287
3288 for page_num in itertools.count(0):
3289 if not continuation:
3290 break
3291 headers = self.generate_api_headers(ytcfg=ytcfg, visitor_data=self._extract_visitor_data(response))
3292 comment_prog_str = f"({tracker['running_total']}/{tracker['est_total']})"
3293 if page_num == 0:
3294 if is_first_continuation:
3295 note_prefix = 'Downloading comment section API JSON'
3296 else:
3297 note_prefix = ' Downloading comment API JSON reply thread %d %s' % (
3298 tracker['current_page_thread'], comment_prog_str)
3299 else:
3300 note_prefix = '%sDownloading comment%s API JSON page %d %s' % (
3301 ' ' if parent else '', ' replies' if parent else '',
3302 page_num, comment_prog_str)
3303 try:
3304 response = self._extract_response(
3305 item_id=None, query=continuation,
3306 ep='next', ytcfg=ytcfg, headers=headers, note=note_prefix,
3307 check_get_keys='onResponseReceivedEndpoints' if not is_forced_continuation else None)
3308 except ExtractorError as e:
3309 # Ignore incomplete data error for replies if retries didn't work.
3310 # This is to allow any other parent comments and comment threads to be downloaded.
3311 # See: https://github.com/yt-dlp/yt-dlp/issues/4669
3312 if 'incomplete data' in str(e).lower() and parent and self.get_param('ignoreerrors') is True:
3313 self.report_warning(
3314 'Received incomplete data for a comment reply thread and retrying did not help. '
3315 'Ignoring to let other comments be downloaded.')
3316 else:
3317 raise
3318 is_forced_continuation = False
3319 continuation_contents = traverse_obj(
3320 response, 'onResponseReceivedEndpoints', expected_type=list, default=[])
3321
3322 continuation = None
3323 for continuation_section in continuation_contents:
3324 continuation_items = traverse_obj(
3325 continuation_section,
3326 (('reloadContinuationItemsCommand', 'appendContinuationItemsAction'), 'continuationItems'),
3327 get_all=False, expected_type=list) or []
3328 if is_first_continuation:
3329 continuation = extract_header(continuation_items)
3330 is_first_continuation = False
3331 if continuation:
3332 break
3333 continue
3334
3335 for entry in extract_thread(continuation_items):
3336 if not entry:
3337 return
3338 yield entry
3339 continuation = self._extract_continuation({'contents': continuation_items})
3340 if continuation:
3341 break
3342
3343 message = self._get_text(root_continuation_data, ('contents', ..., 'messageRenderer', 'text'), max_runs=1)
3344 if message and not parent and tracker['running_total'] == 0:
3345 self.report_warning(f'Youtube said: {message}', video_id=video_id, only_once=True)
3346 raise self.CommentsDisabled
3347
3348 @staticmethod
3349 def _generate_comment_continuation(video_id):
3350 """
3351 Generates initial comment section continuation token from given video id
3352 """
3353 token = f'\x12\r\x12\x0b{video_id}\x18\x062\'"\x11"\x0b{video_id}0\x00x\x020\x00B\x10comments-section'
3354 return base64.b64encode(token.encode()).decode()
3355
3356 def _get_comments(self, ytcfg, video_id, contents, webpage):
3357 """Entry for comment extraction"""
3358 def _real_comment_extract(contents):
3359 renderer = next((
3360 item for item in traverse_obj(contents, (..., 'itemSectionRenderer'), default={})
3361 if item.get('sectionIdentifier') == 'comment-item-section'), None)
3362 yield from self._comment_entries(renderer, ytcfg, video_id)
3363
3364 max_comments = int_or_none(self._configuration_arg('max_comments', [''])[0])
3365 return itertools.islice(_real_comment_extract(contents), 0, max_comments)
3366
3367 @staticmethod
3368 def _get_checkok_params():
3369 return {'contentCheckOk': True, 'racyCheckOk': True}
3370
3371 @classmethod
3372 def _generate_player_context(cls, sts=None):
3373 context = {
3374 'html5Preference': 'HTML5_PREF_WANTS',
3375 }
3376 if sts is not None:
3377 context['signatureTimestamp'] = sts
3378 return {
3379 'playbackContext': {
3380 'contentPlaybackContext': context
3381 },
3382 **cls._get_checkok_params()
3383 }
3384
3385 @staticmethod
3386 def _is_agegated(player_response):
3387 if traverse_obj(player_response, ('playabilityStatus', 'desktopLegacyAgeGateReason')):
3388 return True
3389
3390 reasons = traverse_obj(player_response, ('playabilityStatus', ('status', 'reason')), default=[])
3391 AGE_GATE_REASONS = (
3392 'confirm your age', 'age-restricted', 'inappropriate', # reason
3393 'age_verification_required', 'age_check_required', # status
3394 )
3395 return any(expected in reason for expected in AGE_GATE_REASONS for reason in reasons)
3396
3397 @staticmethod
3398 def _is_unplayable(player_response):
3399 return traverse_obj(player_response, ('playabilityStatus', 'status')) == 'UNPLAYABLE'
3400
3401 _STORY_PLAYER_PARAMS = '8AEB'
3402
3403 def _extract_player_response(self, client, video_id, master_ytcfg, player_ytcfg, player_url, initial_pr, smuggled_data):
3404
3405 session_index = self._extract_session_index(player_ytcfg, master_ytcfg)
3406 syncid = self._extract_account_syncid(player_ytcfg, master_ytcfg, initial_pr)
3407 sts = self._extract_signature_timestamp(video_id, player_url, master_ytcfg, fatal=False) if player_url else None
3408 headers = self.generate_api_headers(
3409 ytcfg=player_ytcfg, account_syncid=syncid, session_index=session_index, default_client=client)
3410
3411 yt_query = {
3412 'videoId': video_id,
3413 }
3414 if smuggled_data.get('is_story') or _split_innertube_client(client)[0] == 'android':
3415 yt_query['params'] = self._STORY_PLAYER_PARAMS
3416
3417 yt_query.update(self._generate_player_context(sts))
3418 return self._extract_response(
3419 item_id=video_id, ep='player', query=yt_query,
3420 ytcfg=player_ytcfg, headers=headers, fatal=True,
3421 default_client=client,
3422 note='Downloading %s player API JSON' % client.replace('_', ' ').strip()
3423 ) or None
3424
3425 def _get_requested_clients(self, url, smuggled_data):
3426 requested_clients = []
3427 default = ['android', 'web']
3428 allowed_clients = sorted(
3429 (client for client in INNERTUBE_CLIENTS.keys() if client[:1] != '_'),
3430 key=lambda client: INNERTUBE_CLIENTS[client]['priority'], reverse=True)
3431 for client in self._configuration_arg('player_client'):
3432 if client in allowed_clients:
3433 requested_clients.append(client)
3434 elif client == 'default':
3435 requested_clients.extend(default)
3436 elif client == 'all':
3437 requested_clients.extend(allowed_clients)
3438 else:
3439 self.report_warning(f'Skipping unsupported client {client}')
3440 if not requested_clients:
3441 requested_clients = default
3442
3443 if smuggled_data.get('is_music_url') or self.is_music_url(url):
3444 requested_clients.extend(
3445 f'{client}_music' for client in requested_clients if f'{client}_music' in INNERTUBE_CLIENTS)
3446
3447 return orderedSet(requested_clients)
3448
3449 def _extract_player_responses(self, clients, video_id, webpage, master_ytcfg, smuggled_data):
3450 initial_pr = None
3451 if webpage:
3452 initial_pr = self._search_json(
3453 self._YT_INITIAL_PLAYER_RESPONSE_RE, webpage, 'initial player response', video_id, fatal=False)
3454
3455 all_clients = set(clients)
3456 clients = clients[::-1]
3457 prs = []
3458
3459 def append_client(*client_names):
3460 """ Append the first client name that exists but not already used """
3461 for client_name in client_names:
3462 actual_client = _split_innertube_client(client_name)[0]
3463 if actual_client in INNERTUBE_CLIENTS:
3464 if actual_client not in all_clients:
3465 clients.append(client_name)
3466 all_clients.add(actual_client)
3467 return
3468
3469 # Android player_response does not have microFormats which are needed for
3470 # extraction of some data. So we return the initial_pr with formats
3471 # stripped out even if not requested by the user
3472 # See: https://github.com/yt-dlp/yt-dlp/issues/501
3473 if initial_pr:
3474 pr = dict(initial_pr)
3475 pr['streamingData'] = None
3476 prs.append(pr)
3477
3478 last_error = None
3479 tried_iframe_fallback = False
3480 player_url = None
3481 while clients:
3482 client, base_client, variant = _split_innertube_client(clients.pop())
3483 player_ytcfg = master_ytcfg if client == 'web' else {}
3484 if 'configs' not in self._configuration_arg('player_skip') and client != 'web':
3485 player_ytcfg = self._download_ytcfg(client, video_id) or player_ytcfg
3486
3487 player_url = player_url or self._extract_player_url(master_ytcfg, player_ytcfg, webpage=webpage)
3488 require_js_player = self._get_default_ytcfg(client).get('REQUIRE_JS_PLAYER')
3489 if 'js' in self._configuration_arg('player_skip'):
3490 require_js_player = False
3491 player_url = None
3492
3493 if not player_url and not tried_iframe_fallback and require_js_player:
3494 player_url = self._download_player_url(video_id)
3495 tried_iframe_fallback = True
3496
3497 try:
3498 pr = initial_pr if client == 'web' and initial_pr else self._extract_player_response(
3499 client, video_id, player_ytcfg or master_ytcfg, player_ytcfg, player_url if require_js_player else None, initial_pr, smuggled_data)
3500 except ExtractorError as e:
3501 if last_error:
3502 self.report_warning(last_error)
3503 last_error = e
3504 continue
3505
3506 if pr:
3507 # YouTube may return a different video player response than expected.
3508 # See: https://github.com/TeamNewPipe/NewPipe/issues/8713
3509 pr_video_id = traverse_obj(pr, ('videoDetails', 'videoId'))
3510 if pr_video_id and pr_video_id != video_id:
3511 self.report_warning(
3512 f'Skipping player response from {client} client (got player response for video "{pr_video_id}" instead of "{video_id}")' + bug_reports_message())
3513 else:
3514 prs.append(pr)
3515
3516 # creator clients can bypass AGE_VERIFICATION_REQUIRED if logged in
3517 if variant == 'embedded' and self._is_unplayable(pr) and self.is_authenticated:
3518 append_client(f'{base_client}_creator')
3519 elif self._is_agegated(pr):
3520 if variant == 'tv_embedded':
3521 append_client(f'{base_client}_embedded')
3522 elif not variant:
3523 append_client(f'tv_embedded.{base_client}', f'{base_client}_embedded')
3524
3525 if last_error:
3526 if not len(prs):
3527 raise last_error
3528 self.report_warning(last_error)
3529 return prs, player_url
3530
3531 def _needs_live_processing(self, live_status, duration):
3532 if (live_status == 'is_live' and self.get_param('live_from_start')
3533 or live_status == 'post_live' and (duration or 0) > 4 * 3600):
3534 return live_status
3535
3536 def _extract_formats_and_subtitles(self, streaming_data, video_id, player_url, live_status, duration):
3537 itags, stream_ids = collections.defaultdict(set), []
3538 itag_qualities, res_qualities = {}, {0: None}
3539 q = qualities([
3540 # Normally tiny is the smallest video-only formats. But
3541 # audio-only formats with unknown quality may get tagged as tiny
3542 'tiny',
3543 'audio_quality_ultralow', 'audio_quality_low', 'audio_quality_medium', 'audio_quality_high', # Audio only formats
3544 'small', 'medium', 'large', 'hd720', 'hd1080', 'hd1440', 'hd2160', 'hd2880', 'highres'
3545 ])
3546 streaming_formats = traverse_obj(streaming_data, (..., ('formats', 'adaptiveFormats'), ...), default=[])
3547
3548 for fmt in streaming_formats:
3549 if fmt.get('targetDurationSec'):
3550 continue
3551
3552 itag = str_or_none(fmt.get('itag'))
3553 audio_track = fmt.get('audioTrack') or {}
3554 stream_id = '%s.%s' % (itag or '', audio_track.get('id', ''))
3555 if stream_id in stream_ids:
3556 continue
3557
3558 quality = fmt.get('quality')
3559 height = int_or_none(fmt.get('height'))
3560 if quality == 'tiny' or not quality:
3561 quality = fmt.get('audioQuality', '').lower() or quality
3562 # The 3gp format (17) in android client has a quality of "small",
3563 # but is actually worse than other formats
3564 if itag == '17':
3565 quality = 'tiny'
3566 if quality:
3567 if itag:
3568 itag_qualities[itag] = quality
3569 if height:
3570 res_qualities[height] = quality
3571 # FORMAT_STREAM_TYPE_OTF(otf=1) requires downloading the init fragment
3572 # (adding `&sq=0` to the URL) and parsing emsg box to determine the
3573 # number of fragment that would subsequently requested with (`&sq=N`)
3574 if fmt.get('type') == 'FORMAT_STREAM_TYPE_OTF':
3575 continue
3576
3577 fmt_url = fmt.get('url')
3578 if not fmt_url:
3579 sc = urllib.parse.parse_qs(fmt.get('signatureCipher'))
3580 fmt_url = url_or_none(try_get(sc, lambda x: x['url'][0]))
3581 encrypted_sig = try_get(sc, lambda x: x['s'][0])
3582 if not all((sc, fmt_url, player_url, encrypted_sig)):
3583 continue
3584 try:
3585 fmt_url += '&%s=%s' % (
3586 traverse_obj(sc, ('sp', -1)) or 'signature',
3587 self._decrypt_signature(encrypted_sig, video_id, player_url)
3588 )
3589 except ExtractorError as e:
3590 self.report_warning('Signature extraction failed: Some formats may be missing',
3591 video_id=video_id, only_once=True)
3592 self.write_debug(e, only_once=True)
3593 continue
3594
3595 query = parse_qs(fmt_url)
3596 throttled = False
3597 if query.get('n'):
3598 try:
3599 decrypt_nsig = self._cached(self._decrypt_nsig, 'nsig', query['n'][0])
3600 fmt_url = update_url_query(fmt_url, {
3601 'n': decrypt_nsig(query['n'][0], video_id, player_url)
3602 })
3603 except ExtractorError as e:
3604 phantomjs_hint = ''
3605 if isinstance(e, JSInterpreter.Exception):
3606 phantomjs_hint = (f' Install {self._downloader._format_err("PhantomJS", self._downloader.Styles.EMPHASIS)} '
3607 f'to workaround the issue. {PhantomJSwrapper.INSTALL_HINT}\n')
3608 if player_url:
3609 self.report_warning(
3610 f'nsig extraction failed: You may experience throttling for some formats\n{phantomjs_hint}'
3611 f' n = {query["n"][0]} ; player = {player_url}', video_id=video_id, only_once=True)
3612 self.write_debug(e, only_once=True)
3613 else:
3614 self.report_warning(
3615 'Cannot decrypt nsig without player_url: You may experience throttling for some formats',
3616 video_id=video_id, only_once=True)
3617 throttled = True
3618
3619 tbr = float_or_none(fmt.get('averageBitrate') or fmt.get('bitrate'), 1000)
3620 language_preference = (
3621 10 if audio_track.get('audioIsDefault') and 10
3622 else -10 if 'descriptive' in (audio_track.get('displayName') or '').lower() and -10
3623 else -1)
3624 # Some formats may have much smaller duration than others (possibly damaged during encoding)
3625 # E.g. 2-nOtRESiUc Ref: https://github.com/yt-dlp/yt-dlp/issues/2823
3626 # Make sure to avoid false positives with small duration differences.
3627 # E.g. __2ABJjxzNo, ySuUZEjARPY
3628 is_damaged = try_get(fmt, lambda x: float(x['approxDurationMs']) / duration < 500)
3629 if is_damaged:
3630 self.report_warning(
3631 f'{video_id}: Some formats are possibly damaged. They will be deprioritized', only_once=True)
3632 dct = {
3633 'asr': int_or_none(fmt.get('audioSampleRate')),
3634 'filesize': int_or_none(fmt.get('contentLength')),
3635 'format_id': itag,
3636 'format_note': join_nonempty(
3637 '%s%s' % (audio_track.get('displayName') or '',
3638 ' (default)' if language_preference > 0 else ''),
3639 fmt.get('qualityLabel') or quality.replace('audio_quality_', ''),
3640 try_get(fmt, lambda x: x['projectionType'].replace('RECTANGULAR', '').lower()),
3641 try_get(fmt, lambda x: x['spatialAudioType'].replace('SPATIAL_AUDIO_TYPE_', '').lower()),
3642 throttled and 'THROTTLED', is_damaged and 'DAMAGED', delim=', '),
3643 # Format 22 is likely to be damaged. See https://github.com/yt-dlp/yt-dlp/issues/3372
3644 'source_preference': -10 if throttled else -5 if itag == '22' else -1,
3645 'fps': int_or_none(fmt.get('fps')) or None,
3646 'audio_channels': fmt.get('audioChannels'),
3647 'height': height,
3648 'quality': q(quality),
3649 'has_drm': bool(fmt.get('drmFamilies')),
3650 'tbr': tbr,
3651 'url': fmt_url,
3652 'width': int_or_none(fmt.get('width')),
3653 'language': join_nonempty(audio_track.get('id', '').split('.')[0],
3654 'desc' if language_preference < -1 else ''),
3655 'language_preference': language_preference,
3656 # Strictly de-prioritize damaged and 3gp formats
3657 'preference': -10 if is_damaged else -2 if itag == '17' else None,
3658 }
3659 mime_mobj = re.match(
3660 r'((?:[^/]+)/(?:[^;]+))(?:;\s*codecs="([^"]+)")?', fmt.get('mimeType') or '')
3661 if mime_mobj:
3662 dct['ext'] = mimetype2ext(mime_mobj.group(1))
3663 dct.update(parse_codecs(mime_mobj.group(2)))
3664 no_audio = dct.get('acodec') == 'none'
3665 no_video = dct.get('vcodec') == 'none'
3666 if no_audio:
3667 dct['vbr'] = tbr
3668 if no_video:
3669 dct['abr'] = tbr
3670 if no_audio or no_video:
3671 dct['downloader_options'] = {
3672 # Youtube throttles chunks >~10M
3673 'http_chunk_size': 10485760,
3674 }
3675 if dct.get('ext'):
3676 dct['container'] = dct['ext'] + '_dash'
3677
3678 if itag:
3679 itags[itag].add(('https', dct.get('language')))
3680 stream_ids.append(stream_id)
3681 yield dct
3682
3683 needs_live_processing = self._needs_live_processing(live_status, duration)
3684 skip_bad_formats = not self._configuration_arg('include_incomplete_formats')
3685
3686 skip_manifests = set(self._configuration_arg('skip'))
3687 if (not self.get_param('youtube_include_hls_manifest', True)
3688 or needs_live_processing == 'is_live' # These will be filtered out by YoutubeDL anyway
3689 or needs_live_processing and skip_bad_formats):
3690 skip_manifests.add('hls')
3691
3692 if not self.get_param('youtube_include_dash_manifest', True):
3693 skip_manifests.add('dash')
3694 if self._configuration_arg('include_live_dash'):
3695 self._downloader.deprecated_feature('[youtube] include_live_dash extractor argument is deprecated. '
3696 'Use include_incomplete_formats extractor argument instead')
3697 elif skip_bad_formats and live_status == 'is_live' and needs_live_processing != 'is_live':
3698 skip_manifests.add('dash')
3699
3700 def process_manifest_format(f, proto, itag):
3701 key = (proto, f.get('language'))
3702 if key in itags[itag]:
3703 return False
3704 itags[itag].add(key)
3705
3706 if any(p != proto for p, _ in itags[itag]):
3707 f['format_id'] = f'{itag}-{proto}'
3708 elif itag:
3709 f['format_id'] = itag
3710
3711 f['quality'] = q(itag_qualities.get(try_get(f, lambda f: f['format_id'].split('-')[0]), -1))
3712 if f['quality'] == -1 and f.get('height'):
3713 f['quality'] = q(res_qualities[min(res_qualities, key=lambda x: abs(x - f['height']))])
3714 return True
3715
3716 subtitles = {}
3717 for sd in streaming_data:
3718 hls_manifest_url = 'hls' not in skip_manifests and sd.get('hlsManifestUrl')
3719 if hls_manifest_url:
3720 fmts, subs = self._extract_m3u8_formats_and_subtitles(
3721 hls_manifest_url, video_id, 'mp4', fatal=False, live=live_status == 'is_live')
3722 subtitles = self._merge_subtitles(subs, subtitles)
3723 for f in fmts:
3724 if process_manifest_format(f, 'hls', self._search_regex(
3725 r'/itag/(\d+)', f['url'], 'itag', default=None)):
3726 yield f
3727
3728 dash_manifest_url = 'dash' not in skip_manifests and sd.get('dashManifestUrl')
3729 if dash_manifest_url:
3730 formats, subs = self._extract_mpd_formats_and_subtitles(dash_manifest_url, video_id, fatal=False)
3731 subtitles = self._merge_subtitles(subs, subtitles) # Prioritize HLS subs over DASH
3732 for f in formats:
3733 if process_manifest_format(f, 'dash', f['format_id']):
3734 f['filesize'] = int_or_none(self._search_regex(
3735 r'/clen/(\d+)', f.get('fragment_base_url') or f['url'], 'file size', default=None))
3736 if needs_live_processing:
3737 f['is_from_start'] = True
3738
3739 yield f
3740 yield subtitles
3741
3742 def _extract_storyboard(self, player_responses, duration):
3743 spec = get_first(
3744 player_responses, ('storyboards', 'playerStoryboardSpecRenderer', 'spec'), default='').split('|')[::-1]
3745 base_url = url_or_none(urljoin('https://i.ytimg.com/', spec.pop() or None))
3746 if not base_url:
3747 return
3748 L = len(spec) - 1
3749 for i, args in enumerate(spec):
3750 args = args.split('#')
3751 counts = list(map(int_or_none, args[:5]))
3752 if len(args) != 8 or not all(counts):
3753 self.report_warning(f'Malformed storyboard {i}: {"#".join(args)}{bug_reports_message()}')
3754 continue
3755 width, height, frame_count, cols, rows = counts
3756 N, sigh = args[6:]
3757
3758 url = base_url.replace('$L', str(L - i)).replace('$N', N) + f'&sigh={sigh}'
3759 fragment_count = frame_count / (cols * rows)
3760 fragment_duration = duration / fragment_count
3761 yield {
3762 'format_id': f'sb{i}',
3763 'format_note': 'storyboard',
3764 'ext': 'mhtml',
3765 'protocol': 'mhtml',
3766 'acodec': 'none',
3767 'vcodec': 'none',
3768 'url': url,
3769 'width': width,
3770 'height': height,
3771 'fps': frame_count / duration,
3772 'rows': rows,
3773 'columns': cols,
3774 'fragments': [{
3775 'url': url.replace('$M', str(j)),
3776 'duration': min(fragment_duration, duration - (j * fragment_duration)),
3777 } for j in range(math.ceil(fragment_count))],
3778 }
3779
3780 def _download_player_responses(self, url, smuggled_data, video_id, webpage_url):
3781 webpage = None
3782 if 'webpage' not in self._configuration_arg('player_skip'):
3783 query = {'bpctr': '9999999999', 'has_verified': '1'}
3784 if smuggled_data.get('is_story'):
3785 query['pp'] = self._STORY_PLAYER_PARAMS
3786 webpage = self._download_webpage(
3787 webpage_url, video_id, fatal=False, query=query)
3788
3789 master_ytcfg = self.extract_ytcfg(video_id, webpage) or self._get_default_ytcfg()
3790
3791 player_responses, player_url = self._extract_player_responses(
3792 self._get_requested_clients(url, smuggled_data),
3793 video_id, webpage, master_ytcfg, smuggled_data)
3794
3795 return webpage, master_ytcfg, player_responses, player_url
3796
3797 def _list_formats(self, video_id, microformats, video_details, player_responses, player_url, duration=None):
3798 live_broadcast_details = traverse_obj(microformats, (..., 'liveBroadcastDetails'))
3799 is_live = get_first(video_details, 'isLive')
3800 if is_live is None:
3801 is_live = get_first(live_broadcast_details, 'isLiveNow')
3802 live_content = get_first(video_details, 'isLiveContent')
3803 is_upcoming = get_first(video_details, 'isUpcoming')
3804 post_live = get_first(video_details, 'isPostLiveDvr')
3805 live_status = ('post_live' if post_live
3806 else 'is_live' if is_live
3807 else 'is_upcoming' if is_upcoming
3808 else 'was_live' if live_content
3809 else 'not_live' if False in (is_live, live_content)
3810 else None)
3811 streaming_data = traverse_obj(player_responses, (..., 'streamingData'), default=[])
3812 *formats, subtitles = self._extract_formats_and_subtitles(streaming_data, video_id, player_url, live_status, duration)
3813
3814 return live_broadcast_details, live_status, streaming_data, formats, subtitles
3815
3816 def _real_extract(self, url):
3817 url, smuggled_data = unsmuggle_url(url, {})
3818 video_id = self._match_id(url)
3819
3820 base_url = self.http_scheme() + '//www.youtube.com/'
3821 webpage_url = base_url + 'watch?v=' + video_id
3822
3823 webpage, master_ytcfg, player_responses, player_url = self._download_player_responses(url, smuggled_data, video_id, webpage_url)
3824
3825 playability_statuses = traverse_obj(
3826 player_responses, (..., 'playabilityStatus'), expected_type=dict, default=[])
3827
3828 trailer_video_id = get_first(
3829 playability_statuses,
3830 ('errorScreen', 'playerLegacyDesktopYpcTrailerRenderer', 'trailerVideoId'),
3831 expected_type=str)
3832 if trailer_video_id:
3833 return self.url_result(
3834 trailer_video_id, self.ie_key(), trailer_video_id)
3835
3836 search_meta = ((lambda x: self._html_search_meta(x, webpage, default=None))
3837 if webpage else (lambda x: None))
3838
3839 video_details = traverse_obj(
3840 player_responses, (..., 'videoDetails'), expected_type=dict, default=[])
3841 microformats = traverse_obj(
3842 player_responses, (..., 'microformat', 'playerMicroformatRenderer'),
3843 expected_type=dict, default=[])
3844
3845 translated_title = self._get_text(microformats, (..., 'title'))
3846 video_title = (self._preferred_lang and translated_title
3847 or get_first(video_details, 'title') # primary
3848 or translated_title
3849 or search_meta(['og:title', 'twitter:title', 'title']))
3850 translated_description = self._get_text(microformats, (..., 'description'))
3851 original_description = get_first(video_details, 'shortDescription')
3852 video_description = (
3853 self._preferred_lang and translated_description
3854 # If original description is blank, it will be an empty string.
3855 # Do not prefer translated description in this case.
3856 or original_description if original_description is not None else translated_description)
3857
3858 multifeed_metadata_list = get_first(
3859 player_responses,
3860 ('multicamera', 'playerLegacyMulticameraRenderer', 'metadataList'),
3861 expected_type=str)
3862 if multifeed_metadata_list and not smuggled_data.get('force_singlefeed'):
3863 if self.get_param('noplaylist'):
3864 self.to_screen('Downloading just video %s because of --no-playlist' % video_id)
3865 else:
3866 entries = []
3867 feed_ids = []
3868 for feed in multifeed_metadata_list.split(','):
3869 # Unquote should take place before split on comma (,) since textual
3870 # fields may contain comma as well (see
3871 # https://github.com/ytdl-org/youtube-dl/issues/8536)
3872 feed_data = urllib.parse.parse_qs(
3873 urllib.parse.unquote_plus(feed))
3874
3875 def feed_entry(name):
3876 return try_get(
3877 feed_data, lambda x: x[name][0], str)
3878
3879 feed_id = feed_entry('id')
3880 if not feed_id:
3881 continue
3882 feed_title = feed_entry('title')
3883 title = video_title
3884 if feed_title:
3885 title += ' (%s)' % feed_title
3886 entries.append({
3887 '_type': 'url_transparent',
3888 'ie_key': 'Youtube',
3889 'url': smuggle_url(
3890 '%swatch?v=%s' % (base_url, feed_data['id'][0]),
3891 {'force_singlefeed': True}),
3892 'title': title,
3893 })
3894 feed_ids.append(feed_id)
3895 self.to_screen(
3896 'Downloading multifeed video (%s) - add --no-playlist to just download video %s'
3897 % (', '.join(feed_ids), video_id))
3898 return self.playlist_result(
3899 entries, video_id, video_title, video_description)
3900
3901 duration = (int_or_none(get_first(video_details, 'lengthSeconds'))
3902 or int_or_none(get_first(microformats, 'lengthSeconds'))
3903 or parse_duration(search_meta('duration')) or None)
3904
3905 live_broadcast_details, live_status, streaming_data, formats, automatic_captions = \
3906 self._list_formats(video_id, microformats, video_details, player_responses, player_url, duration)
3907 if live_status == 'post_live':
3908 self.write_debug(f'{video_id}: Video is in Post-Live Manifestless mode')
3909
3910 if not formats:
3911 if not self.get_param('allow_unplayable_formats') and traverse_obj(streaming_data, (..., 'licenseInfos')):
3912 self.report_drm(video_id)
3913 pemr = get_first(
3914 playability_statuses,
3915 ('errorScreen', 'playerErrorMessageRenderer'), expected_type=dict) or {}
3916 reason = self._get_text(pemr, 'reason') or get_first(playability_statuses, 'reason')
3917 subreason = clean_html(self._get_text(pemr, 'subreason') or '')
3918 if subreason:
3919 if subreason == 'The uploader has not made this video available in your country.':
3920 countries = get_first(microformats, 'availableCountries')
3921 if not countries:
3922 regions_allowed = search_meta('regionsAllowed')
3923 countries = regions_allowed.split(',') if regions_allowed else None
3924 self.raise_geo_restricted(subreason, countries, metadata_available=True)
3925 reason += f'. {subreason}'
3926 if reason:
3927 self.raise_no_formats(reason, expected=True)
3928
3929 keywords = get_first(video_details, 'keywords', expected_type=list) or []
3930 if not keywords and webpage:
3931 keywords = [
3932 unescapeHTML(m.group('content'))
3933 for m in re.finditer(self._meta_regex('og:video:tag'), webpage)]
3934 for keyword in keywords:
3935 if keyword.startswith('yt:stretch='):
3936 mobj = re.search(r'(\d+)\s*:\s*(\d+)', keyword)
3937 if mobj:
3938 # NB: float is intentional for forcing float division
3939 w, h = (float(v) for v in mobj.groups())
3940 if w > 0 and h > 0:
3941 ratio = w / h
3942 for f in formats:
3943 if f.get('vcodec') != 'none':
3944 f['stretched_ratio'] = ratio
3945 break
3946 thumbnails = self._extract_thumbnails((video_details, microformats), (..., ..., 'thumbnail'))
3947 thumbnail_url = search_meta(['og:image', 'twitter:image'])
3948 if thumbnail_url:
3949 thumbnails.append({
3950 'url': thumbnail_url,
3951 })
3952 original_thumbnails = thumbnails.copy()
3953
3954 # The best resolution thumbnails sometimes does not appear in the webpage
3955 # See: https://github.com/yt-dlp/yt-dlp/issues/340
3956 # List of possible thumbnails - Ref: <https://stackoverflow.com/a/20542029>
3957 thumbnail_names = [
3958 # While the *1,*2,*3 thumbnails are just below their corresponding "*default" variants
3959 # in resolution, these are not the custom thumbnail. So de-prioritize them
3960 'maxresdefault', 'hq720', 'sddefault', 'hqdefault', '0', 'mqdefault', 'default',
3961 'sd1', 'sd2', 'sd3', 'hq1', 'hq2', 'hq3', 'mq1', 'mq2', 'mq3', '1', '2', '3'
3962 ]
3963 n_thumbnail_names = len(thumbnail_names)
3964 thumbnails.extend({
3965 'url': 'https://i.ytimg.com/vi{webp}/{video_id}/{name}{live}.{ext}'.format(
3966 video_id=video_id, name=name, ext=ext,
3967 webp='_webp' if ext == 'webp' else '', live='_live' if live_status == 'is_live' else ''),
3968 } for name in thumbnail_names for ext in ('webp', 'jpg'))
3969 for thumb in thumbnails:
3970 i = next((i for i, t in enumerate(thumbnail_names) if f'/{video_id}/{t}' in thumb['url']), n_thumbnail_names)
3971 thumb['preference'] = (0 if '.webp' in thumb['url'] else -1) - (2 * i)
3972 self._remove_duplicate_formats(thumbnails)
3973 self._downloader._sort_thumbnails(original_thumbnails)
3974
3975 category = get_first(microformats, 'category') or search_meta('genre')
3976 channel_id = str_or_none(
3977 get_first(video_details, 'channelId')
3978 or get_first(microformats, 'externalChannelId')
3979 or search_meta('channelId'))
3980 owner_profile_url = get_first(microformats, 'ownerProfileUrl')
3981
3982 live_start_time = parse_iso8601(get_first(live_broadcast_details, 'startTimestamp'))
3983 live_end_time = parse_iso8601(get_first(live_broadcast_details, 'endTimestamp'))
3984 if not duration and live_end_time and live_start_time:
3985 duration = live_end_time - live_start_time
3986
3987 needs_live_processing = self._needs_live_processing(live_status, duration)
3988
3989 def is_bad_format(fmt):
3990 if needs_live_processing and not fmt.get('is_from_start'):
3991 return True
3992 elif (live_status == 'is_live' and needs_live_processing != 'is_live'
3993 and fmt.get('protocol') == 'http_dash_segments'):
3994 return True
3995
3996 for fmt in filter(is_bad_format, formats):
3997 fmt['preference'] = (fmt.get('preference') or -1) - 10
3998 fmt['format_note'] = join_nonempty(fmt.get('format_note'), '(Last 4 hours)', delim=' ')
3999
4000 if needs_live_processing:
4001 self._prepare_live_from_start_formats(
4002 formats, video_id, live_start_time, url, webpage_url, smuggled_data, live_status == 'is_live')
4003
4004 formats.extend(self._extract_storyboard(player_responses, duration))
4005
4006 info = {
4007 'id': video_id,
4008 'title': video_title,
4009 'formats': formats,
4010 'thumbnails': thumbnails,
4011 # The best thumbnail that we are sure exists. Prevents unnecessary
4012 # URL checking if user don't care about getting the best possible thumbnail
4013 'thumbnail': traverse_obj(original_thumbnails, (-1, 'url')),
4014 'description': video_description,
4015 'uploader': get_first(video_details, 'author'),
4016 'uploader_id': self._search_regex(r'/(?:channel|user)/([^/?&#]+)', owner_profile_url, 'uploader id') if owner_profile_url else None,
4017 'uploader_url': owner_profile_url,
4018 'channel_id': channel_id,
4019 'channel_url': format_field(channel_id, None, 'https://www.youtube.com/channel/%s'),
4020 'duration': duration,
4021 'view_count': int_or_none(
4022 get_first((video_details, microformats), (..., 'viewCount'))
4023 or search_meta('interactionCount')),
4024 'average_rating': float_or_none(get_first(video_details, 'averageRating')),
4025 'age_limit': 18 if (
4026 get_first(microformats, 'isFamilySafe') is False
4027 or search_meta('isFamilyFriendly') == 'false'
4028 or search_meta('og:restrictions:age') == '18+') else 0,
4029 'webpage_url': webpage_url,
4030 'categories': [category] if category else None,
4031 'tags': keywords,
4032 'playable_in_embed': get_first(playability_statuses, 'playableInEmbed'),
4033 'live_status': live_status,
4034 'release_timestamp': live_start_time,
4035 '_format_sort_fields': ( # source_preference is lower for throttled/potentially damaged formats
4036 'quality', 'res', 'fps', 'hdr:12', 'source', 'vcodec:vp9.2', 'channels', 'acodec', 'lang', 'proto')
4037 }
4038
4039 subtitles = {}
4040 pctr = traverse_obj(player_responses, (..., 'captions', 'playerCaptionsTracklistRenderer'), expected_type=dict)
4041 if pctr:
4042 def get_lang_code(track):
4043 return (remove_start(track.get('vssId') or '', '.').replace('.', '-')
4044 or track.get('languageCode'))
4045
4046 # Converted into dicts to remove duplicates
4047 captions = {
4048 get_lang_code(sub): sub
4049 for sub in traverse_obj(pctr, (..., 'captionTracks', ...), default=[])}
4050 translation_languages = {
4051 lang.get('languageCode'): self._get_text(lang.get('languageName'), max_runs=1)
4052 for lang in traverse_obj(pctr, (..., 'translationLanguages', ...), default=[])}
4053
4054 def process_language(container, base_url, lang_code, sub_name, query):
4055 lang_subs = container.setdefault(lang_code, [])
4056 for fmt in self._SUBTITLE_FORMATS:
4057 query.update({
4058 'fmt': fmt,
4059 })
4060 lang_subs.append({
4061 'ext': fmt,
4062 'url': urljoin('https://www.youtube.com', update_url_query(base_url, query)),
4063 'name': sub_name,
4064 })
4065
4066 # NB: Constructing the full subtitle dictionary is slow
4067 get_translated_subs = 'translated_subs' not in self._configuration_arg('skip') and (
4068 self.get_param('writeautomaticsub', False) or self.get_param('listsubtitles'))
4069 for lang_code, caption_track in captions.items():
4070 base_url = caption_track.get('baseUrl')
4071 orig_lang = parse_qs(base_url).get('lang', [None])[-1]
4072 if not base_url:
4073 continue
4074 lang_name = self._get_text(caption_track, 'name', max_runs=1)
4075 if caption_track.get('kind') != 'asr':
4076 if not lang_code:
4077 continue
4078 process_language(
4079 subtitles, base_url, lang_code, lang_name, {})
4080 if not caption_track.get('isTranslatable'):
4081 continue
4082 for trans_code, trans_name in translation_languages.items():
4083 if not trans_code:
4084 continue
4085 orig_trans_code = trans_code
4086 if caption_track.get('kind') != 'asr':
4087 if not get_translated_subs:
4088 continue
4089 trans_code += f'-{lang_code}'
4090 trans_name += format_field(lang_name, None, ' from %s')
4091 # Add an "-orig" label to the original language so that it can be distinguished.
4092 # The subs are returned without "-orig" as well for compatibility
4093 if lang_code == f'a-{orig_trans_code}':
4094 process_language(
4095 automatic_captions, base_url, f'{trans_code}-orig', f'{trans_name} (Original)', {})
4096 # Setting tlang=lang returns damaged subtitles.
4097 process_language(automatic_captions, base_url, trans_code, trans_name,
4098 {} if orig_lang == orig_trans_code else {'tlang': trans_code})
4099
4100 info['automatic_captions'] = automatic_captions
4101 info['subtitles'] = subtitles
4102
4103 parsed_url = urllib.parse.urlparse(url)
4104 for component in [parsed_url.fragment, parsed_url.query]:
4105 query = urllib.parse.parse_qs(component)
4106 for k, v in query.items():
4107 for d_k, s_ks in [('start', ('start', 't')), ('end', ('end',))]:
4108 d_k += '_time'
4109 if d_k not in info and k in s_ks:
4110 info[d_k] = parse_duration(query[k][0])
4111
4112 # Youtube Music Auto-generated description
4113 if video_description:
4114 mobj = re.search(
4115 r'''(?xs)
4116 (?P<track>[^·\n]+)·(?P<artist>[^\n]+)\n+
4117 (?P<album>[^\n]+)
4118 (?:.+?℗\s*(?P<release_year>\d{4})(?!\d))?
4119 (?:.+?Released on\s*:\s*(?P<release_date>\d{4}-\d{2}-\d{2}))?
4120 (.+?\nArtist\s*:\s*(?P<clean_artist>[^\n]+))?
4121 .+\nAuto-generated\ by\ YouTube\.\s*$
4122 ''', video_description)
4123 if mobj:
4124 release_year = mobj.group('release_year')
4125 release_date = mobj.group('release_date')
4126 if release_date:
4127 release_date = release_date.replace('-', '')
4128 if not release_year:
4129 release_year = release_date[:4]
4130 info.update({
4131 'album': mobj.group('album'.strip()),
4132 'artist': mobj.group('clean_artist') or ', '.join(a.strip() for a in mobj.group('artist').split('·')),
4133 'track': mobj.group('track').strip(),
4134 'release_date': release_date,
4135 'release_year': int_or_none(release_year),
4136 })
4137
4138 initial_data = None
4139 if webpage:
4140 initial_data = self.extract_yt_initial_data(video_id, webpage, fatal=False)
4141 if not initial_data:
4142 query = {'videoId': video_id}
4143 query.update(self._get_checkok_params())
4144 initial_data = self._extract_response(
4145 item_id=video_id, ep='next', fatal=False,
4146 ytcfg=master_ytcfg, query=query,
4147 headers=self.generate_api_headers(ytcfg=master_ytcfg),
4148 note='Downloading initial data API JSON')
4149
4150 info['comment_count'] = traverse_obj(initial_data, (
4151 'contents', 'twoColumnWatchNextResults', 'results', 'results', 'contents', ..., 'itemSectionRenderer',
4152 'contents', ..., 'commentsEntryPointHeaderRenderer', 'commentCount', 'simpleText'
4153 ), (
4154 'engagementPanels', lambda _, v: v['engagementPanelSectionListRenderer']['panelIdentifier'] == 'comment-item-section',
4155 'engagementPanelSectionListRenderer', 'header', 'engagementPanelTitleHeaderRenderer', 'contextualInfo', 'runs', ..., 'text'
4156 ), expected_type=int_or_none, get_all=False)
4157
4158 try: # This will error if there is no livechat
4159 initial_data['contents']['twoColumnWatchNextResults']['conversationBar']['liveChatRenderer']['continuations'][0]['reloadContinuationData']['continuation']
4160 except (KeyError, IndexError, TypeError):
4161 pass
4162 else:
4163 info.setdefault('subtitles', {})['live_chat'] = [{
4164 # url is needed to set cookies
4165 'url': f'https://www.youtube.com/watch?v={video_id}&bpctr=9999999999&has_verified=1',
4166 'video_id': video_id,
4167 'ext': 'json',
4168 'protocol': ('youtube_live_chat' if live_status in ('is_live', 'is_upcoming')
4169 else 'youtube_live_chat_replay'),
4170 }]
4171
4172 if initial_data:
4173 info['chapters'] = (
4174 self._extract_chapters_from_json(initial_data, duration)
4175 or self._extract_chapters_from_engagement_panel(initial_data, duration)
4176 or self._extract_chapters_from_description(video_description, duration)
4177 or None)
4178
4179 contents = traverse_obj(
4180 initial_data, ('contents', 'twoColumnWatchNextResults', 'results', 'results', 'contents'),
4181 expected_type=list, default=[])
4182
4183 vpir = get_first(contents, 'videoPrimaryInfoRenderer')
4184 if vpir:
4185 stl = vpir.get('superTitleLink')
4186 if stl:
4187 stl = self._get_text(stl)
4188 if try_get(
4189 vpir,
4190 lambda x: x['superTitleIcon']['iconType']) == 'LOCATION_PIN':
4191 info['location'] = stl
4192 else:
4193 mobj = re.search(r'(.+?)\s*S(\d+)\s*•?\s*E(\d+)', stl)
4194 if mobj:
4195 info.update({
4196 'series': mobj.group(1),
4197 'season_number': int(mobj.group(2)),
4198 'episode_number': int(mobj.group(3)),
4199 })
4200 for tlb in (try_get(
4201 vpir,
4202 lambda x: x['videoActions']['menuRenderer']['topLevelButtons'],
4203 list) or []):
4204 tbrs = variadic(
4205 traverse_obj(
4206 tlb, 'toggleButtonRenderer',
4207 ('segmentedLikeDislikeButtonRenderer', ..., 'toggleButtonRenderer'),
4208 default=[]))
4209 for tbr in tbrs:
4210 for getter, regex in [(
4211 lambda x: x['defaultText']['accessibility']['accessibilityData'],
4212 r'(?P<count>[\d,]+)\s*(?P<type>(?:dis)?like)'), ([
4213 lambda x: x['accessibility'],
4214 lambda x: x['accessibilityData']['accessibilityData'],
4215 ], r'(?P<type>(?:dis)?like) this video along with (?P<count>[\d,]+) other people')]:
4216 label = (try_get(tbr, getter, dict) or {}).get('label')
4217 if label:
4218 mobj = re.match(regex, label)
4219 if mobj:
4220 info[mobj.group('type') + '_count'] = str_to_int(mobj.group('count'))
4221 break
4222 sbr_tooltip = try_get(
4223 vpir, lambda x: x['sentimentBar']['sentimentBarRenderer']['tooltip'])
4224 if sbr_tooltip:
4225 like_count, dislike_count = sbr_tooltip.split(' / ')
4226 info.update({
4227 'like_count': str_to_int(like_count),
4228 'dislike_count': str_to_int(dislike_count),
4229 })
4230 vcr = traverse_obj(vpir, ('viewCount', 'videoViewCountRenderer'))
4231 if vcr:
4232 vc = self._get_count(vcr, 'viewCount')
4233 # Upcoming premieres with waiting count are treated as live here
4234 if vcr.get('isLive'):
4235 info['concurrent_view_count'] = vc
4236 elif info.get('view_count') is None:
4237 info['view_count'] = vc
4238
4239 vsir = get_first(contents, 'videoSecondaryInfoRenderer')
4240 if vsir:
4241 vor = traverse_obj(vsir, ('owner', 'videoOwnerRenderer'))
4242 info.update({
4243 'channel': self._get_text(vor, 'title'),
4244 'channel_follower_count': self._get_count(vor, 'subscriberCountText')})
4245
4246 rows = try_get(
4247 vsir,
4248 lambda x: x['metadataRowContainer']['metadataRowContainerRenderer']['rows'],
4249 list) or []
4250 multiple_songs = False
4251 for row in rows:
4252 if try_get(row, lambda x: x['metadataRowRenderer']['hasDividerLine']) is True:
4253 multiple_songs = True
4254 break
4255 for row in rows:
4256 mrr = row.get('metadataRowRenderer') or {}
4257 mrr_title = mrr.get('title')
4258 if not mrr_title:
4259 continue
4260 mrr_title = self._get_text(mrr, 'title')
4261 mrr_contents_text = self._get_text(mrr, ('contents', 0))
4262 if mrr_title == 'License':
4263 info['license'] = mrr_contents_text
4264 elif not multiple_songs:
4265 if mrr_title == 'Album':
4266 info['album'] = mrr_contents_text
4267 elif mrr_title == 'Artist':
4268 info['artist'] = mrr_contents_text
4269 elif mrr_title == 'Song':
4270 info['track'] = mrr_contents_text
4271
4272 fallbacks = {
4273 'channel': 'uploader',
4274 'channel_id': 'uploader_id',
4275 'channel_url': 'uploader_url',
4276 }
4277
4278 # The upload date for scheduled, live and past live streams / premieres in microformats
4279 # may be different from the stream date. Although not in UTC, we will prefer it in this case.
4280 # See: https://github.com/yt-dlp/yt-dlp/pull/2223#issuecomment-1008485139
4281 upload_date = (
4282 unified_strdate(get_first(microformats, 'uploadDate'))
4283 or unified_strdate(search_meta('uploadDate')))
4284 if not upload_date or (
4285 live_status in ('not_live', None)
4286 and 'no-youtube-prefer-utc-upload-date' not in self.get_param('compat_opts', [])
4287 ):
4288 upload_date = strftime_or_none(
4289 self._parse_time_text(self._get_text(vpir, 'dateText')), '%Y%m%d') or upload_date
4290 info['upload_date'] = upload_date
4291
4292 for to, frm in fallbacks.items():
4293 if not info.get(to):
4294 info[to] = info.get(frm)
4295
4296 for s_k, d_k in [('artist', 'creator'), ('track', 'alt_title')]:
4297 v = info.get(s_k)
4298 if v:
4299 info[d_k] = v
4300
4301 badges = self._extract_badges(traverse_obj(contents, (..., 'videoPrimaryInfoRenderer'), get_all=False))
4302
4303 is_private = (self._has_badge(badges, BadgeType.AVAILABILITY_PRIVATE)
4304 or get_first(video_details, 'isPrivate', expected_type=bool))
4305
4306 info['availability'] = (
4307 'public' if self._has_badge(badges, BadgeType.AVAILABILITY_PUBLIC)
4308 else self._availability(
4309 is_private=is_private,
4310 needs_premium=(
4311 self._has_badge(badges, BadgeType.AVAILABILITY_PREMIUM)
4312 or False if initial_data and is_private is not None else None),
4313 needs_subscription=(
4314 self._has_badge(badges, BadgeType.AVAILABILITY_SUBSCRIPTION)
4315 or False if initial_data and is_private is not None else None),
4316 needs_auth=info['age_limit'] >= 18,
4317 is_unlisted=None if is_private is None else (
4318 self._has_badge(badges, BadgeType.AVAILABILITY_UNLISTED)
4319 or get_first(microformats, 'isUnlisted', expected_type=bool))))
4320
4321 info['__post_extractor'] = self.extract_comments(master_ytcfg, video_id, contents, webpage)
4322
4323 self.mark_watched(video_id, player_responses)
4324
4325 return info
4326
4327
4328 class YoutubeTabBaseInfoExtractor(YoutubeBaseInfoExtractor):
4329 @staticmethod
4330 def passthrough_smuggled_data(func):
4331 def _smuggle(info, smuggled_data):
4332 if info.get('_type') not in ('url', 'url_transparent'):
4333 return info
4334 if smuggled_data.get('is_music_url'):
4335 parsed_url = urllib.parse.urlparse(info['url'])
4336 if parsed_url.netloc in ('www.youtube.com', 'music.youtube.com'):
4337 smuggled_data.pop('is_music_url')
4338 info['url'] = urllib.parse.urlunparse(parsed_url._replace(netloc='music.youtube.com'))
4339 if smuggled_data:
4340 info['url'] = smuggle_url(info['url'], smuggled_data)
4341 return info
4342
4343 @functools.wraps(func)
4344 def wrapper(self, url):
4345 url, smuggled_data = unsmuggle_url(url, {})
4346 if self.is_music_url(url):
4347 smuggled_data['is_music_url'] = True
4348 info_dict = func(self, url, smuggled_data)
4349 if smuggled_data:
4350 _smuggle(info_dict, smuggled_data)
4351 if info_dict.get('entries'):
4352 info_dict['entries'] = (_smuggle(i, smuggled_data.copy()) for i in info_dict['entries'])
4353 return info_dict
4354 return wrapper
4355
4356 def _extract_channel_id(self, webpage):
4357 channel_id = self._html_search_meta(
4358 'channelId', webpage, 'channel id', default=None)
4359 if channel_id:
4360 return channel_id
4361 channel_url = self._html_search_meta(
4362 ('og:url', 'al:ios:url', 'al:android:url', 'al:web:url',
4363 'twitter:url', 'twitter:app:url:iphone', 'twitter:app:url:ipad',
4364 'twitter:app:url:googleplay'), webpage, 'channel url')
4365 return self._search_regex(
4366 r'https?://(?:www\.)?youtube\.com/channel/([^/?#&])+',
4367 channel_url, 'channel id')
4368
4369 @staticmethod
4370 def _extract_basic_item_renderer(item):
4371 # Modified from _extract_grid_item_renderer
4372 known_basic_renderers = (
4373 'playlistRenderer', 'videoRenderer', 'channelRenderer', 'showRenderer', 'reelItemRenderer'
4374 )
4375 for key, renderer in item.items():
4376 if not isinstance(renderer, dict):
4377 continue
4378 elif key in known_basic_renderers:
4379 return renderer
4380 elif key.startswith('grid') and key.endswith('Renderer'):
4381 return renderer
4382
4383 def _grid_entries(self, grid_renderer):
4384 for item in grid_renderer['items']:
4385 if not isinstance(item, dict):
4386 continue
4387 renderer = self._extract_basic_item_renderer(item)
4388 if not isinstance(renderer, dict):
4389 continue
4390 title = self._get_text(renderer, 'title')
4391
4392 # playlist
4393 playlist_id = renderer.get('playlistId')
4394 if playlist_id:
4395 yield self.url_result(
4396 'https://www.youtube.com/playlist?list=%s' % playlist_id,
4397 ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
4398 video_title=title)
4399 continue
4400 # video
4401 video_id = renderer.get('videoId')
4402 if video_id:
4403 yield self._extract_video(renderer)
4404 continue
4405 # channel
4406 channel_id = renderer.get('channelId')
4407 if channel_id:
4408 yield self.url_result(
4409 'https://www.youtube.com/channel/%s' % channel_id,
4410 ie=YoutubeTabIE.ie_key(), video_title=title)
4411 continue
4412 # generic endpoint URL support
4413 ep_url = urljoin('https://www.youtube.com/', try_get(
4414 renderer, lambda x: x['navigationEndpoint']['commandMetadata']['webCommandMetadata']['url'],
4415 str))
4416 if ep_url:
4417 for ie in (YoutubeTabIE, YoutubePlaylistIE, YoutubeIE):
4418 if ie.suitable(ep_url):
4419 yield self.url_result(
4420 ep_url, ie=ie.ie_key(), video_id=ie._match_id(ep_url), video_title=title)
4421 break
4422
4423 def _music_reponsive_list_entry(self, renderer):
4424 video_id = traverse_obj(renderer, ('playlistItemData', 'videoId'))
4425 if video_id:
4426 return self.url_result(f'https://music.youtube.com/watch?v={video_id}',
4427 ie=YoutubeIE.ie_key(), video_id=video_id)
4428 playlist_id = traverse_obj(renderer, ('navigationEndpoint', 'watchEndpoint', 'playlistId'))
4429 if playlist_id:
4430 video_id = traverse_obj(renderer, ('navigationEndpoint', 'watchEndpoint', 'videoId'))
4431 if video_id:
4432 return self.url_result(f'https://music.youtube.com/watch?v={video_id}&list={playlist_id}',
4433 ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
4434 return self.url_result(f'https://music.youtube.com/playlist?list={playlist_id}',
4435 ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
4436 browse_id = traverse_obj(renderer, ('navigationEndpoint', 'browseEndpoint', 'browseId'))
4437 if browse_id:
4438 return self.url_result(f'https://music.youtube.com/browse/{browse_id}',
4439 ie=YoutubeTabIE.ie_key(), video_id=browse_id)
4440
4441 def _shelf_entries_from_content(self, shelf_renderer):
4442 content = shelf_renderer.get('content')
4443 if not isinstance(content, dict):
4444 return
4445 renderer = content.get('gridRenderer') or content.get('expandedShelfContentsRenderer')
4446 if renderer:
4447 # TODO: add support for nested playlists so each shelf is processed
4448 # as separate playlist
4449 # TODO: this includes only first N items
4450 yield from self._grid_entries(renderer)
4451 renderer = content.get('horizontalListRenderer')
4452 if renderer:
4453 # TODO
4454 pass
4455
4456 def _shelf_entries(self, shelf_renderer, skip_channels=False):
4457 ep = try_get(
4458 shelf_renderer, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
4459 str)
4460 shelf_url = urljoin('https://www.youtube.com', ep)
4461 if shelf_url:
4462 # Skipping links to another channels, note that checking for
4463 # endpoint.commandMetadata.webCommandMetadata.webPageTypwebPageType == WEB_PAGE_TYPE_CHANNEL
4464 # will not work
4465 if skip_channels and '/channels?' in shelf_url:
4466 return
4467 title = self._get_text(shelf_renderer, 'title')
4468 yield self.url_result(shelf_url, video_title=title)
4469 # Shelf may not contain shelf URL, fallback to extraction from content
4470 yield from self._shelf_entries_from_content(shelf_renderer)
4471
4472 def _playlist_entries(self, video_list_renderer):
4473 for content in video_list_renderer['contents']:
4474 if not isinstance(content, dict):
4475 continue
4476 renderer = content.get('playlistVideoRenderer') or content.get('playlistPanelVideoRenderer')
4477 if not isinstance(renderer, dict):
4478 continue
4479 video_id = renderer.get('videoId')
4480 if not video_id:
4481 continue
4482 yield self._extract_video(renderer)
4483
4484 def _rich_entries(self, rich_grid_renderer):
4485 renderer = traverse_obj(
4486 rich_grid_renderer, ('content', ('videoRenderer', 'reelItemRenderer')), get_all=False) or {}
4487 video_id = renderer.get('videoId')
4488 if not video_id:
4489 return
4490 yield self._extract_video(renderer)
4491
4492 def _video_entry(self, video_renderer):
4493 video_id = video_renderer.get('videoId')
4494 if video_id:
4495 return self._extract_video(video_renderer)
4496
4497 def _hashtag_tile_entry(self, hashtag_tile_renderer):
4498 url = urljoin('https://youtube.com', traverse_obj(
4499 hashtag_tile_renderer, ('onTapCommand', 'commandMetadata', 'webCommandMetadata', 'url')))
4500 if url:
4501 return self.url_result(
4502 url, ie=YoutubeTabIE.ie_key(), title=self._get_text(hashtag_tile_renderer, 'hashtag'))
4503
4504 def _post_thread_entries(self, post_thread_renderer):
4505 post_renderer = try_get(
4506 post_thread_renderer, lambda x: x['post']['backstagePostRenderer'], dict)
4507 if not post_renderer:
4508 return
4509 # video attachment
4510 video_renderer = try_get(
4511 post_renderer, lambda x: x['backstageAttachment']['videoRenderer'], dict) or {}
4512 video_id = video_renderer.get('videoId')
4513 if video_id:
4514 entry = self._extract_video(video_renderer)
4515 if entry:
4516 yield entry
4517 # playlist attachment
4518 playlist_id = try_get(
4519 post_renderer, lambda x: x['backstageAttachment']['playlistRenderer']['playlistId'], str)
4520 if playlist_id:
4521 yield self.url_result(
4522 'https://www.youtube.com/playlist?list=%s' % playlist_id,
4523 ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
4524 # inline video links
4525 runs = try_get(post_renderer, lambda x: x['contentText']['runs'], list) or []
4526 for run in runs:
4527 if not isinstance(run, dict):
4528 continue
4529 ep_url = try_get(
4530 run, lambda x: x['navigationEndpoint']['urlEndpoint']['url'], str)
4531 if not ep_url:
4532 continue
4533 if not YoutubeIE.suitable(ep_url):
4534 continue
4535 ep_video_id = YoutubeIE._match_id(ep_url)
4536 if video_id == ep_video_id:
4537 continue
4538 yield self.url_result(ep_url, ie=YoutubeIE.ie_key(), video_id=ep_video_id)
4539
4540 def _post_thread_continuation_entries(self, post_thread_continuation):
4541 contents = post_thread_continuation.get('contents')
4542 if not isinstance(contents, list):
4543 return
4544 for content in contents:
4545 renderer = content.get('backstagePostThreadRenderer')
4546 if isinstance(renderer, dict):
4547 yield from self._post_thread_entries(renderer)
4548 continue
4549 renderer = content.get('videoRenderer')
4550 if isinstance(renderer, dict):
4551 yield self._video_entry(renderer)
4552
4553 r''' # unused
4554 def _rich_grid_entries(self, contents):
4555 for content in contents:
4556 video_renderer = try_get(content, lambda x: x['richItemRenderer']['content']['videoRenderer'], dict)
4557 if video_renderer:
4558 entry = self._video_entry(video_renderer)
4559 if entry:
4560 yield entry
4561 '''
4562
4563 def _report_history_entries(self, renderer):
4564 for url in traverse_obj(renderer, (
4565 'rows', ..., 'reportHistoryTableRowRenderer', 'cells', ...,
4566 'reportHistoryTableCellRenderer', 'cell', 'reportHistoryTableTextCellRenderer', 'text', 'runs', ...,
4567 'navigationEndpoint', 'commandMetadata', 'webCommandMetadata', 'url')):
4568 yield self.url_result(urljoin('https://www.youtube.com', url), YoutubeIE)
4569
4570 def _extract_entries(self, parent_renderer, continuation_list):
4571 # continuation_list is modified in-place with continuation_list = [continuation_token]
4572 continuation_list[:] = [None]
4573 contents = try_get(parent_renderer, lambda x: x['contents'], list) or []
4574 for content in contents:
4575 if not isinstance(content, dict):
4576 continue
4577 is_renderer = traverse_obj(
4578 content, 'itemSectionRenderer', 'musicShelfRenderer', 'musicShelfContinuation',
4579 expected_type=dict)
4580 if not is_renderer:
4581 if content.get('richItemRenderer'):
4582 for entry in self._rich_entries(content['richItemRenderer']):
4583 yield entry
4584 continuation_list[0] = self._extract_continuation(parent_renderer)
4585 elif content.get('reportHistorySectionRenderer'): # https://www.youtube.com/reporthistory
4586 table = traverse_obj(content, ('reportHistorySectionRenderer', 'table', 'tableRenderer'))
4587 yield from self._report_history_entries(table)
4588 continuation_list[0] = self._extract_continuation(table)
4589 continue
4590
4591 isr_contents = try_get(is_renderer, lambda x: x['contents'], list) or []
4592 for isr_content in isr_contents:
4593 if not isinstance(isr_content, dict):
4594 continue
4595
4596 known_renderers = {
4597 'playlistVideoListRenderer': self._playlist_entries,
4598 'gridRenderer': self._grid_entries,
4599 'reelShelfRenderer': self._grid_entries,
4600 'shelfRenderer': self._shelf_entries,
4601 'musicResponsiveListItemRenderer': lambda x: [self._music_reponsive_list_entry(x)],
4602 'backstagePostThreadRenderer': self._post_thread_entries,
4603 'videoRenderer': lambda x: [self._video_entry(x)],
4604 'playlistRenderer': lambda x: self._grid_entries({'items': [{'playlistRenderer': x}]}),
4605 'channelRenderer': lambda x: self._grid_entries({'items': [{'channelRenderer': x}]}),
4606 'hashtagTileRenderer': lambda x: [self._hashtag_tile_entry(x)]
4607 }
4608 for key, renderer in isr_content.items():
4609 if key not in known_renderers:
4610 continue
4611 for entry in known_renderers[key](renderer):
4612 if entry:
4613 yield entry
4614 continuation_list[0] = self._extract_continuation(renderer)
4615 break
4616
4617 if not continuation_list[0]:
4618 continuation_list[0] = self._extract_continuation(is_renderer)
4619
4620 if not continuation_list[0]:
4621 continuation_list[0] = self._extract_continuation(parent_renderer)
4622
4623 def _entries(self, tab, item_id, ytcfg, account_syncid, visitor_data):
4624 continuation_list = [None]
4625 extract_entries = lambda x: self._extract_entries(x, continuation_list)
4626 tab_content = try_get(tab, lambda x: x['content'], dict)
4627 if not tab_content:
4628 return
4629 parent_renderer = (
4630 try_get(tab_content, lambda x: x['sectionListRenderer'], dict)
4631 or try_get(tab_content, lambda x: x['richGridRenderer'], dict) or {})
4632 yield from extract_entries(parent_renderer)
4633 continuation = continuation_list[0]
4634
4635 for page_num in itertools.count(1):
4636 if not continuation:
4637 break
4638 headers = self.generate_api_headers(
4639 ytcfg=ytcfg, account_syncid=account_syncid, visitor_data=visitor_data)
4640 response = self._extract_response(
4641 item_id=f'{item_id} page {page_num}',
4642 query=continuation, headers=headers, ytcfg=ytcfg,
4643 check_get_keys=('continuationContents', 'onResponseReceivedActions', 'onResponseReceivedEndpoints'))
4644
4645 if not response:
4646 break
4647 # Extracting updated visitor data is required to prevent an infinite extraction loop in some cases
4648 # See: https://github.com/ytdl-org/youtube-dl/issues/28702
4649 visitor_data = self._extract_visitor_data(response) or visitor_data
4650
4651 known_renderers = {
4652 'videoRenderer': (self._grid_entries, 'items'), # for membership tab
4653 'gridPlaylistRenderer': (self._grid_entries, 'items'),
4654 'gridVideoRenderer': (self._grid_entries, 'items'),
4655 'gridChannelRenderer': (self._grid_entries, 'items'),
4656 'playlistVideoRenderer': (self._playlist_entries, 'contents'),
4657 'itemSectionRenderer': (extract_entries, 'contents'), # for feeds
4658 'richItemRenderer': (extract_entries, 'contents'), # for hashtag
4659 'backstagePostThreadRenderer': (self._post_thread_continuation_entries, 'contents'),
4660 'reportHistoryTableRowRenderer': (self._report_history_entries, 'rows'),
4661 'playlistVideoListContinuation': (self._playlist_entries, None),
4662 'gridContinuation': (self._grid_entries, None),
4663 'itemSectionContinuation': (self._post_thread_continuation_entries, None),
4664 'sectionListContinuation': (extract_entries, None), # for feeds
4665 }
4666
4667 continuation_items = traverse_obj(response, (
4668 ('onResponseReceivedActions', 'onResponseReceivedEndpoints'), ...,
4669 'appendContinuationItemsAction', 'continuationItems'
4670 ), 'continuationContents', get_all=False)
4671 continuation_item = traverse_obj(continuation_items, 0, None, expected_type=dict, default={})
4672
4673 video_items_renderer = None
4674 for key in continuation_item.keys():
4675 if key not in known_renderers:
4676 continue
4677 func, parent_key = known_renderers[key]
4678 video_items_renderer = {parent_key: continuation_items} if parent_key else continuation_items
4679 continuation_list = [None]
4680 yield from func(video_items_renderer)
4681 continuation = continuation_list[0] or self._extract_continuation(video_items_renderer)
4682
4683 if not video_items_renderer:
4684 break
4685
4686 @staticmethod
4687 def _extract_selected_tab(tabs, fatal=True):
4688 for tab_renderer in tabs:
4689 if tab_renderer.get('selected'):
4690 return tab_renderer
4691 if fatal:
4692 raise ExtractorError('Unable to find selected tab')
4693
4694 @staticmethod
4695 def _extract_tab_renderers(response):
4696 return traverse_obj(
4697 response, ('contents', 'twoColumnBrowseResultsRenderer', 'tabs', ..., ('tabRenderer', 'expandableTabRenderer')), expected_type=dict)
4698
4699 def _extract_from_tabs(self, item_id, ytcfg, data, tabs):
4700 metadata = self._extract_metadata_from_tabs(item_id, data)
4701
4702 selected_tab = self._extract_selected_tab(tabs)
4703 metadata['title'] += format_field(selected_tab, 'title', ' - %s')
4704 metadata['title'] += format_field(selected_tab, 'expandedText', ' - %s')
4705
4706 return self.playlist_result(
4707 self._entries(
4708 selected_tab, metadata['id'], ytcfg,
4709 self._extract_account_syncid(ytcfg, data),
4710 self._extract_visitor_data(data, ytcfg)),
4711 **metadata)
4712
4713 def _extract_metadata_from_tabs(self, item_id, data):
4714 info = {'id': item_id}
4715
4716 metadata_renderer = traverse_obj(data, ('metadata', 'channelMetadataRenderer'), expected_type=dict)
4717 if metadata_renderer:
4718 info.update({
4719 'uploader': metadata_renderer.get('title'),
4720 'uploader_id': metadata_renderer.get('externalId'),
4721 'uploader_url': metadata_renderer.get('channelUrl'),
4722 })
4723 if info['uploader_id']:
4724 info['id'] = info['uploader_id']
4725 else:
4726 metadata_renderer = traverse_obj(data, ('metadata', 'playlistMetadataRenderer'), expected_type=dict)
4727
4728 # We can get the uncropped banner/avatar by replacing the crop params with '=s0'
4729 # See: https://github.com/yt-dlp/yt-dlp/issues/2237#issuecomment-1013694714
4730 def _get_uncropped(url):
4731 return url_or_none((url or '').split('=')[0] + '=s0')
4732
4733 avatar_thumbnails = self._extract_thumbnails(metadata_renderer, 'avatar')
4734 if avatar_thumbnails:
4735 uncropped_avatar = _get_uncropped(avatar_thumbnails[0]['url'])
4736 if uncropped_avatar:
4737 avatar_thumbnails.append({
4738 'url': uncropped_avatar,
4739 'id': 'avatar_uncropped',
4740 'preference': 1
4741 })
4742
4743 channel_banners = self._extract_thumbnails(
4744 data, ('header', ..., ('banner', 'mobileBanner', 'tvBanner')))
4745 for banner in channel_banners:
4746 banner['preference'] = -10
4747
4748 if channel_banners:
4749 uncropped_banner = _get_uncropped(channel_banners[0]['url'])
4750 if uncropped_banner:
4751 channel_banners.append({
4752 'url': uncropped_banner,
4753 'id': 'banner_uncropped',
4754 'preference': -5
4755 })
4756
4757 # Deprecated - remove primary_sidebar_renderer when layout discontinued
4758 primary_sidebar_renderer = self._extract_sidebar_info_renderer(data, 'playlistSidebarPrimaryInfoRenderer')
4759 playlist_header_renderer = traverse_obj(data, ('header', 'playlistHeaderRenderer'), expected_type=dict)
4760
4761 primary_thumbnails = self._extract_thumbnails(
4762 primary_sidebar_renderer, ('thumbnailRenderer', ('playlistVideoThumbnailRenderer', 'playlistCustomThumbnailRenderer'), 'thumbnail'))
4763 playlist_thumbnails = self._extract_thumbnails(
4764 playlist_header_renderer, ('playlistHeaderBanner', 'heroPlaylistThumbnailRenderer', 'thumbnail'))
4765
4766 info.update({
4767 'title': (traverse_obj(metadata_renderer, 'title')
4768 or self._get_text(data, ('header', 'hashtagHeaderRenderer', 'hashtag'))
4769 or info['id']),
4770 'availability': self._extract_availability(data),
4771 'channel_follower_count': self._get_count(data, ('header', ..., 'subscriberCountText')),
4772 'description': try_get(metadata_renderer, lambda x: x.get('description', '')),
4773 'tags': try_get(metadata_renderer or {}, lambda x: x.get('keywords', '').split()),
4774 'thumbnails': (primary_thumbnails or playlist_thumbnails) + avatar_thumbnails + channel_banners,
4775 })
4776
4777 # Playlist stats is a text runs array containing [video count, view count, last updated].
4778 # last updated or (view count and last updated) may be missing.
4779 playlist_stats = get_first(
4780 (primary_sidebar_renderer, playlist_header_renderer), (('stats', 'briefStats', 'numVideosText'), ))
4781
4782 last_updated_unix = self._parse_time_text(
4783 self._get_text(playlist_stats, 2) # deprecated, remove when old layout discontinued
4784 or self._get_text(playlist_header_renderer, ('byline', 1, 'playlistBylineRenderer', 'text')))
4785 info['modified_date'] = strftime_or_none(last_updated_unix, '%Y%m%d')
4786
4787 info['view_count'] = self._get_count(playlist_stats, 1)
4788 if info['view_count'] is None: # 0 is allowed
4789 info['view_count'] = self._get_count(playlist_header_renderer, 'viewCountText')
4790
4791 info['playlist_count'] = self._get_count(playlist_stats, 0)
4792 if info['playlist_count'] is None: # 0 is allowed
4793 info['playlist_count'] = self._get_count(playlist_header_renderer, ('byline', 0, 'playlistBylineRenderer', 'text'))
4794
4795 if not info.get('uploader_id'):
4796 owner = traverse_obj(playlist_header_renderer, 'ownerText')
4797 if not owner: # Deprecated
4798 owner = traverse_obj(
4799 self._extract_sidebar_info_renderer(data, 'playlistSidebarSecondaryInfoRenderer'),
4800 ('videoOwner', 'videoOwnerRenderer', 'title'))
4801 owner_text = self._get_text(owner)
4802 browse_ep = traverse_obj(owner, ('runs', 0, 'navigationEndpoint', 'browseEndpoint')) or {}
4803 info.update({
4804 'uploader': self._search_regex(r'^by (.+) and \d+ others?$', owner_text, 'uploader', default=owner_text),
4805 'uploader_id': browse_ep.get('browseId'),
4806 'uploader_url': urljoin('https://www.youtube.com', browse_ep.get('canonicalBaseUrl'))
4807 })
4808
4809 info.update({
4810 'channel': info['uploader'],
4811 'channel_id': info['uploader_id'],
4812 'channel_url': info['uploader_url']
4813 })
4814 return info
4815
4816 def _extract_inline_playlist(self, playlist, playlist_id, data, ytcfg):
4817 first_id = last_id = response = None
4818 for page_num in itertools.count(1):
4819 videos = list(self._playlist_entries(playlist))
4820 if not videos:
4821 return
4822 start = next((i for i, v in enumerate(videos) if v['id'] == last_id), -1) + 1
4823 if start >= len(videos):
4824 return
4825 yield from videos[start:]
4826 first_id = first_id or videos[0]['id']
4827 last_id = videos[-1]['id']
4828 watch_endpoint = try_get(
4829 playlist, lambda x: x['contents'][-1]['playlistPanelVideoRenderer']['navigationEndpoint']['watchEndpoint'])
4830 headers = self.generate_api_headers(
4831 ytcfg=ytcfg, account_syncid=self._extract_account_syncid(ytcfg, data),
4832 visitor_data=self._extract_visitor_data(response, data, ytcfg))
4833 query = {
4834 'playlistId': playlist_id,
4835 'videoId': watch_endpoint.get('videoId') or last_id,
4836 'index': watch_endpoint.get('index') or len(videos),
4837 'params': watch_endpoint.get('params') or 'OAE%3D'
4838 }
4839 response = self._extract_response(
4840 item_id='%s page %d' % (playlist_id, page_num),
4841 query=query, ep='next', headers=headers, ytcfg=ytcfg,
4842 check_get_keys='contents'
4843 )
4844 playlist = try_get(
4845 response, lambda x: x['contents']['twoColumnWatchNextResults']['playlist']['playlist'], dict)
4846
4847 def _extract_from_playlist(self, item_id, url, data, playlist, ytcfg):
4848 title = playlist.get('title') or try_get(
4849 data, lambda x: x['titleText']['simpleText'], str)
4850 playlist_id = playlist.get('playlistId') or item_id
4851
4852 # Delegating everything except mix playlists to regular tab-based playlist URL
4853 playlist_url = urljoin(url, try_get(
4854 playlist, lambda x: x['endpoint']['commandMetadata']['webCommandMetadata']['url'],
4855 str))
4856
4857 # Some playlists are unviewable but YouTube still provides a link to the (broken) playlist page [1]
4858 # [1] MLCT, RLTDwFCb4jeqaKWnciAYM-ZVHg
4859 is_known_unviewable = re.fullmatch(r'MLCT|RLTD[\w-]{22}', playlist_id)
4860
4861 if playlist_url and playlist_url != url and not is_known_unviewable:
4862 return self.url_result(
4863 playlist_url, ie=YoutubeTabIE.ie_key(), video_id=playlist_id,
4864 video_title=title)
4865
4866 return self.playlist_result(
4867 self._extract_inline_playlist(playlist, playlist_id, data, ytcfg),
4868 playlist_id=playlist_id, playlist_title=title)
4869
4870 def _extract_availability(self, data):
4871 """
4872 Gets the availability of a given playlist/tab.
4873 Note: Unless YouTube tells us explicitly, we do not assume it is public
4874 @param data: response
4875 """
4876 sidebar_renderer = self._extract_sidebar_info_renderer(data, 'playlistSidebarPrimaryInfoRenderer') or {}
4877 playlist_header_renderer = traverse_obj(data, ('header', 'playlistHeaderRenderer')) or {}
4878 player_header_privacy = playlist_header_renderer.get('privacy')
4879
4880 badges = self._extract_badges(sidebar_renderer)
4881
4882 # Personal playlists, when authenticated, have a dropdown visibility selector instead of a badge
4883 privacy_setting_icon = get_first(
4884 (playlist_header_renderer, sidebar_renderer),
4885 ('privacyForm', 'dropdownFormFieldRenderer', 'dropdown', 'dropdownRenderer', 'entries',
4886 lambda _, v: v['privacyDropdownItemRenderer']['isSelected'], 'privacyDropdownItemRenderer', 'icon', 'iconType'),
4887 expected_type=str)
4888
4889 microformats_is_unlisted = traverse_obj(
4890 data, ('microformat', 'microformatDataRenderer', 'unlisted'), expected_type=bool)
4891
4892 return (
4893 'public' if (
4894 self._has_badge(badges, BadgeType.AVAILABILITY_PUBLIC)
4895 or player_header_privacy == 'PUBLIC'
4896 or privacy_setting_icon == 'PRIVACY_PUBLIC')
4897 else self._availability(
4898 is_private=(
4899 self._has_badge(badges, BadgeType.AVAILABILITY_PRIVATE)
4900 or player_header_privacy == 'PRIVATE' if player_header_privacy is not None
4901 else privacy_setting_icon == 'PRIVACY_PRIVATE' if privacy_setting_icon is not None else None),
4902 is_unlisted=(
4903 self._has_badge(badges, BadgeType.AVAILABILITY_UNLISTED)
4904 or player_header_privacy == 'UNLISTED' if player_header_privacy is not None
4905 else privacy_setting_icon == 'PRIVACY_UNLISTED' if privacy_setting_icon is not None
4906 else microformats_is_unlisted if microformats_is_unlisted is not None else None),
4907 needs_subscription=self._has_badge(badges, BadgeType.AVAILABILITY_SUBSCRIPTION) or None,
4908 needs_premium=self._has_badge(badges, BadgeType.AVAILABILITY_PREMIUM) or None,
4909 needs_auth=False))
4910
4911 @staticmethod
4912 def _extract_sidebar_info_renderer(data, info_renderer, expected_type=dict):
4913 sidebar_renderer = try_get(
4914 data, lambda x: x['sidebar']['playlistSidebarRenderer']['items'], list) or []
4915 for item in sidebar_renderer:
4916 renderer = try_get(item, lambda x: x[info_renderer], expected_type)
4917 if renderer:
4918 return renderer
4919
4920 def _reload_with_unavailable_videos(self, item_id, data, ytcfg):
4921 """
4922 Reload playlists with unavailable videos (e.g. private videos, region blocked, etc.)
4923 """
4924 is_playlist = bool(traverse_obj(
4925 data, ('metadata', 'playlistMetadataRenderer'), ('header', 'playlistHeaderRenderer')))
4926 if not is_playlist:
4927 return
4928 headers = self.generate_api_headers(
4929 ytcfg=ytcfg, account_syncid=self._extract_account_syncid(ytcfg, data),
4930 visitor_data=self._extract_visitor_data(data, ytcfg))
4931 query = {
4932 'params': 'wgYCCAA=',
4933 'browseId': f'VL{item_id}'
4934 }
4935 return self._extract_response(
4936 item_id=item_id, headers=headers, query=query,
4937 check_get_keys='contents', fatal=False, ytcfg=ytcfg,
4938 note='Redownloading playlist API JSON with unavailable videos')
4939
4940 @functools.cached_property
4941 def skip_webpage(self):
4942 return 'webpage' in self._configuration_arg('skip', ie_key=YoutubeTabIE.ie_key())
4943
4944 def _extract_webpage(self, url, item_id, fatal=True):
4945 webpage, data = None, None
4946 for retry in self.RetryManager(fatal=fatal):
4947 try:
4948 webpage = self._download_webpage(url, item_id, note='Downloading webpage')
4949 data = self.extract_yt_initial_data(item_id, webpage or '', fatal=fatal) or {}
4950 except ExtractorError as e:
4951 if isinstance(e.cause, network_exceptions):
4952 if not isinstance(e.cause, urllib.error.HTTPError) or e.cause.code not in (403, 429):
4953 retry.error = e
4954 continue
4955 self._error_or_warning(e, fatal=fatal)
4956 break
4957
4958 try:
4959 self._extract_and_report_alerts(data)
4960 except ExtractorError as e:
4961 self._error_or_warning(e, fatal=fatal)
4962 break
4963
4964 # Sometimes youtube returns a webpage with incomplete ytInitialData
4965 # See: https://github.com/yt-dlp/yt-dlp/issues/116
4966 if not traverse_obj(data, 'contents', 'currentVideoEndpoint', 'onResponseReceivedActions'):
4967 retry.error = ExtractorError('Incomplete yt initial data received')
4968 continue
4969
4970 return webpage, data
4971
4972 def _report_playlist_authcheck(self, ytcfg, fatal=True):
4973 """Use if failed to extract ytcfg (and data) from initial webpage"""
4974 if not ytcfg and self.is_authenticated:
4975 msg = 'Playlists that require authentication may not extract correctly without a successful webpage download'
4976 if 'authcheck' not in self._configuration_arg('skip', ie_key=YoutubeTabIE.ie_key()) and fatal:
4977 raise ExtractorError(
4978 f'{msg}. If you are not downloading private content, or '
4979 'your cookies are only for the first account and channel,'
4980 ' pass "--extractor-args youtubetab:skip=authcheck" to skip this check',
4981 expected=True)
4982 self.report_warning(msg, only_once=True)
4983
4984 def _extract_data(self, url, item_id, ytcfg=None, fatal=True, webpage_fatal=False, default_client='web'):
4985 data = None
4986 if not self.skip_webpage:
4987 webpage, data = self._extract_webpage(url, item_id, fatal=webpage_fatal)
4988 ytcfg = ytcfg or self.extract_ytcfg(item_id, webpage)
4989 # Reject webpage data if redirected to home page without explicitly requesting
4990 selected_tab = self._extract_selected_tab(self._extract_tab_renderers(data), fatal=False) or {}
4991 if (url != 'https://www.youtube.com/feed/recommended'
4992 and selected_tab.get('tabIdentifier') == 'FEwhat_to_watch' # Home page
4993 and 'no-youtube-channel-redirect' not in self.get_param('compat_opts', [])):
4994 msg = 'The channel/playlist does not exist and the URL redirected to youtube.com home page'
4995 if fatal:
4996 raise ExtractorError(msg, expected=True)
4997 self.report_warning(msg, only_once=True)
4998 if not data:
4999 self._report_playlist_authcheck(ytcfg, fatal=fatal)
5000 data = self._extract_tab_endpoint(url, item_id, ytcfg, fatal=fatal, default_client=default_client)
5001 return data, ytcfg
5002
5003 def _extract_tab_endpoint(self, url, item_id, ytcfg=None, fatal=True, default_client='web'):
5004 headers = self.generate_api_headers(ytcfg=ytcfg, default_client=default_client)
5005 resolve_response = self._extract_response(
5006 item_id=item_id, query={'url': url}, check_get_keys='endpoint', headers=headers, ytcfg=ytcfg, fatal=fatal,
5007 ep='navigation/resolve_url', note='Downloading API parameters API JSON', default_client=default_client)
5008 endpoints = {'browseEndpoint': 'browse', 'watchEndpoint': 'next'}
5009 for ep_key, ep in endpoints.items():
5010 params = try_get(resolve_response, lambda x: x['endpoint'][ep_key], dict)
5011 if params:
5012 return self._extract_response(
5013 item_id=item_id, query=params, ep=ep, headers=headers,
5014 ytcfg=ytcfg, fatal=fatal, default_client=default_client,
5015 check_get_keys=('contents', 'currentVideoEndpoint', 'onResponseReceivedActions'))
5016 err_note = 'Failed to resolve url (does the playlist exist?)'
5017 if fatal:
5018 raise ExtractorError(err_note, expected=True)
5019 self.report_warning(err_note, item_id)
5020
5021 _SEARCH_PARAMS = None
5022
5023 def _search_results(self, query, params=NO_DEFAULT, default_client='web'):
5024 data = {'query': query}
5025 if params is NO_DEFAULT:
5026 params = self._SEARCH_PARAMS
5027 if params:
5028 data['params'] = params
5029
5030 content_keys = (
5031 ('contents', 'twoColumnSearchResultsRenderer', 'primaryContents', 'sectionListRenderer', 'contents'),
5032 ('onResponseReceivedCommands', 0, 'appendContinuationItemsAction', 'continuationItems'),
5033 # ytmusic search
5034 ('contents', 'tabbedSearchResultsRenderer', 'tabs', 0, 'tabRenderer', 'content', 'sectionListRenderer', 'contents'),
5035 ('continuationContents', ),
5036 )
5037 display_id = f'query "{query}"'
5038 check_get_keys = tuple({keys[0] for keys in content_keys})
5039 ytcfg = self._download_ytcfg(default_client, display_id) if not self.skip_webpage else {}
5040 self._report_playlist_authcheck(ytcfg, fatal=False)
5041
5042 continuation_list = [None]
5043 search = None
5044 for page_num in itertools.count(1):
5045 data.update(continuation_list[0] or {})
5046 headers = self.generate_api_headers(
5047 ytcfg=ytcfg, visitor_data=self._extract_visitor_data(search), default_client=default_client)
5048 search = self._extract_response(
5049 item_id=f'{display_id} page {page_num}', ep='search', query=data,
5050 default_client=default_client, check_get_keys=check_get_keys, ytcfg=ytcfg, headers=headers)
5051 slr_contents = traverse_obj(search, *content_keys)
5052 yield from self._extract_entries({'contents': list(variadic(slr_contents))}, continuation_list)
5053 if not continuation_list[0]:
5054 break
5055
5056
5057 class YoutubeTabIE(YoutubeTabBaseInfoExtractor):
5058 IE_DESC = 'YouTube Tabs'
5059 _VALID_URL = r'''(?x:
5060 https?://
5061 (?:\w+\.)?
5062 (?:
5063 youtube(?:kids)?\.com|
5064 %(invidious)s
5065 )/
5066 (?:
5067 (?P<channel_type>channel|c|user|browse)/|
5068 (?P<not_channel>
5069 feed/|hashtag/|
5070 (?:playlist|watch)\?.*?\blist=
5071 )|
5072 (?!(?:%(reserved_names)s)\b) # Direct URLs
5073 )
5074 (?P<id>[^/?\#&]+)
5075 )''' % {
5076 'reserved_names': YoutubeBaseInfoExtractor._RESERVED_NAMES,
5077 'invidious': '|'.join(YoutubeBaseInfoExtractor._INVIDIOUS_SITES),
5078 }
5079 IE_NAME = 'youtube:tab'
5080
5081 _TESTS = [{
5082 'note': 'playlists, multipage',
5083 'url': 'https://www.youtube.com/c/ИгорьКлейнер/playlists?view=1&flow=grid',
5084 'playlist_mincount': 94,
5085 'info_dict': {
5086 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
5087 'title': 'Igor Kleiner - Playlists',
5088 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
5089 'uploader': 'Igor Kleiner',
5090 'uploader_id': 'UCqj7Cz7revf5maW9g5pgNcg',
5091 'channel': 'Igor Kleiner',
5092 'channel_id': 'UCqj7Cz7revf5maW9g5pgNcg',
5093 'tags': ['"критическое', 'мышление"', '"наука', 'просто"', 'математика', '"анализ', 'данных"'],
5094 'channel_url': 'https://www.youtube.com/channel/UCqj7Cz7revf5maW9g5pgNcg',
5095 'uploader_url': 'https://www.youtube.com/channel/UCqj7Cz7revf5maW9g5pgNcg',
5096 'channel_follower_count': int
5097 },
5098 }, {
5099 'note': 'playlists, multipage, different order',
5100 'url': 'https://www.youtube.com/user/igorkle1/playlists?view=1&sort=dd',
5101 'playlist_mincount': 94,
5102 'info_dict': {
5103 'id': 'UCqj7Cz7revf5maW9g5pgNcg',
5104 'title': 'Igor Kleiner - Playlists',
5105 'description': 'md5:be97ee0f14ee314f1f002cf187166ee2',
5106 'uploader_id': 'UCqj7Cz7revf5maW9g5pgNcg',
5107 'uploader': 'Igor Kleiner',
5108 'uploader_url': 'https://www.youtube.com/channel/UCqj7Cz7revf5maW9g5pgNcg',
5109 'tags': ['"критическое', 'мышление"', '"наука', 'просто"', 'математика', '"анализ', 'данных"'],
5110 'channel_id': 'UCqj7Cz7revf5maW9g5pgNcg',
5111 'channel': 'Igor Kleiner',
5112 'channel_url': 'https://www.youtube.com/channel/UCqj7Cz7revf5maW9g5pgNcg',
5113 'channel_follower_count': int
5114 },
5115 }, {
5116 'note': 'playlists, series',
5117 'url': 'https://www.youtube.com/c/3blue1brown/playlists?view=50&sort=dd&shelf_id=3',
5118 'playlist_mincount': 5,
5119 'info_dict': {
5120 'id': 'UCYO_jab_esuFRV4b17AJtAw',
5121 'title': '3Blue1Brown - Playlists',
5122 'description': 'md5:e1384e8a133307dd10edee76e875d62f',
5123 'uploader_id': 'UCYO_jab_esuFRV4b17AJtAw',
5124 'uploader': '3Blue1Brown',
5125 'channel_url': 'https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw',
5126 'uploader_url': 'https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw',
5127 'channel': '3Blue1Brown',
5128 'channel_id': 'UCYO_jab_esuFRV4b17AJtAw',
5129 'tags': ['Mathematics'],
5130 'channel_follower_count': int
5131 },
5132 }, {
5133 'note': 'playlists, singlepage',
5134 'url': 'https://www.youtube.com/user/ThirstForScience/playlists',
5135 'playlist_mincount': 4,
5136 'info_dict': {
5137 'id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
5138 'title': 'ThirstForScience - Playlists',
5139 'description': 'md5:609399d937ea957b0f53cbffb747a14c',
5140 'uploader': 'ThirstForScience',
5141 'uploader_id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
5142 'uploader_url': 'https://www.youtube.com/channel/UCAEtajcuhQ6an9WEzY9LEMQ',
5143 'channel_url': 'https://www.youtube.com/channel/UCAEtajcuhQ6an9WEzY9LEMQ',
5144 'channel_id': 'UCAEtajcuhQ6an9WEzY9LEMQ',
5145 'tags': 'count:13',
5146 'channel': 'ThirstForScience',
5147 'channel_follower_count': int
5148 }
5149 }, {
5150 'url': 'https://www.youtube.com/c/ChristophLaimer/playlists',
5151 'only_matching': True,
5152 }, {
5153 'note': 'basic, single video playlist',
5154 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
5155 'info_dict': {
5156 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
5157 'uploader': 'Sergey M.',
5158 'id': 'PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
5159 'title': 'youtube-dl public playlist',
5160 'description': '',
5161 'tags': [],
5162 'view_count': int,
5163 'modified_date': '20201130',
5164 'channel': 'Sergey M.',
5165 'channel_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
5166 'uploader_url': 'https://www.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5167 'channel_url': 'https://www.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5168 'availability': 'public',
5169 },
5170 'playlist_count': 1,
5171 }, {
5172 'note': 'empty playlist',
5173 'url': 'https://www.youtube.com/playlist?list=PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
5174 'info_dict': {
5175 'uploader_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
5176 'uploader': 'Sergey M.',
5177 'id': 'PL4lCao7KL_QFodcLWhDpGCYnngnHtQ-Xf',
5178 'title': 'youtube-dl empty playlist',
5179 'tags': [],
5180 'channel': 'Sergey M.',
5181 'description': '',
5182 'modified_date': '20160902',
5183 'channel_id': 'UCmlqkdCBesrv2Lak1mF_MxA',
5184 'channel_url': 'https://www.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5185 'uploader_url': 'https://www.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5186 'availability': 'public',
5187 },
5188 'playlist_count': 0,
5189 }, {
5190 'note': 'Home tab',
5191 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/featured',
5192 'info_dict': {
5193 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5194 'title': 'lex will - Home',
5195 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5196 'uploader': 'lex will',
5197 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5198 'channel': 'lex will',
5199 'tags': ['bible', 'history', 'prophesy'],
5200 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5201 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5202 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5203 'channel_follower_count': int
5204 },
5205 'playlist_mincount': 2,
5206 }, {
5207 'note': 'Videos tab',
5208 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos',
5209 'info_dict': {
5210 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5211 'title': 'lex will - Videos',
5212 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5213 'uploader': 'lex will',
5214 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5215 'tags': ['bible', 'history', 'prophesy'],
5216 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5217 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5218 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5219 'channel': 'lex will',
5220 'channel_follower_count': int
5221 },
5222 'playlist_mincount': 975,
5223 }, {
5224 'note': 'Videos tab, sorted by popular',
5225 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/videos?view=0&sort=p&flow=grid',
5226 'info_dict': {
5227 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5228 'title': 'lex will - Videos',
5229 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5230 'uploader': 'lex will',
5231 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5232 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5233 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5234 'channel': 'lex will',
5235 'tags': ['bible', 'history', 'prophesy'],
5236 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5237 'channel_follower_count': int
5238 },
5239 'playlist_mincount': 199,
5240 }, {
5241 'note': 'Playlists tab',
5242 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/playlists',
5243 'info_dict': {
5244 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5245 'title': 'lex will - Playlists',
5246 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5247 'uploader': 'lex will',
5248 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5249 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5250 'channel': 'lex will',
5251 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5252 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5253 'tags': ['bible', 'history', 'prophesy'],
5254 'channel_follower_count': int
5255 },
5256 'playlist_mincount': 17,
5257 }, {
5258 'note': 'Community tab',
5259 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/community',
5260 'info_dict': {
5261 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5262 'title': 'lex will - Community',
5263 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5264 'uploader': 'lex will',
5265 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5266 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5267 'channel': 'lex will',
5268 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5269 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5270 'tags': ['bible', 'history', 'prophesy'],
5271 'channel_follower_count': int
5272 },
5273 'playlist_mincount': 18,
5274 }, {
5275 'note': 'Channels tab',
5276 'url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w/channels',
5277 'info_dict': {
5278 'id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5279 'title': 'lex will - Channels',
5280 'description': 'md5:2163c5d0ff54ed5f598d6a7e6211e488',
5281 'uploader': 'lex will',
5282 'uploader_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5283 'uploader_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5284 'channel': 'lex will',
5285 'channel_url': 'https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w',
5286 'channel_id': 'UCKfVa3S1e4PHvxWcwyMMg8w',
5287 'tags': ['bible', 'history', 'prophesy'],
5288 'channel_follower_count': int
5289 },
5290 'playlist_mincount': 12,
5291 }, {
5292 'note': 'Search tab',
5293 'url': 'https://www.youtube.com/c/3blue1brown/search?query=linear%20algebra',
5294 'playlist_mincount': 40,
5295 'info_dict': {
5296 'id': 'UCYO_jab_esuFRV4b17AJtAw',
5297 'title': '3Blue1Brown - Search - linear algebra',
5298 'description': 'md5:e1384e8a133307dd10edee76e875d62f',
5299 'uploader': '3Blue1Brown',
5300 'uploader_id': 'UCYO_jab_esuFRV4b17AJtAw',
5301 'channel_url': 'https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw',
5302 'uploader_url': 'https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw',
5303 'tags': ['Mathematics'],
5304 'channel': '3Blue1Brown',
5305 'channel_id': 'UCYO_jab_esuFRV4b17AJtAw',
5306 'channel_follower_count': int
5307 },
5308 }, {
5309 'url': 'https://invidio.us/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5310 'only_matching': True,
5311 }, {
5312 'url': 'https://www.youtubekids.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5313 'only_matching': True,
5314 }, {
5315 'url': 'https://music.youtube.com/channel/UCmlqkdCBesrv2Lak1mF_MxA',
5316 'only_matching': True,
5317 }, {
5318 'note': 'Playlist with deleted videos (#651). As a bonus, the video #51 is also twice in this list.',
5319 'url': 'https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
5320 'info_dict': {
5321 'title': '29C3: Not my department',
5322 'id': 'PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC',
5323 'uploader': 'Christiaan008',
5324 'uploader_id': 'UCEPzS1rYsrkqzSLNp76nrcg',
5325 'description': 'md5:a14dc1a8ef8307a9807fe136a0660268',
5326 'tags': [],
5327 'uploader_url': 'https://www.youtube.com/c/ChRiStIaAn008',
5328 'view_count': int,
5329 'modified_date': '20150605',
5330 'channel_id': 'UCEPzS1rYsrkqzSLNp76nrcg',
5331 'channel_url': 'https://www.youtube.com/c/ChRiStIaAn008',
5332 'channel': 'Christiaan008',
5333 'availability': 'public',
5334 },
5335 'playlist_count': 96,
5336 }, {
5337 'note': 'Large playlist',
5338 'url': 'https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q',
5339 'info_dict': {
5340 'title': 'Uploads from Cauchemar',
5341 'id': 'UUBABnxM4Ar9ten8Mdjj1j0Q',
5342 'uploader': 'Cauchemar',
5343 'uploader_id': 'UCBABnxM4Ar9ten8Mdjj1j0Q',
5344 'channel_url': 'https://www.youtube.com/c/Cauchemar89',
5345 'tags': [],
5346 'modified_date': r're:\d{8}',
5347 'channel': 'Cauchemar',
5348 'uploader_url': 'https://www.youtube.com/c/Cauchemar89',
5349 'view_count': int,
5350 'description': '',
5351 'channel_id': 'UCBABnxM4Ar9ten8Mdjj1j0Q',
5352 'availability': 'public',
5353 },
5354 'playlist_mincount': 1123,
5355 'expected_warnings': [r'[Uu]navailable videos (are|will be) hidden'],
5356 }, {
5357 'note': 'even larger playlist, 8832 videos',
5358 'url': 'http://www.youtube.com/user/NASAgovVideo/videos',
5359 'only_matching': True,
5360 }, {
5361 'note': 'Buggy playlist: the webpage has a "Load more" button but it doesn\'t have more videos',
5362 'url': 'https://www.youtube.com/playlist?list=UUXw-G3eDE9trcvY2sBMM_aA',
5363 'info_dict': {
5364 'title': 'Uploads from Interstellar Movie',
5365 'id': 'UUXw-G3eDE9trcvY2sBMM_aA',
5366 'uploader': 'Interstellar Movie',
5367 'uploader_id': 'UCXw-G3eDE9trcvY2sBMM_aA',
5368 'uploader_url': 'https://www.youtube.com/c/InterstellarMovie',
5369 'tags': [],
5370 'view_count': int,
5371 'channel_id': 'UCXw-G3eDE9trcvY2sBMM_aA',
5372 'channel_url': 'https://www.youtube.com/c/InterstellarMovie',
5373 'channel': 'Interstellar Movie',
5374 'description': '',
5375 'modified_date': r're:\d{8}',
5376 'availability': 'public',
5377 },
5378 'playlist_mincount': 21,
5379 }, {
5380 'note': 'Playlist with "show unavailable videos" button',
5381 'url': 'https://www.youtube.com/playlist?list=UUTYLiWFZy8xtPwxFwX9rV7Q',
5382 'info_dict': {
5383 'title': 'Uploads from Phim Siêu Nhân Nhật Bản',
5384 'id': 'UUTYLiWFZy8xtPwxFwX9rV7Q',
5385 'uploader': 'Phim Siêu Nhân Nhật Bản',
5386 'uploader_id': 'UCTYLiWFZy8xtPwxFwX9rV7Q',
5387 'view_count': int,
5388 'channel': 'Phim Siêu Nhân Nhật Bản',
5389 'tags': [],
5390 'uploader_url': 'https://www.youtube.com/channel/UCTYLiWFZy8xtPwxFwX9rV7Q',
5391 'description': '',
5392 'channel_url': 'https://www.youtube.com/channel/UCTYLiWFZy8xtPwxFwX9rV7Q',
5393 'channel_id': 'UCTYLiWFZy8xtPwxFwX9rV7Q',
5394 'modified_date': r're:\d{8}',
5395 'availability': 'public',
5396 },
5397 'playlist_mincount': 200,
5398 'expected_warnings': [r'[Uu]navailable videos (are|will be) hidden'],
5399 }, {
5400 'note': 'Playlist with unavailable videos in page 7',
5401 'url': 'https://www.youtube.com/playlist?list=UU8l9frL61Yl5KFOl87nIm2w',
5402 'info_dict': {
5403 'title': 'Uploads from BlankTV',
5404 'id': 'UU8l9frL61Yl5KFOl87nIm2w',
5405 'uploader': 'BlankTV',
5406 'uploader_id': 'UC8l9frL61Yl5KFOl87nIm2w',
5407 'channel': 'BlankTV',
5408 'channel_url': 'https://www.youtube.com/c/blanktv',
5409 'channel_id': 'UC8l9frL61Yl5KFOl87nIm2w',
5410 'view_count': int,
5411 'tags': [],
5412 'uploader_url': 'https://www.youtube.com/c/blanktv',
5413 'modified_date': r're:\d{8}',
5414 'description': '',
5415 'availability': 'public',
5416 },
5417 'playlist_mincount': 1000,
5418 'expected_warnings': [r'[Uu]navailable videos (are|will be) hidden'],
5419 }, {
5420 'note': 'https://github.com/ytdl-org/youtube-dl/issues/21844',
5421 'url': 'https://www.youtube.com/playlist?list=PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
5422 'info_dict': {
5423 'title': 'Data Analysis with Dr Mike Pound',
5424 'id': 'PLzH6n4zXuckpfMu_4Ff8E7Z1behQks5ba',
5425 'uploader_id': 'UC9-y-6csu5WGm29I7JiwpnA',
5426 'uploader': 'Computerphile',
5427 'description': 'md5:7f567c574d13d3f8c0954d9ffee4e487',
5428 'uploader_url': 'https://www.youtube.com/user/Computerphile',
5429 'tags': [],
5430 'view_count': int,
5431 'channel_id': 'UC9-y-6csu5WGm29I7JiwpnA',
5432 'channel_url': 'https://www.youtube.com/user/Computerphile',
5433 'channel': 'Computerphile',
5434 'availability': 'public',
5435 'modified_date': '20190712',
5436 },
5437 'playlist_mincount': 11,
5438 }, {
5439 'url': 'https://invidio.us/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc',
5440 'only_matching': True,
5441 }, {
5442 'note': 'Playlist URL that does not actually serve a playlist',
5443 'url': 'https://www.youtube.com/watch?v=FqZTN594JQw&list=PLMYEtVRpaqY00V9W81Cwmzp6N6vZqfUKD4',
5444 'info_dict': {
5445 'id': 'FqZTN594JQw',
5446 'ext': 'webm',
5447 'title': "Smiley's People 01 detective, Adventure Series, Action",
5448 'uploader': 'STREEM',
5449 'uploader_id': 'UCyPhqAZgwYWZfxElWVbVJng',
5450 'uploader_url': r're:https?://(?:www\.)?youtube\.com/channel/UCyPhqAZgwYWZfxElWVbVJng',
5451 'upload_date': '20150526',
5452 'license': 'Standard YouTube License',
5453 'description': 'md5:507cdcb5a49ac0da37a920ece610be80',
5454 'categories': ['People & Blogs'],
5455 'tags': list,
5456 'view_count': int,
5457 'like_count': int,
5458 },
5459 'params': {
5460 'skip_download': True,
5461 },
5462 'skip': 'This video is not available.',
5463 'add_ie': [YoutubeIE.ie_key()],
5464 }, {
5465 'url': 'https://www.youtubekids.com/watch?v=Agk7R8I8o5U&list=PUZ6jURNr1WQZCNHF0ao-c0g',
5466 'only_matching': True,
5467 }, {
5468 'url': 'https://www.youtube.com/watch?v=MuAGGZNfUkU&list=RDMM',
5469 'only_matching': True,
5470 }, {
5471 'url': 'https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live',
5472 'info_dict': {
5473 'id': 'Wq15eF5vCbI', # This will keep changing
5474 'ext': 'mp4',
5475 'title': str,
5476 'uploader': 'Sky News',
5477 'uploader_id': 'skynews',
5478 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/skynews',
5479 'upload_date': r're:\d{8}',
5480 'description': str,
5481 'categories': ['News & Politics'],
5482 'tags': list,
5483 'like_count': int,
5484 'release_timestamp': int,
5485 'channel': 'Sky News',
5486 'channel_id': 'UCoMdktPbSTixAyNGwb-UYkQ',
5487 'age_limit': 0,
5488 'view_count': int,
5489 'thumbnail': r're:https?://i\.ytimg\.com/vi/[^/]+/maxresdefault(?:_live)?\.jpg',
5490 'playable_in_embed': True,
5491 'release_date': r're:\d+',
5492 'availability': 'public',
5493 'live_status': 'is_live',
5494 'channel_url': 'https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ',
5495 'channel_follower_count': int,
5496 'concurrent_view_count': int,
5497 },
5498 'params': {
5499 'skip_download': True,
5500 },
5501 'expected_warnings': ['Ignoring subtitle tracks found in '],
5502 }, {
5503 'url': 'https://www.youtube.com/user/TheYoungTurks/live',
5504 'info_dict': {
5505 'id': 'a48o2S1cPoo',
5506 'ext': 'mp4',
5507 'title': 'The Young Turks - Live Main Show',
5508 'uploader': 'The Young Turks',
5509 'uploader_id': 'TheYoungTurks',
5510 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/TheYoungTurks',
5511 'upload_date': '20150715',
5512 'license': 'Standard YouTube License',
5513 'description': 'md5:438179573adcdff3c97ebb1ee632b891',
5514 'categories': ['News & Politics'],
5515 'tags': ['Cenk Uygur (TV Program Creator)', 'The Young Turks (Award-Winning Work)', 'Talk Show (TV Genre)'],
5516 'like_count': int,
5517 },
5518 'params': {
5519 'skip_download': True,
5520 },
5521 'only_matching': True,
5522 }, {
5523 'url': 'https://www.youtube.com/channel/UC1yBKRuGpC1tSM73A0ZjYjQ/live',
5524 'only_matching': True,
5525 }, {
5526 'url': 'https://www.youtube.com/c/CommanderVideoHq/live',
5527 'only_matching': True,
5528 }, {
5529 'note': 'A channel that is not live. Should raise error',
5530 'url': 'https://www.youtube.com/user/numberphile/live',
5531 'only_matching': True,
5532 }, {
5533 'url': 'https://www.youtube.com/feed/trending',
5534 'only_matching': True,
5535 }, {
5536 'url': 'https://www.youtube.com/feed/library',
5537 'only_matching': True,
5538 }, {
5539 'url': 'https://www.youtube.com/feed/history',
5540 'only_matching': True,
5541 }, {
5542 'url': 'https://www.youtube.com/feed/subscriptions',
5543 'only_matching': True,
5544 }, {
5545 'url': 'https://www.youtube.com/feed/watch_later',
5546 'only_matching': True,
5547 }, {
5548 'note': 'Recommended - redirects to home page.',
5549 'url': 'https://www.youtube.com/feed/recommended',
5550 'only_matching': True,
5551 }, {
5552 'note': 'inline playlist with not always working continuations',
5553 'url': 'https://www.youtube.com/watch?v=UC6u0Tct-Fo&list=PL36D642111D65BE7C',
5554 'only_matching': True,
5555 }, {
5556 'url': 'https://www.youtube.com/course',
5557 'only_matching': True,
5558 }, {
5559 'url': 'https://www.youtube.com/zsecurity',
5560 'only_matching': True,
5561 }, {
5562 'url': 'http://www.youtube.com/NASAgovVideo/videos',
5563 'only_matching': True,
5564 }, {
5565 'url': 'https://www.youtube.com/TheYoungTurks/live',
5566 'only_matching': True,
5567 }, {
5568 'url': 'https://www.youtube.com/hashtag/cctv9',
5569 'info_dict': {
5570 'id': 'cctv9',
5571 'title': '#cctv9',
5572 'tags': [],
5573 },
5574 'playlist_mincount': 300, # not consistent but should be over 300
5575 }, {
5576 'url': 'https://www.youtube.com/watch?list=PLW4dVinRY435CBE_JD3t-0SRXKfnZHS1P&feature=youtu.be&v=M9cJMXmQ_ZU',
5577 'only_matching': True,
5578 }, {
5579 'note': 'Requires Premium: should request additional YTM-info webpage (and have format 141) for videos in playlist',
5580 'url': 'https://music.youtube.com/playlist?list=PLRBp0Fe2GpgmgoscNFLxNyBVSFVdYmFkq',
5581 'only_matching': True
5582 }, {
5583 'note': '/browse/ should redirect to /channel/',
5584 'url': 'https://music.youtube.com/browse/UC1a8OFewdjuLq6KlF8M_8Ng',
5585 'only_matching': True
5586 }, {
5587 'note': 'VLPL, should redirect to playlist?list=PL...',
5588 'url': 'https://music.youtube.com/browse/VLPLRBp0Fe2GpgmgoscNFLxNyBVSFVdYmFkq',
5589 'info_dict': {
5590 'id': 'PLRBp0Fe2GpgmgoscNFLxNyBVSFVdYmFkq',
5591 'uploader': 'NoCopyrightSounds',
5592 'description': 'Providing you with copyright free / safe music for gaming, live streaming, studying and more!',
5593 'uploader_id': 'UC_aEa8K-EOJ3D6gOs7HcyNg',
5594 'title': 'NCS : All Releases 💿',
5595 'uploader_url': 'https://www.youtube.com/c/NoCopyrightSounds',
5596 'channel_url': 'https://www.youtube.com/c/NoCopyrightSounds',
5597 'modified_date': r're:\d{8}',
5598 'view_count': int,
5599 'channel_id': 'UC_aEa8K-EOJ3D6gOs7HcyNg',
5600 'tags': [],
5601 'channel': 'NoCopyrightSounds',
5602 'availability': 'public',
5603 },
5604 'playlist_mincount': 166,
5605 'expected_warnings': [r'[Uu]navailable videos (are|will be) hidden'],
5606 }, {
5607 'note': 'Topic, should redirect to playlist?list=UU...',
5608 'url': 'https://music.youtube.com/browse/UC9ALqqC4aIeG5iDs7i90Bfw',
5609 'info_dict': {
5610 'id': 'UU9ALqqC4aIeG5iDs7i90Bfw',
5611 'uploader_id': 'UC9ALqqC4aIeG5iDs7i90Bfw',
5612 'title': 'Uploads from Royalty Free Music - Topic',
5613 'uploader': 'Royalty Free Music - Topic',
5614 'tags': [],
5615 'channel_id': 'UC9ALqqC4aIeG5iDs7i90Bfw',
5616 'channel': 'Royalty Free Music - Topic',
5617 'view_count': int,
5618 'channel_url': 'https://www.youtube.com/channel/UC9ALqqC4aIeG5iDs7i90Bfw',
5619 'channel_url': 'https://www.youtube.com/channel/UC9ALqqC4aIeG5iDs7i90Bfw',
5620 'modified_date': r're:\d{8}',
5621 'uploader_url': 'https://www.youtube.com/channel/UC9ALqqC4aIeG5iDs7i90Bfw',
5622 'description': '',
5623 'availability': 'public',
5624 },
5625 'playlist_mincount': 101,
5626 }, {
5627 # Destination channel with only a hidden self tab (tab id is UCtFRv9O2AHqOZjjynzrv-xg)
5628 # Treat as a general feed
5629 'url': 'https://www.youtube.com/channel/UCtFRv9O2AHqOZjjynzrv-xg',
5630 'info_dict': {
5631 'id': 'UCtFRv9O2AHqOZjjynzrv-xg',
5632 'title': 'UCtFRv9O2AHqOZjjynzrv-xg',
5633 'tags': [],
5634 },
5635 'playlist_mincount': 9,
5636 }, {
5637 'note': 'Youtube music Album',
5638 'url': 'https://music.youtube.com/browse/MPREb_gTAcphH99wE',
5639 'info_dict': {
5640 'id': 'OLAK5uy_l1m0thk3g31NmIIz_vMIbWtyv7eZixlH0',
5641 'title': 'Album - Royalty Free Music Library V2 (50 Songs)',
5642 'tags': [],
5643 'view_count': int,
5644 'description': '',
5645 'availability': 'unlisted',
5646 'modified_date': r're:\d{8}',
5647 },
5648 'playlist_count': 50,
5649 }, {
5650 'note': 'unlisted single video playlist',
5651 'url': 'https://www.youtube.com/playlist?list=PLwL24UFy54GrB3s2KMMfjZscDi1x5Dajf',
5652 'info_dict': {
5653 'uploader_id': 'UC9zHu_mHU96r19o-wV5Qs1Q',
5654 'uploader': 'colethedj',
5655 'id': 'PLwL24UFy54GrB3s2KMMfjZscDi1x5Dajf',
5656 'title': 'yt-dlp unlisted playlist test',
5657 'availability': 'unlisted',
5658 'tags': [],
5659 'modified_date': '20220418',
5660 'channel': 'colethedj',
5661 'view_count': int,
5662 'description': '',
5663 'uploader_url': 'https://www.youtube.com/channel/UC9zHu_mHU96r19o-wV5Qs1Q',
5664 'channel_id': 'UC9zHu_mHU96r19o-wV5Qs1Q',
5665 'channel_url': 'https://www.youtube.com/channel/UC9zHu_mHU96r19o-wV5Qs1Q',
5666 },
5667 'playlist_count': 1,
5668 }, {
5669 'note': 'API Fallback: Recommended - redirects to home page. Requires visitorData',
5670 'url': 'https://www.youtube.com/feed/recommended',
5671 'info_dict': {
5672 'id': 'recommended',
5673 'title': 'recommended',
5674 'tags': [],
5675 },
5676 'playlist_mincount': 50,
5677 'params': {
5678 'skip_download': True,
5679 'extractor_args': {'youtubetab': {'skip': ['webpage']}}
5680 },
5681 }, {
5682 'note': 'API Fallback: /videos tab, sorted by oldest first',
5683 'url': 'https://www.youtube.com/user/theCodyReeder/videos?view=0&sort=da&flow=grid',
5684 'info_dict': {
5685 'id': 'UCu6mSoMNzHQiBIOCkHUa2Aw',
5686 'title': 'Cody\'sLab - Videos',
5687 'description': 'md5:d083b7c2f0c67ee7a6c74c3e9b4243fa',
5688 'uploader': 'Cody\'sLab',
5689 'uploader_id': 'UCu6mSoMNzHQiBIOCkHUa2Aw',
5690 'channel': 'Cody\'sLab',
5691 'channel_id': 'UCu6mSoMNzHQiBIOCkHUa2Aw',
5692 'tags': [],
5693 'channel_url': 'https://www.youtube.com/channel/UCu6mSoMNzHQiBIOCkHUa2Aw',
5694 'uploader_url': 'https://www.youtube.com/channel/UCu6mSoMNzHQiBIOCkHUa2Aw',
5695 'channel_follower_count': int
5696 },
5697 'playlist_mincount': 650,
5698 'params': {
5699 'skip_download': True,
5700 'extractor_args': {'youtubetab': {'skip': ['webpage']}}
5701 },
5702 'skip': 'Query for sorting no longer works',
5703 }, {
5704 'note': 'API Fallback: Topic, should redirect to playlist?list=UU...',
5705 'url': 'https://music.youtube.com/browse/UC9ALqqC4aIeG5iDs7i90Bfw',
5706 'info_dict': {
5707 'id': 'UU9ALqqC4aIeG5iDs7i90Bfw',
5708 'uploader_id': 'UC9ALqqC4aIeG5iDs7i90Bfw',
5709 'title': 'Uploads from Royalty Free Music - Topic',
5710 'uploader': 'Royalty Free Music - Topic',
5711 'modified_date': r're:\d{8}',
5712 'channel_id': 'UC9ALqqC4aIeG5iDs7i90Bfw',
5713 'description': '',
5714 'channel_url': 'https://www.youtube.com/channel/UC9ALqqC4aIeG5iDs7i90Bfw',
5715 'tags': [],
5716 'channel': 'Royalty Free Music - Topic',
5717 'view_count': int,
5718 'uploader_url': 'https://www.youtube.com/channel/UC9ALqqC4aIeG5iDs7i90Bfw',
5719 'availability': 'public',
5720 },
5721 'playlist_mincount': 101,
5722 'params': {
5723 'skip_download': True,
5724 'extractor_args': {'youtubetab': {'skip': ['webpage']}}
5725 },
5726 }, {
5727 'note': 'non-standard redirect to regional channel',
5728 'url': 'https://www.youtube.com/channel/UCwVVpHQ2Cs9iGJfpdFngePQ',
5729 'only_matching': True
5730 }, {
5731 'note': 'collaborative playlist (uploader name in the form "by <uploader> and x other(s)")',
5732 'url': 'https://www.youtube.com/playlist?list=PLx-_-Kk4c89oOHEDQAojOXzEzemXxoqx6',
5733 'info_dict': {
5734 'id': 'PLx-_-Kk4c89oOHEDQAojOXzEzemXxoqx6',
5735 'modified_date': '20220407',
5736 'channel_url': 'https://www.youtube.com/channel/UCKcqXmCcyqnhgpA5P0oHH_Q',
5737 'tags': [],
5738 'uploader_id': 'UCKcqXmCcyqnhgpA5P0oHH_Q',
5739 'uploader': 'pukkandan',
5740 'availability': 'unlisted',
5741 'channel_id': 'UCKcqXmCcyqnhgpA5P0oHH_Q',
5742 'channel': 'pukkandan',
5743 'description': 'Test for collaborative playlist',
5744 'title': 'yt-dlp test - collaborative playlist',
5745 'view_count': int,
5746 'uploader_url': 'https://www.youtube.com/channel/UCKcqXmCcyqnhgpA5P0oHH_Q',
5747 },
5748 'playlist_mincount': 2
5749 }, {
5750 'note': 'translated tab name',
5751 'url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA/playlists',
5752 'info_dict': {
5753 'id': 'UCiu-3thuViMebBjw_5nWYrA',
5754 'tags': [],
5755 'uploader_id': 'UCiu-3thuViMebBjw_5nWYrA',
5756 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5757 'description': 'test description',
5758 'title': 'cole-dlp-test-acc - 再生リスト',
5759 'uploader_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5760 'uploader': 'cole-dlp-test-acc',
5761 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
5762 'channel': 'cole-dlp-test-acc',
5763 'channel_follower_count': int,
5764 },
5765 'playlist_mincount': 1,
5766 'params': {'extractor_args': {'youtube': {'lang': ['ja']}}},
5767 'expected_warnings': ['Preferring "ja"'],
5768 }, {
5769 # XXX: this should really check flat playlist entries, but the test suite doesn't support that
5770 'note': 'preferred lang set with playlist with translated video titles',
5771 'url': 'https://www.youtube.com/playlist?list=PLt5yu3-wZAlQAaPZ5Z-rJoTdbT-45Q7c0',
5772 'info_dict': {
5773 'id': 'PLt5yu3-wZAlQAaPZ5Z-rJoTdbT-45Q7c0',
5774 'tags': [],
5775 'view_count': int,
5776 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5777 'uploader': 'cole-dlp-test-acc',
5778 'uploader_id': 'UCiu-3thuViMebBjw_5nWYrA',
5779 'channel': 'cole-dlp-test-acc',
5780 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
5781 'description': 'test',
5782 'uploader_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5783 'title': 'dlp test playlist',
5784 'availability': 'public',
5785 },
5786 'playlist_mincount': 1,
5787 'params': {'extractor_args': {'youtube': {'lang': ['ja']}}},
5788 'expected_warnings': ['Preferring "ja"'],
5789 }, {
5790 # shorts audio pivot for 2GtVksBMYFM.
5791 'url': 'https://www.youtube.com/feed/sfv_audio_pivot?bp=8gUrCikSJwoLMkd0VmtzQk1ZRk0SCzJHdFZrc0JNWUZNGgsyR3RWa3NCTVlGTQ==',
5792 'info_dict': {
5793 'id': 'sfv_audio_pivot',
5794 'title': 'sfv_audio_pivot',
5795 'tags': [],
5796 },
5797 'playlist_mincount': 50,
5798
5799 }, {
5800 # Channel with a real live tab (not to be mistaken with streams tab)
5801 # Do not treat like it should redirect to live stream
5802 'url': 'https://www.youtube.com/channel/UCEH7P7kyJIkS_gJf93VYbmg/live',
5803 'info_dict': {
5804 'id': 'UCEH7P7kyJIkS_gJf93VYbmg',
5805 'title': 'UCEH7P7kyJIkS_gJf93VYbmg - Live',
5806 'tags': [],
5807 },
5808 'playlist_mincount': 20,
5809 }, {
5810 # Tab name is not the same as tab id
5811 'url': 'https://www.youtube.com/channel/UCQvWX73GQygcwXOTSf_VDVg/letsplay',
5812 'info_dict': {
5813 'id': 'UCQvWX73GQygcwXOTSf_VDVg',
5814 'title': 'UCQvWX73GQygcwXOTSf_VDVg - Let\'s play',
5815 'tags': [],
5816 },
5817 'playlist_mincount': 8,
5818 }, {
5819 # Home tab id is literally home. Not to get mistaken with featured
5820 'url': 'https://www.youtube.com/channel/UCQvWX73GQygcwXOTSf_VDVg/home',
5821 'info_dict': {
5822 'id': 'UCQvWX73GQygcwXOTSf_VDVg',
5823 'title': 'UCQvWX73GQygcwXOTSf_VDVg - Home',
5824 'tags': [],
5825 },
5826 'playlist_mincount': 8,
5827 }, {
5828 # Should get three playlists for videos, shorts and streams tabs
5829 'url': 'https://www.youtube.com/channel/UCK9V2B22uJYu3N7eR_BT9QA',
5830 'info_dict': {
5831 'id': 'UCK9V2B22uJYu3N7eR_BT9QA',
5832 'title': 'Polka Ch. 尾丸ポルカ',
5833 'channel_follower_count': int,
5834 'channel_id': 'UCK9V2B22uJYu3N7eR_BT9QA',
5835 'channel_url': 'https://www.youtube.com/channel/UCK9V2B22uJYu3N7eR_BT9QA',
5836 'uploader': 'Polka Ch. 尾丸ポルカ',
5837 'description': 'md5:3b8df1ac5af337aa206e37ee3d181ec9',
5838 'channel': 'Polka Ch. 尾丸ポルカ',
5839 'tags': 'count:35',
5840 'uploader_url': 'https://www.youtube.com/channel/UCK9V2B22uJYu3N7eR_BT9QA',
5841 'uploader_id': 'UCK9V2B22uJYu3N7eR_BT9QA',
5842 },
5843 'playlist_count': 3,
5844 }, {
5845 # Shorts tab with channel with handle
5846 'url': 'https://www.youtube.com/@NotJustBikes/shorts',
5847 'info_dict': {
5848 'id': 'UC0intLFzLaudFG-xAvUEO-A',
5849 'title': 'Not Just Bikes - Shorts',
5850 'tags': 'count:12',
5851 'uploader': 'Not Just Bikes',
5852 'channel_url': 'https://www.youtube.com/channel/UC0intLFzLaudFG-xAvUEO-A',
5853 'description': 'md5:7513148b1f02b924783157d84c4ea555',
5854 'channel_follower_count': int,
5855 'uploader_id': 'UC0intLFzLaudFG-xAvUEO-A',
5856 'channel_id': 'UC0intLFzLaudFG-xAvUEO-A',
5857 'uploader_url': 'https://www.youtube.com/channel/UC0intLFzLaudFG-xAvUEO-A',
5858 'channel': 'Not Just Bikes',
5859 },
5860 'playlist_mincount': 10,
5861 }, {
5862 # Streams tab
5863 'url': 'https://www.youtube.com/channel/UC3eYAvjCVwNHgkaGbXX3sig/streams',
5864 'info_dict': {
5865 'id': 'UC3eYAvjCVwNHgkaGbXX3sig',
5866 'title': '中村悠一 - Live',
5867 'tags': 'count:7',
5868 'channel_id': 'UC3eYAvjCVwNHgkaGbXX3sig',
5869 'channel_url': 'https://www.youtube.com/channel/UC3eYAvjCVwNHgkaGbXX3sig',
5870 'uploader_id': 'UC3eYAvjCVwNHgkaGbXX3sig',
5871 'channel': '中村悠一',
5872 'uploader_url': 'https://www.youtube.com/channel/UC3eYAvjCVwNHgkaGbXX3sig',
5873 'channel_follower_count': int,
5874 'uploader': '中村悠一',
5875 'description': 'md5:e744f6c93dafa7a03c0c6deecb157300',
5876 },
5877 'playlist_mincount': 60,
5878 }, {
5879 # Channel with no uploads and hence no videos, streams, shorts tabs or uploads playlist. This should fail.
5880 # See test_youtube_lists
5881 'url': 'https://www.youtube.com/channel/UC2yXPzFejc422buOIzn_0CA',
5882 'only_matching': True,
5883 }, {
5884 # No uploads and no UCID given. Should fail with no uploads error
5885 # See test_youtube_lists
5886 'url': 'https://www.youtube.com/news',
5887 'only_matching': True
5888 }, {
5889 # No videos tab but has a shorts tab
5890 'url': 'https://www.youtube.com/c/TKFShorts',
5891 'info_dict': {
5892 'id': 'UCgJ5_1F6yJhYLnyMszUdmUg',
5893 'title': 'Shorts Break - Shorts',
5894 'tags': 'count:32',
5895 'channel_id': 'UCgJ5_1F6yJhYLnyMszUdmUg',
5896 'channel': 'Shorts Break',
5897 'description': 'md5:a6c234cf3d50d878ef8721e34457cd11',
5898 'uploader': 'Shorts Break',
5899 'channel_follower_count': int,
5900 'uploader_id': 'UCgJ5_1F6yJhYLnyMszUdmUg',
5901 'uploader_url': 'https://www.youtube.com/channel/UCgJ5_1F6yJhYLnyMszUdmUg',
5902 'channel_url': 'https://www.youtube.com/channel/UCgJ5_1F6yJhYLnyMszUdmUg',
5903 },
5904 'playlist_mincount': 30,
5905 }, {
5906 # Trending Now Tab. tab id is empty
5907 'url': 'https://www.youtube.com/feed/trending',
5908 'info_dict': {
5909 'id': 'trending',
5910 'title': 'trending - Now',
5911 'tags': [],
5912 },
5913 'playlist_mincount': 30,
5914 }, {
5915 # Trending Gaming Tab. tab id is empty
5916 'url': 'https://www.youtube.com/feed/trending?bp=4gIcGhpnYW1pbmdfY29ycHVzX21vc3RfcG9wdWxhcg%3D%3D',
5917 'info_dict': {
5918 'id': 'trending',
5919 'title': 'trending - Gaming',
5920 'tags': [],
5921 },
5922 'playlist_mincount': 30,
5923 }, {
5924 # Shorts url result in shorts tab
5925 'url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA/shorts',
5926 'info_dict': {
5927 'id': 'UCiu-3thuViMebBjw_5nWYrA',
5928 'title': 'cole-dlp-test-acc - Shorts',
5929 'uploader_id': 'UCiu-3thuViMebBjw_5nWYrA',
5930 'channel': 'cole-dlp-test-acc',
5931 'channel_follower_count': int,
5932 'description': 'test description',
5933 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
5934 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5935 'tags': [],
5936 'uploader': 'cole-dlp-test-acc',
5937 'uploader_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5938
5939 },
5940 'playlist': [{
5941 'info_dict': {
5942 '_type': 'url',
5943 'ie_key': 'Youtube',
5944 'url': 'https://www.youtube.com/shorts/sSM9J5YH_60',
5945 'id': 'sSM9J5YH_60',
5946 'channel_id': 'UCiu-3thuViMebBjw_5nWYrA',
5947 'title': 'SHORT short',
5948 'channel': 'cole-dlp-test-acc',
5949 'channel_url': 'https://www.youtube.com/channel/UCiu-3thuViMebBjw_5nWYrA',
5950 'view_count': int,
5951 'thumbnails': list,
5952 }
5953 }],
5954 'params': {'extract_flat': True},
5955 }, {
5956 # Live video status should be extracted
5957 'url': 'https://www.youtube.com/channel/UCQvWX73GQygcwXOTSf_VDVg/live',
5958 'info_dict': {
5959 'id': 'UCQvWX73GQygcwXOTSf_VDVg',
5960 'title': 'UCQvWX73GQygcwXOTSf_VDVg - Live', # TODO, should be Minecraft - Live or Minecraft - Topic - Live
5961 'tags': []
5962 },
5963 'playlist': [{
5964 'info_dict': {
5965 '_type': 'url',
5966 'ie_key': 'Youtube',
5967 'url': 'startswith:https://www.youtube.com/watch?v=',
5968 'id': str,
5969 'title': str,
5970 'live_status': 'is_live',
5971 'channel_id': str,
5972 'channel_url': str,
5973 'concurrent_view_count': int,
5974 'channel': str,
5975 }
5976 }],
5977 'params': {'extract_flat': True},
5978 'playlist_mincount': 1
5979 }]
5980
5981 @classmethod
5982 def suitable(cls, url):
5983 return False if YoutubeIE.suitable(url) else super().suitable(url)
5984
5985 _URL_RE = re.compile(rf'(?P<pre>{_VALID_URL})(?(not_channel)|(?P<tab>/[^?#/]+))?(?P<post>.*)$')
5986
5987 def _get_url_mobj(self, url):
5988 mobj = self._URL_RE.match(url).groupdict()
5989 mobj.update((k, '') for k, v in mobj.items() if v is None)
5990 return mobj
5991
5992 def _extract_tab_id_and_name(self, tab, base_url='https://www.youtube.com'):
5993 tab_name = (tab.get('title') or '').lower()
5994 tab_url = urljoin(base_url, traverse_obj(
5995 tab, ('endpoint', 'commandMetadata', 'webCommandMetadata', 'url')))
5996
5997 tab_id = (tab_url and self._get_url_mobj(tab_url)['tab'][1:]
5998 or traverse_obj(tab, 'tabIdentifier', expected_type=str))
5999 if tab_id:
6000 return {
6001 'TAB_ID_SPONSORSHIPS': 'membership',
6002 }.get(tab_id, tab_id), tab_name
6003
6004 # Fallback to tab name if we cannot get the tab id.
6005 # XXX: should we strip non-ascii letters? e.g. in case of 'let's play' tab example on special gaming channel
6006 # Note that in the case of translated tab name this may result in an empty string, which we don't want.
6007 if tab_name:
6008 self.write_debug(f'Falling back to selected tab name: {tab_name}')
6009 return {
6010 'home': 'featured',
6011 'live': 'streams',
6012 }.get(tab_name, tab_name), tab_name
6013
6014 def _has_tab(self, tabs, tab_id):
6015 return any(self._extract_tab_id_and_name(tab)[0] == tab_id for tab in tabs)
6016
6017 @YoutubeTabBaseInfoExtractor.passthrough_smuggled_data
6018 def _real_extract(self, url, smuggled_data):
6019 item_id = self._match_id(url)
6020 url = urllib.parse.urlunparse(
6021 urllib.parse.urlparse(url)._replace(netloc='www.youtube.com'))
6022 compat_opts = self.get_param('compat_opts', [])
6023
6024 mobj = self._get_url_mobj(url)
6025 pre, tab, post, is_channel = mobj['pre'], mobj['tab'], mobj['post'], not mobj['not_channel']
6026 if is_channel and smuggled_data.get('is_music_url'):
6027 if item_id[:2] == 'VL': # Youtube music VL channels have an equivalent playlist
6028 return self.url_result(
6029 f'https://music.youtube.com/playlist?list={item_id[2:]}', YoutubeTabIE, item_id[2:])
6030 elif item_id[:2] == 'MP': # Resolve albums (/[channel/browse]/MP...) to their equivalent playlist
6031 mdata = self._extract_tab_endpoint(
6032 f'https://music.youtube.com/channel/{item_id}', item_id, default_client='web_music')
6033 murl = traverse_obj(mdata, ('microformat', 'microformatDataRenderer', 'urlCanonical'),
6034 get_all=False, expected_type=str)
6035 if not murl:
6036 raise ExtractorError('Failed to resolve album to playlist')
6037 return self.url_result(murl, YoutubeTabIE)
6038 elif mobj['channel_type'] == 'browse': # Youtube music /browse/ should be changed to /channel/
6039 return self.url_result(
6040 f'https://music.youtube.com/channel/{item_id}{tab}{post}', YoutubeTabIE, item_id)
6041
6042 original_tab_id, display_id = tab[1:], f'{item_id}{tab}'
6043 if is_channel and not tab and 'no-youtube-channel-redirect' not in compat_opts:
6044 url = f'{pre}/videos{post}'
6045
6046 # Handle both video/playlist URLs
6047 qs = parse_qs(url)
6048 video_id, playlist_id = [traverse_obj(qs, (key, 0)) for key in ('v', 'list')]
6049 if not video_id and mobj['not_channel'].startswith('watch'):
6050 if not playlist_id:
6051 # If there is neither video or playlist ids, youtube redirects to home page, which is undesirable
6052 raise ExtractorError('A video URL was given without video ID', expected=True)
6053 # Common mistake: https://www.youtube.com/watch?list=playlist_id
6054 self.report_warning(f'A video URL was given without video ID. Trying to download playlist {playlist_id}')
6055 return self.url_result(
6056 f'https://www.youtube.com/playlist?list={playlist_id}', YoutubeTabIE, playlist_id)
6057
6058 if not self._yes_playlist(playlist_id, video_id):
6059 return self.url_result(
6060 f'https://www.youtube.com/watch?v={video_id}', YoutubeIE, video_id)
6061
6062 data, ytcfg = self._extract_data(url, display_id)
6063
6064 # YouTube may provide a non-standard redirect to the regional channel
6065 # See: https://github.com/yt-dlp/yt-dlp/issues/2694
6066 # https://support.google.com/youtube/answer/2976814#zippy=,conditional-redirects
6067 redirect_url = traverse_obj(
6068 data, ('onResponseReceivedActions', ..., 'navigateAction', 'endpoint', 'commandMetadata', 'webCommandMetadata', 'url'), get_all=False)
6069 if redirect_url and 'no-youtube-channel-redirect' not in compat_opts:
6070 redirect_url = ''.join((urljoin('https://www.youtube.com', redirect_url), tab, post))
6071 self.to_screen(f'This playlist is likely not available in your region. Following conditional redirect to {redirect_url}')
6072 return self.url_result(redirect_url, YoutubeTabIE)
6073
6074 tabs, extra_tabs = self._extract_tab_renderers(data), []
6075 if is_channel and tabs and 'no-youtube-channel-redirect' not in compat_opts:
6076 selected_tab = self._extract_selected_tab(tabs)
6077 selected_tab_id, selected_tab_name = self._extract_tab_id_and_name(selected_tab, url) # NB: Name may be translated
6078 self.write_debug(f'Selected tab: {selected_tab_id!r} ({selected_tab_name}), Requested tab: {original_tab_id!r}')
6079
6080 if not original_tab_id and selected_tab_name:
6081 self.to_screen('Downloading all uploads of the channel. '
6082 'To download only the videos in a specific tab, pass the tab\'s URL')
6083 if self._has_tab(tabs, 'streams'):
6084 extra_tabs.append(''.join((pre, '/streams', post)))
6085 if self._has_tab(tabs, 'shorts'):
6086 extra_tabs.append(''.join((pre, '/shorts', post)))
6087 # XXX: Members-only tab should also be extracted
6088
6089 if not extra_tabs and selected_tab_id != 'videos':
6090 # Channel does not have streams, shorts or videos tabs
6091 if item_id[:2] != 'UC':
6092 raise ExtractorError('This channel has no uploads', expected=True)
6093
6094 # Topic channels don't have /videos. Use the equivalent playlist instead
6095 pl_id = f'UU{item_id[2:]}'
6096 pl_url = f'https://www.youtube.com/playlist?list={pl_id}'
6097 try:
6098 data, ytcfg = self._extract_data(pl_url, pl_id, ytcfg=ytcfg, fatal=True, webpage_fatal=True)
6099 except ExtractorError:
6100 raise ExtractorError('This channel has no uploads', expected=True)
6101 else:
6102 item_id, url = pl_id, pl_url
6103 self.to_screen(
6104 f'The channel does not have a videos, shorts, or live tab. Redirecting to playlist {pl_id} instead')
6105
6106 elif extra_tabs and selected_tab_id != 'videos':
6107 # When there are shorts/live tabs but not videos tab
6108 url, data = f'{pre}{post}', None
6109
6110 elif (original_tab_id or 'videos') != selected_tab_id:
6111 if original_tab_id == 'live':
6112 # Live tab should have redirected to the video
6113 # Except in the case the channel has an actual live tab
6114 # Example: https://www.youtube.com/channel/UCEH7P7kyJIkS_gJf93VYbmg/live
6115 raise UserNotLive(video_id=item_id)
6116 elif selected_tab_name:
6117 raise ExtractorError(f'This channel does not have a {original_tab_id} tab', expected=True)
6118
6119 # For channels such as https://www.youtube.com/channel/UCtFRv9O2AHqOZjjynzrv-xg
6120 url = f'{pre}{post}'
6121
6122 # YouTube sometimes provides a button to reload playlist with unavailable videos.
6123 if 'no-youtube-unavailable-videos' not in compat_opts:
6124 data = self._reload_with_unavailable_videos(display_id, data, ytcfg) or data
6125 self._extract_and_report_alerts(data, only_once=True)
6126
6127 tabs, entries = self._extract_tab_renderers(data), []
6128 if tabs:
6129 entries = [self._extract_from_tabs(item_id, ytcfg, data, tabs)]
6130 entries[0].update({
6131 'extractor_key': YoutubeTabIE.ie_key(),
6132 'extractor': YoutubeTabIE.IE_NAME,
6133 'webpage_url': url,
6134 })
6135 if self.get_param('playlist_items') == '0':
6136 entries.extend(self.url_result(u, YoutubeTabIE) for u in extra_tabs)
6137 else: # Users expect to get all `video_id`s even with `--flat-playlist`. So don't return `url_result`
6138 entries.extend(map(self._real_extract, extra_tabs))
6139
6140 if len(entries) == 1:
6141 return entries[0]
6142 elif entries:
6143 metadata = self._extract_metadata_from_tabs(item_id, data)
6144 uploads_url = 'the Uploads (UU) playlist URL'
6145 if try_get(metadata, lambda x: x['channel_id'].startswith('UC')):
6146 uploads_url = f'https://www.youtube.com/playlist?list=UU{metadata["channel_id"][2:]}'
6147 self.to_screen(
6148 'Downloading as multiple playlists, separated by tabs. '
6149 f'To download as a single playlist instead, pass {uploads_url}')
6150 return self.playlist_result(entries, item_id, **metadata)
6151
6152 # Inline playlist
6153 playlist = traverse_obj(
6154 data, ('contents', 'twoColumnWatchNextResults', 'playlist', 'playlist'), expected_type=dict)
6155 if playlist:
6156 return self._extract_from_playlist(item_id, url, data, playlist, ytcfg)
6157
6158 video_id = traverse_obj(
6159 data, ('currentVideoEndpoint', 'watchEndpoint', 'videoId'), expected_type=str) or video_id
6160 if video_id:
6161 if tab != '/live': # live tab is expected to redirect to video
6162 self.report_warning(f'Unable to recognize playlist. Downloading just video {video_id}')
6163 return self.url_result(f'https://www.youtube.com/watch?v={video_id}', YoutubeIE, video_id)
6164
6165 raise ExtractorError('Unable to recognize tab page')
6166
6167
6168 class YoutubePlaylistIE(InfoExtractor):
6169 IE_DESC = 'YouTube playlists'
6170 _VALID_URL = r'''(?x)(?:
6171 (?:https?://)?
6172 (?:\w+\.)?
6173 (?:
6174 (?:
6175 youtube(?:kids)?\.com|
6176 %(invidious)s
6177 )
6178 /.*?\?.*?\blist=
6179 )?
6180 (?P<id>%(playlist_id)s)
6181 )''' % {
6182 'playlist_id': YoutubeBaseInfoExtractor._PLAYLIST_ID_RE,
6183 'invidious': '|'.join(YoutubeBaseInfoExtractor._INVIDIOUS_SITES),
6184 }
6185 IE_NAME = 'youtube:playlist'
6186 _TESTS = [{
6187 'note': 'issue #673',
6188 'url': 'PLBB231211A4F62143',
6189 'info_dict': {
6190 'title': '[OLD]Team Fortress 2 (Class-based LP)',
6191 'id': 'PLBB231211A4F62143',
6192 'uploader': 'Wickman',
6193 'uploader_id': 'UCKSpbfbl5kRQpTdL7kMc-1Q',
6194 'description': 'md5:8fa6f52abb47a9552002fa3ddfc57fc2',
6195 'view_count': int,
6196 'uploader_url': 'https://www.youtube.com/c/WickmanVT',
6197 'modified_date': r're:\d{8}',
6198 'channel_id': 'UCKSpbfbl5kRQpTdL7kMc-1Q',
6199 'channel': 'Wickman',
6200 'tags': [],
6201 'channel_url': 'https://www.youtube.com/c/WickmanVT',
6202 'availability': 'public',
6203 },
6204 'playlist_mincount': 29,
6205 }, {
6206 'url': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
6207 'info_dict': {
6208 'title': 'YDL_safe_search',
6209 'id': 'PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl',
6210 },
6211 'playlist_count': 2,
6212 'skip': 'This playlist is private',
6213 }, {
6214 'note': 'embedded',
6215 'url': 'https://www.youtube.com/embed/videoseries?list=PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
6216 'playlist_count': 4,
6217 'info_dict': {
6218 'title': 'JODA15',
6219 'id': 'PL6IaIsEjSbf96XFRuNccS_RuEXwNdsoEu',
6220 'uploader': 'milan',
6221 'uploader_id': 'UCEI1-PVPcYXjB73Hfelbmaw',
6222 'description': '',
6223 'channel_url': 'https://www.youtube.com/channel/UCEI1-PVPcYXjB73Hfelbmaw',
6224 'tags': [],
6225 'modified_date': '20140919',
6226 'view_count': int,
6227 'channel': 'milan',
6228 'channel_id': 'UCEI1-PVPcYXjB73Hfelbmaw',
6229 'uploader_url': 'https://www.youtube.com/channel/UCEI1-PVPcYXjB73Hfelbmaw',
6230 'availability': 'public',
6231 },
6232 'expected_warnings': [r'[Uu]navailable videos? (is|are|will be) hidden'],
6233 }, {
6234 'url': 'http://www.youtube.com/embed/_xDOZElKyNU?list=PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
6235 'playlist_mincount': 455,
6236 'info_dict': {
6237 'title': '2018 Chinese New Singles (11/6 updated)',
6238 'id': 'PLsyOSbh5bs16vubvKePAQ1x3PhKavfBIl',
6239 'uploader': 'LBK',
6240 'uploader_id': 'UC21nz3_MesPLqtDqwdvnoxA',
6241 'description': 'md5:da521864744d60a198e3a88af4db0d9d',
6242 'channel': 'LBK',
6243 'view_count': int,
6244 'channel_url': 'https://www.youtube.com/c/愛低音的國王',
6245 'tags': [],
6246 'uploader_url': 'https://www.youtube.com/c/愛低音的國王',
6247 'channel_id': 'UC21nz3_MesPLqtDqwdvnoxA',
6248 'modified_date': r're:\d{8}',
6249 'availability': 'public',
6250 },
6251 'expected_warnings': [r'[Uu]navailable videos (are|will be) hidden'],
6252 }, {
6253 'url': 'TLGGrESM50VT6acwMjAyMjAxNw',
6254 'only_matching': True,
6255 }, {
6256 # music album playlist
6257 'url': 'OLAK5uy_m4xAFdmMC5rX3Ji3g93pQe3hqLZw_9LhM',
6258 'only_matching': True,
6259 }]
6260
6261 @classmethod
6262 def suitable(cls, url):
6263 if YoutubeTabIE.suitable(url):
6264 return False
6265 from ..utils import parse_qs
6266 qs = parse_qs(url)
6267 if qs.get('v', [None])[0]:
6268 return False
6269 return super().suitable(url)
6270
6271 def _real_extract(self, url):
6272 playlist_id = self._match_id(url)
6273 is_music_url = YoutubeBaseInfoExtractor.is_music_url(url)
6274 url = update_url_query(
6275 'https://www.youtube.com/playlist',
6276 parse_qs(url) or {'list': playlist_id})
6277 if is_music_url:
6278 url = smuggle_url(url, {'is_music_url': True})
6279 return self.url_result(url, ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
6280
6281
6282 class YoutubeYtBeIE(InfoExtractor):
6283 IE_DESC = 'youtu.be'
6284 _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}
6285 _TESTS = [{
6286 'url': 'https://youtu.be/yeWKywCrFtk?list=PL2qgrgXsNUG5ig9cat4ohreBjYLAPC0J5',
6287 'info_dict': {
6288 'id': 'yeWKywCrFtk',
6289 'ext': 'mp4',
6290 'title': 'Small Scale Baler and Braiding Rugs',
6291 'uploader': 'Backus-Page House Museum',
6292 'uploader_id': 'backuspagemuseum',
6293 'uploader_url': r're:https?://(?:www\.)?youtube\.com/user/backuspagemuseum',
6294 'upload_date': '20161008',
6295 'description': 'md5:800c0c78d5eb128500bffd4f0b4f2e8a',
6296 'categories': ['Nonprofits & Activism'],
6297 'tags': list,
6298 'like_count': int,
6299 'age_limit': 0,
6300 'playable_in_embed': True,
6301 'thumbnail': 'https://i.ytimg.com/vi_webp/yeWKywCrFtk/maxresdefault.webp',
6302 'channel': 'Backus-Page House Museum',
6303 'channel_id': 'UCEfMCQ9bs3tjvjy1s451zaw',
6304 'live_status': 'not_live',
6305 'view_count': int,
6306 'channel_url': 'https://www.youtube.com/channel/UCEfMCQ9bs3tjvjy1s451zaw',
6307 'availability': 'public',
6308 'duration': 59,
6309 'comment_count': int,
6310 'channel_follower_count': int
6311 },
6312 'params': {
6313 'noplaylist': True,
6314 'skip_download': True,
6315 },
6316 }, {
6317 'url': 'https://youtu.be/uWyaPkt-VOI?list=PL9D9FC436B881BA21',
6318 'only_matching': True,
6319 }]
6320
6321 def _real_extract(self, url):
6322 mobj = self._match_valid_url(url)
6323 video_id = mobj.group('id')
6324 playlist_id = mobj.group('playlist_id')
6325 return self.url_result(
6326 update_url_query('https://www.youtube.com/watch', {
6327 'v': video_id,
6328 'list': playlist_id,
6329 'feature': 'youtu.be',
6330 }), ie=YoutubeTabIE.ie_key(), video_id=playlist_id)
6331
6332
6333 class YoutubeLivestreamEmbedIE(InfoExtractor):
6334 IE_DESC = 'YouTube livestream embeds'
6335 _VALID_URL = r'https?://(?:\w+\.)?youtube\.com/embed/live_stream/?\?(?:[^#]+&)?channel=(?P<id>[^&#]+)'
6336 _TESTS = [{
6337 'url': 'https://www.youtube.com/embed/live_stream?channel=UC2_KI6RB__jGdlnK6dvFEZA',
6338 'only_matching': True,
6339 }]
6340
6341 def _real_extract(self, url):
6342 channel_id = self._match_id(url)
6343 return self.url_result(
6344 f'https://www.youtube.com/channel/{channel_id}/live',
6345 ie=YoutubeTabIE.ie_key(), video_id=channel_id)
6346
6347
6348 class YoutubeYtUserIE(InfoExtractor):
6349 IE_DESC = 'YouTube user videos; "ytuser:" prefix'
6350 IE_NAME = 'youtube:user'
6351 _VALID_URL = r'ytuser:(?P<id>.+)'
6352 _TESTS = [{
6353 'url': 'ytuser:phihag',
6354 'only_matching': True,
6355 }]
6356
6357 def _real_extract(self, url):
6358 user_id = self._match_id(url)
6359 return self.url_result(f'https://www.youtube.com/user/{user_id}', YoutubeTabIE, user_id)
6360
6361
6362 class YoutubeFavouritesIE(YoutubeBaseInfoExtractor):
6363 IE_NAME = 'youtube:favorites'
6364 IE_DESC = 'YouTube liked videos; ":ytfav" keyword (requires cookies)'
6365 _VALID_URL = r':ytfav(?:ou?rite)?s?'
6366 _LOGIN_REQUIRED = True
6367 _TESTS = [{
6368 'url': ':ytfav',
6369 'only_matching': True,
6370 }, {
6371 'url': ':ytfavorites',
6372 'only_matching': True,
6373 }]
6374
6375 def _real_extract(self, url):
6376 return self.url_result(
6377 'https://www.youtube.com/playlist?list=LL',
6378 ie=YoutubeTabIE.ie_key())
6379
6380
6381 class YoutubeNotificationsIE(YoutubeTabBaseInfoExtractor):
6382 IE_NAME = 'youtube:notif'
6383 IE_DESC = 'YouTube notifications; ":ytnotif" keyword (requires cookies)'
6384 _VALID_URL = r':ytnotif(?:ication)?s?'
6385 _LOGIN_REQUIRED = True
6386 _TESTS = [{
6387 'url': ':ytnotif',
6388 'only_matching': True,
6389 }, {
6390 'url': ':ytnotifications',
6391 'only_matching': True,
6392 }]
6393
6394 def _extract_notification_menu(self, response, continuation_list):
6395 notification_list = traverse_obj(
6396 response,
6397 ('actions', 0, 'openPopupAction', 'popup', 'multiPageMenuRenderer', 'sections', 0, 'multiPageMenuNotificationSectionRenderer', 'items'),
6398 ('actions', 0, 'appendContinuationItemsAction', 'continuationItems'),
6399 expected_type=list) or []
6400 continuation_list[0] = None
6401 for item in notification_list:
6402 entry = self._extract_notification_renderer(item.get('notificationRenderer'))
6403 if entry:
6404 yield entry
6405 continuation = item.get('continuationItemRenderer')
6406 if continuation:
6407 continuation_list[0] = continuation
6408
6409 def _extract_notification_renderer(self, notification):
6410 video_id = traverse_obj(
6411 notification, ('navigationEndpoint', 'watchEndpoint', 'videoId'), expected_type=str)
6412 url = f'https://www.youtube.com/watch?v={video_id}'
6413 channel_id = None
6414 if not video_id:
6415 browse_ep = traverse_obj(
6416 notification, ('navigationEndpoint', 'browseEndpoint'), expected_type=dict)
6417 channel_id = traverse_obj(browse_ep, 'browseId', expected_type=str)
6418 post_id = self._search_regex(
6419 r'/post/(.+)', traverse_obj(browse_ep, 'canonicalBaseUrl', expected_type=str),
6420 'post id', default=None)
6421 if not channel_id or not post_id:
6422 return
6423 # The direct /post url redirects to this in the browser
6424 url = f'https://www.youtube.com/channel/{channel_id}/community?lb={post_id}'
6425
6426 channel = traverse_obj(
6427 notification, ('contextualMenu', 'menuRenderer', 'items', 1, 'menuServiceItemRenderer', 'text', 'runs', 1, 'text'),
6428 expected_type=str)
6429 notification_title = self._get_text(notification, 'shortMessage')
6430 if notification_title:
6431 notification_title = notification_title.replace('\xad', '') # remove soft hyphens
6432 # TODO: handle recommended videos
6433 title = self._search_regex(
6434 rf'{re.escape(channel or "")}[^:]+: (.+)', notification_title,
6435 'video title', default=None)
6436 timestamp = (self._parse_time_text(self._get_text(notification, 'sentTimeText'))
6437 if self._configuration_arg('approximate_date', ie_key=YoutubeTabIE)
6438 else None)
6439 return {
6440 '_type': 'url',
6441 'url': url,
6442 'ie_key': (YoutubeIE if video_id else YoutubeTabIE).ie_key(),
6443 'video_id': video_id,
6444 'title': title,
6445 'channel_id': channel_id,
6446 'channel': channel,
6447 'thumbnails': self._extract_thumbnails(notification, 'videoThumbnail'),
6448 'timestamp': timestamp,
6449 }
6450
6451 def _notification_menu_entries(self, ytcfg):
6452 continuation_list = [None]
6453 response = None
6454 for page in itertools.count(1):
6455 ctoken = traverse_obj(
6456 continuation_list, (0, 'continuationEndpoint', 'getNotificationMenuEndpoint', 'ctoken'), expected_type=str)
6457 response = self._extract_response(
6458 item_id=f'page {page}', query={'ctoken': ctoken} if ctoken else {}, ytcfg=ytcfg,
6459 ep='notification/get_notification_menu', check_get_keys='actions',
6460 headers=self.generate_api_headers(ytcfg=ytcfg, visitor_data=self._extract_visitor_data(response)))
6461 yield from self._extract_notification_menu(response, continuation_list)
6462 if not continuation_list[0]:
6463 break
6464
6465 def _real_extract(self, url):
6466 display_id = 'notifications'
6467 ytcfg = self._download_ytcfg('web', display_id) if not self.skip_webpage else {}
6468 self._report_playlist_authcheck(ytcfg)
6469 return self.playlist_result(self._notification_menu_entries(ytcfg), display_id, display_id)
6470
6471
6472 class YoutubeSearchIE(YoutubeTabBaseInfoExtractor, SearchInfoExtractor):
6473 IE_DESC = 'YouTube search'
6474 IE_NAME = 'youtube:search'
6475 _SEARCH_KEY = 'ytsearch'
6476 _SEARCH_PARAMS = 'EgIQAQ%3D%3D' # Videos only
6477 _TESTS = [{
6478 'url': 'ytsearch5:youtube-dl test video',
6479 'playlist_count': 5,
6480 'info_dict': {
6481 'id': 'youtube-dl test video',
6482 'title': 'youtube-dl test video',
6483 }
6484 }]
6485
6486
6487 class YoutubeSearchDateIE(YoutubeTabBaseInfoExtractor, SearchInfoExtractor):
6488 IE_NAME = YoutubeSearchIE.IE_NAME + ':date'
6489 _SEARCH_KEY = 'ytsearchdate'
6490 IE_DESC = 'YouTube search, newest videos first'
6491 _SEARCH_PARAMS = 'CAISAhAB' # Videos only, sorted by date
6492 _TESTS = [{
6493 'url': 'ytsearchdate5:youtube-dl test video',
6494 'playlist_count': 5,
6495 'info_dict': {
6496 'id': 'youtube-dl test video',
6497 'title': 'youtube-dl test video',
6498 }
6499 }]
6500
6501
6502 class YoutubeSearchURLIE(YoutubeTabBaseInfoExtractor):
6503 IE_DESC = 'YouTube search URLs with sorting and filter support'
6504 IE_NAME = YoutubeSearchIE.IE_NAME + '_url'
6505 _VALID_URL = r'https?://(?:www\.)?youtube\.com/(?:results|search)\?([^#]+&)?(?:search_query|q)=(?:[^&]+)(?:[&#]|$)'
6506 _TESTS = [{
6507 'url': 'https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video',
6508 'playlist_mincount': 5,
6509 'info_dict': {
6510 'id': 'youtube-dl test video',
6511 'title': 'youtube-dl test video',
6512 }
6513 }, {
6514 'url': 'https://www.youtube.com/results?search_query=python&sp=EgIQAg%253D%253D',
6515 'playlist_mincount': 5,
6516 'info_dict': {
6517 'id': 'python',
6518 'title': 'python',
6519 }
6520 }, {
6521 'url': 'https://www.youtube.com/results?search_query=%23cats',
6522 'playlist_mincount': 1,
6523 'info_dict': {
6524 'id': '#cats',
6525 'title': '#cats',
6526 # The test suite does not have support for nested playlists
6527 # 'entries': [{
6528 # 'url': r're:https://(www\.)?youtube\.com/hashtag/cats',
6529 # 'title': '#cats',
6530 # }],
6531 },
6532 }, {
6533 'url': 'https://www.youtube.com/results?q=test&sp=EgQIBBgB',
6534 'only_matching': True,
6535 }]
6536
6537 def _real_extract(self, url):
6538 qs = parse_qs(url)
6539 query = (qs.get('search_query') or qs.get('q'))[0]
6540 return self.playlist_result(self._search_results(query, qs.get('sp', (None,))[0]), query, query)
6541
6542
6543 class YoutubeMusicSearchURLIE(YoutubeTabBaseInfoExtractor):
6544 IE_DESC = 'YouTube music search URLs with selectable sections, e.g. #songs'
6545 IE_NAME = 'youtube:music:search_url'
6546 _VALID_URL = r'https?://music\.youtube\.com/search\?([^#]+&)?(?:search_query|q)=(?:[^&]+)(?:[&#]|$)'
6547 _TESTS = [{
6548 'url': 'https://music.youtube.com/search?q=royalty+free+music',
6549 'playlist_count': 16,
6550 'info_dict': {
6551 'id': 'royalty free music',
6552 'title': 'royalty free music',
6553 }
6554 }, {
6555 'url': 'https://music.youtube.com/search?q=royalty+free+music&sp=EgWKAQIIAWoKEAoQAxAEEAkQBQ%3D%3D',
6556 'playlist_mincount': 30,
6557 'info_dict': {
6558 'id': 'royalty free music - songs',
6559 'title': 'royalty free music - songs',
6560 },
6561 'params': {'extract_flat': 'in_playlist'}
6562 }, {
6563 'url': 'https://music.youtube.com/search?q=royalty+free+music#community+playlists',
6564 'playlist_mincount': 30,
6565 'info_dict': {
6566 'id': 'royalty free music - community playlists',
6567 'title': 'royalty free music - community playlists',
6568 },
6569 'params': {'extract_flat': 'in_playlist'}
6570 }]
6571
6572 _SECTIONS = {
6573 'albums': 'EgWKAQIYAWoKEAoQAxAEEAkQBQ==',
6574 'artists': 'EgWKAQIgAWoKEAoQAxAEEAkQBQ==',
6575 'community playlists': 'EgeKAQQoAEABagoQChADEAQQCRAF',
6576 'featured playlists': 'EgeKAQQoADgBagwQAxAJEAQQDhAKEAU==',
6577 'songs': 'EgWKAQIIAWoKEAoQAxAEEAkQBQ==',
6578 'videos': 'EgWKAQIQAWoKEAoQAxAEEAkQBQ==',
6579 }
6580
6581 def _real_extract(self, url):
6582 qs = parse_qs(url)
6583 query = (qs.get('search_query') or qs.get('q'))[0]
6584 params = qs.get('sp', (None,))[0]
6585 if params:
6586 section = next((k for k, v in self._SECTIONS.items() if v == params), params)
6587 else:
6588 section = urllib.parse.unquote_plus((url.split('#') + [''])[1]).lower()
6589 params = self._SECTIONS.get(section)
6590 if not params:
6591 section = None
6592 title = join_nonempty(query, section, delim=' - ')
6593 return self.playlist_result(self._search_results(query, params, default_client='web_music'), title, title)
6594
6595
6596 class YoutubeFeedsInfoExtractor(InfoExtractor):
6597 """
6598 Base class for feed extractors
6599 Subclasses must re-define the _FEED_NAME property.
6600 """
6601 _LOGIN_REQUIRED = True
6602 _FEED_NAME = 'feeds'
6603
6604 def _real_initialize(self):
6605 YoutubeBaseInfoExtractor._check_login_required(self)
6606
6607 @classproperty
6608 def IE_NAME(self):
6609 return f'youtube:{self._FEED_NAME}'
6610
6611 def _real_extract(self, url):
6612 return self.url_result(
6613 f'https://www.youtube.com/feed/{self._FEED_NAME}', ie=YoutubeTabIE.ie_key())
6614
6615
6616 class YoutubeWatchLaterIE(InfoExtractor):
6617 IE_NAME = 'youtube:watchlater'
6618 IE_DESC = 'Youtube watch later list; ":ytwatchlater" keyword (requires cookies)'
6619 _VALID_URL = r':ytwatchlater'
6620 _TESTS = [{
6621 'url': ':ytwatchlater',
6622 'only_matching': True,
6623 }]
6624
6625 def _real_extract(self, url):
6626 return self.url_result(
6627 'https://www.youtube.com/playlist?list=WL', ie=YoutubeTabIE.ie_key())
6628
6629
6630 class YoutubeRecommendedIE(YoutubeFeedsInfoExtractor):
6631 IE_DESC = 'YouTube recommended videos; ":ytrec" keyword'
6632 _VALID_URL = r'https?://(?:www\.)?youtube\.com/?(?:[?#]|$)|:ytrec(?:ommended)?'
6633 _FEED_NAME = 'recommended'
6634 _LOGIN_REQUIRED = False
6635 _TESTS = [{
6636 'url': ':ytrec',
6637 'only_matching': True,
6638 }, {
6639 'url': ':ytrecommended',
6640 'only_matching': True,
6641 }, {
6642 'url': 'https://youtube.com',
6643 'only_matching': True,
6644 }]
6645
6646
6647 class YoutubeSubscriptionsIE(YoutubeFeedsInfoExtractor):
6648 IE_DESC = 'YouTube subscriptions feed; ":ytsubs" keyword (requires cookies)'
6649 _VALID_URL = r':ytsub(?:scription)?s?'
6650 _FEED_NAME = 'subscriptions'
6651 _TESTS = [{
6652 'url': ':ytsubs',
6653 'only_matching': True,
6654 }, {
6655 'url': ':ytsubscriptions',
6656 'only_matching': True,
6657 }]
6658
6659
6660 class YoutubeHistoryIE(YoutubeFeedsInfoExtractor):
6661 IE_DESC = 'Youtube watch history; ":ythis" keyword (requires cookies)'
6662 _VALID_URL = r':ythis(?:tory)?'
6663 _FEED_NAME = 'history'
6664 _TESTS = [{
6665 'url': ':ythistory',
6666 'only_matching': True,
6667 }]
6668
6669
6670 class YoutubeStoriesIE(InfoExtractor):
6671 IE_DESC = 'YouTube channel stories; "ytstories:" prefix'
6672 IE_NAME = 'youtube:stories'
6673 _VALID_URL = r'ytstories:UC(?P<id>[A-Za-z0-9_-]{21}[AQgw])$'
6674 _TESTS = [{
6675 'url': 'ytstories:UCwFCb4jeqaKWnciAYM-ZVHg',
6676 'only_matching': True,
6677 }]
6678
6679 def _real_extract(self, url):
6680 playlist_id = f'RLTD{self._match_id(url)}'
6681 return self.url_result(
6682 smuggle_url(f'https://www.youtube.com/playlist?list={playlist_id}&playnext=1', {'is_story': True}),
6683 ie=YoutubeTabIE, video_id=playlist_id)
6684
6685
6686 class YoutubeShortsAudioPivotIE(InfoExtractor):
6687 IE_DESC = 'YouTube Shorts audio pivot (Shorts using audio of a given video)'
6688 IE_NAME = 'youtube:shorts:pivot:audio'
6689 _VALID_URL = r'https?://(?:www\.)?youtube\.com/source/(?P<id>[\w-]{11})/shorts'
6690 _TESTS = [{
6691 'url': 'https://www.youtube.com/source/Lyj-MZSAA9o/shorts',
6692 'only_matching': True,
6693 }]
6694
6695 @staticmethod
6696 def _generate_audio_pivot_params(video_id):
6697 """
6698 Generates sfv_audio_pivot browse params for this video id
6699 """
6700 pb_params = b'\xf2\x05+\n)\x12\'\n\x0b%b\x12\x0b%b\x1a\x0b%b' % ((video_id.encode(),) * 3)
6701 return urllib.parse.quote(base64.b64encode(pb_params).decode())
6702
6703 def _real_extract(self, url):
6704 video_id = self._match_id(url)
6705 return self.url_result(
6706 f'https://www.youtube.com/feed/sfv_audio_pivot?bp={self._generate_audio_pivot_params(video_id)}',
6707 ie=YoutubeTabIE)
6708
6709
6710 class YoutubeTruncatedURLIE(InfoExtractor):
6711 IE_NAME = 'youtube:truncated_url'
6712 IE_DESC = False # Do not list
6713 _VALID_URL = r'''(?x)
6714 (?:https?://)?
6715 (?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie)?\.com/
6716 (?:watch\?(?:
6717 feature=[a-z_]+|
6718 annotation_id=annotation_[^&]+|
6719 x-yt-cl=[0-9]+|
6720 hl=[^&]*|
6721 t=[0-9]+
6722 )?
6723 |
6724 attribution_link\?a=[^&]+
6725 )
6726 $
6727 '''
6728
6729 _TESTS = [{
6730 'url': 'https://www.youtube.com/watch?annotation_id=annotation_3951667041',
6731 'only_matching': True,
6732 }, {
6733 'url': 'https://www.youtube.com/watch?',
6734 'only_matching': True,
6735 }, {
6736 'url': 'https://www.youtube.com/watch?x-yt-cl=84503534',
6737 'only_matching': True,
6738 }, {
6739 'url': 'https://www.youtube.com/watch?feature=foo',
6740 'only_matching': True,
6741 }, {
6742 'url': 'https://www.youtube.com/watch?hl=en-GB',
6743 'only_matching': True,
6744 }, {
6745 'url': 'https://www.youtube.com/watch?t=2372',
6746 'only_matching': True,
6747 }]
6748
6749 def _real_extract(self, url):
6750 raise ExtractorError(
6751 'Did you forget to quote the URL? Remember that & is a meta '
6752 'character in most shells, so you want to put the URL in quotes, '
6753 'like youtube-dl '
6754 '"https://www.youtube.com/watch?feature=foo&v=BaW_jenozKc" '
6755 ' or simply youtube-dl BaW_jenozKc .',
6756 expected=True)
6757
6758
6759 class YoutubeClipIE(YoutubeTabBaseInfoExtractor):
6760 IE_NAME = 'youtube:clip'
6761 _VALID_URL = r'https?://(?:www\.)?youtube\.com/clip/(?P<id>[^/?#]+)'
6762 _TESTS = [{
6763 # FIXME: Other metadata should be extracted from the clip, not from the base video
6764 'url': 'https://www.youtube.com/clip/UgytZKpehg-hEMBSn3F4AaABCQ',
6765 'info_dict': {
6766 'id': 'UgytZKpehg-hEMBSn3F4AaABCQ',
6767 'ext': 'mp4',
6768 'section_start': 29.0,
6769 'section_end': 39.7,
6770 'duration': 10.7,
6771 'age_limit': 0,
6772 'availability': 'public',
6773 'categories': ['Gaming'],
6774 'channel': 'Scott The Woz',
6775 'channel_id': 'UC4rqhyiTs7XyuODcECvuiiQ',
6776 'channel_url': 'https://www.youtube.com/channel/UC4rqhyiTs7XyuODcECvuiiQ',
6777 'description': 'md5:7a4517a17ea9b4bd98996399d8bb36e7',
6778 'like_count': int,
6779 'playable_in_embed': True,
6780 'tags': 'count:17',
6781 'thumbnail': 'https://i.ytimg.com/vi_webp/ScPX26pdQik/maxresdefault.webp',
6782 'title': 'Mobile Games on Console - Scott The Woz',
6783 'upload_date': '20210920',
6784 'uploader': 'Scott The Woz',
6785 'uploader_id': 'scottthewoz',
6786 'uploader_url': 'http://www.youtube.com/user/scottthewoz',
6787 'view_count': int,
6788 'live_status': 'not_live',
6789 'channel_follower_count': int
6790 }
6791 }]
6792
6793 def _real_extract(self, url):
6794 clip_id = self._match_id(url)
6795 _, data = self._extract_webpage(url, clip_id)
6796
6797 video_id = traverse_obj(data, ('currentVideoEndpoint', 'watchEndpoint', 'videoId'))
6798 if not video_id:
6799 raise ExtractorError('Unable to find video ID')
6800
6801 clip_data = traverse_obj(data, (
6802 'engagementPanels', ..., 'engagementPanelSectionListRenderer', 'content', 'clipSectionRenderer',
6803 'contents', ..., 'clipAttributionRenderer', 'onScrubExit', 'commandExecutorCommand', 'commands', ...,
6804 'openPopupAction', 'popup', 'notificationActionRenderer', 'actionButton', 'buttonRenderer', 'command',
6805 'commandExecutorCommand', 'commands', ..., 'loopCommand'), get_all=False)
6806
6807 return {
6808 '_type': 'url_transparent',
6809 'url': f'https://www.youtube.com/watch?v={video_id}',
6810 'ie_key': YoutubeIE.ie_key(),
6811 'id': clip_id,
6812 'section_start': int(clip_data['startTimeMs']) / 1000,
6813 'section_end': int(clip_data['endTimeMs']) / 1000,
6814 }
6815
6816
6817 class YoutubeTruncatedIDIE(InfoExtractor):
6818 IE_NAME = 'youtube:truncated_id'
6819 IE_DESC = False # Do not list
6820 _VALID_URL = r'https?://(?:www\.)?youtube\.com/watch\?v=(?P<id>[0-9A-Za-z_-]{1,10})$'
6821
6822 _TESTS = [{
6823 'url': 'https://www.youtube.com/watch?v=N_708QY7Ob',
6824 'only_matching': True,
6825 }]
6826
6827 def _real_extract(self, url):
6828 video_id = self._match_id(url)
6829 raise ExtractorError(
6830 f'Incomplete YouTube ID {video_id}. URL {url} looks truncated.',
6831 expected=True)