]> _ Git - cubist_util.git/commitdiff
wip #7232 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 16 Dec 2024 17:33:14 +0000 (18:33 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 16 Dec 2024 17:33:14 +0000 (18:33 +0100)
src/CommandLine.php
src/YoutubeDL.php

index b64b43c2671ec4ba66d09c73ad6525ff7706269c..55602b4377a13e2066f8c250ac85f219fc7db8a8 100644 (file)
@@ -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()
     {
 
index 8770df021df6849b0e6e6e65707ef51ff44ab1d8..b3e3c4afae0d1172141bf7358906e0c915054772 100644 (file)
@@ -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;
     }