使用 .NET 发布媒体服务内容Publish Media Services content using .NET

概述Overview

可通过创建 OnDemand 流式处理定位符并生成流式处理 URL,来流式处理自适应比特率 MP4 集。You can stream an adaptive bitrate MP4 set by creating an OnDemand streaming locator and building a streaming URL. 对资产进行编码主题说明了如何编码成自适应比特率 MP4 集。The encoding an asset topic shows how to encode into an adaptive bitrate MP4 set.

备注

如果内容已加密,则在创建定位符之前配置资产传送策略(如本主题中所述)。If your content is encrypted, configure asset delivery policy (as described in this topic) before creating a locator.

也可以使用 OnDemand 流式处理定位符生成指向可渐进式下载的 MP4 文件的 URL。You can also use an OnDemand streaming locator to build URLs that point to MP4 files that can be progressively downloaded.

本主题说明如何创建按需流式处理定位符,以发布资产及生成平滑流式处理、MPEG DASH 和 HLS 流式处理 URL。This topic shows how to create an OnDemand streaming locator to publish your asset and build a Smooth, MPEG DASH, and HLS streaming URLs. 此外,还会示范如何生成渐进式下载 URL。It also shows hot to build progressive download URLs.

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

若要创建按需流式处理定位符并获取 URL,需要执行以下操作:To create the OnDemand streaming locator and get URLs, you need to do the following things:

  1. 如果内容已加密,请定义访问策略。If the content is encrypted, define an access policy.

  2. 创建 OnDemand 流式处理定位符。Create an OnDemand streaming locator.

  3. 如果计划进行流式处理,请获取资产中的流式处理清单文件 (.ism)。If you plan to stream, get the streaming manifest file (.ism) in the asset.

    若计划进行渐进式下载,请获取资产中的 MP4 文件名。If you plan to progressively download, get the names of MP4 files in the asset.

  4. 生成清单文件或 MP4 文件的 URL。Build URLs to the manifest file or MP4 files.

备注

不同 AMS 策略的策略限制为 1,000,000 个(例如,对于定位器策略或 ContentKeyAuthorizationPolicy)。There is a limit of 1,000,000 policies for different AMS policies (for example, for Locator policy or ContentKeyAuthorizationPolicy). 如果始终使用相同的天数/访问权限,则使用相同的策略 ID。Use the same policy ID if you are always using the same days / access permissions. 例如,适用于需要长期保留使用的定位符的策略(非上传策略)。For example, policies for locators that are intended to remain in place for a long time (non-upload policies). 有关详细信息,请参阅主题。For more information, see this topic.

使用媒体服务 .NET SDKUse Media Services .NET SDK

生成流式处理 URLBuild Streaming URLs

    private static void BuildStreamingURLs(IAsset asset)
    {

        // Create a 30-day readonly access policy. 
          // You cannot create a streaming locator using an AccessPolicy that includes write or delete permissions.
        IAccessPolicy policy = _context.AccessPolicies.Create("Streaming policy",
            TimeSpan.FromDays(30),
            AccessPermissions.Read);

        // Create a locator to the streaming content on an origin. 
        ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
            policy,
            DateTime.UtcNow.AddMinutes(-5));

        // Display some useful values based on the locator.
        Console.WriteLine("Streaming asset base path on origin: ");
        Console.WriteLine(originLocator.Path);
        Console.WriteLine();

        // Get a reference to the streaming manifest file from the  
        // collection of files in the asset. 
        var manifestFile = asset.AssetFiles.ToList().Where(f => f.Name.ToLower().
                                    EndsWith(".ism")).
                                    FirstOrDefault();

        // Create a full URL to the manifest file. Use this for playback
        // in streaming media clients. 
        string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest";
        Console.WriteLine("URL to manifest for client streaming using Smooth Streaming protocol: ");
        Console.WriteLine(urlForClientStreaming);
        Console.WriteLine("URL to manifest for client streaming using HLS protocol: ");
        Console.WriteLine(urlForClientStreaming + "(format=m3u8-aapl)");
        Console.WriteLine("URL to manifest for client streaming using MPEG DASH protocol: ");
        Console.WriteLine(urlForClientStreaming + "(format=mpd-time-csf)"); 
        Console.WriteLine();
    }

输出:The outputs:

  • 用于使用平滑流式处理协议进行客户端流式处理的清单的 URL:URL to manifest for client streaming using Smooth Streaming protocol:
    http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest
  • 用于使用 HLS 协议进行客户端流式处理的清单的 URL:URL to manifest for client streaming using HLS protocol:
    http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest(format=m3u8-aapl)
  • 用于使用 MPEG DASH 协议进行客户端流式处理的清单的 URL:URL to manifest for client streaming using MPEG DASH protocol:
    http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest(format=mpd-time-csf)

备注

也可通过 TLS 连接流式传输内容。You can also stream your content over a TLS connection. 若要执行此方法,请确保流式处理 URL 以 HTTPS 开头。To do this approach, make sure your streaming URLs start with HTTPS. 目前,AMS 对自定义域不支持 TLS。Currently, AMS doesn’t support TLS with custom domains.

生成渐进式下载 URLBuild progressive download URLs

    private static void BuildProgressiveDownloadURLs(IAsset asset)
    {
        // Create a 30-day readonly access policy. 
        IAccessPolicy policy = _context.AccessPolicies.Create("Streaming policy",
            TimeSpan.FromDays(30),
            AccessPermissions.Read);

        // Create an OnDemandOrigin locator to the asset. 
        ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
            policy,
            DateTime.UtcNow.AddMinutes(-5));

        // Display some useful values based on the locator.
        Console.WriteLine("Streaming asset base path on origin: ");
        Console.WriteLine(originLocator.Path);
        Console.WriteLine();

        // Get MP4 files.
        IEnumerable<IAssetFile> mp4AssetFiles = asset
            .AssetFiles
            .ToList()
            .Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase));

        // Create a full URL to the MP4 files. Use this to progressively download files.
        foreach (var pd in mp4AssetFiles)
            Console.WriteLine(originLocator.Path + pd.Name);
    }

输出:The outputs:

  • http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_650kbps_AAC_und_ch2_96kbps.mp4

  • http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_400kbps_AAC_und_ch2_96kbps.mp4

  • http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_3400kbps_AAC_und_ch2_96kbps.mp4

  • http://amstest1.streaming.mediaservices.chinacloudapi.cn/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_2250kbps_AAC_und_ch2_96kbps.mp4

    .. .. ..

使用 Azure 媒体服务 .NET SDK 扩展Use Media Services .NET SDK Extensions

以下代码调用 .NET SDK 扩展方法,以创建定位符,并为自适应流式处理生成平滑流式处理、HLS 和 MPEG-DASH URL。The following code calls .NET SDK extensions methods that create a locator and generate the Smooth Streaming, HLS, and MPEG-DASH URLs for adaptive streaming.

    // Create a loctor.
    _context.Locators.Create(
        LocatorType.OnDemandOrigin,
        inputAsset,
        AccessPermissions.Read,
        TimeSpan.FromDays(30));

    // Get the streaming URLs.
    Uri smoothStreamingUri = inputAsset.GetSmoothStreamingUri();
    Uri hlsUri = inputAsset.GetHlsUri();
    Uri mpegDashUri = inputAsset.GetMpegDashUri();

    Console.WriteLine(smoothStreamingUri);
    Console.WriteLine(hlsUri);
    Console.WriteLine(mpegDashUri);

媒体服务学习路径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