Give streams with audio a higher priority
[playVideoOnDreamboxProxy.git] / www / functions.php
index 617724a6fe382e71505d70bef33f604246e1023a..3e4e2beeeda9aa1d8c9fe76e27bc294f595d3bd0 100644 (file)
@@ -2,11 +2,28 @@
 function getPageUrl()
 {
     global $argv, $argc;
+
+    $dryRun = false;
     if (php_sapi_name() == 'cli') {
         if ($argc < 2) {
-            errorInput('No URL given as command line parameter');
+            errorInput(
+                "No URL given as command line parameter\n"
+                . "Usage:\n"
+                . " play.php [--dry-run|-n] <url>"
+            );
+        }
+        $options = [];
+        array_shift($argv);//remove script itself
+        foreach ($argv as $val) {
+            if ($val[0] == '-') {
+                $options[$val] = true;
+            } else {
+                $pageUrl = $val;
+            }
+        }
+        if (isset($options['--dry-run']) || isset($options['-n'])) {
+            $dryRun = true;
         }
-        $pageUrl = $argv[1];
     } else if (!isset($_SERVER['CONTENT_TYPE'])) {
         errorInput('Content type header missing');
     } else if ($_SERVER['CONTENT_TYPE'] == 'text/plain') {
@@ -30,7 +47,7 @@ function getPageUrl()
     } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
         errorInput('Invalid URL in POST data: Non-HTTP scheme');
     }
-    return $pageUrl;
+    return [$pageUrl, $dryRun];
 }
 
 function getYoutubeDlJson($pageUrl, $youtubedlPath)
@@ -95,7 +112,9 @@ function extractVideoUrlFromJson($json)
             //dreambox 7080hd does not play hls files
             continue;
         }
-        if (strpos($format->format, 'vp9') !== false) {
+        if (strpos($format->format, 'vp9') !== false
+            || $format->vcodec == 'vp9'
+        ) {
             //dreambox 7080hd does not play VP9 video streams
             continue;
         }
@@ -114,6 +133,9 @@ function extractVideoUrlFromJson($json)
 
     //filter: best quality
     usort($safeFormats, function ($a, $b) {
+        if ((($a->acodec != 'none') + ($b->acodec != 'none')) == 1) {
+            return ($b->acodec != 'none') - ($a->acodec != 'none');
+        }
         return ($b->quality ?? 0) - ($a->quality ?? 0);
     });
     foreach ($safeFormats as $format) {