使用 .NET 通过 Media Encoder Standard 对资产进行编码Encode an asset with Media Encoder Standard using .NET

编码作业是媒体服务中最常见的处理操作之一。Encoding jobs are one of the most common processing operations in Media Services. 可通过创建编码作业将媒体文件从一种编码转换为另一种编码。You create encoding jobs to convert media files from one encoding to another. 进行编码时,可以使用媒体服务内置的 Media Encoder。When you encode, you can use the Media Services built-in Media Encoder. 另外,也可以使用媒体服务合作伙伴提供的编码器;第三方编码器可通过 Azure 市场购得。You can also use an encoder provided by a Media Services partner; third-party encoders are available through the Azure Marketplace.

本文介绍如何使用 .NET 通过 Media Encoder Standard (MES) 对资产进行编码。This article shows how to use .NET to encode your assets with Media Encoder Standard (MES). Media Encoder Standard 使用此处所述的其中一个编码器预设进行配置。Media Encoder Standard is configured using one of the encoders presets described here.

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

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

备注

MES 将生成一个输出文件,其名称包含输入文件名的前 32 个字符。MES produces an output file with a name that contains the first 32 characters of the input file name. 该名称基于预设文件中指定的内容。The name is based on what is specified in the preset file. 例如,"FileName": "{Basename}{Index}{Extension}"。For example, "FileName": "{Basename}{Index}{Extension}". {Basename} 替换为输入文件名的前 32 个字符。{Basename} is replaced by the first 32 characters of the input file name.

MES 格式MES Formats

格式和编解码器Formats and codecs

MES 预设MES Presets

Media Encoder Standard 使用此处所述的其中一个编码器预设进行配置。Media Encoder Standard is configured using one of the encoders presets described here.

输入和输出元数据Input and output metadata

如果使用 MES 为输入资产(或资产)编码,在该编码任务成功完成时,便能获取输出资产。When you encode an input asset (or assets) using MES, you get an output asset at the successful completion of that encode task. 输出资产包含视频、音频、缩略图、清单等等,具体视你使用的编码预设而定。The output asset contains video, audio, thumbnails, manifest, etc. based on the encoding preset you use.

输出资产还包含提供输入资产相关元数据的文件。The output asset also contains a file with metadata about the input asset. 元数据 XML 文件的名称采用下列格式:<asset_id>_metadata.xml(例如,41114ad3-eb5e-4c57-8d92-5354e2b7d4a4_metadata.xml),其中 <asset_id> 是输入资产的 AssetId 值。The name of the metadata XML file has the following format: <asset_id>_metadata.xml (for example, 41114ad3-eb5e-4c57-8d92-5354e2b7d4a4_metadata.xml), where <asset_id> is the AssetId value of the input asset. 此处描述了此输入元数据 XML 的架构。The schema of this input metadata XML is described here.

输出资产还包含提供输出资产相关元数据的文件。The output asset also contains a file with metadata about the output asset. 元数据 XML 文件的名称采用下列格式:<source_file_name>_manifest.xml(例如,BigBuckBunny_manifest.xml)。The name of the metadata XML file has the following format: <source_file_name>_manifest.xml (for example, BigBuckBunny_manifest.xml). 此处描述了此输出元数据 XML 的架构。The schema of this output metadata XML is described here.

如果想要检查这两个元数据文件中的任意一个,可以创建 SAS 定位器并将文件下载到本地计算机。If you want to examine either of the two metadata files, you can create a SAS locator and download the file to your local computer. 可以就如何创建 SAS 定位器并下载使用媒体服务 .NET SDK 扩展的文件找到相关示例。You can find an example on how to create a SAS locator and download a file Using the Media Services .NET SDK Extensions.

下载示例Download sample

可从此处获取并运行说明如何使用 MES 进行编码的示例。You can get and run a sample that shows how to encode with MES from here.

.NET 示例代码.NET sample code

以下代码示例使用媒体服务 .NET SDK 执行下列任务:The following code example uses Media Services .NET SDK to perform the following tasks:

  • 创建编码作业。Create an encoding job.
  • 获取对 Media Encoder Standard 编码器的引用。Get a reference to the Media Encoder Standard encoder.
  • 指定使用自适应流式处理预设。Specify to use the Adaptive Streaming preset.
  • 将一个编码任务添加到该作业。Add a single encoding task to the job.
  • 指定要编码的输入资产。Specify the input asset to be encoded.
  • 创建包含所编码资产的输出资产。Create an output asset that contains the encoded asset.
  • 添加事件处理程序以检查作业进度。Add an event handler to check the job progress.
  • 提交作业。Submit the job.

创建和配置 Visual Studio 项目Create and configure a Visual Studio project

设置开发环境,并根据使用 .NET 进行媒体服务开发中所述,在 app.config 文件中填充连接信息。Set up your development environment and populate the app.config file with connection information, as described in Media Services development with .NET.

示例Example

using System;
using System.Linq;
using System.Configuration;
using System.IO;
using System.Threading;
using Microsoft.WindowsAzure.MediaServices.Client;

namespace MediaEncoderStandardSample
{
    class Program
    {
        private static readonly string _AADTenantDomain =
            ConfigurationManager.AppSettings["AMSAADTenantDomain"];
        private static readonly string _RESTAPIEndpoint =
            ConfigurationManager.AppSettings["AMSRESTAPIEndpoint"];
        private static readonly string _AMSClientId =
            ConfigurationManager.AppSettings["AMSClientId"];
        private static readonly string _AMSClientSecret =
            ConfigurationManager.AppSettings["AMSClientSecret"];

        // Field for service context.
        private static CloudMediaContext _context = null;

        private static readonly string _supportFiles =
            Path.GetFullPath(@"../..\Media");

        static void Main(string[] args)
        {
            AzureAdTokenCredentials tokenCredentials =
                new AzureAdTokenCredentials(_AADTenantDomain,
                    new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                    AzureEnvironments.AzureChinaCloudEnvironment);

            var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

            _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);


            // Get an uploaded asset.
            var asset = _context.Assets.FirstOrDefault();

            // Encode and generate the output using the "Adaptive Streaming" preset.
            EncodeToAdaptiveBitrateMP4Set(asset);

            Console.ReadLine();
        }

        static public IAsset EncodeToAdaptiveBitrateMP4Set(IAsset asset)
        {
            // Declare a new job.
            IJob job = _context.Jobs.Create("Media Encoder Standard Job");
            // Get a media processor reference, and pass to it the name of the 
            // processor to use for the specific task.
            IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");

            // Create a task with the encoding details, using a string preset.
            // In this case "Adaptive Streaming" preset is used.
            ITask task = job.Tasks.AddNew("My encoding task",
                processor,
                "Adaptive Streaming",
                TaskOptions.None);

            // Specify the input asset to be encoded.
            task.InputAssets.Add(asset);
            // Add an output asset to contain the results of the job. 
            // This output is specified as AssetCreationOptions.None, which 
            // means the output asset is not encrypted. 
            task.OutputAssets.AddNew("Output asset",
                AssetCreationOptions.None);

            job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
            job.Submit();
            job.GetExecutionProgressTask(CancellationToken.None).Wait();

            return job.OutputMediaAssets[0];
        }

        private static void JobStateChanged(object sender, JobStateChangedEventArgs e)
        {
            Console.WriteLine("Job state changed event:");
            Console.WriteLine("  Previous state: " + e.PreviousState);
            Console.WriteLine("  Current state: " + e.CurrentState);
            switch (e.CurrentState)
            {
                case JobState.Finished:
                    Console.WriteLine();
                    Console.WriteLine("Job is finished. Please wait while local tasks or downloads complete...");
                    break;
                case JobState.Canceling:
                case JobState.Queued:
                case JobState.Scheduled:
                case JobState.Processing:
                    Console.WriteLine("Please wait...\n");
                    break;
                case JobState.Canceled:
                case JobState.Error:

                    // Cast sender as a job.
                    IJob job = (IJob)sender;

                    // Display or log error details as needed.
                    break;
                default:
                    break;
            }
        }

        private static IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
        {
            var processor = _context.MediaProcessors.Where(p => p.Name == mediaProcessorName).
            ToList().OrderBy(p => new Version(p.Version)).LastOrDefault();

            if (processor == null)
                throw new ArgumentException(string.Format("Unknown media processor", mediaProcessorName));

            return processor;
        }
    }
}

要浏览的高级编码功能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

如何使用 Media Encoder Standard 通过 .NET 来生成缩略图 媒体服务编码概述How to generate thumbnail using Media Encoder Standard with .NET Media Services Encoding Overview