feat: Make generic formats dynamic

See #223
This commit is contained in:
Pierre Rudloff 2019-05-08 19:46:37 +02:00
parent ceec503ff8
commit 43d5cc1dbb
5 changed files with 66 additions and 15 deletions

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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.

View File

@ -27,19 +27,9 @@
{/if}
<select name="format" id="format" class="formats monospace">
<optgroup label="{t}Generic formats{/t}">
<option value="{$defaultFormat}">
{strip}
{t}Best{/t} ({$video->ext})
{/strip}
</option>
{if $config->remux}
<option value="bestvideo+bestaudio">
{t}Remux best video with best audio{/t}
</option>
{/if}
<option value="{$defaultFormat|replace:best:worst}">
{t}Worst{/t}
</option>
{foreach $config->genericFormats as $format => $name}
<option value="{$format}">{t}{$name}{/t}</option>
{/foreach}
</optgroup>
<optgroup label="{t}Detailed formats{/t}" class="monospace">
{foreach $video->formats as $format}

View File

@ -169,7 +169,7 @@ class VideoTest extends BaseTest
}
/**
* Provides M3U8 URLs for tests.
* Provides URLs for remux tests.
*
* @return array[]
*/
@ -186,7 +186,7 @@ class VideoTest extends BaseTest
}
/**
* Provides URLs for remux tests.
* Provides M3U8 URLs for tests.
*
* @return array[]
*/