如何对自定义转换进行编码 - RESTHow to encode with a custom transform - REST

使用 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 tutorial. 也可以构建自定义预设以针对特定方案或设备要求。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

定义自定义预设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.

{
    "properties": {
        "description": "Basic Transform using a custom encoding preset",
        "outputs": [
            {
                "onError": "StopProcessingJob",
                "relativePriority": "Normal",
                "preset": {
                    "@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. 创建转换时,应首先使用 Get 检查是否已存在转换。When creating a Transform, you should first use Get to check if one already exists. 如果存在转换,请重新使用它。If the Transform exists, reuse it.

在下载的 Postman 集合中,选择“转换和作业” ->“创建或更新转换” 。In the Postman's collection that you downloaded, select Transforms and Jobs->Create or Update Transform.

PUT HTTP 请求方法类似于:The PUT HTTP request method is similar to:

PUT https://management.chinacloudapi.cn/subscriptions/:subscriptionId/resourceGroups/:resourceGroupName/providers/Microsoft.Media/mediaServices/:accountName/transforms/:transformName?api-version={{api-version}}

选择“正文” 选项卡,并将正文替换为之前定义的 json 代码。Select the Body tab and replace the body with the json code you defined earlier. 要使媒体服务将转换应用于指定的视频或音频,需要在该转换下提交作业。For Media Services to apply the Transform to the specified video or audio, you need to submit a Job under that Transform.

选择“发送”。 Select Send.

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

后续步骤Next steps

请参阅其他 REST 操作See other REST operations