ffmpeg resize down larger video to fit desired size and add padding

亡梦爱人 提交于 2019-11-30 01:48:58

try -vf "scale=iw*min(405/iw\,320/ih):ih*min(405/iw\,320/ih),pad=405:320:(405-iw)/2:(320-ih)/2"

Edit to clarify what's going on in that line: you are asking how to scale one box to fit inside another box. The boxes might have different aspect ratios. If they do, you want to fill one dimension, and center along the other dimension.

# you defined the max width and max height in your original question
max_width     = 405
max_height    = 320

# first, scale the image to fit along one dimension
scale         = min(max_width/input_width, max_height/input_height)
scaled_width  = input_width  * scale
scaled_height = input_height * scale

# then, position the image on the padded background
padding_ofs_x = (max_width  - input_width) / 2
padding_ofs_y = (max_height - input_height) / 2
Seth

Here is a generic filter expression for scaling (maintaining aspect ratio) and padding any source size to any target size:

-vf "scale=min(iw*TARGET_HEIGHT/ih\,TARGET_WIDTH):min(TARGET_HEIGHT\,ih*TARGET_WIDTH/iw),
     pad=TARGET_WIDTH:TARGET_HEIGHT:(TARGET_WIDTH-iw)/2:(TARGET_HEIGHT-ih)/2"

Replace TARGET_WIDTH and TARGET_HEIGHT with your desired values. I use this to pull a 200x120 padded thumbnail from any video. Props to davin for his nice overview of the math.

Try this:

-vf 'scale=640:480:force_original_aspect_ratio=decrease,pad=640:480:x=(640-iw)/2:y=(480-ih)/2:color=black'

According to FFmpeg documentation, the force_original_aspect_ratio option is useful to keep the original aspect ratio when scaling:

   force_original_aspect_ratio
       Enable decreasing or increasing output video width or height if
       necessary to keep the original aspect ratio. Possible values:

       disable
           Scale the video as specified and disable this feature.

       decrease
           The output video dimensions will automatically be decreased if
           needed.

       increase
           The output video dimensions will automatically be increased if
           needed.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!