From 2e1218cfd1a93d8fdeb230acc58ef5d050d85f8f Mon Sep 17 00:00:00 2001 From: Grischa Erbe Date: Fri, 21 Feb 2025 01:33:16 +0100 Subject: [PATCH 1/3] Handle dimensions of rotated videos --- src/Assets/Attributes.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index 010de336cc..fe85840235 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -88,10 +88,19 @@ private function getVideoAttributes() { $id3 = ExtractInfo::fromAsset($this->asset); + $width = Arr::get($id3, 'video.resolution_x'); + $height = Arr::get($id3, 'video.resolution_y'); + $rotate = Arr::get($id3, 'video.rotate', 0); + + // Adjust width and height if the video is rotated + if (in_array($rotate, [90, 270, -90, -270])) { + list($width, $height) = [$height, $width]; + } + return [ - 'width' => Arr::get($id3, 'video.resolution_x'), - 'height' => Arr::get($id3, 'video.resolution_y'), - 'duration' => Arr::get($id3, 'playtime_seconds'), + 'width' => $width, + 'height' => $height, + 'duration' => Arr::get($id3, 'playtime_seconds') ]; } } From 43bbf7a303bc8a10198f5c7e920c6a8a19da2d86 Mon Sep 17 00:00:00 2001 From: Grischa Erbe Date: Fri, 21 Feb 2025 10:49:31 +0100 Subject: [PATCH 2/3] linting fixes --- src/Assets/Attributes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Assets/Attributes.php b/src/Assets/Attributes.php index fe85840235..fba9435e2f 100644 --- a/src/Assets/Attributes.php +++ b/src/Assets/Attributes.php @@ -94,13 +94,13 @@ private function getVideoAttributes() // Adjust width and height if the video is rotated if (in_array($rotate, [90, 270, -90, -270])) { - list($width, $height) = [$height, $width]; + [$width, $height] = [$height, $width]; } return [ 'width' => $width, 'height' => $height, - 'duration' => Arr::get($id3, 'playtime_seconds') + 'duration' => Arr::get($id3, 'playtime_seconds'), ]; } } From d981ca31d5ac0784126c5a7ab7d74746457f0428 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 21 Feb 2025 11:23:40 -0500 Subject: [PATCH 3/3] Add test --- tests/Assets/AttributesTest.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/Assets/AttributesTest.php b/tests/Assets/AttributesTest.php index 6cd3786556..87fc3d02ca 100644 --- a/tests/Assets/AttributesTest.php +++ b/tests/Assets/AttributesTest.php @@ -6,6 +6,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Assets\Asset; use Statamic\Assets\Attributes; @@ -78,23 +79,37 @@ public function it_gets_the_attributes_of_audio_file() } #[Test] - public function it_gets_the_attributes_of_video_file() + #[DataProvider('videoProvider')] + public function it_gets_the_attributes_of_video_file($playtimeSeconds, $resolutionX, $resolutionY, $rotate, $expected) { $asset = (new Asset) ->container(AssetContainer::make('test-container')->disk('test')) ->path('path/to/asset.mp4'); ExtractInfo::shouldReceive('fromAsset')->with($asset)->andReturn([ - 'playtime_seconds' => 13, + 'playtime_seconds' => $playtimeSeconds, 'video' => [ - 'resolution_x' => 1920, - 'resolution_y' => 1080, + 'resolution_x' => $resolutionX, + 'resolution_y' => $resolutionY, + 'rotate' => $rotate, ], ]); $attributes = $this->attributes->asset($asset); - $this->assertEquals(['duration' => 13, 'width' => 1920, 'height' => 1080], $attributes->get()); + $this->assertEquals($expected, $attributes->get()); + } + + public static function videoProvider() + { + return [ + 'not rotated' => [13, 1920, 1080, null, ['duration' => 13, 'width' => 1920, 'height' => 1080]], + 'rotated 90' => [13, 1920, 1080, 90, ['duration' => 13, 'width' => 1080, 'height' => 1920]], + 'rotated -90' => [13, 1920, 1080, -90, ['duration' => 13, 'width' => 1080, 'height' => 1920]], + 'rotated 270' => [13, 1920, 1080, 270, ['duration' => 13, 'width' => 1080, 'height' => 1920]], + 'rotated -270' => [13, 1920, 1080, -270, ['duration' => 13, 'width' => 1080, 'height' => 1920]], + 'rotated 180' => [13, 1920, 1080, 180, ['duration' => 13, 'width' => 1920, 'height' => 1080]], + ]; } #[Test]