|
8 | 8 | use Elegantly\Media\Enums\MediaType;
|
9 | 9 | use Elegantly\Media\Models\Media;
|
10 | 10 | use Elegantly\Media\Models\MediaConversion;
|
11 |
| -use FFMpeg\Coordinate\AspectRatio; |
| 11 | +use FFMpeg\Coordinate\Dimension; |
12 | 12 | use FFMpeg\Filters\Video\ResizeFilter;
|
| 13 | +use FFMpeg\Filters\Video\VideoFilters; |
13 | 14 | use FFMpeg\Format\FormatInterface;
|
14 | 15 | use FFMpeg\Format\Video\X264;
|
15 | 16 | use Illuminate\Contracts\Filesystem\Filesystem;
|
@@ -69,30 +70,29 @@ public function handle(
|
69 | 70 |
|
70 | 71 | $fileName = $this->fileName ?? "{$media->name}.mp4";
|
71 | 72 |
|
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 |
| - |
84 | 73 | $ffmpeg = FFMpeg::fromFilesystem($filesystem)
|
85 | 74 | ->open($file)
|
86 | 75 | ->export()
|
87 | 76 | ->inFormat($this->format);
|
88 | 77 |
|
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 | + )); |
96 | 96 | }
|
97 | 97 |
|
98 | 98 | $ffmpeg->save($fileName);
|
|
0 commit comments