Tom
Tom

Reputation: 797

ffmpeg change resolution by condition

I want to change my video resolution with ffmpeg:

-s 852×480

How can I do it only when video width or height greater than 852×480?

I want something like this with ffmpeg, not with my programming language:

if video.width > 852:
   resize width and proportionally resize height

if video.height > 480:
   resize height and proportionally resize width

if video.width > 852 and video.height > 480:
   resize height width

Upvotes: 3

Views: 2212

Answers (2)

Dirk V
Dirk V

Reputation: 1469

use this filter in your command:

-filter_complex "scale=if(gt(iw,852),852,-1)':'if(gt(ih,480),480,-1)':force_original_aspect_ratio=decrease"

Upvotes: 3

Tom
Tom

Reputation: 797

@LordNeckbeard gave me a link https://superuser.com/questions/566998/how-can-i-fit-a-video-to-a-certain-size-but-dont-upscale-it-with-ffmpeg/567934#567934 - but it is really messy solution.

So I just did it with python:

maximal_width = 852.0
maximal_height = 480.0
if width > maximal_width and height > maximal_height:
    args += ['-vf', 'scale=%s:%s' % (maximal_width, maximal_height)]
else:
    if width > maximal_width:
        coefficient = width / maximal_width
        calculated_height = height / coefficient
        args += ['-vf', 'scale=%s:%s' % (maximal_width, calculated_height)]

    if height > maximal_height:
        coefficient = height / maximal_height
        calculated_width = width / coefficient
        args += ['-vf', 'scale=%s:%s' % (calculated_width, maximal_height)]

Upvotes: 1

Related Questions