Skip to content

Commit

Permalink
PS-670-rendition-video_enhance1 (#458)
Browse files Browse the repository at this point in the history
* add mime+family to video output ; add video options

* rename (ffmpeg) threads option
  • Loading branch information
jygaulier authored Oct 2, 2024
1 parent 0189c9b commit f80897e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/php/rendition-factory/src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function configure(): void

$this->addArgument('src', InputArgument::REQUIRED, 'The source file');
$this->addArgument('build-config', InputArgument::REQUIRED, 'The build config YAML file');
$this->addOption('type', 't', InputOption::VALUE_REQUIRED, 'The MIME type of file');
$this->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Force the MIME type of file');
$this->addOption('working-dir', 'w', InputOption::VALUE_REQUIRED, 'The working directory. Defaults to system temp directory');
$this->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'The output file name WITHOUT extension');
$this->addOption('debug', 'd', InputOption::VALUE_NONE, 'set to debug mode (keep files in working directory)');
Expand All @@ -56,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (null === $mimeType) {
$mimeType = $this->mimeTypeGuesser->guessMimeTypeFromPath($src);
$output->writeln(sprintf('MIME type guessed: %s', $mimeType));
}

$buildConfig = $this->yamlLoader->load($input->getArgument('build-config'));
Expand Down
7 changes: 6 additions & 1 deletion lib/php/rendition-factory/src/FileFamilyGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public function getFamily(string $src, string $mimeType): FamilyEnum
}

return match ($mimeType) {
'application/pdf', 'text/rtf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.oasis.opendocument.spreadsheet' => FamilyEnum::Document,
'application/pdf', 'text/rtf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.oasis.opendocument.spreadsheet' => FamilyEnum::Document,

'application/mxf', 'application/ogg' => FamilyEnum::Video,

default => FamilyEnum::Unknown,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Alchemy\RenditionFactory\Transformer\Video;

use Alchemy\RenditionFactory\Context\TransformationContext;
use Alchemy\RenditionFactory\Context\TransformationContextInterface;
use Alchemy\RenditionFactory\DTO\FamilyEnum;
use Alchemy\RenditionFactory\DTO\InputFileInterface;
use Alchemy\RenditionFactory\DTO\OutputFile;
use Alchemy\RenditionFactory\DTO\OutputFileInterface;
use Alchemy\RenditionFactory\FileFamilyGuesser;
use Alchemy\RenditionFactory\Transformer\TransformerModuleInterface;
use FFMpeg;
use FFMpeg\Coordinate\TimeCode;
Expand Down Expand Up @@ -43,7 +42,7 @@ public function transform(InputFileInterface $inputFile, array $options, Transfo
throw new \InvalidArgumentException(sprintf('Invalid format %s', $format));
}

private function doVideo(string $format, string $extension, InputFileInterface $inputFile, array $options, TransformationContext $context): OutputFileInterface
private function doVideo(string $format, string $extension, InputFileInterface $inputFile, array $options, TransformationContextInterface $context): OutputFileInterface
{
$fqcnFormat = 'FFMpeg\\Format\\Video\\'.$format;
/** @var FormatInterface $ouputFormat */
Expand All @@ -61,8 +60,52 @@ private function doVideo(string $format, string $extension, InputFileInterface $
}
$ouputFormat->setAudioCodec($audioCodec);
}
if (null !== ($videoKilobitrate = $options['video_kilobitrate'] ?? null)) {
if(!method_exists($ouputFormat, 'setKiloBitrate')) {
throw new \InvalidArgumentException(sprintf('format %s does not support video_kilobitrate', $format));
}
if(!is_int($videoKilobitrate)) {
throw new \InvalidArgumentException('Invalid video kilobitrate');
}
$ouputFormat->setKiloBitrate($videoKilobitrate);
}
if (null !== ($audioKilobitrate = $options['audio_kilobitrate'] ?? null)) {
if(!method_exists($ouputFormat, 'setAudioKiloBitrate')) {
throw new \InvalidArgumentException(sprintf('format %s does not support audio_kilobitrate', $format));
}
if(!is_int($audioKilobitrate)) {
throw new \InvalidArgumentException('Invalid audio kilobitrate');
}
$ouputFormat->setAudioKiloBitrate($audioKilobitrate);
}
if (null !== ($passes = $options['passes'] ?? null)) {
if(!method_exists($ouputFormat, 'setPasses')) {
throw new \InvalidArgumentException(sprintf('format %s does not support passes', $format));
}
if(!is_int($passes) || $passes < 1) {
throw new \InvalidArgumentException('Invalid passes count');
}
if(0 === $videoKilobitrate) {
throw new \InvalidArgumentException('passes must not be set if video_kilobitrate is 0');
}
$ouputFormat->setPasses($passes);
}

$ffmpegOptions = [];
if ($timeout = $options['timeout'] ?? null) {
if(!is_int($timeout)) {
throw new \InvalidArgumentException('Invalid timeout');
}
$ffmpegOptions['timeout'] = $timeout;
}
if ($threads = $options['threads'] ?? null) {
if(!is_int($threads) || $threads < 1) {
throw new \InvalidArgumentException('Invalid threads count');
}
$ffmpegOptions['ffmpeg.threads'] = $threads;
}
$ffmpeg = FFMpeg\FFMpeg::create($ffmpegOptions, $context->getLogger());

$ffmpeg = FFMpeg\FFMpeg::create([], $context->getLogger());
/** @var Video $video */
$video = $ffmpeg->open($inputFile->getPath());

Expand Down Expand Up @@ -98,10 +141,14 @@ private function doVideo(string $format, string $extension, InputFileInterface $
unset($clip);
unset($video);

$mimeType = $context->guessMimeTypeFromPath($outputPath);
$fileFamilyGuesser = new FileFamilyGuesser();
$family = $fileFamilyGuesser->getFamily($outputPath, $mimeType);

return new OutputFile(
$outputPath,
'application/octet-stream',
FamilyEnum::Unknown
$mimeType,
$family
);
}

Expand All @@ -124,7 +171,7 @@ private function doAudio(string $format, string $extension, InputFileInterface $
throw new \InvalidArgumentException('Audio transformation not implemented');
}

private function preClip(Video $video, array $options, TransformationContext $context): Clip
private function preClip(Video $video, array $options, TransformationContextInterface $context): Clip
{
$start = $options['start'] ?? 0;
$duration = $options['duration'] ?? null;
Expand Down

0 comments on commit f80897e

Please sign in to comment.