Simplify overly complicated format selection template

This commit is contained in:
Pierre Rudloff 2022-02-06 20:26:36 +01:00
parent d9ba01f017
commit 27439c7e14
2 changed files with 52 additions and 56 deletions

View file

@ -13,6 +13,7 @@ use Alltube\Locale;
use Alltube\Middleware\CspMiddleware;
use Exception;
use Slim\Http\StatusCode;
use stdClass;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable;
use Psr\Container\ContainerInterface;
@ -175,6 +176,47 @@ class FrontController extends BaseController
]
);
}
$formats = [];
$genericFormatsLabel = $this->localeManager->t('Generic formats');
$detailedFormatsLabel = $this->localeManager->t('Detailed formats');
foreach ($this->config->genericFormats as $id => $genericFormat) {
$formats[$genericFormatsLabel][$id] = $this->localeManager->t($genericFormat);
}
/** @var stdClass $format */
foreach ($this->video->getJson()->formats as $format) {
if ($this->config->stream || in_array($format->protocol, ['http', 'https'])) {
$formatParts = [
// File extension
$format->ext,
];
if (isset($format->width) || isset($format->height)) {
// Video dimensions
$formatParts[] = implode('x', array_filter([$format->width, $format->height]));
}
if (isset($format->filesize)) {
// File size
$formatParts[] = round($format->filesize / 1000000, 2) . ' MB';
}
if (isset($format->format_note)) {
// Format name
$formatParts[] = $format->format_note;
}
if (isset($format->format_id)) {
// Format ID
$formatParts[] = '(' . $format->format_id . ')';
}
$formats[$detailedFormatsLabel][$format->format_id] = implode(' ', $formatParts);
}
}
$this->view->render(
$response,
$template,
@ -184,6 +226,7 @@ class FrontController extends BaseController
'title' => $title,
'description' => $description,
'defaultFormat' => $this->defaultFormat,
'formats' => $formats
]
);

View file

@ -23,57 +23,13 @@
{/if}
{if isset($video->formats) && count($video->formats) > 1}
<h3><label for="format">{t}Available formats:{/t}</label></h3>
<select name="format" id="format" class="formats monospace">
<optgroup label="{t}Generic formats{/t}">
{foreach $config->genericFormats as $format => $name}
{*
To make the default generic formats translatable:
{t}Best{/t}
{t}Remux best video with best audio{/t}
{t}Worst{/t}
*}
<option value="{$format}">{t}{$name}{/t}</option>
{/foreach}
</optgroup>
<optgroup label="{t}Detailed formats{/t}" class="monospace">
{foreach $video->formats as $format}
{if $config->stream || $format->protocol|in_array:array('http', 'https')}
{strip}
<option value="{$format->format_id}">
{$format->ext}
{for $foo=1 to (5 - ($format->ext|strlen))}
&nbsp;
{/for}
{if isset($format->width)}
{$format->width}x{$format->height}
{for $foo=1 to (10 - (("{$format->width}x{$format->height}")|strlen))}
&nbsp;
{/for}
{else}
{for $foo=1 to 10}
&nbsp;
{/for}
{/if}
{if isset($format->filesize)}
{($format->filesize/1000000)|round:2} MB
{for $foo=1 to (7 - (($format->filesize/1000000)|round:2|strlen))}
&nbsp;
{/for}
{else}
{for $foo=1 to 10}
&nbsp;
{/for}
{/if}
{if isset($format->format_note)}
{$format->format_note}
{/if}
&nbsp;({$format->format_id})
</option>
{/strip}
{/if}
{/foreach}
</optgroup>
</select>
{*
To make the default generic formats translatable:
{t}Best{/t}
{t}Remux best video with best audio{/t}
{t}Worst{/t}
*}
{html_options name='format' options=$formats selected=$defaultFormat id="format" class="formats monospace"}
<br/>
<br/>
{/if}
@ -86,11 +42,8 @@
{if $config->convertAdvanced}
<input type="checkbox" name="customConvert" id="customConvert"/>
<label for="customConvert">{t}Convert into a custom format:{/t}</label>
<select title="{t}Custom format{/t}" name="customFormat" aria-label="{t}Format to convert to{/t}">
{foreach $config->convertAdvancedFormats as $format}
<option>{$format}</option>
{/foreach}
</select>
{html_options name='customFormat' values=$config->convertAdvancedFormats output=$config->convertAdvancedFormats
title="{t}Custom format{/t}" name="customFormat" aria-label="{t}Format to convert to{/t}"}
{t}with{/t}
<label for="customBitrate" class="sr-only">{t}Bit rate{/t}</label>
<input type="number" value="{$config->audioBitrate}" title="{t}Custom bitrate{/t}"