Compare commits

...

5 Commits

Author SHA1 Message Date
OverShifted
41f07cf2ba
Merge a784be7395cbb0426b8111ca126e73fe4cf653cc into da7223d4aa42ff9fc680b0951d043dd03cec2d30 2025-03-22 07:20:32 +08:00
dirkf
da7223d4aa [YouTube] Improve support for tce-style player JS
* improve extraction of global "useful data" Array from player JS
* also handle tv-player and add tests: thx seproDev (yt-dlp/yt-dlp#12684)

Co-Authored-By: sepro <sepro@sepr0.com>
2025-03-21 16:26:25 +00:00
dirkf
37c2440d6a [YouTube] Update player client data
thx seproDev (yt-dlp/yt-dlp#12603)

Co-authored-by: sepro <sepro@sepr0.com>
2025-03-21 16:13:24 +00:00
Sepehr Kalanaki
a784be7395
[invidious] Add more tests 2022-12-15 20:04:39 +03:30
Sepehr Kalanaki
991ac059a4
[invidious] Add new extractor 2022-12-15 19:35:03 +03:30
4 changed files with 252 additions and 10 deletions

View File

@ -232,8 +232,32 @@ _NSIG_TESTS = [
'W9HJZKktxuYoDTqW', 'jHbbkcaxm54',
),
(
'https://www.youtube.com/s/player/91201489/player_ias_tce.vflset/en_US/base.js',
'W9HJZKktxuYoDTqW', 'U48vOZHaeYS6vO',
'https://www.youtube.com/s/player/643afba4/player_ias.vflset/en_US/base.js',
'W9HJZKktxuYoDTqW', 'larxUlagTRAcSw',
),
(
'https://www.youtube.com/s/player/e7567ecf/player_ias_tce.vflset/en_US/base.js',
'Sy4aDGc0VpYRR9ew_', '5UPOT1VhoZxNLQ',
),
(
'https://www.youtube.com/s/player/d50f54ef/player_ias_tce.vflset/en_US/base.js',
'Ha7507LzRmH3Utygtj', 'XFTb2HoeOE5MHg',
),
(
'https://www.youtube.com/s/player/074a8365/player_ias_tce.vflset/en_US/base.js',
'Ha7507LzRmH3Utygtj', 'ufTsrE0IVYrkl8v',
),
(
'https://www.youtube.com/s/player/643afba4/player_ias.vflset/en_US/base.js',
'N5uAlLqm0eg1GyHO', 'dCBQOejdq5s-ww',
),
(
'https://www.youtube.com/s/player/69f581a5/tv-player-ias.vflset/tv-player-ias.js',
'-qIP447rVlTTwaZjY', 'KNcGOksBAvwqQg',
),
(
'https://www.youtube.com/s/player/643afba4/tv-player-ias.vflset/tv-player-ias.js',
'ir9-V6cdbCiyKxhr', '2PL7ZDYAALMfmA',
),
]

View File

@ -1701,3 +1701,4 @@ from .zingmp3 import (
)
from .zoom import ZoomIE
from .zype import ZypeIE
from .invidious import InvidiousIE, InvidiousPlaylistIE

View File

@ -0,0 +1,217 @@
from __future__ import unicode_literals
from .common import InfoExtractor
from ..compat import compat_urllib_parse
INSTANCES = [
'y.com.sb',
'yt.artemislena.eu'
]
INSTANCES_HOST_REGEX = '(?:' + '|'.join([instance.replace('.', r'\.') for instance in INSTANCES]) + ')'
class InvidiousIE(InfoExtractor):
_VALID_URL = r'(?:https?://)?(?:www\.)?' + INSTANCES_HOST_REGEX + r'/watch\?v=(?P<id>.+)'
_TESTS = [
{
'url': 'https://y.com.sb/watch?v=xKTygGa6hg0',
'info_dict': {
'id': 'xKTygGa6hg0',
'ext': 'mp4',
'title': 'Coding in C++ - Creating a Player Controller - CRYENGINE Summer Academy S1E5 - [Tutorial]',
'uploader': 'CRYENGINE',
'uploader_id': 'UCtaXcIVFp8HEpthm7qwtKCQ',
'description': 'md5:7aa75816d40ffccdbf3e15a90b05fca3',
}
},
{
'url': 'https://yt.artemislena.eu/watch?v=BaW_jenozKc',
'md5': '5515885fed58607bfae88f7d2090bc93',
'info_dict': {
'id': 'BaW_jenozKc',
'ext': 'mp4',
'title': 'youtube-dl test video "\'/\\ä↭𝕐',
'uploader': 'Philipp Hagemeister',
'uploader_id': 'UCLqxVugv74EIW3VWh2NOa3Q',
'channel_id': 'UCLqxVugv74EIW3VWh2NOa3Q',
'description': 'test chars: "\'/\\ä↭𝕐\ntest URL: https://github.com/rg3/youtube-dl/issues/1892\n\nThis is a test video for youtube-dl.\n\nFor more information, contact phihag@phihag.de .',
'tags': ['youtube-dl'],
'duration': 10,
'view_count': int,
'like_count': int,
'dislike_count': int,
}
},
]
def __init__(self, downloader=None):
super().__init__(downloader)
# type is either 'video' or 'audio'
# ext is the file extension
@staticmethod
def _get_additional_format_data(format_type, bitrate, resolution, fps):
out = {}
try:
type_and_ext, codecs = format_type.split(';')
except Exception:
pass
try:
type_, ext = type_and_ext.split('/')
# codec = codecs.split('"')[1]
out['ext'] = ext
# if type_ == 'audio':
# out['acodec'] = codec
# elif type_ == 'video':
# out['vcodec'] = codec
except Exception:
pass
try:
bitrate = float(bitrate) / 1000
# if type_ == 'audio':
# out['abr'] = bitrate
# elif type_ == 'video':
# out['vbr'] = bitrate
# out['tbr'] = bitrate
except Exception:
pass
try:
if type_ == 'audio':
out['resolution'] = type_and_ext + ' @ ' + str(bitrate) + 'k - audio only'
elif type_ == 'video':
out['resolution'] = resolution + ' - ' + type_and_ext + ' @ ' + str(fps) + 'fps - video only'
except Exception:
pass
return out
def _patch_url(self, url):
return compat_urllib_parse.urlparse(url)._replace(netloc=self.url_netloc).geturl()
def _get_formats(self, api_response):
all_formats = []
# Video/audio only
for format_ in api_response.get('adaptiveFormats') or []:
all_formats.append({
'url': self._patch_url(format_['url']),
'format_id': format_.get('itag'),
# 'fps': format_.get('fps'),
# 'container': format_.get('container')
} | InvidiousIE._get_additional_format_data(format_.get('type'), format_.get('bitrate'), format_.get('resolution'), format_.get('fps')))
# Both video and audio
for format_ in api_response.get('formatStreams') or []:
all_formats.append({
'url': self._patch_url(format_['url']),
'format_id': format_.get('itag'),
# 'fps': format_.get('fps'),
# 'container': format_.get('container')
} | InvidiousIE._get_additional_format_data(format_.get('type'), format_.get('bitrate'), format_.get('resolution'), format_.get('fps')))
return all_formats
def _get_thumbnails(self, api_response):
thumbnails = []
video_thumbnails = api_response.get('videoThumbnails') or []
for inversed_quality, thumbnail in enumerate(video_thumbnails):
thumbnails.append({
'id': thumbnail.get('quality'),
'url': thumbnail.get('url'),
'quality': len(video_thumbnails) - inversed_quality,
'width': thumbnail.get('width'),
'height': thumbnail.get('height')
})
return thumbnails
def _real_extract(self, url):
video_id = self._match_id(url)
webpage = None
# host_url will contain `http[s]://example.com` where `example.com` is the used invidious instance.
url_parsed = compat_urllib_parse.urlparse(url)
self.url_netloc = url_parsed.netloc
host_url = url_parsed.scheme + '://' + url_parsed.netloc
api_response = self._download_json(host_url + '/api/v1/videos/' + video_id, video_id)
def download_webpage_and(fn, fatal=True):
global webpage
if webpage is None:
webpage = self._download_webpage(url, video_id, fatal=fatal)
return fn()
out = {
'id': video_id,
'title': api_response.get('title') or download_webpage_and(lambda: self._og_search_title(webpage)),
'description': api_response.get('description') or download_webpage_and(lambda: self._og_search_description(webpage)),
'release_timestamp': api_response.get('published'),
'uploader': api_response.get('author'),
'uploader_id': api_response.get('authorId'),
'channel': api_response.get('author'),
'channel_id': api_response.get('authorId'),
'channel_url': host_url + api_response.get('authorUrl'),
'duration': api_response.get('lengthSeconds'),
'view_count': api_response.get('viewCount'),
'like_count': api_response.get('likeCount'),
'dislike_count': api_response.get('dislikeCount'),
# 'isFamilyFriendly': 18 if api_response.get('isFamilyFriendly') == False else None
'tags': api_response.get('keywords'),
'is_live': api_response.get('liveNow'),
'formats': self._get_formats(api_response),
'thumbnails': self._get_thumbnails(api_response)
}
if api_response.get('isFamilyFriendly') is False:
out['age_limit'] = 18
return out
class InvidiousPlaylistIE(InfoExtractor):
_VALID_URL = r'(?:https?://)?(?:www\.)?' + INSTANCES_HOST_REGEX + r'/playlist\?list=(?P<id>.+)'
_TEST = {
'url': 'https://yt.artemislena.eu/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU',
'md5': 'de4a9175071169961fe7cf2b6740da12',
'info_dict': {
'id': 'HyznrdDSSGM',
'ext': 'mp4',
'title': '8-bit computer update',
'uploader': 'Ben Eater',
'uploader_id': 'UCS0N5baNlQWJCUrhCEo8WlA',
'description': 'An update on my plans to build another 8-bit computer from scratch and make videos of the whole process! Buy a kit and build your own! https://eater.net/8bit/kits\n\nSupport me on Patreon: https://www.patreon.com/beneater',
}
}
def _get_entries(self, api_response):
return [InvidiousIE(self._downloader)._real_extract(self.host_url + '/watch?v=' + video['videoId'])
for video in api_response['videos']]
def _real_extract(self, url):
playlist_id = self._match_id(url)
# host_url will contain `http[s]://example.com` where `example.com` is the used invidious instance.
url_parsed = compat_urllib_parse.urlparse(url)
self.host_url = url_parsed.scheme + '://' + url_parsed.netloc
api_response = self._download_json(self.host_url + '/api/v1/playlists/' + playlist_id, playlist_id)
return InfoExtractor.playlist_result(self._get_entries(api_response), playlist_id, api_response.get('title'), api_response.get('description')) | {
'release_timestamp': api_response.get('updated'),
'uploader': api_response.get('author'),
'uploader_id': api_response.get('authorId'),
}

View File

@ -91,12 +91,12 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
'INNERTUBE_CONTEXT': {
'client': {
'clientName': 'IOS',
'clientVersion': '19.45.4',
'clientVersion': '20.10.4',
'deviceMake': 'Apple',
'deviceModel': 'iPhone16,2',
'userAgent': 'com.google.ios.youtube/19.45.4 (iPhone16,2; U; CPU iOS 18_1_0 like Mac OS X;)',
'userAgent': 'com.google.ios.youtube/20.10.4 (iPhone16,2; U; CPU iOS 18_3_2 like Mac OS X;)',
'osName': 'iPhone',
'osVersion': '18.1.0.22B83',
'osVersion': '18.3.2.22D82',
},
},
'INNERTUBE_CONTEXT_CLIENT_NAME': 5,
@ -109,7 +109,7 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
'INNERTUBE_CONTEXT': {
'client': {
'clientName': 'MWEB',
'clientVersion': '2.20241202.07.00',
'clientVersion': '2.20250311.03.00',
# mweb previously did not require PO Token with this UA
'userAgent': 'Mozilla/5.0 (iPad; CPU OS 16_7_10 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1,gzip(gfe)',
},
@ -122,7 +122,7 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
'INNERTUBE_CONTEXT': {
'client': {
'clientName': 'TVHTML5',
'clientVersion': '7.20250120.19.00',
'clientVersion': '7.20250312.16.00',
'userAgent': 'Mozilla/5.0 (ChromiumStylePlatform) Cobalt/Version',
},
},
@ -133,7 +133,7 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
'INNERTUBE_CONTEXT': {
'client': {
'clientName': 'WEB',
'clientVersion': '2.20241126.01.00',
'clientVersion': '2.20250312.04.00',
},
},
'INNERTUBE_CONTEXT_CLIENT_NAME': 1,
@ -692,7 +692,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
'invidious': '|'.join(_INVIDIOUS_SITES),
}
_PLAYER_INFO_RE = (
r'/s/player/(?P<id>[a-zA-Z0-9_-]{8,})/player',
r'/s/player/(?P<id>[a-zA-Z0-9_-]{8,})//(?:tv-)?player',
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$',
r'\b(?P<id>vfl[a-zA-Z0-9_-]+)\b.*?\.js$',
)
@ -1857,7 +1857,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
def _extract_n_function_code_jsi(self, video_id, jsi, player_id=None):
var_ay = self._search_regex(
r'(?:[;\s]|^)\s*(var\s*[\w$]+\s*=\s*"[^"]+"\s*\.\s*split\("\{"\))(?=\s*[,;])',
r'(?:[;\s]|^)\s*(var\s*[\w$]+\s*=\s*"(?:\\"|[^"])+"\s*\.\s*split\("\W+"\))(?=\s*[,;])',
jsi.code, 'useful values', default='')
func_name = self._extract_n_function_name(jsi.code)