From 29756cf290006fbd610a46f74040ce6cf29f0077 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Sat, 20 Jun 2020 13:33:16 +0200 Subject: [PATCH] Add fallback to bestvideo and worstvideo (fixes #288) --- classes/Config.php | 29 +++++++++++++++++------------ config/config.example.yml | 4 ++-- controllers/BaseController.php | 7 ++++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index d8ca08f..62a1698 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -151,9 +151,9 @@ class Config if (empty($this->genericFormats)) { // We don't put this in the class definition so it can be detected by xgettext. $this->genericFormats = [ - 'best' => $localeManager->t('Best'), + 'best/bestvideo' => $localeManager->t('Best'), 'bestvideo+bestaudio' => $localeManager->t('Remux best video with best audio'), - 'worst' => $localeManager->t('Worst'), + 'worst/worstvideo' => $localeManager->t('Worst'), ]; } @@ -165,26 +165,31 @@ class Config } } elseif (!$this->stream) { // Force HTTP if stream is not enabled. - $this->replaceGenericFormat($format, $format . '[protocol=https]/' . $format . '[protocol=http]'); + $keys = array_keys($this->genericFormats); + $keys[array_search($format, $keys)] = $this->addHttpToFormat($format); + if ($genericFormats = array_combine($keys, $this->genericFormats)) { + $this->genericFormats = $genericFormats; + } } } } /** - * Replace a format key. + * Add HTTP condition to a format. * - * @param string $oldFormat Old format - * @param string $newFormat New format + * @param string $format Format * - * @return void + * @return string */ - private function replaceGenericFormat($oldFormat, $newFormat) + public static function addHttpToFormat($format) { - $keys = array_keys($this->genericFormats); - $keys[array_search($oldFormat, $keys)] = $newFormat; - if ($genericFormats = array_combine($keys, $this->genericFormats)) { - $this->genericFormats = $genericFormats; + $newFormat = []; + foreach (explode('/', $format) as $subformat) { + $newFormat[] = $subformat . '[protocol=https]'; + $newFormat[] = $subformat . '[protocol=http]'; } + + return implode('/', $newFormat); } /** diff --git a/config/config.example.yml b/config/config.example.yml index 7bf520c..29d6c5d 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -49,9 +49,9 @@ appName: AllTube Download # Generic formats supported by youtube-dl genericFormats: - best: Best + best/bestvideo: Best bestvideo+bestaudio: Remux best video with best audio - worst: Worst + worst/worstvideo: Worst # Enable debug mode. debug: false diff --git a/controllers/BaseController.php b/controllers/BaseController.php index 59e9862..bf48a24 100644 --- a/controllers/BaseController.php +++ b/controllers/BaseController.php @@ -31,7 +31,7 @@ abstract class BaseController * * @var string */ - protected $defaultFormat = 'best[protocol=https]/best[protocol=http]'; + protected $defaultFormat = 'best/bestvideo'; /** * Slim dependency container. @@ -74,8 +74,9 @@ abstract class BaseController $this->sessionSegment = $session->getSegment(self::class); $this->localeManager = $this->container->get('locale'); - if ($this->config->stream) { - $this->defaultFormat = 'best'; + if (!$this->config->stream) { + // Force HTTP if stream is not enabled. + $this->defaultFormat = Config::addHttpToFormat($this->defaultFormat); } }