Skip to content

Commit

Permalink
rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jygaulier committed Nov 4, 2024
1 parent a8a931a commit 6c203eb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\ServiceLocator;

abstract readonly class AbstractVideoTransformerBase
abstract readonly class AbstractVideoTransformer
{
public function __construct(#[AutowireLocator(FormatInterface::TAG, defaultIndexMethod: 'getFormat')] protected ServiceLocator $formats,
protected ModuleOptionsResolver $optionsResolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use FFMpeg\Media\Clip;
use FFMpeg\Media\Video;

final readonly class FFMpegTransformerModule extends AbstractVideoTransformerBase implements TransformerModuleInterface
final readonly class FFMpegTransformerModule extends AbstractVideoTransformer implements TransformerModuleInterface
{
public static function getName(): string
{
Expand All @@ -29,7 +29,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException('Invalid input file family, should be video');
}

$commonArgs = new ModuleCommonArgsDTO($this->formats, $options, $context, $this->optionsResolver);
$commonArgs = new ModuleCommonArgs($this->formats, $options, $context, $this->optionsResolver);

if (FamilyEnum::Video === $commonArgs->getOutputFormat()->getFamily()) {
return $this->doVideo($options, $inputFile, $context, $commonArgs);
Expand All @@ -42,7 +42,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException(sprintf('Invalid format %s, only video or audio format supported', $commonArgs->getOutputFormat()->getFormat()));
}

private function doVideo(array $options, InputFileInterface $inputFile, TransformationContextInterface $transformationContext, ModuleCommonArgsDTO $commonArgs): OutputFileInterface
private function doVideo(array $options, InputFileInterface $inputFile, TransformationContextInterface $transformationContext, ModuleCommonArgs $commonArgs): OutputFileInterface
{
$outputFormat = $commonArgs->getOutputFormat();
$format = $outputFormat->getFormat();
Expand Down Expand Up @@ -149,7 +149,7 @@ function ($filter) use ($resolverContext) {
/**
* todo: implement audio filters.
*/
private function doAudio(array $options, InputFileInterface $inputFile, TransformationContextInterface $context, ModuleCommonArgsDTO $commonArgs): OutputFileInterface
private function doAudio(array $options, InputFileInterface $inputFile, TransformationContextInterface $context, ModuleCommonArgs $commonArgs): OutputFileInterface
{
$resolverContext = [
'metadata' => $context->getTemplatingContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use FFMpeg;
use Symfony\Component\DependencyInjection\ServiceLocator;

class ModuleCommonArgsDTO
class ModuleCommonArgs
{
private FormatInterface $outputFormat;
private string $extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

final readonly class VideoSummaryTransformerModule extends AbstractVideoTransformerBase implements TransformerModuleInterface
final readonly class VideoSummaryTransformerModule extends AbstractVideoTransformer implements TransformerModuleInterface
{
public static function getName(): string
{
Expand All @@ -33,7 +33,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException('Invalid input file family, should be video');
}

$commonArgs = new ModuleCommonArgsDTO($this->formats, $options, $context, $this->optionsResolver);
$commonArgs = new ModuleCommonArgs($this->formats, $options, $context, $this->optionsResolver);
$outputFormat = $commonArgs->getOutputFormat();
$format = $outputFormat->getFormat();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use Alchemy\RenditionFactory\DTO\OutputFileInterface;
use Alchemy\RenditionFactory\Transformer\TransformerModuleInterface;
use FFMpeg;
use FFMpeg\Coordinate\TimeCode;

final readonly class VideoToAnimationTransformerModule extends AbstractVideoTransformerBase implements TransformerModuleInterface
final readonly class VideoToAnimationTransformerModule extends AbstractVideoTransformer implements TransformerModuleInterface
{
public static function getName(): string
{
Expand All @@ -25,7 +26,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException('Invalid input file family, should be video');
}

$commonArgs = new ModuleCommonArgsDTO($this->formats, $options, $context, $this->optionsResolver);
$commonArgs = new ModuleCommonArgs($this->formats, $options, $context, $this->optionsResolver);
$outputFormat = $commonArgs->getOutputFormat();

/** @var FFMpeg\Media\Video $video */
Expand All @@ -36,11 +37,31 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
'input' => $video->getStreams()->videos()->first()->all(),
];

$from = FFMpeg\Coordinate\TimeCode::fromSeconds($this->optionsResolver->resolveOption($options['from_seconds'] ?? 0, $resolverContext));
$start = $this->optionsResolver->resolveOption($options['start'] ?? 0, $resolverContext);
$startAsTimecode = false;
if (is_numeric($start) && (float) $start >= 0) {
$startAsTimecode = TimeCode::fromSeconds($start);
} elseif (is_string($start)) {
$startAsTimecode = TimeCode::fromString($start);
}
if (false === $startAsTimecode) {
throw new \InvalidArgumentException('Invalid start.');
}
$start = $startAsTimecode->toSeconds();


$duration = $this->optionsResolver->resolveOption($options['duration'] ?? null, $resolverContext);
if (null !== $duration && ($duration = (int) $duration) <= 0) {
throw new \InvalidArgumentException('Invalid duration');
$durationAsTimecode = false;
if (is_numeric($duration) && (float) $duration >= 0) {
$durationAsTimecode = TimeCode::fromSeconds($duration);
} elseif (is_string($duration)) {
$durationAsTimecode = TimeCode::fromString($duration);
}
if (null !== $duration ) {
if (false === $durationAsTimecode) {
throw new \InvalidArgumentException('Invalid duration for filter "clip"');
}
$duration = $durationAsTimecode->toSeconds();
}

if (($fps = (int) $this->optionsResolver->resolveOption($options['fps'] ?? 1, $resolverContext)) <= 0) {
Expand Down Expand Up @@ -74,13 +95,13 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException('Invalid resize mode');
}

$context->log(sprintf(' from=%s, duration=%s, fps=%s, width=%d, height=%d', $from, $duration, $fps, $width, $height));
$context->log(sprintf(' start=%s, duration=%s, fps=%s, width=%d, height=%d', $start, $duration, $fps, $width, $height));

$commands = [
'-i',
$inputFile->getPath(),
'-ss',
$from,
$start,
];
if (null !== $duration) {
$commands[] = '-t';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use FFMpeg;
use FFMpeg\Media\Video;

final readonly class VideoToFrameTransformerModule extends AbstractVideoTransformerBase implements TransformerModuleInterface
final readonly class VideoToFrameTransformerModule extends AbstractVideoTransformer implements TransformerModuleInterface
{
public static function getName(): string
{
Expand All @@ -26,7 +26,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException('Invalid input file family, should be video');
}

$commonArgs = new ModuleCommonArgsDTO($this->formats, $options, $context, $this->optionsResolver);
$commonArgs = new ModuleCommonArgs($this->formats, $options, $context, $this->optionsResolver);
$outputFormat = $commonArgs->getOutputFormat();

/** @var Video $video */
Expand Down

0 comments on commit 6c203eb

Please sign in to comment.