<?phpnamespace Tests;use App\Media\Process;use App\Media\RangedDownload;use App\Media\Tools;use App\Media\YouTube;use App\Media\YouTubeInnerTube;use App\Media\YouTubeNativeJob;use JumPHP\Async\Task;use JumPHP\Http\HttpResponse;/** * YouTube without yt-dlp (the phone app): InnerTube's answers (fixtures of real ones, 2026-11), a fake HTTP, a local * server for the streams (by ranges), a fake ffmpeg. No network. */final class YouTubeNativeTest extends PlayerTestCase{ /** @var list<array{url: string, body: array<mixed>}> what was asked to YouTube */ private array $asked = []; /** @var array<string, mixed> the player's answer given by the fake YouTube */ private array $player = []; /** @var list<list<string>> the ffmpeg commands */ private array $ffmpeg = []; /** @var list<string> the first stream given to each ffmpeg, as it was then */ private array $streams = []; private string $music = ''; private string $videos = ''; private static function fixture(string $name): array { return (array)json_decode((string)file_get_contents(__DIR__ . "/fixtures/innertube/$name.json"), true); } /** InnerTube answering with the fixtures (search: page 1, then a continuation page). */ private function fakeYouTube(): YouTubeInnerTube { $this->player = $this->player ?: self::fixture('player'); return new YouTubeInnerTube(function (string $method, string $url, ?string $body, array $headers): Task { if ($method === 'GET' && str_contains($url, '/watch?v=')) { $page = new Task(); $page->settle(new HttpResponse(200, '<script>ytcfg.set({"VISITOR_DATA":"CgtWaXNpdG9yVGVzdA%3D%3D"});</script>', ['set-cookie' => 'YSC=abc; path=/, VISITOR_INFO1_LIVE=xyz; path=/'])); return $page; } $json = (array)json_decode((string)$body, true); $this->asked[] = ['url' => $url, 'body' => $json, 'headers' => $headers]; self::assertSame('POST', $method); self::assertSame('application/json', $headers['Content-Type']); self::assertStringNotContainsStringIgnoringCase('winzik', implode(' ', $headers), 'nothing of the user nor of WinZik sent'); $answer = match (true) { str_contains($url, '/search') && isset($json['continuation']) => ['onResponseReceivedCommands' => [['appendContinuationItemsAction' => ['continuationItems' => [ ['itemSectionRenderer' => ['contents' => [['videoRenderer' => ['videoId' => 'Page2Video1', 'title' => ['runs' => [['text' => 'Renaud - Laisse béton']]], 'ownerText' => ['runs' => [['text' => 'Renaud']]], 'lengthText' => ['simpleText' => '1:02:05']]]]]], ]]]]], str_contains($url, '/search') => self::fixture('search'), str_contains($url, '/player') => $this->player, default => [], }; $t = new Task(); $t->settle(new HttpResponse(200, (string)json_encode($answer))); return $t; }); } /** WinZik as on a phone: no yt-dlp, the app's ffmpeg (a fake that writes what the real one would). */ private function startPhone(?string $ffmpeg = '/app/lib/ffmpeg'): YouTube { $this->startPlayer(); $this->music = $this->wz->storagePath('test/Musique/WinZik'); $this->videos = $this->wz->storagePath('test/Vidéos/WinZik'); $tools = Tools::forPhone($this->wz->storagePath('bin'), $this->wz->storagePath('tmp'), $ffmpeg); $this->wz->useMedia($tools, function (array $cmd): Process { $this->ffmpeg[] = $cmd; $input = (string)($cmd[(int)array_search('-i', $cmd, true) + 1] ?? ''); $this->streams[] = is_file($input) ? (string)file_get_contents($input) : ''; $out = (string)end($cmd); return Process::finished($cmd, [], 0, function () use ($out): void { @mkdir(dirname($out), 0777, true); copy($this->wz->path('assets/music/partage.mp3'), $out); }); }, $this->music, $this->videos); $yt = $this->wz->youtube; self::assertNotNull($yt); $yt->innerTube = $this->fakeYouTube(); return $yt; } public function testThePhoneHasNoYtdlpNorWhisperAndItsFfmpegComesFromTheApp(): void { $file = sys_get_temp_dir() . '/wz-ffmpeg-' . getmypid(); file_put_contents($file, '#!/bin/sh'); putenv('JUMPHP_FFMPEG=' . $file); try { $tools = Tools::forPhone(sys_get_temp_dir() . '/wz-bin', sys_get_temp_dir() . '/wz-tmp'); self::assertSame($file, $tools->program('ffmpeg'), 'JUMPHP_FFMPEG'); self::assertNull($tools->program('yt-dlp')); self::assertFalse($tools->whisper()); $tools->tick(10.0, true); self::assertSame('unsupported', $tools->state['yt-dlp'], 'nothing downloaded on a phone'); } finally { putenv('JUMPHP_FFMPEG'); @unlink($file); } $none = Tools::forPhone(sys_get_temp_dir() . '/wz-bin', sys_get_temp_dir() . '/wz-tmp'); self::assertNull($none->program('ffmpeg')); self::assertStringContainsString('ffmpeg manque', $none->ffmpegMissing()); $yt = $this->startPhone(); self::assertTrue($yt->native()); self::assertTrue($yt->ready()); self::assertNull($this->wz->transcriber, 'no Whisper on a phone'); } public function testASearchOfYoutubeWithoutYtdlpGivesTitlesChannelsLengthsAndItsNextPage(): void { $yt = $this->startPhone(); self::assertTrue($yt->search('renaud mistral gagnant')); $this->game->run(2); self::assertSame('', $yt->error); self::assertSame('', $yt->searching); self::assertCount(5, $yt->results, 'the 4 of the first answer, then its continuation (15 a page wanted)'); self::assertSame(3725, $yt->results[4]['duration']); self::assertSame(['id' => '_YqzuE-5RE8', 'title' => 'Renaud - Mistral gagnant (Clip officiel)', 'channel' => 'Renaud', 'duration' => 163, 'url' => 'https://www.youtube.com/watch?v=_YqzuE-5RE8'], $yt->results[0]); self::assertStringEndsWith('/youtubei/v1/search?prettyPrint=false', $this->asked[0]['url']); self::assertSame('WEB', $this->asked[0]['body']['context']['client']['clientName']); self::assertSame('renaud mistral gagnant', $this->asked[0]['body']['query']); self::assertArrayHasKey('continuation', $this->asked[1]['body'], 'the next answer by the token of the one before'); // page 2: nothing more (the continuation had no next one) self::assertTrue($yt->search('renaud mistral gagnant', 2)); $this->game->run(2); self::assertCount(2, $this->asked, 'not asked again'); self::assertSame([], $yt->results); // YouTube Music: the same search; SoundCloud: needs yt-dlp $yt->search('renaud', 1, 'ytmusic'); $this->game->run(2); self::assertSame('', $yt->error); $yt->search('renaud', 1, 'soundcloud'); self::assertStringContainsString('il faut yt-dlp', $yt->error); } public function testWhenYoutubesBotCheckRefusesAnAppTheNextOneIsAsked(): void { $asked = []; $ok = self::fixture('player'); $api = new YouTubeInnerTube(function (string $method, string $url, ?string $body, array $headers) use (&$asked, $ok): Task { if ($method === 'GET') { $page = new Task(); $page->settle(new HttpResponse(200, '"VISITOR_DATA":"CgtWaXNpdG9yVGVzdA%3D%3D"')); return $page; } $ctx = (array)(json_decode((string)$body, true)['context']['client'] ?? []); self::assertSame('CgtWaXNpdG9yVGVzdA%3D%3D', $ctx['visitorData'] ?? null, 'the visitor of the watch page, as yt-dlp sends it'); self::assertSame('CgtWaXNpdG9yVGVzdA%3D%3D', $headers['X-Goog-Visitor-Id'] ?? null); $client = (string)($ctx['clientName'] ?? ''); $asked[] = $client; $answer = $client === 'ANDROID_VR' ? $ok : ['playabilityStatus' => ['status' => 'LOGIN_REQUIRED', 'reason' => "Connectez-vous pour confirmer que vous n'êtes pas un robot"]]; $t = new Task(); $t->settle(new HttpResponse(200, (string)json_encode($answer))); return $t; }); $video = null; $api->video('sMcWOaJFuw0')->then(function (mixed $v) use (&$video): void { $video = $v; }); self::assertSame(['VISIONOS', 'ANDROID_VR'], $asked, 'refused by the bot check: the next app is asked'); self::assertIsArray($video); self::assertNotEmpty($video['streams']); } public function testThePlayerGivesPlainStreamsAndTheBestOnesAreTaken(): void { $video = YouTubeInnerTube::parsePlayer(self::fixture('player')); self::assertSame('Rick Astley', $video['author']); self::assertSame(213, $video['duration']); $mp3 = YouTubeInnerTube::pick($video['streams'], 'mp3'); self::assertCount(1, $mp3); self::assertTrue($mp3[0]['audio'] && !$mp3[0]['video'], 'the sound only'); self::assertSame(251, $mp3[0]['itag'], 'the best sound'); $mp4 = YouTubeInnerTube::pick($video['streams'], 'mp4'); self::assertSame([136, 140], array_column($mp4, 'itag'), 'H.264 720p + the M4A sound'); self::assertSame('dQw4w9WgXcQ', YouTubeInnerTube::videoId('https://youtu.be/dQw4w9WgXcQ?t=3')); self::assertSame('dQw4w9WgXcQ', YouTubeInnerTube::videoId('https://music.youtube.com/watch?v=dQw4w9WgXcQ&list=x')); self::assertSame('dQw4w9WgXcQ', YouTubeInnerTube::videoId('https://www.youtube.com/shorts/dQw4w9WgXcQ')); self::assertNull(YouTubeInnerTube::videoId('https://soundcloud.com/a/b')); } public function testAnAgeRestrictedAnUnavailableOrACipheredVideoSaysWhyInFrench(): void { foreach (['player-age' => 'réservée aux adultes', 'player-unavailable' => 'indisponible', 'player-ciphered' => 'chiffré'] as $name => $said) { try { YouTubeInnerTube::parsePlayer(self::fixture($name)); self::fail("$name: an error"); } catch (\RuntimeException $e) { self::assertStringContainsString($said, $e->getMessage(), $name); } } // in the queue: the line says it, nothing is converted $yt = $this->startPhone(); $this->player = self::fixture('player-age'); $yt->innerTube = $this->fakeYouTube(); $yt->download(['id' => 'x', 'title' => 'Interdit', 'url' => 'https://www.youtube.com/watch?v=HtVdAasjOgU'], 'mp3'); $this->game->run(3); self::assertSame('error', $yt->jobs[0]['state']); self::assertStringContainsString('réservée aux adultes', $yt->jobs[0]['error']); self::assertSame([], $this->ffmpeg); } public function testTheConversionCommandLineMakesATaggedMp3OrCopiesTheMp4(): void { $tags = ['title' => 'Mistral gagnant', 'artist' => 'Renaud', 'comment' => '']; self::assertSame(['/app/ffmpeg', '-y', '-v', 'error', '-nostdin', '-i', '/tmp/a.stream', '-map', '0:a:0', '-vn', '-c:a', 'libmp3lame', '-q:a', '0', '-id3v2_version', '3', '-map_metadata', '-1', '-threads', '1', '-metadata', 'title=Mistral gagnant', '-metadata', 'artist=Renaud', '/m/Mistral gagnant.mp3'], YouTubeNativeJob::command('/app/ffmpeg', ['/tmp/a.stream'], 'mp3', '/m/Mistral gagnant.mp3', $tags)); self::assertSame(['/app/ffmpeg', '-y', '-v', 'error', '-nostdin', '-i', '/tmp/v.stream', '-i', '/tmp/a.stream', '-map', '0:v:0', '-map', '1:a:0', '-c', 'copy', '-movflags', '+faststart', '-map_metadata', '-1', '-threads', '1', '-metadata', 'title=Mistral gagnant', '-metadata', 'artist=Renaud', '/v/Mistral gagnant.mp4'], YouTubeNativeJob::command('/app/ffmpeg', ['/tmp/v.stream', '/tmp/a.stream'], 'mp4', '/v/Mistral gagnant.mp4', $tags)); self::assertSame('AC⧸DC: Back?', YouTubeNativeJob::fileName('AC/DC: Back?')); self::assertLessThanOrEqual(120, strlen(YouTubeNativeJob::fileName(str_repeat('é', 200)))); } public function testASpotifySongIsFoundOnYoutubeDownloadedByRangesThenMadeAnMp3InThePlayList(): void { $port = 19000 + random_int(0, 999); $log = sys_get_temp_dir() . '/wz-streams-' . getmypid() . '.log'; @unlink($log); $server = proc_open([PHP_BINARY, '-S', "127.0.0.1:$port", __DIR__ . '/fixtures/stream-server.php'], [1 => ['file', '/dev/null', 'w'], 2 => ['file', '/dev/null', 'w']], $pipes, null, ['WZ_STREAM_LOG' => $log] + getenv()); self::assertIsResource($server); try { usleep(400000); // the streams on the local server, small (3 ranges of 100 000 bytes for the sound chosen) $player = self::fixture('player'); foreach ($player['streamingData']['adaptiveFormats'] as &$f) { $f['url'] = str_replace('https://rr1---sn-fixture.googlevideo.com', "http://127.0.0.1:$port", $f['url']); $f['contentLength'] = '250000'; } unset($f); $this->player = $player; $yt = $this->startPhone(); $yt->streamDownload = fn (string $url, string $target, int $size): RangedDownload => new RangedDownload($url, $target, $size, 100000); $before = $this->wz->playlist->count(); // what an import of a Spotify playlist puts in the queue (SpotifyAccount::songs()) $yt->download(['id' => 'ytsearch1:Renaud Mistral gagnant', 'title' => 'Renaud - Mistral gagnant', 'url' => 'ytsearch1:Renaud Mistral gagnant'], 'mp3', 'Renaud - Mistral gagnant'); $this->ffmpeg = $this->streams = []; $until = microtime(true) + 20; while (($yt->jobs[0]['state'] ?? '') !== 'done' && ($yt->jobs[0]['state'] ?? '') !== 'error' && microtime(true) < $until) { $this->game->run(1); usleep(2000); } self::assertSame('done', $yt->jobs[0]['state'], 'downloaded: ' . $yt->jobs[0]['error']); // the search of the song, then its video's streams (the first video found) self::assertSame('Renaud Mistral gagnant', $this->asked[0]['body']['query']); self::assertSame('_YqzuE-5RE8', $this->asked[1]['body']['videoId']); self::assertSame('VISIONOS', $this->asked[1]['body']['context']['client']['clientName']); // by ranges, in order, joined whole $ranges = array_map(fn (string $l) => preg_match('/range=(\d+-\d+)/', $l, $m) ? $m[1] : '', array_filter(explode("\n", (string)file_get_contents($log)))); self::assertSame(['0-99999', '100000-199999', '200000-249999'], array_values($ranges)); self::assertStringContainsString('itag=251', (string)file_get_contents($log), 'the best sound'); $pattern = ''; for ($i = 0; $i < 251; $i++) { $pattern .= chr($i); } self::assertSame(substr(str_repeat($pattern, 1000), 0, 250000), $this->streams[0] ?? null, 'the stream as YouTube has it, byte for byte'); // ffmpeg: the MP3 with its tags, into the music folder $cmd = $this->ffmpeg[0]; self::assertSame('/app/lib/ffmpeg', $cmd[0]); self::assertContains('libmp3lame', $cmd); self::assertContains('title=Rick Astley - Never Gonna Give You Up (Official Video) (4K Remaster)', $cmd); self::assertContains('artist=Rick Astley', $cmd); $file = $this->music . '/Rick Astley - Never Gonna Give You Up (Official Video) (4K Remaster).mp3'; self::assertSame($file, end($cmd)); self::assertFileExists($file); $this->game->run(2); self::assertSame($before + 1, $this->wz->playlist->count(), 'added to the play list'); self::assertSame([], glob($this->wz->storagePath('tmp/youtube') . '/*') ?: [], 'the streams removed once converted'); // the import remembers it: not downloaded again $yt->forgetFiles(); self::assertSame($file, $yt->alreadyHave('Renaud - Mistral gagnant', 'ytsearch1:Renaud Mistral gagnant')); @unlink($file); } finally { proc_terminate($server); proc_close($server); @unlink($log); } } public function testWithoutItsFfmpegThePhoneSaysWhyForADownloadAndAFilm(): void { $yt = $this->startPhone(null); self::assertStringContainsString('Ffmpeg manque', $yt->cannotDownload()); $yt->download(['id' => 'x', 'title' => 'Un', 'url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'], 'mp3'); self::assertSame('error', $yt->jobs[0]['state']); self::assertStringContainsString('ffmpeg manque', $yt->jobs[0]['error']); $film = $this->wz->storagePath('test/film.mp4'); @mkdir(dirname($film), 0777, true); file_put_contents($film, 'film'); $video = $this->wz->video; self::assertNotNull($video); self::assertNull($video->audio($film)); self::assertStringContainsString('ffmpeg manque', $video->error($film)); } public function testAYtdlpThatFailsOnYoutubeIsReplacedByYoutubesOwnApi(): void { self::assertTrue(YouTube::ytdlpBroken('ERROR: [youtube] dQw4w9WgXcQ: Sign in to confirm you are not a bot')); self::assertTrue(YouTube::ytdlpBroken(''), 'a yt-dlp that did not even run'); self::assertFalse(YouTube::ytdlpBroken('erreur 1')); }}