使用 Media Encoder Standard 裁剪视频Crop videos with Media Encoder Standard

可以使用 Media Encoder Standard (MES) 裁剪输入视频。You can use Media Encoder Standard (MES) to crop your input video. 裁剪是指在视频帧内选择一个矩形窗口并且只编码该窗口内的像素的过程。Cropping is the process of selecting a rectangular window within the video frame, and encoding just the pixels within that window. 下图可帮助说明该过程。The following diagram helps illustrate the process.

裁剪视频

假设输入视频的分辨率为 1920x1080 像素(16:9 纵横比),不过因为左侧和右侧有黑色长条(圆柱框),导致包含活动视频的区域只有 4:3 窗口或 1440x1080 像素。Suppose you have as input a video that has a resolution of 1920x1080 pixels (16:9 aspect ratio), but has black bars (pillar boxes) at the left and right, so that only a 4:3 window or 1440x1080 pixels contains active video. 用户可以使用 MES 裁剪或去掉黑色长条,并编码 1440x1080 区域。You can use MES to crop or edit out the black bars, and encode the 1440x1080 region.

MES 中的裁剪是预处理阶段,因此,编码预设​​值中的裁剪参数适用于原始输入视频。Cropping in MES is a pre-processing stage, so the cropping parameters in the encoding preset apply to the original input video. 编码是后续阶段,因此,宽度/高度设置适用于 经过预先处理 的视频,而非原始视频。Encoding is a subsequent stage, and the width/height settings apply to the pre-processed video, and not to the original video. 在设计预设值时,需要执行以下操作:(a) 根据原始输入视频选择裁剪参数;(b) 根据裁剪后的视频选择编码设置。When designing your preset you need to do the following: (a) select the crop parameters based on the original input video, and (b) select your encode settings based on the cropped video. 如果编码设置与裁剪后的视频不匹配,输出会不如预期。If you do not match your encode settings to the cropped video, the output will not be as you expect.

以下主题说明如何使用 MES 创建编码作业,以及如何为编码任务指定自定义预设值。The following topic shows how to create an encoding job with MES and how to specify a custom preset for the encoding task.

创建自定义预设值Creating a custom preset

在下图所示的示例中:In the example shown in the diagram:

  1. 原始输入为 1920x1080Original input is 1920x1080
  2. 需将其裁剪为 1440x1080 的输出,使其能位于输入帧的中央It needs to be cropped to an output of 1440x1080, which is centered in the input frame
  3. 这表示 X 偏移量为 (1920 – 1440)/2 = 240,Y 偏移量为零This means an X offset of (1920 – 1440)/2 = 240, and a Y offset of zero
  4. 裁剪矩形的宽度和高度分别为 1440 和 1080The Width and Height of the Crop rectangle are 1440 and 1080, respectively
  5. 在编码阶段,我们的任务是生成三层,分别为 1440x1080、960x720 和 480x360 的分辨率In the encode stage, the ask is to produce three layers, are resolutions 1440x1080, 960x720 and 480x360, respectively

JSON 预设JSON preset

{
  "Version": 1.0,
  "Sources": [
    {
      "Streams": [],
      "Filters": {
        "Crop": {
            "X": 240,
            "Y": 0,
            "Width": 1440,
            "Height": 1080
        }
      },
      "Pad": true
    }
  ],
  "Codecs": [
    {
      "KeyFrameInterval": "00:00:02",
      "H264Layers": [
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 3400,
          "MaxBitrate": 3400,
          "BufferWindow": "00:00:05",
          "Width": 1440,
          "Height": 1080,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 2250,
          "MaxBitrate": 2250,
          "BufferWindow": "00:00:05",
          "Width": 960,
          "Height": 720,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 1250,
          "MaxBitrate": 1250,
          "BufferWindow": "00:00:05",
          "Width": 480,
          "Height": 360,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        }
      ],
      "Type": "H264Video"
    },
    {
      "Profile": "AACLC",
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 128,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Width}x{Height}_{VideoBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

裁剪的限制Restrictions on cropping

裁剪功能仅供手动操作。The cropping feature is meant to be manual. 需要将输入视频加载到合适的编辑工具中,以便选择感兴趣的帧、定位光标以确定裁剪矩形的偏移量,以此确定针对该特定视频调整的编码预设值等。此功能并非是用来实现诸如自动检测及删除输入视频黑边之类的操作的。You would need to load your input video into a suitable editing tool that lets you select frames of interest, position the cursor to determine offsets for the cropping rectangle, to determine the encoding preset that is tuned for that particular video, etc. This feature is not meant to enable things like: automatic detection and removal of black letterbox/pillarbox borders in your input video.

裁剪功能受到以下限制。Following constraints apply to the cropping feature. 如果不符合以下情况,编码任务可能会失败或生成非预期的输出。If these are not met, the encode Task can fail, or produce an unexpected output.

  1. 裁剪矩形的坐标和大小不得超出输入视频The co-ordinates and size of the Crop rectangle have to fit within the input video
  2. 如前文所述,编码设置的宽度和高度必须与裁剪后的视频对应As mentioned above, the Width & Height in the encode settings have to correspond to the cropped video
  3. 裁剪适用于以横向模式捕获的视频(即,不适用于以垂直方向或纵向模式手持的智能手机录制的视频)Cropping applies to videos captured in landscape mode (i.e. not applicable to videos recorded with a smartphone held vertically or in portrait mode)
  4. 最适合用于以方形像素捕获的渐进式视频Works best with progressive video captured with square pixels

后续步骤Next step

请参阅 Azure 媒体服务学习途径了解 AMS 提供的强大功能。See Azure Media Services learning paths to help you learn about great features offered by AMS.

媒体服务 v3(最新版本)Media Services v3 (latest)

查看最新版本的 Azure 媒体服务!Check out the latest version of Azure Media Services!

媒体服务 v2(旧版)Media Services v2 (legacy)