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

本教程介绍如何使用 Azure 媒体服务和 Azure CLI 在各种浏览器与设备上轻松编码和流式传输视频。This tutorial shows how to easily encode and stream videos on a variety of browsers and devices by using Azure Media Services and the Azure CLI. 可以使用 HTTPS、SAS URL 或位于 Azure Blob 存储中的文件路径来指定输入内容。You can specify input content by using HTTPS or SAS URLs or paths to files in Azure Blob storage.

本文中的示例对可通过 HTTPS URL 访问的内容进行编码。The example in this article encodes content that you make accessible via an HTTPS URL. 媒体服务 v3 目前不支持基于 HTTPS URL 的块式传输编码。Media Services v3 doesn't currently support chunked transfer encoding over HTTPS URLs.

完成本教程后,即可流式传输视频。By the end of this tutorial, you'll be able to stream a video.

播放视频

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

创建媒体服务帐户Create a Media Services account

在加密、编码、管理和流式传输 Azure 中的媒体内容之前,需要创建一个媒体服务帐户。Before you can encrypt, encode, manage, and stream media content in Azure, you need to create a Media Services account. 该帐户必须与一个或多个存储帐户相关联。That account must be associated with one or more storage accounts.

媒体服务帐户和所有关联的存储帐户必须位于同一 Azure 订阅中。Your Media Services account and all associated storage accounts must be in the same Azure subscription. 我们建议在媒体服务帐户所在的同一位置使用存储帐户,以限制延迟和数据传出费用。We recommend that you use storage accounts that are in the same place as the Media Services account to limit latency and data egress costs.

创建资源组Create a resource group

az group create -n amsResourceGroup -l chinaeast2

创建 Azure 存储帐户Create an Azure storage account

本示例创建一个常规用途 v2 标准 LRS 帐户。In this example, we create a General-Purpose v2 Standard LRS account.

若要通过存储帐户进行试验,请使用 --sku Standard_LRSIf you want to experiment with storage accounts, use --sku Standard_LRS. 选取用于生产的 SKU 时,请考虑使用 --sku Standard_RAGRS,以便通过异地复制来确保业务连续性。When you're picking a SKU for production, consider using --sku Standard_RAGRS, which provides geographic replication for business continuity. 有关详细信息,请参阅存储帐户For more information, see storage accounts.

az storage account create -n amsstorageaccount --kind StorageV2 --sku Standard_LRS -l chinaeast2 -g amsResourceGroup

创建 Azure 媒体服务帐户Create an Azure Media Services account

az ams account create --n amsaccount -g amsResourceGroup --storage-account amsstorageaccount -l chinaeast2

响应如下所示:You get a response like this:

{
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount",
  "location": "China East 2",
  "mediaServiceId": "8b569c2e-d648-4fcb-9035-c7fcc3aa7ddf",
  "name": "amsaccount",
  "resourceGroup": "amsResourceGroupTest",
  "storageAccounts": [
    {
      "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Storage/storageAccounts/amsstorageaccount",
      "resourceGroup": "amsResourceGroupTest",
      "type": "Primary"
    }
  ],
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

启动流式处理终结点Start the streaming endpoint

以下 Azure CLI 命令将启动默认的流式处理终结点The following Azure CLI command starts the default Streaming Endpoint.

az ams streaming-endpoint start  -n default -a amsaccount -g amsResourceGroup

响应如下所示:You get a response like this:

{
  "accessControl": null,
  "availabilitySetName": null,
  "created": "2019-02-06T21:58:03.604954+00:00",
  "crossSiteAccessPolicies": null,
  "customHostNames": [],
  "description": "",
  "freeTrialEndTime": "2019-02-21T22:05:31.277936+00:00",
  "hostName": "amsaccount-cne21.streaming.media.chinacloudapi.cn",
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount/streamingendpoints/default",
  "lastModified": "2019-02-06T21:58:03.604954+00:00",
  "location": "China East 2",
  "maxCacheAge": null,
  "name": "default",
  "provisioningState": "Succeeded",
  "resourceGroup": "amsResourceGroup",
  "resourceState": "Running",
  "scaleUnits": 0,
  "tags": {},
  "type": "Microsoft.Media/mediaservices/streamingEndpoints"
}

如果流式处理终结点已运行,则会显示以下消息:If the streaming endpoint is already running, you get this message:

(InvalidOperation) The server cannot execute the operation in its current state.

创建适用于自适应比特率编码的转换Create a transform for adaptive bitrate encoding

创建一个转换,以便配置对视频进行编码的常见任务。Create a Transform to configure common tasks for encoding videos. 本示例执行自适应比特率编码。In this example, we do adaptive bitrate encoding. 然后,在创建的转换下提交某个作业。We then submit a job under the transform that we created. 该作业是向媒体服务发出的请求,目的是将转换应用到给定的视频或音频内容输入。The job is the request to Media Services to apply the transform to the given video or audio content input.

az ams transform create --name testEncodingTransform --preset AdaptiveStreaming --description 'a simple Transform for Adaptive Bitrate Encoding' -g amsResourceGroup -a amsaccount

响应如下所示:You get a response like this:

{
  "created": "2019-02-15T00:11:18.506019+00:00",
  "description": "a simple Transform for Adaptive Bitrate Encoding",
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount/transforms/testEncodingTransform",
  "lastModified": "2019-02-15T00:11:18.506019+00:00",
  "name": "testEncodingTransform",
  "outputs": [
    {
      "onError": "StopProcessingJob",
      "preset": {
        "odatatype": "#Microsoft.Media.BuiltInStandardEncoderPreset",
        "presetName": "AdaptiveStreaming"
      },
      "relativePriority": "Normal"
    }
  ],
  "resourceGroup": "amsResourceGroup",
  "type": "Microsoft.Media/mediaservices/transforms"
}

创建输出资产Create an output asset

创建一个输出资产用作编码作业的输出。Create an output Asset to use as the encoding job's output.

az ams asset create -n testOutputAssetName -a amsaccount -g amsResourceGroup

响应如下所示:You get a response like this:

{
  "alternateId": null,
  "assetId": "96427438-bbce-4a74-ba91-e38179b72f36",
  "container": null,
  "created": "2019-02-14T23:58:19.127000+00:00",
  "description": null,
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount/assets/testOutputAssetName",
  "lastModified": "2019-02-14T23:58:19.127000+00:00",
  "name": "testOutputAssetName",
  "resourceGroup": "amsResourceGroup",
  "storageAccountName": "amsstorageaccount",
  "storageEncryptionFormat": "None",
  "type": "Microsoft.Media/mediaservices/assets"
}

使用 HTTPS 输入启动作业Start a job by using HTTPS input

提交用于处理视频的作业时,必须告知媒体服务查找输入视频的位置。When you submit jobs to process videos, you have to tell Media Services where to find the input video. 一个选项是指定 HTTPS URL 作为作业输入(如本示例所示)。One option is to specify an HTTPS URL as the job input, as shown in this example.

运行 az ams job start 时,可以在作业的输出上设置一个标签。When you run az ams job start, you can set a label on the job's output. 然后可以使用该标签来标识该输出资产的用途。You can then use the label to identify what the output asset is for.

  • 如果为标签赋值,请将“--output-assets”设置为“assetname=label”。If you assign a value to the label, set '--output-assets' to "assetname=label".

  • 如果不为标签赋值,请将“--output-assets”设置为“assetname=”。If you don't assign a value to the label, set '--output-assets' to "assetname=".

    请注意,我们将“=”添加到了 output-assetsNotice that we add "=" to the output-assets.

az ams job start --name testJob001 --transform-name testEncodingTransform --base-uri 'https://nimbuscdn-nimbuspm.streaming.mediaservices.windows.net/2b533311-b215-4409-80af-529c3e853622/' --files 'Ignite-short.mp4' --output-assets testOutputAssetName= -a amsaccount -g amsResourceGroup

响应如下所示:You get a response like this:

{
  "correlationData": {},
  "created": "2019-02-15T05:08:26.266104+00:00",
  "description": null,
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount/transforms/testEncodingTransform/jobs/testJob001",
  "input": {
    "baseUri": "https://nimbuscdn-nimbuspm.streaming.mediaservices.windows.net/2b533311-b215-4409-80af-529c3e853622/",
    "files": [
      "Ignite-short.mp4"
    ],
    "label": null,
    "odatatype": "#Microsoft.Media.JobInputHttp"
  },
  "lastModified": "2019-02-15T05:08:26.266104+00:00",
  "name": "testJob001",
  "outputs": [
    {
      "assetName": "testOutputAssetName",
      "error": null,
      "label": "",
      "odatatype": "#Microsoft.Media.JobOutputAsset",
      "progress": 0,
      "state": "Queued"
    }
  ],
  "priority": "Normal",
  "resourceGroup": "amsResourceGroup",
  "state": "Queued",
  "type": "Microsoft.Media/mediaservices/transforms/jobs"
}

查看状态Check status

在 5 分钟内检查作业的状态。In five minutes, check the status of the job. 该状态应该是“Finished”。It should be "Finished." 如果作业未完成,请在几分钟后重新检查。It's not finished, check again in a few minutes. 如果作业已完成,请转到下一步骤并创建流式处理定位符When it's finished, go to the next step and create a Streaming Locator.

az ams job show -a amsaccount -g amsResourceGroup -t testEncodingTransform -n testJob001

创建流式处理定位符并获取路径Create a streaming locator and get a path

编码完成后,下一步是使输出资产中的视频可供客户端播放。After the encoding is complete, the next step is to make the video in the output asset available to clients for playback. 为此,请先创建一个流式处理定位符。To do this, first create a Streaming Locator. 然后生成客户端可以使用的流 URL。Then, build streaming URLs that clients can use.

创建流式处理定位符Create a streaming locator

az ams streaming-locator create -n testStreamingLocator --asset-name testOutputAssetName --streaming-policy-name Predefined_ClearStreamingOnly  -g amsResourceGroup -a amsaccount 

响应如下所示:You get a response like this:

{
  "alternativeMediaId": null,
  "assetName": "output-3b6d7b1dffe9419fa104b952f7f6ab76",
  "contentKeys": [],
  "created": "2019-02-15T04:35:46.270750+00:00",
  "defaultContentKeyPolicyName": null,
  "endTime": "9999-12-31T23:59:59.999999+00:00",
  "id": "/subscriptions/<id>/resourceGroups/amsResourceGroup/providers/Microsoft.Media/mediaservices/amsaccount/streamingLocators/testStreamingLocator",
  "name": "testStreamingLocator",
  "resourceGroup": "amsResourceGroup",
  "startTime": null,
  "streamingLocatorId": "e01b2be1-5ea4-42ca-ae5d-7fe704a5962f",
  "streamingPolicyName": "Predefined_ClearStreamingOnly",
  "type": "Microsoft.Media/mediaservices/streamingLocators"
}

获取流式处理定位符路径Get streaming locator paths

az ams streaming-locator get-paths -a amsaccount -g amsResourceGroup -n testStreamingLocator

响应如下所示:You get a response like this:

{
  "downloadPaths": [],
  "streamingPaths": [
    {
      "encryptionScheme": "NoEncryption",
      "paths": [
        "/e01b2be1-5ea4-42ca-ae5d-7fe704a5962f/ignite.ism/manifest(format=m3u8-aapl)"
      ],
      "streamingProtocol": "Hls"
    },
    {
      "encryptionScheme": "NoEncryption",
      "paths": [
        "/e01b2be1-5ea4-42ca-ae5d-7fe704a5962f/ignite.ism/manifest(format=mpd-time-csf)"
      ],
      "streamingProtocol": "Dash"
    },
    {
      "encryptionScheme": "NoEncryption",
      "paths": [
        "/e01b2be1-5ea4-42ca-ae5d-7fe704a5962f/ignite.ism/manifest"
      ],
      "streamingProtocol": "SmoothStreaming"
    }
  ]
}

复制 HTTP 实时传送视频流 (HLS) 路径。Copy the HTTP live streaming (HLS) path. 在本例中,该值为 /e01b2be1-5ea4-42ca-ae5d-7fe704a5962f/ignite.ism/manifest(format=m3u8-aapl)In this case, it's /e01b2be1-5ea4-42ca-ae5d-7fe704a5962f/ignite.ism/manifest(format=m3u8-aapl).

生成 URLBuild the URL

获取流式处理终结点主机名Get the streaming endpoint host name

az ams streaming-endpoint list -a amsaccount -g amsResourceGroup -n default

复制 hostName 值。Copy the hostName value. 在本例中,该值为 amsaccount-cne21.streaming.media.chinacloudapi.cnIn this case, it's amsaccount-cne21.streaming.media.chinacloudapi.cn.

汇编 URLAssemble the URL

“https:// ” + <hostName 值> + <Hls 路径值>"https:// " + <hostName value> + <Hls path value>

下面是一个示例:Here's an example:

https://amsaccount-cne21.streaming.media.chinacloudapi.cn/7f19e783-927b-4e0a-a1c0-8a140c49856c/ignite.ism/manifest(format=m3u8-aapl)

使用 Azure Media Player 测试播放Test playback by using Azure Media Player

备注

如果播放器托管在 HTTPS 站点上,请确保在 URL 的开头添加“https”。If a player is hosted on an HTTPS site, make sure to start the URL with "https".

  1. 打开 Web 浏览器并转到 https://aka.ms/azuremediaplayer/Open a web browser and go to https://aka.ms/azuremediaplayer/.
  2. 在“URL”框中,粘贴在上一部分生成的 URL。In the URL box, paste the URL that you built in the previous section. 可以粘贴 HLS、Dash 或 Smooth 格式的 URL。You can paste the URL in HLS, Dash, or Smooth format. Azure Media Player 将自动使用相应的流协议在设备上播放。Azure Media Player will automatically use an appropriate streaming protocol for playback on your device.
  3. 选择“更新播放器”。Select 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 that you created for this tutorial, delete the resource group.

运行此 Azure CLI 命令:Run this Azure CLI command:

az group delete --name amsResourceGroup

后续步骤Next steps

媒体服务概述Media Services overview