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