Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResizeFilter with ffmpeg autorotation #890

Open
giulioprovasi opened this issue Feb 2, 2023 · 2 comments
Open

ResizeFilter with ffmpeg autorotation #890

giulioprovasi opened this issue Feb 2, 2023 · 2 comments

Comments

@giulioprovasi
Copy link

Q A
Bug? yes
New Feature? no
Version Used Specific tag or commit sha
FFmpeg Version FFmpeg 5.1.2
OS arch linux

Actual Behavior

When doing a vertical video conversion, the resize filter stretches the video

Expected Behavior

Applying the resize filter should keep into consideration the input video rotation

Steps to Reproduce

require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mov');
$video->filters()
    ->resize(
        new FFMpeg\Coordinate\Dimension(320, 180),
        FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_INSET
    )
    ->synchronize();

$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg'); // stretched
$video
    ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4'); // stretched

example taken from here

Possible Solutions

I actually tried this and works for my use case, maybe we may change the ResizeFilter accordingly ? (I may work on a PR if this works for you)

- $commands[] = '[in]scale=' . $dimensions->getWidth() . ':' . $dimensions->getHeight() . ' [out]';
+ $commands[] = "[in]scale=w='if(gt(iw\,ih),{$dimensions->getWidth()},{$dimensions->getHeight()})':h='if(gt(iw\,ih),{$dimensions->getHeight()},{$dimensions->getWidth()})'[out]";

By checking the input width/height before applying the resize filter, the output video has a good format/orientation.

Maybe the ResizeFilter may introduce a default parameter to "handle auto detect of input orientation"

-    public function __construct(Dimension $dimension, $mode = self::RESIZEMODE_FIT, $forceStandards = true, $priority = 0)
+    public function __construct(Dimension $dimension, $mode = self::RESIZEMODE_FIT, $forceStandards = true, $priority = 0, $guessOrientation = false)

...

+ if ($this->guessOrientation) {
+    $commands[] = "[in]scale=w='if(gt(iw\,ih),{$dimensions->getWidth()},{$dimensions->getHeight()})':h='if(gt(iw\,ih),{$dimensions->getHeight()},{$dimensions->getWidth()})'[out]";
+ } else {
   $commands[] = '[in]scale=' . $dimensions->getWidth() . ':' . $dimensions->getHeight() . ' [out]';
+ }
@fritzmg
Copy link

fritzmg commented Feb 14, 2024

I noticed the same issue. Had a 1080x1920 vertical video from an iPhone (according to the meta data it has a 90° rotation) and when using

$video
    ->filters()
    ->resize(new Dimension(1920, 1080), ResizeFilter::RESIZEMODE_INSET, false)
    ->synchronize()
;

the video will end up being a 1920x1080 stretched video, instead of 607x1080 (which I would have expected at least).

@fritzmg
Copy link

fritzmg commented Apr 5, 2024

Using this solution solves the issue for me: #177 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants