如何对自定义转换进行编码 - Azure CLIHow to encode with a custom transform - Azure CLI

使用 Azure 媒体服务进行编码时,可以根据流式传输文件快速入门中演示的行业最佳做法,使用推荐的内置预设之一快速入门。When encoding with Azure Media Services, you can get started quickly with one of the recommended built-in presets, based on industry best practices, as demonstrated in the Streaming files quickstart. 也可以构建自定义预设以针对特定方案或设备要求。You can also build a custom preset to target your specific scenario or device requirements.

注意事项Considerations

创建自定义预设时,请注意以下事项:When creating custom presets, the following considerations apply:

  • AVC 内容上的所有高度和宽度值必须是 4 的倍数。All values for height and width on AVC content must be a multiple of 4.
  • 在 Azure 媒体服务 v3 中,所有编码比特率均以每秒比特数为单位。In Azure Media Services v3, all of the encoding bitrates are in bits per second. 这与我们的 v2 API 的预设不同,后者使用 千比特/秒作为单位。This is different from the presets with our v2 APIs, which used kilobits/second as the unit. 例如,如果 v2 中的比特率指定为 128(千比特/秒),则在 v3 中它将设置为 128000(比特/秒)。For example, if the bitrate in v2 was specified as 128 (kilobits/second), in v3 it would be set to 128000 (bits/second).

先决条件Prerequisites

创建媒体服务帐户Create a Media Services account.

请务必记住资源组名称和媒体服务帐户名称。Make sure to remember the resource group name and the Media Services account name.

CLICLI

可以在本地安装 CLI。You can install the CLI locally. 有关适用于你的平台的说明,请参阅安装 Azure CLISee Install the Azure CLI for instructions for your platform.

登录Sign in

使用本地安装的 CLI 需要登录到 Azure。Using a local install of the CLI requires signing in to Azure. 使用 az login 命令登录。Sign in with the az login command.

如果 CLI 可以打开默认的浏览器,则它会打开该浏览器并加载登录页。If the CLI can open your default browser, it will do so and load a sign-in page. 否则,你需要打开一个浏览器页面,在浏览器中导航到 https://microsoft.com/deviceloginchina 后,按照有关命令行的说明输入授权代码。Otherwise, you need to open a browser page and follow the instructions on the command line to enter an authorization code after navigating to https://microsoft.com/deviceloginchina in your browser.

指定文件位置Specify location of files

许多媒体服务 CLI 命令允许你通过文件名来传递参数。Many Media Services CLI commands allow you to pass a parameter with a file name.

需要根据所用的 OS 或 Shell(Bash 或 PowerShell)指定文件路径。You need to specify the file path according to the OS or Shell (Bash or PowerShell) that you are using. 下面是一些示例:Below are some examples:

文件(所有 OS)的相对路径Relative path to the file (all OS)

  • @"mytestfile.json"
  • @"../mytestfile.json"

Linux/Mac 和 Windows OS 上的绝对文件路径Absolute file path on Linux/Mac and Windows OS

  • @ "/usr/home/mytestfile.json"
  • @"c:\tmp\user\mytestfile.json"

如果命令要求提供文件路径,请使用 {file}Use {file} if the command is asking for a path to the file. 例如,az ams transform create -a amsaccount -g resourceGroup -n custom --preset .\customPreset.jsonFor example, az ams transform create -a amsaccount -g resourceGroup -n custom --preset .\customPreset.json.
如果命令将加载指定的文件,请使用 @{file}Use @{file} if the command is going to load the specified file. 例如,az ams account-filter create -a amsaccount -g resourceGroup -n filterName --tracks @tracks.jsonFor example, az ams account-filter create -a amsaccount -g resourceGroup -n filterName --tracks @tracks.json.

定义自定义预设Define a custom preset

以下示例定义新转换的请求正文。The following example defines the request body of a new Transform. 我们定义了一组希望在使用此转换时生成的输出。We define a set of outputs that we want to be generated when this Transform is used.

在此示例中,我们首先为音频编码添加一个 AacAudio 层,为视频编码添加两个 H264Video 层。In this example, we first add an AacAudio layer for the audio encoding and two H264Video layers for the video encoding. 在视频层中,我们分配标签,以便可以在输出文件名中使用它们。In the video layers, we assign labels so that they can be used in the output file names. 接下来,我们希望输出还包括缩略图。Next, we want the output to also include thumbnails. 在以下示例中,我们指定 PNG 格式的图像,这些图像以输入视频分辨率的 50% 生成,并以输入视频长度的 {25%, 50%, 75} 三个时间戳生成。In the example below we specify images in PNG format, generated at 50% of the resolution of the input video, and at three timestamps - {25%, 50%, 75} of the length of the input video. 最后,我们指定输出文件的格式 - 一个用于视频 + 音频,另一个用于缩略图。Lastly, we specify the format for the output files - one for video + audio, and another for the thumbnails. 由于我们有多个 H264 层,因此我们必须使用宏来为每个层生成唯一的名称。Since we have multiple H264Layers, we have to use macros that produce unique names per layer. 可以使用 {Label}{Bitrate} 宏,此示例显示了前者。We can either use a {Label} or {Bitrate} macro, the example shows the former.

我们要将此转换保存到某个文件中。We are going to save this transform in a file. 在此示例中,文件命名为 customPreset.jsonIn this example, we name the file customPreset.json.

{
    "@odata.type": "#Microsoft.Media.StandardEncoderPreset",
    "codecs": [
        {
            "@odata.type": "#Microsoft.Media.AacAudio",
            "channels": 2,
            "samplingRate": 48000,
            "bitrate": 128000,
            "profile": "AacLc"
        },
        {
            "@odata.type": "#Microsoft.Media.H264Video",
            "keyFrameInterval": "PT2S",
            "stretchMode": "AutoSize",
            "sceneChangeDetection": false,
            "complexity": "Balanced",
            "layers": [
                {
                    "width": "1280",
                    "height": "720",
                    "label": "HD",
                    "bitrate": 3400000,
                    "maxBitrate": 3400000,
                    "bFrames": 3,
                    "slices": 0,
                    "adaptiveBFrame": true,
                    "profile": "Auto",
                    "level": "auto",
                    "bufferWindow": "PT5S",
                    "referenceFrames": 3,
                    "entropyMode": "Cabac"
                },
                {
                    "width": "640",
                    "height": "360",
                    "label": "SD",
                    "bitrate": 1000000,
                    "maxBitrate": 1000000,
                    "bFrames": 3,
                    "slices": 0,
                    "adaptiveBFrame": true,
                    "profile": "Auto",
                    "level": "auto",
                    "bufferWindow": "PT5S",
                    "referenceFrames": 3,
                    "entropyMode": "Cabac"
                }
            ]
        },
        {
            "@odata.type": "#Microsoft.Media.PngImage",
            "stretchMode": "AutoSize",
            "start": "25%",
            "step": "25%",
            "range": "80%",
            "layers": [
                {
                    "width": "50%",
                    "height": "50%"
                }
            ]
        }
    ],
    "formats": [
        {
            "@odata.type": "#Microsoft.Media.Mp4Format",
            "filenamePattern": "Video-{Basename}-{Label}-{Bitrate}{Extension}",
            "outputFiles": []
        },
        {
            "@odata.type": "#Microsoft.Media.PngFormat",
            "filenamePattern": "Thumbnail-{Basename}-{Index}{Extension}"
        }
    ]
}

创建新转换Create a new transform

在此示例中,我们基于前面定义的自定义预设创建转换In this example, we create a Transform that is based on the custom preset we defined earlier. 创建转换时,应首先检查是否已存在转换。When creating a Transform, you should first check if one already exist. 如果存在转换,请重新使用它。If the Transform exists, reuse it. 以下 show 命令返回 customTransformName 转换(如果存在):The following show command returns the customTransformName transform if it exists:

az ams transform show -a amsaccount -g amsResourceGroup -n customTransformName

以下 Azure CLI 命令根据自定义预设(先前已定义)创建转换。The following Azure CLI command creates the Transform based on the custom preset (defined earlier).

az ams transform create -a amsaccount -g amsResourceGroup -n customTransformName --description "Basic Transform using a custom encoding preset" --preset customPreset.json

要使媒体服务将转换应用于指定的视频或音频,需要在该转换下提交作业。For Media Services to apply the Transform to the specified video or audio, you need to submit a Job under that Transform. 有关演示如何在转换下提交作业的完整示例,请参阅快速入门:对视频文件进行流式处理 - Azure CLIFor a complete example that shows how to submit a job under a transform, see Quickstart: Stream video files - Azure CLI.

另请参阅See also

Azure CLIAzure CLI