From 43d5cc1dbbc021b0ba51bf86f6a55261b1cd54ef Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Wed, 8 May 2019 19:46:37 +0200 Subject: [PATCH] feat: Make generic formats dynamic See #223 --- classes/Config.php | 43 +++++++++++++++++++++++++++++++++++++++ config/config.example.yml | 6 ++++++ resources/FAQ.md | 12 +++++++++++ templates/info.tpl | 16 +++------------ tests/VideoTest.php | 4 ++-- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index e302bf7..c6805e1 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -126,6 +126,13 @@ class Config */ private $file; + /** + * Generic formats supported by youtube-dl. + * + * @var array + */ + public $genericFormats = []; + /** * Config constructor. * @@ -135,6 +142,42 @@ class Config { $this->applyOptions($options); $this->getEnv(); + + if (empty($this->genericFormats)) { + // We don't put this in the class definition so it can be detected by xgettext. + $this->genericFormats = [ + 'best' => _('Best'), + 'bestvideo+bestaudio' => _('Remux best video with best audio'), + 'worst' => _('Worst') + ]; + } + + foreach ($this->genericFormats as $format => $name) { + if (strpos($format, '+') !== false) { + if (!$this->remux) { + // Disable combined formats if remux mode is not enabled. + unset($this->genericFormats[$format]); + } + } elseif (!$this->stream) { + // Force HTTP if stream is not enabled. + $this->replaceGenericFormat($format, $format.'[protocol=https]/'.$format.'[protocol=http]'); + } + } + } + + /** + * Replace a format key. + * + * @param string $oldFormat Old format + * @param string $newFormat New format + * + * @return void + */ + private function replaceGenericFormat($oldFormat, $newFormat) + { + $keys = array_keys($this->genericFormats); + $keys[array_search($oldFormat, $keys)] = $newFormat; + $this->genericFormats = array_combine($keys, $this->genericFormats); } /** diff --git a/config/config.example.yml b/config/config.example.yml index 9d1c4f4..3cd1fe0 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -45,3 +45,9 @@ audioBitrate: 128 # App name appName: AllTube Download + +# Generic formats supported by youtube-dl +genericFormats: + best: Best + bestvideo+bestaudio: Remux best video with best audio + worst: Worst diff --git a/resources/FAQ.md b/resources/FAQ.md index 11f175b..110b44b 100644 --- a/resources/FAQ.md +++ b/resources/FAQ.md @@ -172,3 +172,15 @@ convertAdvancedFormats: [mp3, avi, flv, wav] This will add new inputs on the download page that allow users to converted videos to other formats. + +## Use other youtube-dl generic formats (e.g. `bestaudio`) + +You can add new formats by using the `genericFormats` option, +for example: + +```yaml +genericFormats: + bestaudio: Best audio +``` + +These will be available on every video page. diff --git a/templates/info.tpl b/templates/info.tpl index 54847ef..3e329ae 100644 --- a/templates/info.tpl +++ b/templates/info.tpl @@ -27,19 +27,9 @@ {/if}