Skip to content

Commit 1103b41

Browse files
committed
auto scale video conversion
1 parent 347a2a1 commit 1103b41

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/Definitions/MediaConversionVideo.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Elegantly\Media\Enums\MediaType;
99
use Elegantly\Media\Models\Media;
1010
use Elegantly\Media\Models\MediaConversion;
11-
use FFMpeg\Coordinate\AspectRatio;
11+
use FFMpeg\Coordinate\Dimension;
1212
use FFMpeg\Filters\Video\ResizeFilter;
13+
use FFMpeg\Filters\Video\VideoFilters;
1314
use FFMpeg\Format\FormatInterface;
1415
use FFMpeg\Format\Video\X264;
1516
use Illuminate\Contracts\Filesystem\Filesystem;
@@ -69,30 +70,29 @@ public function handle(
6970

7071
$fileName = $this->fileName ?? "{$media->name}.mp4";
7172

72-
$source = $parent ?? $media;
73-
74-
$ratio = new AspectRatio($source->aspect_ratio);
75-
76-
$modulus = match (true) {
77-
$this->format instanceof X264 => 2, // dimensions must be divisible by 2
78-
default => 1,
79-
};
80-
81-
$width = $this->width ?? ($this->height ? $ratio->calculateWidth($this->height, $modulus) : null);
82-
$height = $this->height ?? ($this->width ? $ratio->calculateHeight($this->width, $modulus) : null);
83-
8473
$ffmpeg = FFMpeg::fromFilesystem($filesystem)
8574
->open($file)
8675
->export()
8776
->inFormat($this->format);
8877

89-
if ($width && $height) {
90-
$ffmpeg->resize(
91-
$width,
92-
$height,
93-
$this->fitMethod,
94-
$this->forceStandards
95-
);
78+
if ($this->width && $this->height) {
79+
$ffmpeg->addFilter(fn (VideoFilters $filters) => $filters->resize(
80+
dimension: new Dimension($this->width, $this->height),
81+
mode: $this->fitMethod,
82+
forceStandards: $this->forceStandards,
83+
));
84+
} elseif ($this->width) {
85+
$ffmpeg->addFilter(fn (VideoFilters $filters) => $filters->resize(
86+
dimension: new Dimension($this->width, 1),
87+
mode: ResizeFilter::RESIZEMODE_SCALE_HEIGHT,
88+
forceStandards: $this->forceStandards,
89+
));
90+
} elseif ($this->height) {
91+
$ffmpeg->addFilter(fn (VideoFilters $filters) => $filters->resize(
92+
dimension: new Dimension(1, $this->height),
93+
mode: ResizeFilter::RESIZEMODE_SCALE_WIDTH,
94+
forceStandards: $this->forceStandards,
95+
));
9696
}
9797

9898
$ffmpeg->save($fileName);

0 commit comments

Comments
 (0)