From 50fee8ef81a4050c9b6b626dc6b350906826efdc Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 16 Dec 2024 18:33:14 +0100 Subject: [PATCH] wip #7232 @1.5 --- src/CommandLine.php | 7 ++++++- src/YoutubeDL.php | 47 ++++++++++++++++----------------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/CommandLine.php b/src/CommandLine.php index b64b43c..55602b4 100644 --- a/src/CommandLine.php +++ b/src/CommandLine.php @@ -59,7 +59,7 @@ class CommandLine if (null === $name && null === $val) { return; } - if ((stristr($val, ' ') || stristr($val, '&')) && !stristr($val, '>') && !stristr($val, '<')) { + if ((stristr($val, ';') || stristr($val, ' ') || stristr($val, '&')) && !stristr($val, '>') && !stristr($val, '<')) { $val = '"' . $val . '"'; } $this->args[] = array($name, $val); @@ -118,6 +118,11 @@ class CommandLine $this->args[] = array(null, $val); } + public function hashArgs() + { + return hash('sha256', json_encode($this->args)); + } + protected function _preExecute() { diff --git a/src/YoutubeDL.php b/src/YoutubeDL.php index 8770df0..b3e3c4a 100644 --- a/src/YoutubeDL.php +++ b/src/YoutubeDL.php @@ -9,10 +9,12 @@ use YoutubeDl\Options; class YoutubeDL { protected static $_cookiesFile = null; + protected static $_cookiesBrowser = 'chrome'; - public static function setCookiesFile($cookieFile) + public static function setCookiesFile($cookieFile, $cookiesBrowser = 'chrome') { static::$_cookiesFile = $cookieFile; + static::$_cookiesBrowser = $cookiesBrowser; } public static function downloadVideo($url, $path) @@ -27,43 +29,28 @@ class YoutubeDL protected static function _download($url, $path, $onlyAudio = false) { - $ext = 'mp4'; - - $options = Options::create() - ->downloadPath($path) - ->cacheDir(storage_path('cache/youtubedl')) - ->format('bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4') - ->url($url); - - if (null !== static::$_cookiesFile) { - $options = $options->cookies(static::$_cookiesFile); - } + $ext = $onlyAudio ? 'mp3' : 'mp4'; + $cli = new CommandLine('/usr/local/bin/yt-dlp'); + $cli->setArg('cache-dir', storage_path('cache/youtubedl')); + $cli->setArg('format', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4'); + $cli->setArg('extractor-args', 'youtube:player_client=web,default,-ios'); if ($onlyAudio) { - $ext = 'mp3'; - $options = $options->extractAudio(true) - ->audioFormat('mp3'); + $cli->setArg('extract-audio'); + $cli->setArg('audio-format', 'mp3'); + } + if (null !== static::$_cookiesFile) { + $cli->setArg('cookies', static::$_cookiesFile); } - $cacheKey = hash('sha256', $url . print_r($options->toArray(), true)); + $cacheKey = hash('sha256', $url . "-" . $cli->hashArgs()); $fname = $cacheKey . '.' . $ext; $file = Files::mkdir($path) . $fname; - $options = $options->output($fname); - if (!file_exists($file)) { - $yt = new \YoutubeDl\YoutubeDl(); - $yt->setBinPath('/usr/local/bin/yt-dlp'); - $collection = $yt->download($options); - - foreach ($collection->getVideos() as $video) { - if ($video->getError() !== null) { - Log::debug("Error downloading video: {$video->getError()}."); - } else { - $video->getFile(); - } - } - + $cli->setArg('output', $file); + $cli->setArg(null, $url); + $cli->execute(); } return $file; } -- 2.39.5