Skip to content

Commit

Permalink
PS-670-rendition-video_enhance2 (#472)
Browse files Browse the repository at this point in the history
* WIP twig

* allow dynamic options (twig), e.g. `width: "{{ input.width / 5 }}"`

* cs

* use existing TemplateResolver ; Avoid compiling "not-twig" templates

* cleanup ; filter now logs resolved options values

* commit forgotten files...

* commit forgotten files...

* remove state from ModuleOptionsResolver

* call template resolver only for _strings_ containing "{"

* resolve options one by one ; factorize common code

* remove state from service(s) ; set "projection"

* add log ; check input file family

* turn base class abstract

* rename classes
  • Loading branch information
jygaulier authored Nov 4, 2024
1 parent ce359b0 commit 48b901f
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 247 deletions.
6 changes: 4 additions & 2 deletions databox/api/src/Asset/Attribute/TemplateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public function __construct()

public function resolve(string $template, array $values): string
{
$template = $this->twig->createTemplate($template);
if (str_contains($template, '{')) {
return $this->twig->createTemplate($template)->render($values);
}

return $this->twig->render($template, $values);
return $template;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ services:
Alchemy\RenditionFactory\MimeType\MimeTypeGuesser: ~
Alchemy\RenditionFactory\Format\FormatGuesser: ~
Alchemy\RenditionFactory\Format\FormatFactory: ~
Alchemy\RenditionFactory\Config\ModuleOptionsResolver: ~
17 changes: 17 additions & 0 deletions lib/php/rendition-factory/src/Config/ModuleOptionsResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Alchemy\RenditionFactory\Config;

use Alchemy\RenditionFactory\Templating\TemplateResolverInterface;

class ModuleOptionsResolver
{
public function __construct(private TemplateResolverInterface $templateResolver)
{
}

public function resolveOption(mixed $option, array $context): mixed
{
return is_string($option) ? $this->templateResolver->resolve($option, $context) : $option;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Alchemy\RenditionFactory\Transformer\Video;

use Alchemy\RenditionFactory\Config\ModuleOptionsResolver;
use Alchemy\RenditionFactory\Transformer\Video\FFMpeg\Format\FormatInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\ServiceLocator;

abstract readonly class AbstractVideoTransformer
{
public function __construct(#[AutowireLocator(FormatInterface::TAG, defaultIndexMethod: 'getFormat')] protected ServiceLocator $formats,
protected ModuleOptionsResolver $optionsResolver,
) {
}
}
39 changes: 29 additions & 10 deletions lib/php/rendition-factory/src/Transformer/Video/FFMpegHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@

namespace Alchemy\RenditionFactory\Transformer\Video;

use Alchemy\RenditionFactory\Context\TransformationContextInterface;
use FFMpeg\FFMpeg;
use FFMpeg;

class FFMpegHelper
{
public static function createFFMpeg(array $options, TransformationContextInterface $context): FFMpeg
public static function createFFMpeg(array $options): FFMpeg\FFMpeg
{
$ffmpegOptions = [];
if ($timeout = $options['timeout'] ?? 3600) {
if (!is_int($timeout)) {
throw new \InvalidArgumentException('Invalid timeout');
}
$ffmpegOptions['timeout'] = $timeout;

if (!is_int($timeout = $options['timeout'] ?? 3600) || $timeout < 1) {
throw new \InvalidArgumentException('Invalid timeout');
}
if ($threads = $options['threads'] ?? null) {
$ffmpegOptions['timeout'] = $timeout;

if ($threads = $options['threads']) {
if (!is_int($threads) || $threads < 1) {
throw new \InvalidArgumentException('Invalid threads count');
}
$ffmpegOptions['ffmpeg.threads'] = $threads;
}

return FFMpeg::create($ffmpegOptions, $context->getLogger());
return FFMpeg\FFMpeg::create($ffmpegOptions, $options['logger'] ?? null);
}

public static function pointAsText(FFMpeg\Coordinate\Point $point): string
{
return sprintf('(%d, %d)', $point->getX(), $point->getY());
}

public static function dimensionAsText(FFMpeg\Coordinate\Dimension $dimension): string
{
return sprintf('%d x %d', $dimension->getWidth(), $dimension->getHeight());
}

public static function coordAsText(array $coord): string
{
$s = [];
foreach ($coord as $k => $v) {
$s[] = sprintf('%s=%d', $k, $v);
}

return '['.implode(', ', $s).']';
}
}
Loading

0 comments on commit 48b901f

Please sign in to comment.