Maintaining aspect ratio with FFmpeg

前端 未结 8 1420
悲&欢浪女
悲&欢浪女 2021-01-29 22:39

I need to convert a bunch of video files using FFmpeg. I run a Bash file that converts all the files nicely, however there is a problem if a file converted is not in 16:9 format

8条回答
  •  温柔的废话
    2021-01-29 22:58

    If you are trying to fit a bounding box, then using force_original_aspect_ratio as per xmedeko's answer is a good starting point.

    However, this does not work if your input video has a weird size and you are encoding to a format that requires the dimensions to be divisible by 2, resulting in an error.

    In this case, you can use expression evaluation in the scale function, like that used in Charlie's answer.

    Assuming an output bounding box of 720x400:

    -vf "scale='trunc(min(1,min(720/iw,400/ih))*iw/2)*2':'trunc(min(1,min(720/iw,400/ih))*ih/2)*2'"

    To break this down:

    • min(1,min(720/iw,400/ih) finds the scaling factor to fit within the bounding box (from here), constraining it to a maximum of 1 to ensure it only downscales, and
    • trunc(*iw/2)*2 and trunc(*iw/2)*2 ensure that the dimensions are divisible by 2 by dividing by 2, making the result an integer, then multiplying it back by 2.

    This eliminates the need for finding the dimensions of the input video prior to encoding.

提交回复
热议问题