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