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