如何使用 Media Encoder Standard 对资产进行编码How to encode an asset by using Media Encoder Standard

概述Overview

若要通过 Internet 传送数字视频,必须压缩媒体文件。To deliver digital video over the Internet, you must compress the media. 数字视频文件较大,可能因过大而无法通过 Internet 传送或者无法在客户的设备上正常显示。Digital video files are large and may be too large to deliver over the Internet, or for your customers’ devices to display properly. 编码是压缩视频和音频以便客户能够查看媒体的过程。Encoding is the process of compressing video and audio so your customers can view your media.

编码作业是 Azure 媒体服务中最常见的处理操作之一。Encoding jobs are one of the most common processing operations in Azure Media Services. 可通过创建编码作业将媒体文件从一种编码转换为另一种编码。You create encoding jobs to convert media files from one encoding to another. 编码时,可以使用媒体服务的内置编码器(Media Encoder Standard)。When you encode, you can use the Media Services built-in encoder (Media Encoder Standard). 还可使用媒体服务合作伙伴提供的编码器。You can also use an encoder provided by a Media Services partner. 可通过 Azure 市场获取第三方编码器。Third-party encoders are available through the Azure Marketplace. 可以使用为编码器定义的预设字符串,或使用预设配置文件来指定编码任务的详细信息。You can specify the details of encoding tasks by using preset strings defined for your encoder, or by using preset configuration files. 若要查看可用预设的类型,请参阅 Media Encoder Standard 的任务预设To see the types of presets that are available, see Task Presets for Media Encoder Standard.

每个作业可以有一个或多个任务,具体因要完成的处理类型而异。Each job can have one or more tasks depending on the type of processing that you want to accomplish. 通过 REST API,可采用以下两种方式之一创建作业及相关任务:Through the REST API, you can create jobs and their related tasks in one of two ways:

  • 可通过作业实体上的任务导航属性以内联方式定义任务。Tasks can be defined inline through the Tasks navigation property on Job entities.
  • 使用 OData 批处理Use OData batch processing.

建议始终将源文件编码为自适应比特率 MP4 集,然后使用动态打包将该集转换为所需格式。We recommend that you always encode your source files into an adaptive bitrate MP4 set, and then convert the set to the desired format by using dynamic packaging.

如果输出资产已经过存储加密,则必须配置资产传送策略。If your output asset is storage encrypted, you must configure the asset delivery policy. 有关详细信息,请参阅配置资产传送策略For more information, see Configuring asset delivery policy.

注意事项Considerations

访问媒体服务中的实体时,必须在 HTTP 请求中设置特定标头字段和值。When accessing entities in Media Services, you must set specific header fields and values in your HTTP requests. 有关详细信息,请参阅媒体服务 REST API 开发的设置For more information, see Setup for Media Services REST API Development.

开始引用媒体处理器之前,请确认媒体处理器 ID 正确无误。Before you start referencing media processors, verify that you have the correct media processor ID. 有关详细信息,请参阅获取媒体处理器For more information, see Get media processors.

连接到媒体服务Connect to Media Services

若要了解如何连接到 AMS API,请参阅通过 Azure AD 身份验证访问 Azure 媒体服务 APIFor information on how to connect to the AMS API, see Access the Azure Media Services API with Azure AD authentication.

创建包含单个编码任务的作业Create a job with a single encoding task

备注

使用媒体服务 REST API 时,需注意以下事项:When you're working with the Media Services REST API, the following considerations apply:

访问媒体服务中的实体时,必须在 HTTP 请求中设置特定标头字段和值。When accessing entities in Media Services, you must set specific header fields and values in your HTTP requests. 有关详细信息,请参阅媒体服务 REST API 开发的设置For more information, see Setup for Media Services REST API development.

使用 JSON 并指定在请求中使用 __metadata 关键字(例如,为了引用某个链接对象)时,必须将 Accept 标头设置为 JSON 详细格式 :Accept: application/json;odata=verbose。When using JSON and specifying to use the __metadata keyword in the request (for example, to reference a linked object), you must set the Accept header to JSON Verbose format: Accept: application/json;odata=verbose.

以下示例说明了如何使用一个任务集来创建和发布作业,从而以特定分辨率和质量对视频进行编码。The following example shows you how to create and post a job with one task set to encode a video at a specific resolution and quality. 使用 Media Encoder Standard 编码时,可以使用此处指定的任务配置预设。When you encode with Media Encoder Standard, you can use task configuration presets specified here.

请求:Request:

POST https://media.chinacloudapi.cn/API/Jobs HTTP/1.1
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
x-ms-version: 2.19
    Authorization: Bearer <ENCODED JWT TOKEN> 
    x-ms-client-request-id: 00000000-0000-0000-0000-000000000000
Host: media.chinacloudapi.cn

{"Name" : "NewTestJob", "InputMediaAssets" : [{"__metadata" : {"uri" : "https://media.chinacloudapi.cn/api/Assets('nb%3Acid%3AUUID%3Aaab7f15b-3136-4ddf-9962-e9ecb28fb9d2')"}}],  "Tasks" : [{"Configuration" : "Adaptive Streaming", "MediaProcessorId" : "nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",  "TaskBody" : "<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody>"}]}

响应:Response:

HTTP/1.1 201 Created

. . .

设置输出资产的名称Set the output asset's name

以下示例说明了如何设置 assetName 属性:The following example shows how to set the assetName attribute:

{ "TaskBody" : "<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetName=\"CustomOutputAssetName\">JobOutputAsset(0)</outputAsset></taskBody>"}

注意事项Considerations

  • TaskBody 属性必须使用文本 XML 来定义任务使用的输入资产或输出资产的数量。TaskBody properties must use literal XML to define the number of input, or output assets that are used by the task. 任务文章包含 XML 的 XML 架构定义。The task article contains the XML Schema Definition for the XML.
  • 在 TaskBody 定义中,必须将 <inputAsset><outputAsset> 的每个内部值设置为 JobInputAsset(value) 或 JobOutputAsset(value)。In the TaskBody definition, each inner value for <inputAsset> and <outputAsset> must be set as JobInputAsset(value) or JobOutputAsset(value).
  • 一个任务可以有多个输出资产。A task can have multiple output assets. 一个 JobOutputAsset(x) 只能一次用作作业中任务的输出。One JobOutputAsset(x) can only be used once as an output of a task in a job.
  • 可以将 JobInputAsset 或 JobOutputAsset 指定为任务的输入资产。You can specify JobInputAsset or JobOutputAsset as an input asset of a task.
  • 任务不得构成循环。Tasks must not form a cycle.
  • 传递给 JobInputAsset 或 JobOutputAsset 的 value 参数代表资产的索引值。The value parameter that you pass to JobInputAsset or JobOutputAsset represents the index value for an asset. 在作业实体定义的 InputMediaAssets 和 OutputMediaAssets 导航属性中定义实际资产。The actual assets are defined in the InputMediaAssets and OutputMediaAssets navigation properties on the job entity definition.
  • 由于媒体服务基于 OData v3,因此 InputMediaAssets 和 OutputMediaAssets 导航属性集合中的单个资产将通过“__metadata : uri”名称/值对进行引用。Because Media Services is built on OData v3, the individual assets in the InputMediaAssets and OutputMediaAssets navigation property collections are referenced through a "__metadata: uri" name-value pair.
  • InputMediaAssets 映射到已在媒体服务中创建的一个或多个资产。InputMediaAssets maps to one or more assets that you created in Media Services. OutputMediaAssets 由系统创建。OutputMediaAssets are created by the system. 它们不引用现有资产。They don't reference an existing asset.
  • 可使用 assetName 属性来命名 OutputMediaAssets。OutputMediaAssets can be named by using the assetName attribute. 如果该属性不存在,则 OutputMediaAsset 的名称为 <outputAsset> 元素的任意内部文本值,并以作业名称值或作业 ID 值(在没有定义名称属性的情况下)为后缀。If this attribute is not present, then the name of the OutputMediaAsset is whatever the inner text value of the <outputAsset> element is with a suffix of either the Job Name value, or the Job Id value (in the case where the Name property isn't defined). 例如,如果将 assetName 的值设置为“Sample”,则会将 OutputMediaAsset 名称属性设置为“Sample”。For example, if you set a value for assetName to "Sample," then the OutputMediaAsset Name property is set to "Sample." 但是,如果未设置 assetName 的值,但已将作业名称设置为“NewJob”,则 OutputMediaAsset 名称为“JobOutputAsset(value)_NewJob”。However, if you didn't set a value for assetName, but did set the job name to "NewJob," then the OutputMediaAsset Name would be "JobOutputAsset(value)_NewJob."

创建包含连锁任务的作业Create a job with chained tasks

在许多应用程序方案中,开发人员希望创建一系列处理任务。In many application scenarios, developers want to create a series of processing tasks. 在媒体服务中,可以创建一系列连锁任务。In Media Services, you can create a series of chained tasks. 每个任务执行不同的处理步骤,并且可以使用不同的媒体处理器。Each task performs different processing steps and can use different media processors. 连锁任务可以将资产从一个任务转给另一个任务,从而对资产执行线性序列的任务。The chained tasks can hand off an asset from one task to another, performing a linear sequence of tasks on the asset. 但是,在作业中执行的任务不需要处于序列中。However, the tasks performed in a job are not required to be in a sequence. 创建连锁任务时,连锁 ITask 对象在单个 IJob 对象中创建 。When you create a chained task, the chained ITask objects are created in a single IJob object.

备注

每个作业当前有 30 个任务的限制。There is currently a limit of 30 tasks per job. 如果需要连锁 30 个以上的任务,请创建多个作业以包含任务。If you need to chain more than 30 tasks, create more than one job to contain the tasks.

POST https://media.chinacloudapi.cn/api/Jobs HTTP/1.1
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
x-ms-version: 2.19
Authorization: Bearer <ENCODED JWT TOKEN> 
x-ms-client-request-id: 00000000-0000-0000-0000-000000000000

{  
   "Name":"NewTestJob",
   "InputMediaAssets":[  
      {  
         "__metadata":{  
            "uri":"https://testrest.chinacloudapp.cn/api/Assets('nb%3Acid%3AUUID%3A910ffdc1-2e25-4b17-8a42-61ffd4b8914c')"
         }
      }
   ],
   "Tasks":[  
      {  
         "Configuration":"H264 Adaptive Bitrate MP4 Set 720p",
         "MediaProcessorId":"nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",
         "TaskBody":"<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody>"
      },
      {  
         "Configuration":"H264 Smooth Streaming 720p",
         "MediaProcessorId":"nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",
         "TaskBody":"<?xml version=\"1.0\" encoding=\"utf-16\"?><taskBody><inputAsset>JobOutputAsset(0)</inputAsset><outputAsset>JobOutputAsset(1)</outputAsset></taskBody>"
      }
   ]
}

注意事项Considerations

若要启用任务链,必须满足以下条件:To enable task chaining:

  • 一个作业必须包含至少两个任务。A job must have at least two tasks.
  • 必须至少有一个任务的输入是作业中另一个任务的输出。There must be at least one task whose input is the output of another task in the job.

使用 OData 批处理Use OData batch processing

以下示例演示如何使用 OData 批处理来创建作业和任务。The following example shows how to use OData batch processing to create a job and tasks. 有关批处理的信息,请参阅 Open Data Protocol (OData) 批处理For information on batch processing, see Open Data Protocol (OData) Batch Processing.

POST https://media.chinacloudapi.cn/api/$batch HTTP/1.1
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 3.0;NetFx
Content-Type: multipart/mixed; boundary=batch_a01a5ec4-ba0f-4536-84b5-66c5a5a6d34e
Accept: multipart/mixed
Accept-Charset: UTF-8
Authorization: Bearer <ENCODED JWT TOKEN> 
x-ms-version: 2.19
x-ms-client-request-id: 00000000-0000-0000-0000-000000000000
Host: media.chinacloudapi.cn


--batch_a01a5ec4-ba0f-4536-84b5-66c5a5a6d34e
Content-Type: multipart/mixed; boundary=changeset_122fb0a4-cd80-4958-820f-346309967e4d

--changeset_122fb0a4-cd80-4958-820f-346309967e4d
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://media.chinacloudapi.cn/api/Jobs HTTP/1.1
Content-ID: 1
Content-Type: application/json
Accept: application/json
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
Accept-Charset: UTF-8
Authorization: Bearer <ENCODED JWT TOKEN> 
x-ms-version: 2.19
x-ms-client-request-id: 00000000-0000-0000-0000-000000000000

{"Name" : "NewTestJob", "InputMediaAssets@odata.bind":["https://media.chinacloudapi.cn/api/Assets('nb%3Acid%3AUUID%3A2a22445d-1500-80c6-4b34-f1e5190d33c6')"]}

--changeset_122fb0a4-cd80-4958-820f-346309967e4d
Content-Type: application/http
Content-Transfer-Encoding: binary

POST https://media.chinacloudapi.cn/api/$1/Tasks HTTP/1.1
Content-ID: 2
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
Accept-Charset: UTF-8
Authorization: Bearer <ENCODED JWT TOKEN> 
x-ms-version: 2.19
x-ms-client-request-id: 00000000-0000-0000-0000-000000000000

{  
   "Configuration":"H264 Adaptive Bitrate MP4 Set 720p",
   "MediaProcessorId":"nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",
   "TaskBody":"<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetName=\"Custom output name\">JobOutputAsset(0)</outputAsset></taskBody>"
}

--changeset_122fb0a4-cd80-4958-820f-346309967e4d--
--batch_a01a5ec4-ba0f-4536-84b5-66c5a5a6d34e--

使用 JobTemplate 创建作业Create a job by using a JobTemplate

使用一组常用任务处理多个资产时,JobTemplate 可用于指定默认任务预设或设置任务顺序。When you process multiple assets by using a common set of tasks, use a JobTemplate to specify the default task presets, or to set the order of tasks.

以下示例演示如何使用以内联方式定义的 TaskTemplate 创建 JobTemplate。The following example shows how to create a JobTemplate with a TaskTemplate that is defined inline. TaskTemplate 将 Media Encoder Standard 用作 MediaProcessor 来编码资产文件。The TaskTemplate uses the Media Encoder Standard as the MediaProcessor to encode the asset file. 但是,也可使用其他 MediaProcessor。However, other MediaProcessors can be used as well.

POST https://media.chinacloudapi.cn/API/JobTemplates HTTP/1.1
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
x-ms-version: 2.19
Authorization: Bearer <ENCODED JWT TOKEN> 
Host: media.chinacloudapi.cn


{"Name" : "NewJobTemplate25", "JobTemplateBody" : "<?xml version=\"1.0\" encoding=\"utf-8\"?><jobTemplate><taskBody taskTemplateId=\"nb:ttid:UUID:071370A3-E63E-4E81-A099-AD66BCAC3789\"><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody></jobTemplate>", "TaskTemplates" : [{"Id" : "nb:ttid:UUID:071370A3-E63E-4E81-A099-AD66BCAC3789", "Configuration" : "H264 Smooth Streaming 720p", "MediaProcessorId" : "nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56", "Name" : "SampleTaskTemplate2", "NumberofInputAssets" : 1, "NumberofOutputAssets" : 1}] }

备注

与其他媒体服务实体不同的是,必须为每个 TaskTemplate 定义一个新的 GUID 标识符并将其放入请求正文中的 taskTemplateId 和 ID 属性中。Unlike other Media Services entities, you must define a new GUID identifier for each TaskTemplate and place it in the taskTemplateId and Id property in your request body. 内容标识方案必须遵循“标识 Azure 媒体服务实体”中所述的方案。The content identification scheme must follow the scheme described in Identify Azure Media Services Entities. 此外,不能更新 JobTemplate。Also, JobTemplates cannot be updated. 而必须使用更新的更改创建新的 JobTemplate。Instead, you must create a new one with your updated changes.

如果成功,返回以下响应:If successful, the following response is returned:

HTTP/1.1 201 Created

. . .

以下示例演示如何创建引用 JobTemplate Id 的作业:The following example shows how to create a job that references a JobTemplate Id:

POST https://media.chinacloudapi.cn/API/Jobs HTTP/1.1
Content-Type: application/json;odata=verbose
Accept: application/json;odata=verbose
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
x-ms-version: 2.19
Authorization: Bearer <ENCODED JWT TOKEN> 
Host: media.chinacloudapi.cn


{"Name" : "NewTestJob", "InputMediaAssets" : [{"__metadata" : {"uri" : "https://media.chinacloudapi.cn/api/Assets('nb%3Acid%3AUUID%3A3f1fe4a2-68f5-4190-9557-cd45beccef92')"}}], "TemplateId" : "nb:jtid:UUID:15e6e5e6-ac85-084e-9dc2-db3645fbf0aa"}

如果成功,返回以下响应:If successful, the following response is returned:

HTTP/1.1 201 Created

. . .

要浏览的高级编码功能Advanced Encoding Features to explore

媒体服务学习路径Media Services learning paths

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

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

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

后续步骤Next steps

了解如何创建对资产进行编码的作业后,请参阅如何使用媒体服务检查作业进度Now that you know how to create a job to encode an asset, see How to check job progress with Media Services.

另请参阅See also

获取媒体处理器Get Media Processors