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

本教程展示了使用 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 article 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

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

git clone https://github.com/Azure-Samples/media-services-v3-node-tutorials.git

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

在下载的项目中打开 index.jsOpen index.js in you downloaded project. endpoint config 值替换为从访问 API 获得的凭据。Replace the endpoint config 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.

运行示例应用Run the sample app

  1. 该应用下载编码文件。The app downloads encoded files. 创建想要输出文件位于的文件夹,并更新 index.js 文件中 outputFolder 变量的值。Create a folder where you want for the output files to go and update the value of the outputFolder variable in the index.js file.

  2. 打开命令提示符,浏览到示例的目录,然后执行以下命令。Open command prompt, browse to the sample's directory, and execute the following commands.

    npm install 
    node index.js
    

运行完成后,应该会看到与此类似的输出:After it is done running, you should see similar output:

运行

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

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

Note

如果播放器在 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

另请参阅See also

作业错误代码Job error codes.

后续步骤Next steps