Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
jygaulier committed Oct 28, 2024
1 parent d71cdc9 commit 37ea3ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 62 deletions.
8 changes: 4 additions & 4 deletions lib/php/rendition-factory/src/Config/TemplatedOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ private function compile(array $option): array
{
$r = [];
foreach ($option as $k => $o) {
if(is_array($o)) {
if (is_array($o)) {
$r[$k] = $this->compile($o);
}
else {
} else {
try {
$r[$k] = $this->twig->createTemplate($o)->render($this->twigContext);
} catch (LoaderError | SyntaxError $e) {
} catch (LoaderError|SyntaxError $e) {
// not enough context to evaluate ? just ignore this option
}
}
}

return $r;
}
}
1 change: 0 additions & 1 deletion lib/php/rendition-factory/src/RenditionCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Alchemy\RenditionFactory\DTO\InputFile;
use Alchemy\RenditionFactory\DTO\OutputFileInterface;
use Alchemy\RenditionFactory\Exception\NoBuildConfigException;
use Alchemy\RenditionFactory\Templating\TemplateResolverInterface;
use Alchemy\RenditionFactory\Transformer\BuildHashDiffInterface;
use Alchemy\RenditionFactory\Transformer\TransformerModuleInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,45 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
$options = $templatedOptions->asArray();

if (!($format = $options['format'] ?? null)) {
throw new InvalidArgumentException('Missing format');
throw new \InvalidArgumentException('Missing format');
}

if(!$this->formats->has($format)) {
throw new InvalidArgumentException(sprintf('Invalid format %s', $format));
if (!$this->formats->has($format)) {
throw new \InvalidArgumentException(sprintf('Invalid format %s', $format));
}
/** @var FormatInterface $outputFormat */
$outputFormat = $this->formats->get($format);

if (null != ($extension = $options['extension'] ?? null)) {
if(!in_array($extension, $outputFormat->getAllowedExtensions())) {
throw new InvalidArgumentException(sprintf('Invalid extension %s for format %s', $extension, $format));
if (!in_array($extension, $outputFormat->getAllowedExtensions())) {
throw new \InvalidArgumentException(sprintf('Invalid extension %s for format %s', $extension, $format));
}
}
else {
} else {
$extension = $outputFormat->getAllowedExtensions()[0];
}

if($outputFormat->getFamily() !== FamilyEnum::Video) {
throw new InvalidArgumentException(sprintf('Invalid format %s, only video formats supported', $format));
if (FamilyEnum::Video !== $outputFormat->getFamily()) {
throw new \InvalidArgumentException(sprintf('Invalid format %s, only video formats supported', $format));
}

if($outputFormat->getFamily() === FamilyEnum::Video) {
if (FamilyEnum::Video === $outputFormat->getFamily()) {
return $this->doVideo($outputFormat, $extension, $inputFile, $templatedOptions, $context);
}

if ($outputFormat->getFamily() === FamilyEnum::Audio) {
if (FamilyEnum::Audio === $outputFormat->getFamily()) {
return $this->doAudio($outputFormat, $extension, $inputFile, $templatedOptions, $context);
}

throw new InvalidArgumentException(sprintf('Invalid format %s, only video or audio format supported', $format));
throw new \InvalidArgumentException(sprintf('Invalid format %s, only video or audio format supported', $format));
}

private function doVideo(FormatInterface $ouputFormat, string $extension, InputFileInterface $inputFile, TemplatedOptions $templatedOptions, TransformationContextInterface $context): OutputFileInterface
{
$options = $templatedOptions->asArray();

$format = $ouputFormat->getFormat();
if(!method_exists($ouputFormat, 'getFFMpegFormat')) {
throw new InvalidArgumentException('format %s does not declare FFMpeg format', $format);
if (!method_exists($ouputFormat, 'getFFMpegFormat')) {
throw new \InvalidArgumentException('format %s does not declare FFMpeg format', $format);
}

$ffmpeg = FFMpegHelper::createFFMpeg($options, $context);
Expand All @@ -93,40 +92,40 @@ private function doVideo(FormatInterface $ouputFormat, string $extension, InputF

if ($videoCodec = $options['video_codec'] ?? null) {
if (!in_array($videoCodec, $FFMpegFormat->getAvailableVideoCodecs())) {
throw new InvalidArgumentException(sprintf('Invalid video codec %s for format %s', $videoCodec, $format));
throw new \InvalidArgumentException(sprintf('Invalid video codec %s for format %s', $videoCodec, $format));
}
$FFMpegFormat->setVideoCodec($videoCodec);
}
if ($audioCodec = $options['audio_codec'] ?? null) {
if (!in_array($audioCodec, $FFMpegFormat->getAvailableAudioCodecs())) {
throw new InvalidArgumentException(sprintf('Invalid audio codec %s for format %s', $audioCodec, $format));
throw new \InvalidArgumentException(sprintf('Invalid audio codec %s for format %s', $audioCodec, $format));
}
$FFMpegFormat->setAudioCodec($audioCodec);
}
if (null !== ($videoKilobitrate = $options['video_kilobitrate'] ?? null)) {
$videoKilobitrate = (int)$videoKilobitrate;
$videoKilobitrate = (int) $videoKilobitrate;
if (!method_exists($FFMpegFormat, 'setKiloBitrate')) {
throw new InvalidArgumentException(sprintf('format %s does not support video_kilobitrate', $format));
throw new \InvalidArgumentException(sprintf('format %s does not support video_kilobitrate', $format));
}
$FFMpegFormat->setKiloBitrate($videoKilobitrate);
}
if (null !== ($audioKilobitrate = $options['audio_kilobitrate'] ?? null)) {
$audioKilobitrate = (int)$audioKilobitrate;
$audioKilobitrate = (int) $audioKilobitrate;
if (!method_exists($FFMpegFormat, 'setAudioKiloBitrate')) {
throw new InvalidArgumentException(sprintf('format %s does not support audio_kilobitrate', $format));
throw new \InvalidArgumentException(sprintf('format %s does not support audio_kilobitrate', $format));
}
$FFMpegFormat->setAudioKiloBitrate($audioKilobitrate);
}
if (null !== ($passes = $options['passes'] ?? null)) {
$passes = (int)$passes;
$passes = (int) $passes;
if (!method_exists($FFMpegFormat, 'setPasses')) {
throw new InvalidArgumentException(sprintf('format %s does not support passes', $format));
throw new \InvalidArgumentException(sprintf('format %s does not support passes', $format));
}
if ($passes < 1) {
throw new InvalidArgumentException('Invalid passes count');
throw new \InvalidArgumentException('Invalid passes count');
}
if (0 === $videoKilobitrate) {
throw new InvalidArgumentException('passes must not be set if video_kilobitrate is 0');
throw new \InvalidArgumentException('passes must not be set if video_kilobitrate is 0');
}
$FFMpegFormat->setPasses($passes);
}
Expand All @@ -136,22 +135,6 @@ function ($filter) {
return $filter['enabled'] ?? true;
}));

// // patch filters to transform relative "from" and "duration" to absolute values
// foreach($filters as &$f) {
// if (isset($f['from']) && str_ends_with(trim($f['from']), '%')) {
// if(null === $duration) {
// throw new InvalidArgumentException('Unknown video duration, cannot use relative "from"');
// }
// $f['from'] = floor(($duration * (int)$f['from']) / 100);
// }
// if (isset($f['duration']) && str_ends_with(trim($f['duration']), '%')) {
// if(null === $duration) {
// throw new InvalidArgumentException('Unknown video duration, cannot use relative "duration"');
// }
// $f['duration'] = floor(($duration * (int)$f['duration']) / 100);
// }
// }

// first, turn the video into a clip
if (!empty($filters) && 'pre_clip' === $filters[0]['name']) {
$filter = array_shift($filters);
Expand All @@ -162,10 +145,10 @@ function ($filter) {

foreach ($filters as $filter) {
if ('pre_clip' === $filter['name']) {
throw new InvalidArgumentException('"pre_clip" filter must be the first filter');
throw new \InvalidArgumentException('"pre_clip" filter must be the first filter');
}
if (!method_exists($this, $filter['name'])) {
throw new InvalidArgumentException(sprintf('Invalid filter: %s', $filter['name']));
throw new \InvalidArgumentException(sprintf('Invalid filter: %s', $filter['name']));
}

/* @uses self::resize(), self::rotate(), self::pad(), self::crop(), self::clip(), self::synchronize()
Expand Down Expand Up @@ -196,15 +179,15 @@ private function doAudio(FormatInterface $ouputFormat, string $extension, InputF
$options = $compiledOptions->asArray();

$format = $ouputFormat->getFormat();
if(!method_exists($ouputFormat, 'getFFMpegFormat')) {
throw new InvalidArgumentException('format %s does not declare FFMpeg format', $format);
if (!method_exists($ouputFormat, 'getFFMpegFormat')) {
throw new \InvalidArgumentException('format %s does not declare FFMpeg format', $format);
}
/** @var FFMpegFormatInterface $FFMpegFormat */
$FFMpegFormat = $ouputFormat->getFFMpegFormat();

if ($audioCodec = $options['audio_codec'] ?? null) {
if (!in_array($audioCodec, $FFMpegFormat->getAvailableAudioCodecs())) {
throw new InvalidArgumentException(sprintf('Invalid audio codec %s for format %s', $audioCodec, $format));
throw new \InvalidArgumentException(sprintf('Invalid audio codec %s for format %s', $audioCodec, $format));
}
$FFMpegFormat->setAudioCodec($audioCodec);
}
Expand All @@ -219,7 +202,7 @@ private function preClip(Video $video, array $options, TransformationContextInte

$startAsTimecode = $durationAsTimecode = false;

if (is_numeric($start) && (float)$start >= 0) {
if (is_numeric($start) && (float) $start >= 0) {
$startAsTimecode = TimeCode::fromSeconds($start);
} elseif (is_string($start)) {
$startAsTimecode = TimeCode::fromString($start);
Expand All @@ -228,7 +211,7 @@ private function preClip(Video $video, array $options, TransformationContextInte
throw new \InvalidArgumentException('Invalid start for filter "clip"');
}

if (is_numeric($duration) && (float)$duration > 0) {
if (is_numeric($duration) && (float) $duration > 0) {
$durationAsTimecode = TimeCode::fromSeconds($duration);
} elseif (is_string($duration)) {
$durationAsTimecode = TimeCode::fromString($duration);
Expand All @@ -238,6 +221,7 @@ private function preClip(Video $video, array $options, TransformationContextInte
}

$context->log(sprintf("Applying 'pre_clip' filter: start=%s, duration=%s", $startAsTimecode, $durationAsTimecode));

return $video->clip($startAsTimecode, $durationAsTimecode);
}

Expand Down Expand Up @@ -278,7 +262,7 @@ private function rotate(Clip $clip, array $options, TransformationContextInterfa
180 => FFMpeg\Filters\Video\RotateFilter::ROTATE_180,
270 => FFMpeg\Filters\Video\RotateFilter::ROTATE_270,
];
$angle = (int)($options['angle'] ?? 0);
$angle = (int) ($options['angle'] ?? 0);
if (!array_key_exists($angle, $rotations)) {
throw new \InvalidArgumentException('Invalid rotation, must be 90, 180 or 270 for filter "rotate"');
}
Expand All @@ -299,10 +283,10 @@ private function crop(Clip $clip, array $options, TransformationContextInterface
{
$x = $options['x'] ?? 0;
$y = $options['y'] ?? 0;
if(!is_numeric($x) || !is_numeric($y)) {
if (!is_numeric($x) || !is_numeric($y)) {
throw new \InvalidArgumentException('Invalid x/y for filter "crop"');
}
$point = new FFMpeg\Coordinate\Point((int)$x, (int)$y);
$point = new FFMpeg\Coordinate\Point((int) $x, (int) $y);
$dimension = $this->getDimension($options, 'crop');

$context->log(sprintf("Applying 'crop' filter: point=%s, dimension=%s", var_export($point, true), var_export($dimension, true)));
Expand All @@ -316,7 +300,7 @@ private function clip(Clip $clip, array $options, TransformationContextInterface

$startAsTimecode = $durationAsTimecode = false;

if (is_numeric($start) && (float)$start >= 0) {
if (is_numeric($start) && (float) $start >= 0) {
$startAsTimecode = TimeCode::fromSeconds($start);
} elseif (is_string($start)) {
$startAsTimecode = TimeCode::fromString($start);
Expand All @@ -325,7 +309,7 @@ private function clip(Clip $clip, array $options, TransformationContextInterface
throw new \InvalidArgumentException('Invalid start for filter "clip"');
}

if (is_numeric($duration) && (float)$duration > 0) {
if (is_numeric($duration) && (float) $duration > 0) {
$durationAsTimecode = TimeCode::fromSeconds($duration);
} elseif (is_string($duration)) {
$durationAsTimecode = TimeCode::fromString($duration);
Expand Down Expand Up @@ -364,28 +348,28 @@ private function watermark(Clip $clip, array $options, TransformationContextInte
throw new \InvalidArgumentException('Invalid position for filter "watermark"');
}

array_walk($coord, fn (&$v) => $v = (int)$v);
array_walk($coord, fn (&$v) => $v = (int) $v);

$context->log(sprintf("Applying 'watermark' filter: path=%s, coord=%s", $path, var_export($coord, true)));
$clip->filters()->watermark($path, $coord);
}

private function framerate(Clip $clip, array $options, TransformationContextInterface $context): void
{
$framerate = (int)($options['framerate'] ?? 0);
$framerate = (int) ($options['framerate'] ?? 0);
if ($framerate <= 0) {
throw new \InvalidArgumentException('Invalid framerate for filter "framerate"');
}
$gop = (int)($options['gop'] ?? 0);
$gop = (int) ($options['gop'] ?? 0);

$context->log(sprintf("Applying 'framerate' filter: framerate=%d, gop=%d", $framerate, $gop));
$clip->filters()->framerate(new FFMpeg\Coordinate\FrameRate($framerate), $gop);
}

private function getDimension(array $options, string $filterName): FFMpeg\Coordinate\Dimension
{
$width = (int)($options['width'] ?? 0);
$height = (int)($options['height'] ?? 0);
$width = (int) ($options['width'] ?? 0);
$height = (int) ($options['height'] ?? 0);
if ($width <= 0 || $height <= 0) {
throw new \InvalidArgumentException(sprintf('Invalid width/height for filter "%s"', $filterName));
}
Expand Down

0 comments on commit 37ea3ab

Please sign in to comment.