教程:基于 URL 对远程文件进行编码并流式传输视频 - .NETTutorial: Encode a remote file based on URL and stream the video - .NET

本教程展示了使用 Azure 媒体服务在各种浏览器和设备上对视频进行编码和流式处理有多轻松。This tutorial shows you how easy it is to encode and start streaming videos on a wide variety of browsers and devices using Azure Media Services. 可以使用 HTTPS、URL、SAS URL 或位于 Azure Blob 存储中的文件路径来指定输入内容。An input content can be specified using HTTPS URLs, SAS URLs, or paths to files located in Azure Blob storage. 本主题中的示例对可通过 HTTPS URL 访问的内容进行编码。The sample in this topic encodes content that you make accessible via an HTTPS URL. 请注意,目前,AMS v3 不支持基于 HTTPS URL 的块传输编码。Note that currently, AMS v3 does not support chunked transfer encoding over HTTPS URLs.

完成本教程后即可对视频进行流式处理。By the end of the tutorial you will be able to stream a video.

播放视频

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

先决条件Prerequisites

下载并配置示例Download and configure the sample

使用以下命令将包含流式处理 .NET 示例的 GitHub 存储库克隆到计算机:Clone a GitHub repository that contains the streaming .NET sample to your machine using the following command:

git clone https://github.com/Azure-Samples/media-services-v3-dotnet-quickstarts.git

该示例位于 EncodeAndStreamFiles 文件夹。The sample is located in the EncodeAndStreamFiles folder.

打开下载的项目中的 appsettings.jsonOpen appsettings.json in your downloaded project. 将值替换为在访问 API 中获取的凭据。Replace the values with credentials that you got from accessing APIs.

该示例执行以下操作:The sample performs the following actions:

  1. 创建一个转换(首先,检查指定的转换是否存在)。Creates a Transform (first, checks if the specified Transform exists).
  2. 创建一个输出资产,用作编码作业的输出。Creates an output Asset that is used as the encoding Job's output.
  3. 创建基于 HTTPS URL 的作业输入。Creates the Job's input that is based on an HTTPS URL.
  4. 使用先前创建的输入和输出提交编码作业Submits the encoding Job using the input and output that was created earlier.
  5. 检查作业的状态。Checks the Job's status.
  6. 创建流定位符Creates a Streaming Locator.
  7. 生成流式处理 URL。Builds streaming URLs.

有关示例代码中的每个功能的内容介绍,请在此源文件中查看相关代码和注释。For explanations about what each function in the sample does, examine the code and look at the comments in this source file.

运行示例应用Run the sample app

运行应用时,显示使用不同协议播放视频的 URL。When you run the app, URLs that can be used to playback the video using different protocols are displayed.

  1. 按 Ctrl+F5 运行 EncodeAndStreamFiles 应用程序 。Press Ctrl+F5 to run the EncodeAndStreamFiles application.
  2. 选择 Apple 的“HLS”协议(以 manifest(format=m3u8-aapl) 结束),并从控制台复制流式处理 URL 。Choose the Apple's HLS protocol (ends with manifest(format=m3u8-aapl)) and copy the streaming URL from the console.

输出

在此示例的源代码中,可查看 URL 的生成方式。In the sample's source code, you can see how the URL is built. 若要生成 URL,需要连接流式处理终结点的主机名和流式处理定位符路径。To build it, you need to concatenate the streaming endpoint's host name and the streaming locator path.

使用 Azure Media Player 进行测试Test with Azure Media Player

本文使用 Azure Media Player 测试流式传输。To test the stream, this article uses Azure Media Player.

备注

如果播放器在 Https 站点上进行托管,请确保将 URL 更新为“https”。If a player is hosted on an https site, make sure to update the URL to "https".

  1. 打开 Web 浏览器并导航到 https://aka.ms/azuremediaplayer/Open a web browser and navigate to https://aka.ms/azuremediaplayer/.

  2. 在“URL:”框中,粘贴运行应用程序时获取的某个流式处理 URL 值 。In the URL: box, paste one of the streaming URL values you got when you ran the application.

    可以粘贴 HLS、Dash 或 Smooth 格式的 URL,Azure Media Player将切换到适当的流协议,以便在你的设备上自动播放。You can paste the URL in HLS, Dash, or Smooth format and Azure Media Player will switch to an appropriate streaming protocol for playback on your device automatically.

  3. 按“更新播放器” 。Press Update Player.

Azure Media Player 可用于测试,但不可在生产环境中使用。Azure Media Player can be used for testing but should not be used in a production environment.

清理资源Clean up resources

如果不再需要你的资源组中的任何一个资源(包括为本教程创建的媒体服务和存储帐户),请删除该资源组。If you no longer need any of the resources in your resource group, including the Media Services and storage accounts you created for this tutorial, delete the resource group.

执行以下 CLI 命令:Execute the following CLI command:

az group delete --name amsResourceGroup

检查代码Examine the code

有关示例代码中的每个功能的内容介绍,请在此源文件中查看相关代码和注释。For explanations about what each function in the sample does, examine the code and look at the comments in this source file.

如需了解更高级的流式处理示例及其详细说明,请参阅上传、编码和流式处理文件教程。The upload, encode, and stream files tutorial gives you a more advanced streaming example with detailed explanations.

作业错误代码Job error codes

请参阅错误代码See Error codes.

多线程处理Multithreading

Azure 媒体服务 v3 SDK 不是线程安全的。The Azure Media Services v3 SDKs are not thread-safe. 使用多线程应用程序时,应在每个线程上生成一个新的 AzureMediaServicesClient 对象。When working with multi-threaded application, you should generate a new AzureMediaServicesClient object per thread.

后续步骤Next steps