【问题标题】:ffmpeg resize down larger video to fit desired size and add paddingffmpeg 调整较大视频的大小以适合所需大小并添加填充
【发布时间】:2011-12-29 07:32:22
【问题描述】:

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

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

因此,该命令的填充参数将是-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

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

【问题讨论】:

    标签: ffmpeg padding


    【解决方案1】:

    试试这个:

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

    根据 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.
    

    【讨论】:

      【解决方案2】:

      这是一个用于缩放(保持纵横比)和将任何源尺寸填充到任何目标尺寸的通用过滤器表达式:

      -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"
      

      TARGET_WIDTHTARGET_HEIGHT 替换为您想要的值。我使用它从任何视频中提取 200x120 的填充缩略图。支持davin for his nice overview of the math

      【讨论】:

      • 对我来说最好的答案,易于编写脚本。
      • 这将适用于变形视频:-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"
      【解决方案3】:

      试试-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
      

      【讨论】:

      • 非常好,我喜欢!
      • 像魅力一样工作!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2015-07-03
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      相关资源
      最近更新 更多