NL
apps/winzik / src / Media / YouTubeInnerTube.php · commit 6576d6f
<?phpnamespace App\Media;use JumPHP\Async\Task;/** * YouTube without yt-dlp (the phone has no Python; a computer whose yt-dlp is missing or broken): YouTube's own web API, * InnerTube, the way yt-dlp talks to it. * * - search: POST youtubei/v1/search as the web site (client WEB), its "videoRenderer"s (id, title, channel, length), the *   next pages by the "continuation" token of the page before; * - a video's streams: POST youtubei/v1/player as the visionOS app (client VISIONOS, yt-dlp's client when it has no *   JavaScript runtime, 2026): direct addresses of googlevideo.com, not ciphered, no PO token. Downloaded by ranges *   (`&range=a-b`, 10 Mo each: a whole file in one go is throttled to the speed of playing). * * What it cannot do (an age-restricted video asks for an account, a ciphered answer needs YouTube's JavaScript, a * live): a French error that says so. Never blocks: every request is a Task of JumPHP's Http. */final class YouTubeInnerTube{    public const API = 'https://www.youtube.com/youtubei/v1/';    /** the web site's client: the search */    public const WEB = ['clientName' => 'WEB', 'clientVersion' => '2.20260708.00.00', 'hl' => 'fr', 'gl' => 'FR'];    public const WEB_ID = 1;    public const WEB_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36';    /** the visionOS app: its streams are plain addresses (yt-dlp 2026.08: _DEFAULT_JSLESS_CLIENTS) */    public const PLAYER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 15_7_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15';    public const PLAYER = ['clientName' => 'VISIONOS', 'clientVersion' => '1.02', 'deviceMake' => 'Apple', 'deviceModel' => 'RealityDevice17,1',        'userAgent' => self::PLAYER_AGENT, 'osName' => 'visionOS', 'osVersion' => '26.5.23O471', 'hl' => 'fr', 'gl' => 'FR'];    public const PLAYER_ID = 101;    /**     * The apps asked one after the other for a video's streams: YouTube's bot check ("confirmez que vous n'êtes pas un robot")     * refuses some of them from some addresses (an address flagged by its bot check): the next is tried. Not the iOS app: without a proof token it serves only the first megabyte of a stream.     * @var list<array{0: int, 1: array<string, mixed>, 2: string}> [client id, client, User-Agent]     */    public const PLAYERS = [        [self::PLAYER_ID, self::PLAYER, self::PLAYER_AGENT],        [28, ['clientName' => 'ANDROID_VR', 'clientVersion' => '1.65.10', 'deviceMake' => 'Oculus', 'deviceModel' => 'Quest 3', 'androidSdkVersion' => 32,            'osName' => 'Android', 'osVersion' => '12L', 'hl' => 'fr', 'gl' => 'FR'],            'com.google.android.apps.youtube.vr.oculus/1.65.10 (Linux; U; Android 12L; eureka-user Build/SQ3A.220605.009.A1) gzip'],    ];    /** search filter "type: video" (no channels, no playlists) */    private const ONLY_VIDEOS = 'EgIQAQ%3D%3D';    /** @var array{query: string, results: list<array{id: string, title: string, channel: string, duration: int, url: string}>, next: ?string, asked: bool} what a search found so far (the next pages go on from it) */    private array $found = ['query' => '', 'results' => [], 'next' => null, 'asked' => false];    /** the visitor YouTube gave this session (its watch page's VISITOR_DATA) and its cookies: without them its bot check refuses the apps */    private string $visitor = '';    private string $cookies = '';    /**     * @param \Closure(string, string, ?string, array<string, string>): Task $request HTTP (method, url, body, headers) -> Task of     *        an \JumPHP\Http\HttpResponse     */    public function __construct(private \Closure $request)    {    }    /** With JumPHP's Http (never waits). */    public static function over(\JumPHP\Http\Http $http): self    {        return new self(fn (string $method, string $url, ?string $body, array $headers): Task => $http->request($method, $url, $body, $headers, 20));    }    /**     * A page of a search ($perPage videos).     * @return Task list<array{id: string, title: string, channel: string, duration: int, url: string}>, or an error (French)     */    public function search(string $query, int $page = 1, int $perPage = YouTube::RESULTS): Task    {        $result = new Task();        $query = trim($query);        if ($this->found['query'] !== $query) {            $this->found = ['query' => $query, 'results' => [], 'next' => null, 'asked' => false];        }        $want = max(1, $page) * $perPage;        $more = function () use (&$more, $result, $query, $want, $page, $perPage): void {            $started = $this->found['asked'];            if (count($this->found['results']) >= $want || ($started && $this->found['next'] === null)) {                $result->settle(array_slice($this->found['results'], ($page - 1) * $perPage, $perPage));                return;            }            $body = ['context' => ['client' => self::WEB]];            $body += $started ? ['continuation' => $this->found['next']] : ['query' => $query, 'params' => rawurldecode(self::ONLY_VIDEOS)];            $this->post('search', $body, self::WEB_ID, self::WEB['clientVersion'], self::WEB_AGENT)->then(function (array $json) use ($more, $query): void {                if ($this->found['query'] !== $query) {                    return;     // another search meanwhile                }                [$videos, $next] = self::parseSearch($json);                $known = array_column($this->found['results'], 'id');                foreach ($videos as $v) {                    if (!in_array($v['id'], $known, true)) {                        $this->found['results'][] = $v;                    }                }                // no video in a page: the end (never asked again and again)                $this->found['asked'] = true;                $this->found['next'] = $videos === [] ? null : $next;                $more();            }, fn (\Throwable $e) => $result->settle(null, $e));        };        $more();        return $result;    }    /**     * A video: its title, channel, length and streams.     * @return Task array{id: string, title: string, author: string, duration: int, description: string, streams: list<array<string, mixed>>},     *         or an error with a French message (age, unavailable, ciphered...)     */    public function video(string $id): Task    {        $result = new Task();        $first = null;        $try = function (int $i) use (&$try, &$first, $id, $result): void {            [$clientId, $client, $agent] = self::PLAYERS[$i];            if ($this->visitor !== '') {                $client['visitorData'] = $this->visitor;            }            $body = ['context' => ['client' => $client], 'videoId' => $id,                'playbackContext' => ['contentPlaybackContext' => ['html5Preference' => 'HTML5_PREF_WANTS']], 'contentCheckOk' => true, 'racyCheckOk' => true];            $next = function (\Throwable $e) use (&$try, &$first, $i, $result): void {                $first ??= $e;                isset(self::PLAYERS[$i + 1]) ? $try($i + 1) : $result->settle(null, $first);            };            $this->post('player', $body, $clientId, (string)$client['clientVersion'], $agent)->then(function (array $json) use ($result, $next): void {                try {                    $result->settle(self::parsePlayer($json));                } catch (\RuntimeException $e) {                    // an adult video, a direct, a video gone: the same for every app; the bot check or no stream: the next app                    $status = (array)($json['playabilityStatus'] ?? []);                    $why = (string)($status['status'] ?? '');                    $gone = in_array($why, ['ERROR', 'LIVE_STREAM_OFFLINE'], true) || str_contains($e->getMessage(), 'adultes') || str_contains($e->getMessage(), 'direct');                    $gone ? $result->settle(null, $e) : $next($e);                }            }, $next);        };        // like yt-dlp: the watch page first (it gives the visitor the player then names), else "confirm you're not a bot"        $this->visit($id)->then(fn () => $try(0), fn () => $try(0));        return $result;    }    /** The watch page of a video, once a session: YouTube's VISITOR_DATA and its cookies. @return Task of string (the visitor, '' if none) */    private function visit(string $id): Task    {        $result = new Task();        if ($this->visitor !== '') {            $result->settle($this->visitor);            return $result;        }        $headers = ['User-Agent' => self::WEB_AGENT, 'Accept-Language' => 'fr-FR,fr;q=0.9', 'Cookie' => 'SOCS=CAI'];   // SOCS: the consent page of Europe answered no        ($this->request)('GET', 'https://www.youtube.com/watch?v=' . rawurlencode($id) . '&bpctr=9999999999&has_verified=1', null, $headers)->then(function (mixed $r) use ($result): void {            $body = is_object($r) && property_exists($r, 'body') ? (string)$r->body : '';            if (preg_match('/"VISITOR_DATA"\s*:\s*"([^"]+)"/', $body, $m) === 1 || preg_match('/"visitorData"\s*:\s*"([^"]+)"/', $body, $m) === 1) {                $this->visitor = $m[1];            }            $set = is_object($r) && property_exists($r, 'headers') ? (string)($r->headers['set-cookie'] ?? '') : '';            $jar = ['SOCS=CAI'];            foreach (preg_split('/,(?=\s*[A-Za-z0-9_]+=)|\n/', $set) ?: [] as $c) {                if (preg_match('/^\s*((?:VISITOR_INFO1_LIVE|VISITOR_PRIVACY_METADATA|YSC|__Secure-ROLLOUT_TOKEN)=[^;]*)/', $c, $m) === 1) {                    $jar[] = $m[1];                }            }            $this->cookies = implode('; ', $jar);            $result->settle($this->visitor);        }, fn (\Throwable $e) => $result->settle(''));        return $result;    }    /** @param array<string, mixed> $body @return Task array (the JSON answer) */    private function post(string $endpoint, array $body, int $clientId, string $clientVersion, string $agent): Task    {        $result = new Task();        $headers = ['Content-Type' => 'application/json', 'X-YouTube-Client-Name' => (string)$clientId, 'X-YouTube-Client-Version' => $clientVersion,            'Origin' => 'https://www.youtube.com', 'User-Agent' => $agent];        if ($this->visitor !== '') {            $headers['X-Goog-Visitor-Id'] = $this->visitor;        }        if ($this->cookies !== '') {            $headers['Cookie'] = $this->cookies;        }        ($this->request)('POST', self::API . $endpoint . '?prettyPrint=false', (string)json_encode($body, JSON_UNESCAPED_SLASHES), $headers)->then(function (mixed $r) use ($result): void {            $json = is_object($r) && property_exists($r, 'body') ? json_decode((string)$r->body, true) : null;            $status = is_object($r) && property_exists($r, 'status') ? (int)$r->status : 0;            if ($status < 200 || $status >= 300 || !is_array($json)) {                $said = is_array($json) ? (string)($json['error']['message'] ?? '') : '';                $result->settle(null, new \RuntimeException('YouTube ne répond pas comme prévu (HTTP ' . $status . ($said !== '' ? ' : ' . $said : '') . ')'));                return;            }            $result->settle($json);        }, fn (\Throwable $e) => $result->settle(null, new \RuntimeException('YouTube injoignable : ' . $e->getMessage(), 0, $e)));        return $result;    }    /**     * The videos of a search answer (first page or continuation) and the token of the next page.     * @param array<mixed> $json     * @return array{0: list<array{id: string, title: string, channel: string, duration: int, url: string}>, 1: ?string}     */    public static function parseSearch(array $json): array    {        $videos = [];        $next = null;        $walk = function (mixed $node) use (&$walk, &$videos, &$next): void {            if (!is_array($node)) {                return;            }            if (isset($node['videoRenderer']) && is_array($node['videoRenderer'])) {                $v = $node['videoRenderer'];                $id = (string)($v['videoId'] ?? '');                if (preg_match('/^[A-Za-z0-9_-]{11}$/', $id)) {                    $videos[] = ['id' => $id, 'title' => self::text($v['title'] ?? null) ?: $id,                        'channel' => self::text($v['ownerText'] ?? $v['longBylineText'] ?? $v['shortBylineText'] ?? null),                        'duration' => self::seconds(self::text($v['lengthText'] ?? null)), 'url' => 'https://www.youtube.com/watch?v=' . $id];                }                return;            }            if (isset($node['continuationCommand']['token']) && is_string($node['continuationCommand']['token'])) {                $next = $node['continuationCommand']['token'];            }            foreach ($node as $child) {                $walk($child);            }        };        $walk($json);        return [$videos, $next];    }    /**     * A player answer: the video and its plain streams, or why not (a French message).     * @param array<mixed> $json     * @return array{id: string, title: string, author: string, duration: int, description: string, streams: list<array<string, mixed>>}     * @throws \RuntimeException     */    public static function parsePlayer(array $json): array    {        $status = (array)($json['playabilityStatus'] ?? []);        $state = (string)($status['status'] ?? '');        $reason = trim((string)($status['reason'] ?? ''));        if ($state !== 'OK') {            $adult = isset($status['desktopLegacyAgeGateReason']) || preg_match('/\b(age|âge)\b|adult|confirm your age|confirmer votre âge/iu', $reason . ' ' . json_encode($status));            throw new \RuntimeException(match (true) {                $state === 'LOGIN_REQUIRED' && $adult => 'vidéo réservée aux adultes : YouTube demande un compte (impossible sans yt-dlp)',                $state === 'LOGIN_REQUIRED' && preg_match('/robot|bot\b/iu', $reason) === 1                    => 'YouTube bloque pour le moment les téléchargements depuis ce réseau (vérification anti-robot). Réessayez plus tard, '                        . 'ou laissez le PC le récupérer : playlist cloud, « Le PC envoie le MP3 »',                $state === 'LOGIN_REQUIRED' => 'YouTube demande un compte pour cette vidéo' . ($reason !== '' ? ' (' . $reason . ')' : ''),                $state === 'LIVE_STREAM_OFFLINE' => 'ce direct n\'a pas encore commencé',                default => 'vidéo indisponible' . ($reason !== '' ? ' : ' . $reason : ''),            });        }        $details = (array)($json['videoDetails'] ?? []);        if (!empty($details['isLive']) || !empty($details['isLiveContent']) && empty($details['lengthSeconds'])) {            throw new \RuntimeException('un direct ne se télécharge pas');        }        $data = (array)($json['streamingData'] ?? []);        $streams = [];        $ciphered = 0;        foreach ([...(array)($data['formats'] ?? []), ...(array)($data['adaptiveFormats'] ?? [])] as $f) {            if (!is_array($f)) {                continue;            }            if (!isset($f['url'])) {                $ciphered += isset($f['signatureCipher']) || isset($f['cipher']) ? 1 : 0;                continue;            }            $mime = (string)($f['mimeType'] ?? '');            $streams[] = ['itag' => (int)($f['itag'] ?? 0), 'url' => (string)$f['url'], 'mime' => $mime,                'audio' => isset($f['audioQuality']) || str_starts_with($mime, 'audio/'), 'video' => str_starts_with($mime, 'video/'),                'height' => (int)($f['height'] ?? 0), 'bitrate' => (int)($f['bitrate'] ?? 0), 'size' => (int)($f['contentLength'] ?? 0)];        }        if ($streams === []) {            throw new \RuntimeException($ciphered > 0 ? 'YouTube a chiffré les adresses de cette vidéo (signature) : il faut yt-dlp'                : 'YouTube n\'a donné aucun flux téléchargeable pour cette vidéo');        }        return ['id' => (string)($details['videoId'] ?? ''), 'title' => (string)($details['title'] ?? ''), 'author' => (string)($details['author'] ?? ''),            'duration' => (int)($details['lengthSeconds'] ?? 0), 'description' => (string)($details['shortDescription'] ?? ''), 'streams' => $streams];    }    /**     * The streams to take: for an MP3 the best sound; for an MP4 a video up to 720p (H.264 first: every player reads it)     * and the M4A sound (copied as it is into the MP4), or one stream with both.     * @param list<array<string, mixed>> $streams     * @return list<array<string, mixed>> what to download, in the order ffmpeg gets them (the video first)     */    public static function pick(array $streams, string $format): array    {        $audio = array_values(array_filter($streams, fn (array $s) => $s['audio'] && !$s['video']));        $m4a = array_values(array_filter($audio, fn (array $s) => str_contains((string)$s['mime'], 'audio/mp4')));        $best = fn (array $list): ?array => $list === [] ? null : array_reduce($list, fn (?array $a, array $s) => $a === null || $s['bitrate'] > $a['bitrate'] ? $s : $a);        if ($format !== 'mp4') {            $sound = $best($audio) ?? $best(array_values(array_filter($streams, fn (array $s) => $s['audio'])));            return $sound !== null ? [$sound] : [];        }        $videos = array_values(array_filter($streams, fn (array $s) => $s['video'] && !$s['audio'] && $s['height'] > 0 && $s['height'] <= 720));        $h264 = array_values(array_filter($videos, fn (array $s) => str_contains((string)$s['mime'], 'avc1')));        $byHeight = fn (array $list): ?array => $list === [] ? null : array_reduce($list, fn (?array $a, array $s) => $a === null            || $s['height'] > $a['height'] || ($s['height'] === $a['height'] && $s['bitrate'] > $a['bitrate']) ? $s : $a);        $video = $byHeight($h264) ?? $byHeight($videos);        $sound = $best($m4a) ?? $best($audio);        if ($video !== null && $sound !== null) {            return [$video, $sound];        }        // one stream with both (itag 18), else what there is        $both = $byHeight(array_values(array_filter($streams, fn (array $s) => $s['video'] && $s['audio'])));        return $both !== null ? [$both] : array_values(array_filter([$video, $sound]));    }    /** The id of a YouTube address (watch, youtu.be, shorts, embed, music, live), or an id alone; null: not YouTube. */    public static function videoId(string $url): ?string    {        $url = trim($url);        if (preg_match('/^[A-Za-z0-9_-]{11}$/', $url)) {            return $url;        }        if (!preg_match('#^(?:https?://)?(?:[a-z0-9-]+\.)?(?:youtube\.com|youtube-nocookie\.com|youtu\.be)/#i', $url)) {            return null;        }        if (preg_match('#youtu\.be/([A-Za-z0-9_-]{11})#', $url, $m) || preg_match('#/(?:shorts|embed|live|v)/([A-Za-z0-9_-]{11})#', $url, $m)            || preg_match('#[?&]v=([A-Za-z0-9_-]{11})#', $url, $m)) {            return $m[1];        }        return null;    }    /** "1:02:05" -> 3725 */    public static function seconds(string $clock): int    {        if (!preg_match('/^\d+(:\d{1,2}){0,2}$/', trim($clock))) {            return 0;        }        $total = 0;        foreach (explode(':', trim($clock)) as $part) {            $total = $total * 60 + (int)$part;        }        return $total;    }    /** The text of a "runs" / "simpleText" node. */    private static function text(mixed $node): string    {        if (!is_array($node)) {            return '';        }        if (isset($node['simpleText'])) {            return (string)$node['simpleText'];        }        return implode('', array_map(fn ($r) => is_array($r) ? (string)($r['text'] ?? '') : '', (array)($node['runs'] ?? [])));    }}