<?phpnamespace App\Media;/** * YouTube through yt-dlp: a search ("ytsearch15:...", -J: the results as JSON, nothing downloaded) and downloads, each * a yt-dlp in the background: MP3 (-x --audio-format mp3: ffmpeg converts) into Musique/WinZik, or MP4 (best video * up to 720p + its sound, merged by ffmpeg) into Vidéos/WinZik. The finished files come back by finished(): WinZik adds * them to the play list. * * Without yt-dlp (the phone app; a computer whose yt-dlp is missing or fails): YouTubeInnerTube, YouTube's own web API in * PHP: the search of YouTube (and YouTube Music: the same search), the downloads of YouTube addresses and of * "ytsearch1:" songs (Spotify / Deezer imports), each a YouTubeNativeJob in the same queue (same states, progress, notices). */final class YouTube{ public const RESULTS = 15; /** Spotify's search: 10 at a time at most */ public const SPOTIFY_RESULTS = 10; /** * Where to search (what yt-dlp knows how to search), and how. Spotify and Deezer protect their songs (DRM): nothing * downloads them; Deezer's public search gives the songs, each then taken from YouTube ("ytsearch1:artist - title"). */ public const SOURCES = ['youtube' => 'YouTube', 'ytmusic' => 'YouTube Music', 'soundcloud' => 'SoundCloud', 'dailymotion' => 'Dailymotion', 'bilibili' => 'Bilibili', 'niconico' => 'Niconico', 'google' => 'Google Vidéos', 'deezer' => 'Deezer → YouTube', 'spotify' => 'Spotify → YouTube']; /** the user's own Spotify keys (developer.spotify.com, free): the search of Spotify needs them */ public string $spotifyId = ''; public string $spotifySecret = ''; private string $spotifyToken = ''; private float $spotifyUntil = 0.0; public string $source = 'youtube'; public int $page = 1; public string $query = ''; /** downloads at the same time: the others wait in the queue */ public const PARALLEL = 1; /** a search that does not answer is given up */ public const SEARCH_TIMEOUT = 45.0; private float $searchStarted = 0.0; /** @var list<array{id: string, title: string, channel: string, duration: int, url: string}> */ public array $results = []; public string $searching = ''; public string $error = ''; private ?Process $search = null; /** @var list<array{title: string, format: string, process: ?Process, progress: float, state: string, file: string, error: string, args: list<string>, url?: string, key?: string, bounds?: array{0: float, 1: float}|null, native?: ?YouTubeNativeJob, nativeTried?: bool}> state: queued | download | convert | done | error */ public array $jobs = []; /** @var list<array{file: string, format: string, title: string, bounds: array{0: float, 1: float}|null}> finished since the last call of finished() */ private array $done = []; /** @var \Closure(list<string>): Process */ private \Closure $runner; /** YouTube without yt-dlp (null: no network here) */ public ?YouTubeInnerTube $innerTube = null; /** a native search: its number (an older answer arriving late is dropped) */ private int $searchId = 0; /** @var (\Closure(string, string, int): RangedDownload)|null how a native job downloads a stream (tests) */ public ?\Closure $streamDownload = null; /** @param (\Closure(list<string>): Process)|null $runner how a program is started (tests: a fake one) */ public function __construct(private Tools $tools, private string $logDir, ?\Closure $runner = null, public string $musicDir = '', public string $videoDir = '', private ?\JumPHP\Http\Http $http = null) { $this->runner = $runner ?? fn (array $command): Process => Process::start($command, $this->logDir); $this->musicDir = $musicDir !== '' ? $musicDir : MediaDirs::winzik('MUSIC'); $this->videoDir = $videoDir !== '' ? $videoDir : MediaDirs::winzik('VIDEOS'); $this->innerTube = $http !== null ? YouTubeInnerTube::over($http) : null; } /** A search can be made: yt-dlp, or YouTube's own API. */ public function ready(): bool { return $this->tools->ready('yt-dlp') || $this->native(); } /** No yt-dlp here (the phone, or not there yet / any more): YouTube through its own API (YouTubeInnerTube). */ public function native(): bool { return !$this->tools->ready('yt-dlp') && $this->innerTube !== null; } /** Why nothing can be downloaded now (French), '' when it can. */ public function cannotDownload(): string { if (!$this->ready()) { return 'yt-dlp arrive (téléchargement en cours) : réessayez dans un instant.'; } $missing = $this->tools->ffmpegMissing(); return $missing !== '' ? ucfirst($missing) . '.' : ''; } /** @param list<string> $args */ private function ytdlp(array $args, bool $background = false): Process { $command = array_merge([(string)$this->tools->program('yt-dlp'), '--no-warnings', '--encoding', 'utf-8'], $args); if ($background && PHP_OS_FAMILY !== 'Windows') { // a download: the lowest priority (processor and disk), ffmpeg after it too: the sound never waits for it if (Tools::onPath('ionice') !== null) { $command = ['ionice', '-c', '3', ...$command]; } if (Tools::onPath('nice') !== null) { $command = ['nice', '-n', '19', ...$command]; } } $process = ($this->runner)($command); if ($background && PHP_OS_FAMILY === 'Windows' && ($pid = $process->pid()) > 0) { // Windows: the class "idle" (ffmpeg, started by yt-dlp, inherits it) ($this->runner)(['powershell', '-NoProfile', '-WindowStyle', 'Hidden', '-Command', "(Get-Process -Id $pid).PriorityClass = 'Idle'"]); } return $process; } /** A search on a source, its page $page (15 results a page). */ public function search(string $query, int $page = 1, ?string $source = null): bool { $query = trim($query); $source = $source !== null && isset(self::SOURCES[$source]) ? $source : $this->source; if ($query === '' || (!in_array($source, ['deezer', 'spotify'], true) && !$this->ready())) { return false; } $this->search?->kill(); [$this->query, $this->page, $this->source] = [$query, max(1, $page), $source]; $this->searching = $query; $this->error = ''; $this->searchStarted = microtime(true); if ($source === 'deezer') { $this->searchDeezer($query, $this->page); return true; } if ($source === 'spotify') { $this->searchSpotify($query, $this->page); return true; } if ($this->native()) { if ($source === 'youtube' || $source === 'ytmusic') { $this->searchNative($query, $this->page); } else { $this->searching = ''; $this->results = []; $this->error = self::SOURCES[$source] . ' : il faut yt-dlp (WinZik sur un ordinateur) ; YouTube, Deezer et Spotify marchent ici'; } return true; } $n = self::RESULTS * $this->page; $from = self::RESULTS * ($this->page - 1) + 1; $target = match ($source) { 'ytmusic' => 'https://music.youtube.com/search?q=' . rawurlencode($query) . '#songs', 'soundcloud' => "scsearch$n:$query", 'dailymotion' => 'https://www.dailymotion.com/search/' . rawurlencode($query) . '/videos', 'bilibili' => "bilisearch$n:$query", 'niconico' => "nicosearch$n:$query", 'google' => "gvsearch$n:$query", default => "ytsearch$n:$query", }; $this->search = $this->ytdlp(['--flat-playlist', '-J', '--playlist-items', "$from-$n", $target]); return true; } /** The search of YouTube through its own API (no yt-dlp). */ private function searchNative(string $query, int $page): void { $id = ++$this->searchId; $api = $this->innerTube; if ($api === null) { return; } $api->search($query, $page, self::RESULTS)->then(function (mixed $results) use ($id): void { if ($id !== $this->searchId) { return; } $this->searching = ''; $this->results = is_array($results) ? array_values($results) : []; $this->error = ''; }, function (\Throwable $e) use ($id): void { if ($id !== $this->searchId) { return; } $this->searching = ''; $this->results = []; $this->error = 'recherche impossible : ' . $e->getMessage(); }); } public function nextPage(): bool { return $this->query !== '' && $this->search($this->query, $this->page + 1); } public function previousPage(): bool { return $this->query !== '' && $this->page > 1 && $this->search($this->query, $this->page - 1); } /** Spotify's search, with the user's own keys (client credentials): its songs, each downloaded from YouTube. */ private function searchSpotify(string $query, int $page): void { if ($this->http === null || $this->spotifyId === '' || $this->spotifySecret === '') { $this->searching = ''; $this->error = 'Spotify : mettez vos identifiants (gratuits, developer.spotify.com > Create app) dans les Options'; return; } $search = function (string $token) use ($query, $page): void { // Spotify gives 10 results at most at a time (more: HTTP 400 "Invalid limit") $url = 'https://api.spotify.com/v1/search?' . http_build_query(['q' => $query, 'type' => 'track', 'limit' => self::SPOTIFY_RESULTS, 'offset' => ($page - 1) * self::SPOTIFY_RESULTS]); $this->http?->request('GET', $url, null, ['Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json'], 20)->then(function (\JumPHP\Http\HttpResponse $r): void { $this->searching = ''; $this->results = []; foreach ($r->ok() ? (array)($r->json()['tracks']['items'] ?? []) : [] as $t) { if (!is_array($t)) { continue; } $artist = implode(', ', array_map(fn ($a) => (string)($a['name'] ?? ''), (array)($t['artists'] ?? []))); $title = (string)($t['name'] ?? ''); $this->results[] = ['id' => (string)($t['id'] ?? $title), 'title' => "$artist - $title", 'channel' => 'Spotify → YouTube', 'duration' => intdiv((int)($t['duration_ms'] ?? 0), 1000), 'url' => 'ytsearch1:' . str_replace(',', '', $artist) . ' ' . $title]; } $said = $r->ok() ? '' : (string)($r->json()['error']['message'] ?? ''); $this->error = $r->ok() ? '' : 'Spotify : HTTP ' . $r->status . ($said !== '' ? ' (' . $said . ')' : '') . ($r->status === 401 ? ' : vos identifiants ?' : ''); }, function (\Throwable $e): void { $this->searching = ''; $this->error = 'Spotify injoignable : ' . $e->getMessage(); }); }; if ($this->spotifyToken !== '' && microtime(true) < $this->spotifyUntil) { $search($this->spotifyToken); return; } $this->http->request('POST', 'https://accounts.spotify.com/api/token', 'grant_type=client_credentials', [ 'Authorization' => 'Basic ' . base64_encode($this->spotifyId . ':' . $this->spotifySecret), 'Content-Type' => 'application/x-www-form-urlencoded'], 20)->then(function (\JumPHP\Http\HttpResponse $r) use ($search): void { $token = $r->ok() ? (string)($r->json()['access_token'] ?? '') : ''; if ($token === '') { $this->searching = ''; $this->error = 'Spotify refuse ces identifiants (HTTP ' . $r->status . ')'; return; } $this->spotifyToken = $token; $this->spotifyUntil = microtime(true) + (int)($r->json()['expires_in'] ?? 3600) - 60; $search($token); }, function (\Throwable $e): void { $this->searching = ''; $this->error = 'Spotify injoignable : ' . $e->getMessage(); }); } /** Deezer's public search (no account): its songs, each downloaded from YouTube by its artist and title. */ private function searchDeezer(string $query, int $page): void { if ($this->http === null) { $this->searching = ''; $this->error = 'Deezer : pas de réseau ici'; return; } $url = 'https://api.deezer.com/search?' . http_build_query(['q' => $query, 'index' => ($page - 1) * self::RESULTS, 'limit' => self::RESULTS]); $this->http->request('GET', $url, null, ['Accept' => 'application/json', 'User-Agent' => 'WinZik'], 20)->then(function (\JumPHP\Http\HttpResponse $r): void { $this->searching = ''; $data = $r->ok() ? $r->json() : null; $this->results = []; foreach (is_array($data) ? (array)($data['data'] ?? []) : [] as $t) { if (!is_array($t)) { continue; } $artist = (string)($t['artist']['name'] ?? ''); $title = (string)($t['title'] ?? ''); $this->results[] = ['id' => (string)($t['id'] ?? $title), 'title' => trim("$artist - $title", ' -'), 'channel' => 'Deezer → YouTube', 'duration' => (int)($t['duration'] ?? 0), 'url' => 'ytsearch1:' . trim("$artist $title")]; } $this->error = $r->ok() ? '' : 'Deezer : HTTP ' . $r->status; }, function (\Throwable $e): void { $this->searching = ''; $this->error = 'Deezer injoignable : ' . $e->getMessage(); }); } /** * A link of Spotify or Deezer (a song, an album, a playlist): its songs (artist and title), each then taken from * YouTube ("ytsearch1:..."). Spotify: its public embed page (no account); Deezer: its public API. * @return \JumPHP\Async\Task list<array{title: string, url: string}> (empty: nothing found) */ public function resolveLink(string $url): \JumPHP\Async\Task { $result = new \JumPHP\Async\Task(); if ($this->http === null) { $result->settle([]); return $result; } $song = fn (string $artist, string $title): array => ['title' => trim("$artist - $title", ' -'), 'url' => 'ytsearch1:' . trim("$artist $title")]; if (preg_match('#deezer\.com/(?:[a-z]{2}/)?(track|album|playlist)/(\d+)#', $url, $m)) { $api = $m[1] === 'track' ? "https://api.deezer.com/track/{$m[2]}" : "https://api.deezer.com/{$m[1]}/{$m[2]}/tracks?limit=300"; $this->http->request('GET', $api, null, ['Accept' => 'application/json', 'User-Agent' => 'WinZik'], 20)->then(function (\JumPHP\Http\HttpResponse $r) use ($result, $song, $m): void { $data = $r->ok() ? $r->json() : null; $tracks = $m[1] === 'track' ? [$data] : (array)($data['data'] ?? []); $out = []; foreach ($tracks as $t) { if (is_array($t) && isset($t['title'])) { $out[] = $song((string)($t['artist']['name'] ?? ''), (string)$t['title']); } } $result->settle($out); }, fn () => $result->settle([])); return $result; } if (preg_match('#open\.spotify\.com/(?:intl-[a-z]+/)?(track|album|playlist)/([A-Za-z0-9]+)#', $url, $m)) { // the embed page of Spotify (public): its data has the list of the songs $this->http->request('GET', "https://open.spotify.com/embed/{$m[1]}/{$m[2]}", null, ['User-Agent' => 'Mozilla/5.0 (WinZik)'], 20)->then(function (\JumPHP\Http\HttpResponse $r) use ($result, $song): void { $out = []; if (preg_match('#<script id="__NEXT_DATA__" type="application/json">(.*?)</script>#s', $r->body, $j)) { $entity = json_decode($j[1], true)['props']['pageProps']['state']['data']['entity'] ?? null; if (is_array($entity)) { $list = (array)($entity['trackList'] ?? []); if ($list === [] && isset($entity['name'])) { $artists = implode(' ', array_map(fn ($a) => (string)($a['name'] ?? ''), (array)($entity['artists'] ?? []))); $out[] = $song($artists, (string)$entity['name']); } foreach ($list as $t) { if (is_array($t) && isset($t['title'])) { $out[] = $song(str_replace(["\u{a0}", ','], [' ', ''], (string)($t['subtitle'] ?? '')), (string)$t['title']); } } } } $result->settle($out); }, fn () => $result->settle([])); return $result; } $result->settle([]); return $result; } /** @param array{id: string, title: string, url: string} $entry $format: mp3 | mp4 */ /** * @param string $key an import's own name of the song ("Artiste - Titre" of Spotify): kept with its file once downloaded * (imports.json), the next import of the same playlist does not download it again */ public function download(array $entry, string $format, string $key = ''): void { $mp4 = $format === 'mp4'; $dir = $mp4 ? $this->videoDir : $this->musicDir; @mkdir($dir, 0777, true); // ffmpeg (the conversion) on one core only: the computer stays free for the sound // its chapters too: an "Intro" at the start, an "Outro" at the end are left out when it is played $args = ['--no-playlist', '--no-mtime', '--newline', '--progress', '--print', 'after_move:filepath', '--print', 'after_move:WZCH%(chapters)j', '-o', $dir . '/%(title).120B.%(ext)s', '--postprocessor-args', 'ffmpeg:-threads 1']; $ffmpeg = $this->tools->ffmpegDir(); if ($ffmpeg !== null) { array_push($args, '--ffmpeg-location', $ffmpeg); } $args = $mp4 ? array_merge($args, ['-f', 'bv*[height<=720][ext=mp4]+ba[ext=m4a]/b[height<=720][ext=mp4]/b', '--merge-output-format', 'mp4']) : array_merge($args, ['-x', '--audio-format', 'mp3', '--audio-quality', '0', '--embed-metadata']); // "--": what follows is the address, never an option (an address "--exec=..." of the saved queue or of a service // would otherwise have yt-dlp run a command) array_push($args, '--', (string)$entry['url']); // into the queue: one after the other (the next one starts when one ends) $this->jobs[] = ['title' => $entry['title'], 'format' => $format, 'process' => null, 'progress' => 0.0, 'state' => 'queued', 'file' => '', 'error' => '', 'args' => $args, 'url' => (string)$entry['url'], 'key' => $key]; $this->changed = true; $this->startQueued(); } /** the queue changed (WinZik writes it to its file: it goes on after a restart) */ public bool $changed = false; /** * From the video's chapters: the end of an intro at its start, the start of an outro at its end. * @return array{0: float, 1: float}|null (0: none) null: nothing to leave out */ public static function bounds(string $chaptersJson): ?array { $chapters = json_decode($chaptersJson, true); if (!is_array($chapters) || count($chapters) < 2) { return null; } $first = $chapters[0]; $last = $chapters[count($chapters) - 1]; $start = is_array($first) && (float)($first['start_time'] ?? 99) < 1 && preg_match('/\b(intro|introduction|g[ée]n[ée]rique)\b/iu', (string)($first['title'] ?? '')) ? (float)($first['end_time'] ?? 0) : 0.0; $end = is_array($last) && preg_match('/\b(outro|fin|ending|end ?credits|g[ée]n[ée]rique de fin)\b/iu', (string)($last['title'] ?? '')) ? (float)($last['start_time'] ?? 0) : 0.0; return $start > 0 || $end > 0 ? [$start, $end] : null; } /** where the songs of the imports are kept (key => file): set by WinZik (var/imports.json) */ public string $importsFile = ''; /** file => the YouTube address it came from: set by WinZik (var/youtube-sources.json) */ public string $sourcesFile = ''; /** @return array<string, string> */ private function imports(): array { $data = $this->importsFile !== '' && is_file($this->importsFile) ? json_decode((string)@file_get_contents($this->importsFile), true) : null; return is_array($data) ? array_map('strval', $data) : []; } /** "Renaud - Mistral gagnant (Clip officiel) [HD]" -> "renaud mistral gagnant": no accents, case, brackets, signs */ public static function normalize(string $name): string { $name = (string)preg_replace('/\([^)]*\)|\[[^\]]*\]|\b(feat|ft)\.?\s.*$/iu', ' ', $name); $ascii = function_exists('iconv') ? (string)@iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $name) : $name; $ascii = strtolower($ascii !== '' ? $ascii : $name); return trim((string)preg_replace('/[^a-z0-9]+/', ' ', $ascii)); } /** * Is this song ("Artiste - Titre") here already? Downloaded before by an import (its file still there), waiting in the * queue, or a file of the music / video folders (or of $others: the play list) whose name has its artist and its title. * @param list<string> $others * @return string|null what it is (a file, or "en attente"), null: not here */ public function alreadyHave(string $key, string $url, array $others = []): ?string { $imports = $this->imports(); $file = $imports[self::normalize($key)] ?? null; if ($file !== null && is_file($file)) { return $file; } foreach ($this->jobs as $job) { if (in_array($job['state'], ['queued', 'download', 'convert'], true) && (($job['url'] ?? '') === $url || self::normalize((string)($job['key'] ?? '')) === self::normalize($key))) { return 'en attente'; } } [$artist, $title] = str_contains($key, ' - ') ? array_map('trim', explode(' - ', $key, 2)) : ['', $key]; $artist = self::normalize((string)preg_split('/,|&/', $artist)[0]); $title = self::normalize($title); if ($title === '') { return null; } $this->filesHere ??= $this->listFiles(); foreach ([...$this->filesHere, ...$others] as $path) { $name = ' ' . self::normalize(pathinfo($path, PATHINFO_FILENAME)) . ' '; if (str_contains($name, ' ' . $title . ' ') && ($artist === '' || str_contains($name, ' ' . $artist . ' '))) { return $path; } } return null; } /** @var list<string>|null the files of the music and video folders (looked at once per import) */ private ?array $filesHere = null; /** @return list<string> */ private function listFiles(): array { $out = []; foreach (array_unique([$this->musicDir, $this->videoDir]) as $dir) { foreach (is_dir($dir) ? (scandir($dir) ?: []) : [] as $f) { if ($f !== '.' && $f !== '..' && is_file("$dir/$f")) { $out[] = "$dir/$f"; } } } return $out; } /** A new import: the folders looked at again. */ public function forgetFiles(): void { $this->filesHere = null; } /** @return list<array{url: string, title: string, format: string}> what is still to download (in its order) */ public function pending(): array { $out = []; foreach ($this->jobs as $job) { if (in_array($job['state'], ['queued', 'download', 'convert'], true) && ($job['url'] ?? '') !== '') { $out[] = ['url' => (string)$job['url'], 'title' => $job['title'], 'format' => $job['format'], 'key' => (string)($job['key'] ?? '')]; } } return $out; } /** A line of the queue removed (stopped if it was downloading). */ public function remove(int $index): void { $job = $this->jobs[$index] ?? null; if ($job === null) { return; } if ($job['process'] !== null && in_array($job['state'], ['download', 'convert'], true)) { $job['process']->kill(); $job['process']->cleanup(); } if (($job['native'] ?? null) !== null && in_array($job['state'], ['download', 'convert'], true)) { $job['native']->stop(); } array_splice($this->jobs, $index, 1); $this->changed = true; $this->startQueued(); } /** Everything removed from the queue (the one downloading stopped). */ public function clearQueue(): void { foreach (array_reverse(array_keys($this->jobs)) as $i) { $this->remove($i); } $this->jobs = []; $this->changed = true; } /** WinZik ends: the download going on stops, still in the queue (it goes on at the next start, from where it was). */ public function stopAll(): void { foreach ($this->jobs as &$job) { if ($job['process'] !== null && in_array($job['state'], ['download', 'convert'], true)) { $job['process']->kill(); $job['process']->cleanup(); $job['process'] = null; $job['state'] = 'queued'; } if (($job['native'] ?? null) !== null && in_array($job['state'], ['download', 'convert'], true)) { $job['native']->stop(); $job['native'] = null; $job['state'] = 'queued'; $job['progress'] = 0.0; } } unset($job); } private function startQueued(): void { $running = count(array_filter($this->jobs, fn (array $j) => $j['state'] === 'download' || $j['state'] === 'convert')); foreach ($this->jobs as &$job) { if ($running >= self::PARALLEL) { break; } if ($job['state'] !== 'queued') { continue; } if ($this->tools->ready('yt-dlp') && empty($job['nativeTried'])) { $job['process'] = $this->ytdlp($job['args'], true); $job['state'] = 'download'; $running++; continue; } // without yt-dlp: YouTube's own API, then ffmpeg (the app's own on a phone) $url = (string)($job['url'] ?? ''); $ffmpeg = $this->tools->program('ffmpeg'); if ($this->innerTube === null || !YouTubeNativeJob::handles($url)) { if (!$this->tools->phone && !in_array($this->tools->state['yt-dlp'], ['unsupported', 'error'], true) && empty($job['nativeTried'])) { continue; // yt-dlp is on its way: it waits for it } $job['state'] = 'error'; $job['error'] = $this->innerTube === null ? 'pas de réseau ici' : 'sans yt-dlp, seules les adresses YouTube se téléchargent'; $this->changed = true; continue; } if ($ffmpeg === null) { if ($this->tools->phone || in_array($this->tools->state['ffmpeg'], ['unsupported', 'error'], true)) { $job['state'] = 'error'; $job['error'] = $this->tools->ffmpegMissing(); $this->changed = true; } continue; // ffmpeg is on its way: it waits for it } $job['native'] = new YouTubeNativeJob($this->innerTube, $url, $job['format'], $job['format'] === 'mp4' ? $this->videoDir : $this->musicDir, $this->tools->tmpDir . '/youtube', $ffmpeg, $this->runner, $this->streamDownload); $job['nativeTried'] = true; $job['process'] = null; $job['state'] = 'download'; $job['progress'] = 0.0; $running++; } unset($job); } /** * The queue as a whole: [finished, all, fraction 0..1] (the finished ones of the last batch included, until it is all done). * @return array{0: int, 1: int, 2: float} */ public function total(): array { $all = count($this->jobs); $finished = count(array_filter($this->jobs, fn (array $j) => $j['state'] === 'done' || $j['state'] === 'error')); $sum = array_sum(array_map(fn (array $j) => $j['state'] === 'done' || $j['state'] === 'error' ? 1.0 : ($j['state'] === 'convert' ? 0.95 : $j['progress'] * 0.9), $this->jobs)); return [$finished, $all, $all === 0 ? 0.0 : $sum / $all]; } /** The finished lines go (the queue starts again from 0 for the next downloads). */ public function clearFinished(): void { $this->jobs = array_values(array_filter($this->jobs, fn (array $j) => $j['state'] !== 'done' && $j['state'] !== 'error')); } /** Each frame. */ public function poll(): void { if ($this->search !== null) { $this->search->poll(); if (!$this->search->done() && microtime(true) - $this->searchStarted > self::SEARCH_TIMEOUT) { $this->search->kill(); // no answer: given up (it says so: "recherche impossible") } if ($this->search->done()) { $this->results = $this->search->ok() ? self::parseSearch($this->search->output()) : []; $this->error = $this->search->ok() ? '' : ('recherche impossible : ' . $this->search->error()); $failed = !$this->search->ok() && self::ytdlpBroken($this->search->error()); $this->search->cleanup(); $this->search = null; $this->searching = ''; if ($failed && $this->innerTube !== null && ($this->source === 'youtube' || $this->source === 'ytmusic')) { // yt-dlp broken (YouTube changed, an old copy...): YouTube's own API instead $this->searching = $this->query; $this->error = ''; $this->searchNative($this->query, $this->page); } } } foreach ($this->jobs as $i => &$job) { if (($job['native'] ?? null) !== null && ($job['state'] === 'download' || $job['state'] === 'convert')) { $this->pollNative($job); continue; } if (($job['state'] !== 'download' && $job['state'] !== 'convert') || $job['process'] === null) { continue; } foreach ($job['process']->poll() as $line) { if (str_starts_with($line, 'WZCH')) { $job['bounds'] = self::bounds((string)substr($line, 4)); continue; } if (preg_match('/^\[download\]\s+([\d.]+)%/', $line, $m)) { $job['progress'] = (float)$m[1] / 100; $job['state'] = $job['progress'] >= 1.0 ? 'convert' : 'download'; } elseif ($line[0] !== '[' && is_file(trim($line))) { $job['file'] = trim($line); } } if ($job['process']->done()) { if ($job['process']->ok() && $job['file'] !== '') { $this->finishedJob($job); } elseif ($this->innerTube !== null && empty($job['nativeTried']) && self::ytdlpBroken($job['process']->error()) && YouTubeNativeJob::handles((string)($job['url'] ?? ''))) { // yt-dlp failed on a YouTube video: once again through YouTube's own API $job['state'] = 'queued'; $job['nativeTried'] = true; $job['progress'] = 0.0; } else { $job['state'] = 'error'; $job['error'] = $job['process']->error() ?: 'yt-dlp a échoué'; } $job['process']->cleanup(); $this->changed = true; } } unset($job); $this->startQueued(); } /** @param array<string, mixed> $job a download without yt-dlp: its progress, then its file (as a yt-dlp one) */ private function pollNative(array &$job): void { $native = $job['native']; $ended = $native->poll(); $job['progress'] = $native->progress; $job['state'] = $native->state === 'resolve' ? 'download' : $native->state; if ($native->title !== '' && preg_match('#^(https?://|ytsearch)#', (string)$job['title'])) { $job['title'] = $native->title; // an address typed: the video's own title once known } if (!$ended) { return; } $job['native'] = null; if ($native->state === 'done') { $job['file'] = $native->file; $this->finishedJob($job); } else { $job['state'] = 'error'; $job['error'] = $native->error ?: 'téléchargement impossible'; } $this->changed = true; } /** @param array<string, mixed> $job a download done: kept for the imports, given to WinZik by finished() */ private function finishedJob(array &$job): void { $job['state'] = 'done'; $job['progress'] = 1.0; if (($job['key'] ?? '') !== '' && $this->importsFile !== '') { // an import's song: kept with its file (the next import of the playlist does not take it again) $imports = $this->imports(); $imports[self::normalize((string)$job['key'])] = $job['file']; @mkdir(dirname($this->importsFile), 0777, true); @file_put_contents($this->importsFile, (string)json_encode($imports, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); } if ($this->sourcesFile !== '' && ($job['file'] ?? '') !== '' && preg_match('#(?:v=|youtu\.be/|/shorts/)[A-Za-z0-9_-]{11}#', (string)($job['url'] ?? ''))) { // which video each file came from (the cloud's "le mobile la récupère sur YouTube" gives its id) $sources = json_decode((string)@file_get_contents($this->sourcesFile), true); $sources = is_array($sources) ? array_slice($sources, -4999, null, true) : []; $sources[(string)$job['file']] = (string)$job['url']; @mkdir(dirname($this->sourcesFile), 0777, true); @file_put_contents($this->sourcesFile, (string)json_encode($sources, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); } $this->filesHere = null; $this->done[] = ['file' => $job['file'], 'format' => $job['format'], 'title' => $job['title'], 'bounds' => $job['bounds'] ?? null]; } /** @return list<array{file: string, format: string, title: string, bounds: array{0: float, 1: float}|null}> */ public function finished(): array { [$done, $this->done] = [$this->done, []]; return $done; } public function busy(): bool { foreach ($this->jobs as $job) { if ($job['state'] === 'download' || $job['state'] === 'convert' || $job['state'] === 'queued') { return true; } } return false; } /** * yt-dlp failed on YouTube's side (its "ERROR: [youtube] ...": YouTube changed, a format refused...), or did not run * at all (nothing said: a broken program, given up): worth trying again through YouTube's own API. */ public static function ytdlpBroken(string $error): bool { return $error === '' || str_contains($error, 'ERROR'); } /** @return list<array{id: string, title: string, channel: string, duration: int, url: string}> */ public static function parseSearch(string $json): array { $data = json_decode($json, true); $out = []; foreach (is_array($data) ? (array)($data['entries'] ?? []) : [] as $e) { if (!is_array($e) || !is_string($e['id'] ?? null)) { continue; } $url = (string)($e['url'] ?? $e['webpage_url'] ?? ''); if ($url === '' || !preg_match('#^https?://#', $url)) { $url = 'https://www.youtube.com/watch?v=' . $e['id']; } $out[] = ['id' => $e['id'], 'title' => (string)($e['title'] ?? $e['id']), 'channel' => (string)($e['channel'] ?? $e['uploader'] ?? ''), 'duration' => (int)($e['duration'] ?? 0), 'url' => $url]; } return $out; } public static function clock(int $seconds): string { return $seconds <= 0 ? 'direct' : ($seconds >= 3600 ? sprintf('%d:%02d:%02d', intdiv($seconds, 3600), intdiv($seconds % 3600, 60), $seconds % 60) : sprintf('%d:%02d', intdiv($seconds, 60), $seconds % 60)); }}