ffmpeg调整较大的视频大小以适合所需的大小并添加填充

mis*_*inx 20 ffmpeg padding

我正在尝试调整较大的视频以适应我所拥有的区域.为了实现这一点,我首先计算调整大小的视频的尺寸,使其适合我的区域,然后我尝试为此视频添加填充,以便最终结果具有所需的尺寸,同时保持纵横比.

所以,假设我的原始视频尺寸为1280x720,为了适应我的405x320区域,我需要先将视频大小调整为405x227.我这样做.此时一切都很好.我做了一些数学计算,我发现我必须在顶部和底部添加46像素的填充.

所以命令的padding参数就是-vf "pad=405:320:0:46:black".但是每次运行命令时都会出现错误Input area 0:46:405:273 not within the padded area 0:0:404:226.

我发现的唯一填充文档是http://ffmpeg.org/libavfilter.html#pad.

我不知道我做错了什么.以前有人有这个问题吗?你有什么建议吗?

doo*_*che 44

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

编辑以澄清该行中发生的事情:您正在询问如何缩放一个框以适合另一个框.这些方框可能有不同的宽高比.如果他们这样做,您想要填充一个维度,并沿着另一个维度居中.

# 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
Run Code Online (Sandbox Code Playgroud)


Set*_*eth 8

以下是用于缩放(保持宽高比)和将任何源大小填充到任何目标大小的通用过滤器表达式:

-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"
Run Code Online (Sandbox Code Playgroud)

替换TARGET_WIDTHTARGET_HEIGHT使用您想要的值.我用它来从任何视频中提取200x120填充缩略图.为了他对数学的精彩概述而道具.

  • 这将适用于变形视频:`-vf "scale=(iw*sar)*min(TARGET_WIDTH/(iw*sar)\,TARGET_HEIGHT/ih):ih*min(TARGET_WIDTH/(iw*sar)\,TARGET_HEIGHT/ ih), pad=TARGET_WIDTH:TARGET_HEIGHT:(TARGET_WIDTH-iw)/2:(TARGET_HEIGHT-ih)/2"` (2认同)

Sta*_*ant 5

尝试这个:

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

根据 FFmpeg 文档,该force_original_aspect_ratio选项可用于在缩放时保持原始纵横比:

   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.
Run Code Online (Sandbox Code Playgroud)