使用媒体服务编码视频和音频Encoding video and audio with Media Services

媒体服务徽标 v3media services logo v3


媒体服务中的术语“编码”适用于将包含数字视频和/或音频的文件从一种标准格式转换为另一种标准格式的过程,其目的是 (a) 减小文件大小,和/或 (b) 生成与各种设备和应用兼容的格式。The term encoding in Media Services applies to the process of converting files containing digital video and/or audio from one standard format to another, with the purpose of (a) reducing the size of the files, and/or (b) producing a format that's compatible with a broad range of devices and apps. 此过程也称为视频压缩或转码。This process is also referred to as video compression, or transcoding. 有关概念的进一步讨论,请参阅数据压缩什么是编码和转码?See the Data compression and the What Is Encoding and Transcoding? for further discussion of the concepts.

视频通常通过渐进式下载方式或自适应比特率流式处理传送到设备和应用。Videos are typically delivered to devices and apps by progressive download or through adaptive bitrate streaming.

重要

媒体服务不会对已取消或已出错的作业计费。Media Services does not bill for canceled or errored jobs. 例如,进度已达到 50% 而被取消的作业不会按作业时间的 50% 计费。For example, a job that has reached 50% progress and is canceled is not billed at 50% of the job minutes. 你仅为已完成作业付费。You are only charged for finished jobs.

  • 若要通过渐进式下载方式传送内容,可以使用 Azure 媒体服务将数字媒体文件(夹层)转换为 MP4 文件,其中包含已通过 H.264 编解码器编码的视频,以及已通过 AAC 编解码器编码的音频。To deliver by progressive download, you can use Azure Media Services to convert a digital media file (mezzanine) into an MP4 file, which contains video that's been encoded with the H.264 codec, and audio that's been encoded with the AAC codec. 此 MP4 文件将写入到存储帐户中的资产。This MP4 file is written to an Asset in your storage account. 可以使用 Azure 存储 API 或 SDK(例如存储 REST API.NET SDK)直接下载文件。You can use the Azure Storage APIs or SDKs (for example, Storage REST API or .NET SDK) to download the file directly. 如果在存储中创建了具有特定容器名称的输出资产,请使用该位置。If you created the output Asset with a specific container name in storage, use that location. 否则,可以使用媒体服务列出资产容器 URLOtherwise, you can use Media Services to list the asset container URLs.
  • 若要准备通过自适应比特率流式处理传送的内容,需以多个比特率(从高到低)编码夹层文件。To prepare content for delivery by adaptive bitrate streaming, the mezzanine file needs to be encoded at multiple bitrates (high to low). 为了确保质量的平稳过渡,视频的分辨率会随着比特率的降低而降低。To ensure graceful transition of quality, the resolution of the video is lowered as the bitrate is lowered. 这会造成所谓的编码梯度 – 分辨率和比特率的表(请参阅自动生成的自适应比特率梯度)。This results in a so-called encoding ladder–a table of resolutions and bitrates (see auto-generated adaptive bitrate ladder). 可以使用媒体服务以多个比特率编码夹层文件。You can use Media Services to encode your mezzanine files at multiple bitrates. 在此过程中,你将获得一组 MP4 文件和关联的流式处理配置文件,这些文件将写入到存储帐户中的资产。In doing so, you'll get a set of MP4 files and associated streaming configuration files written to an Asset in your storage account. 然后,可以使用媒体服务中的动态打包功能,通过 MPEG-DASHHLS 等流式处理协议来传送视频。You can then use the Dynamic Packaging capability in Media Services to deliver the video via streaming protocols like MPEG-DASH and HLS. 这需要创建一个流定位符并生成与受支持协议对应的流 URL,然后,可以根据功能将这些内容移交到设备/应用。This requires you to create a Streaming Locator and build streaming URLs corresponding to the supported protocols, which can then be handed off to devices/apps based on their capabilities.

下图显示了使用动态打包进行按需编码的工作流。The following diagram shows the workflow for on-demand encoding with dynamic packaging.

使用动态打包进行按需编码的工作流

本主题介绍如何使用媒体服务 v3 对内容进行编码。This topic gives you guidance on how to encode your content with Media Services v3.

转换和作业Transforms and jobs

若要使用媒体服务 v3 进行编码,需创建转换作业To encode with Media Services v3, you need to create a Transform and a Job. 转换用于定义编码设置和输出的配方,作业则是该配方的一个实例。The transform defines a recipe for your encoding settings and outputs; the job is an instance of the recipe. 有关详细信息,请参阅转换和作业For more information, see Transforms and Jobs.

使用媒体服务进行编码时,可以使用预设来指示编码器应如何处理输入媒体文件。When encoding with Media Services, you use presets to tell the encoder how the input media files should be processed. 在媒体服务 v3 中,使用标准编码器对文件进行编码。In Media Services v3, you use Standard Encoder to encode your files. 例如,可以在编码内容中指定所需的视频分辨率和/或音频信道数量。For example, you can specify the video resolution and/or the number of audio channels you want in the encoded content.

可以使用行业最佳做法推荐的内置预设之一快速入门,也可以选择针对特定方案或设备要求生成自定义预设。You can get started quickly with one of the recommended built-in presets based on industry best practices or you can choose to build a custom preset to target your specific scenario or device requirements. 有关详细信息,请参阅使用自定义转换编码For more information, see Encode with a custom Transform.

从 2019 年 1 月开始,使用标准编码器编码以生成 MP4 文件时,将生成一个新的 .mpi 文件并将其添加到输出资产中。Starting with January 2019, when encoding with the Standard Encoder to produce MP4 file(s), a new .mpi file is generated and added to the output Asset. 此 MPI 文件旨在提高动态打包和流式处理方案的性能。This MPI file is intended to improve performance for dynamic packaging and streaming scenarios.

备注

不应修改或删除该 MPI 文件,也不应在存在(或不存在)此类文件的情况下采用服务中的任何依赖项。You shouldn't modify or remove the MPI file, or take any dependency in your service on the existence (or not) of such a file.

从 HTTPS URL 创建作业输入Creating job input from an HTTPS URL

提交用于处理视频的作业时,必须告知媒体服务查找输入视频的位置。When you submit Jobs to process your videos, you have to tell Media Services where to find the input video. 其中一个选项是指定 HTTPS URL 作为作业输入。One of the options is to specify an HTTPS URL as a job input. 媒体服务 v3 目前不支持基于 HTTPS URL 的块式传输编码。Currently, Media Services v3 doesn't support chunked transfer encoding over HTTPS URLs.

示例Examples

从本地文件创建作业输入Creating job input from a local file

可将输入视频存储为媒体服务资产,这种情况下会基于文件(存储在本地或 Azure Blob 存储中)创建输入资产。The input video can be stored as a Media Service Asset, in which case you create an input asset based on a file (stored locally or in Azure Blob storage).

示例Examples

使用内置预设对本地文件进行编码Encode a local file using built-in presets

使用子剪辑创建作业输入Creating job input with subclipping

对视频进行编码时,可以指定同时修剪或剪裁源文件,并生成只包含所需输入视频部分的输出。When encoding a video, you can specify to also trim or clip the source file and produce an output that has only a desired portion of the input video. 此功能适用于使用 BuiltInStandardEncoderPreset 预设或 StandardEncoderPreset 预设生成的任何转换This functionality works with any Transform that's built using either the BuiltInStandardEncoderPreset presets, or the StandardEncoderPreset presets.

可以指定使用点播视频或实时存档(录制的事件)的单个剪辑创建作业You can specify to create a Job with a single clip of a video on-demand or live archive (a recorded event). 作业输入可以是资产或 HTTPS URL。The job input could be an Asset or an HTTPS URL.

提示

若要流式传输视频的子剪辑而不重新编码视频,请考虑使用动态打包器预筛选清单If you want to stream a sublip of your video without re-encoding the video, consider using Pre-filtering manifests with Dynamic Packager.

示例Examples

参阅示例:See examples:

内置预设Built-in presets

媒体服务支持以下内置编码预设:Media Services supports the following built-in encoding presets:

BuiltInStandardEncoderPresetBuiltInStandardEncoderPreset

BuiltInStandardEncoderPreset 用于设置内置预设,以便使用标准编码器对输入视频进行编码。BuiltInStandardEncoderPreset is used to set a built-in preset for encoding the input video with the Standard Encoder.

目前支持以下预设:The following presets are currently supported:

  • EncoderNamedPreset.AACGoodQualityAudio:生成一个 MP4 文件,其中仅包含以 192 kbps 编码的立体声音频。EncoderNamedPreset.AACGoodQualityAudio: produces a single MP4 file containing only stereo audio encoded at 192 kbps.

  • EncoderNamedPreset.AdaptiveStreaming(推荐):有关详细信息,请参阅自动生成比特率梯形图EncoderNamedPreset.AdaptiveStreaming (recommended): For more information, see auto-generating a bitrate ladder.

  • EncoderNamedPreset.ContentAwareEncoding:公开内容感知编码的预设。EncoderNamedPreset.ContentAwareEncoding: exposes a preset for content-aware encoding. 在提供任何输入内容的情况下,服务将尝试自动确定最佳层数,以及自适应流式处理适合使用的比特率和分辨率设置。Given any input content, the service attempts to automatically determine the optimal number of layers, and appropriate bitrate and resolution settings for delivery by adaptive streaming. 底层算法将不断演进。The underlying algorithms will continue to evolve over time. 输出将包含带有交错式视频和音频的 MP4 文件。The output will contain MP4 files with video and audio interleaved. 有关详细信息,请参阅内容感知编码For more information, see content-aware encoding.

    备注

    请确保使用 ContentAwareEncoding 而不使用 ContentAwareEncodingExperimental。Make sure to use ContentAwareEncoding not ContentAwareEncodingExperimental.

  • EncoderNamedPreset.H264MultipleBitrate1080p:生成一组 8 GOP 对齐的 MP4 文件(范围从 6000 kbps 到 400 kbps)和立体声 AAC 音频。EncoderNamedPreset.H264MultipleBitrate1080p: produces a set of eight GOP-aligned MP4 files, ranging from 6000 kbps to 400 kbps, and stereo AAC audio. 起始分辨率为 1080p,之后下降到 360p。Resolution starts at 1080p and goes down to 360p.

  • EncoderNamedPreset.H264MultipleBitrate720p:生成一组 6 GOP 对齐的 MP4 文件(范围从 3400 kbps 到 400 kbps)和立体声 AAC 音频。EncoderNamedPreset.H264MultipleBitrate720p: produces a set of six GOP-aligned MP4 files, ranging from 3400 kbps to 400 kbps, and stereo AAC audio. 起始分辨率为 720p,之后下降到 360p。Resolution starts at 720p and goes down to 360p.

  • EncoderNamedPreset.H264MultipleBitrateSD:生成一组 5 GOP 对齐的 MP4 文件(范围从 1600 kbps 到 400 kbps)和立体声 AAC 音频。EncoderNamedPreset.H264MultipleBitrateSD: produces a set of five GOP-aligned MP4 files, ranging from 1600 kbps to 400 kbps, and stereo AAC audio. 起始分辨率为 480p,之后下降到 360p。Resolution starts at 480p and goes down to 360p.

  • EncoderNamedPreset.H264SingleBitrate1080p:生成一个 MP4 文件,其中的视频已使用 H.264 编解码器以 6750 kbps 编码,图片高度为 1080 像素,立体声音频已使用 AAC-LC 编解码器以 64 kbps 编码。EncoderNamedPreset.H264SingleBitrate1080p: produces an MP4 file where the video is encoded with H.264 codec at 6750 kbps and a picture height of 1080 pixels, and the stereo audio is encoded with AAC-LC codec at 64 kbps.

  • EncoderNamedPreset.H264SingleBitrate720p:生成一个 MP4 文件,其中的视频已使用 H.264 编解码器以 4500 kbps 编码,图片高度为 720 像素,立体声音频已使用 AAC-LC 编解码器以 64 kbps 编码。EncoderNamedPreset.H264SingleBitrate720p: produces an MP4 file where the video is encoded with H.264 codec at 4500 kbps and a picture height of 720 pixels, and the stereo audio is encoded with AAC-LC codec at 64 kbps.

  • EncoderNamedPreset.H264SingleBitrateSD:生成一个 MP4 文件,其中的视频已使用 H.264 编解码器以 2200 kbps 编码,图片高度为 480 像素,立体声音频已使用 AAC-LC 编解码器以 64 kbps 编码。EncoderNamedPreset.H264SingleBitrateSD: produces an MP4 file where the video is encoded with H.264 codec at 2200 kbps and a picture height of 480 pixels, and the stereo audio is encoded with AAC-LC codec at 64 kbps.

若要查看最新预设列表,请参阅用于编码视频的内置预设To see the most up-to-date presets list, see built-in presets to be used for encoding videos.

若要了解预设的用法,请参阅上传、编码和流式处理文件To see how the presets are used, see Uploading, encoding, and streaming files.

StandardEncoderPresetStandardEncoderPreset

StandardEncoderPreset 介绍使用标准编码器对输入视频进行编码时要使用的设置。StandardEncoderPreset describes settings to be used when encoding the input video with the Standard Encoder. 自定义转换预设时使用此预设。Use this preset when customizing Transform presets.

注意事项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 four.
  • 在 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).

自定义预设Customizing presets

媒体服务完全支持自定义预设中的所有值,可满足特定的编码需求和要求。Media Services fully supports customizing all values in presets to meet your specific encoding needs and requirements. 有关演示如何自定义编码器预设的示例,请参阅以下列表:For examples that show how to customize encoder presets, see the list below:

示例Examples

预设架构Preset schema

在媒体服务 v3 中,预设是 API 本身中的强类型化实体。In Media Services v3, presets are strongly typed entities in the API itself. 可以在开放 API 规范(或 Swagger)中找到这些对象的“架构”定义。You can find the "schema" definition for these objects in Open API Specification (or Swagger). 也可以在 REST API.NET SDK(或其他媒体服务 v3 SDK 参考文档)中查看预设定义(例如 StandardEncoderPreset)。You can also view the preset definitions (like StandardEncoderPreset) in the REST API, .NET SDK (or other Media Services v3 SDK reference documentation).

在 v3 中缩放编码Scaling encoding in v3

若要缩放媒体处理,请参阅使用 CLI 进行缩放To scale media processing, see Scale with CLI.

计费Billing

媒体服务不会对已取消或已出错的作业计费。Media Services does not bill for canceled or errored jobs. 例如,进度已达到 50% 而被取消的作业不会按作业时间的 50% 计费。For example, a job that has reached 50% progress and is canceled is not billed at 50% of the job minutes. 你仅为已完成作业付费。You are only charged for finished jobs.

有关详细信息,请参阅定价For more information, see pricing.

后续步骤Next steps