From 436cdc46134658a7ccefce109604fe9fc54da5b3 Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Sun, 12 Jan 2025 16:31:07 +0100 Subject: [PATCH] add FFmpeg conversion --- src/Definitions/MediaConversionFFmpeg.php | 89 +++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/Definitions/MediaConversionFFmpeg.php diff --git a/src/Definitions/MediaConversionFFmpeg.php b/src/Definitions/MediaConversionFFmpeg.php new file mode 100644 index 0000000..ee307c4 --- /dev/null +++ b/src/Definitions/MediaConversionFFmpeg.php @@ -0,0 +1,89 @@ + null, + when: $when, + onCompleted: $onCompleted, + immediate: $immediate, + queued: $queued, + queue: $queue, + conversions: $conversions + ); + } + + public function shouldExecute(Media $media, ?MediaConversion $parent): bool + { + if ($this->when !== null) { + return parent::shouldExecute($media, $parent); + } + + $source = $parent ?? $media; + + return in_array($source->type, [MediaType::Video, MediaType::Audio]); + } + + public function handle( + Media $media, + ?MediaConversion $parent, + ?string $file, + Filesystem $filesystem, + SpatieTemporaryDirectory $temporaryDirectory + ): ?MediaConversion { + + if (! $file) { + return null; + } + + $fileName = $this->fileName; + $manipulate = $this->manipulate; + + $newFile = $fileName($media, $parent); + + $ffmpeg = FFMpeg::fromFilesystem($filesystem) + ->open($file) + ->export(); + + $manipulate($ffmpeg); + + $ffmpeg->save($newFile); + + return $media->addConversion( + file: $filesystem->path($newFile), + conversionName: $this->name, + parent: $parent, + ); + + } +}