导出 (0) 打印
全部展开

使用用于 .NET 的 Media Services SDK 传送资产

更新时间: 2013年4月

本主题介绍将媒体内容传送到客户端的三个最常见选项。

  • 以下列格式之一流式处理自适应内容:MPEG DASH、Apple HTTP 实时流 (HLS) 和平滑流。

    生成流 URL(如本主题中所示)后,您可以使用以下播放器对其进行测试:

    要测试平滑流 URL,请使用 smf.cloudapp.net/healthmonitor 播放器。

    要测试 MPEG DASH URL,请使用 dashif.org/reference/players/javascript/ 播放器。

    要测试 HLS URL,请使用 Safari 和 iOS 设备或 3ivx-hls-player

  • 渐进式下载 MP4 文件。

  • 将资产文件下载到本地计算机。

有关详细信息以及与传送媒体内容相关的概念和术语,请参阅传送内容

Warning警告
构建流内容的 URL 时,媒体服务 会使用 IAssetFile.Name 属性的值(如 http://{WAMSAccount}.origin.mediaservices.windows.net/{GUID}/{IAssetFile.Name}/streamingParameters)。出于这个原因,不允许使用百分号编码。Name 属性的值不能含有任何以下保留的百分号编码字符:!*'();:@&=+$,/?%#[]"。此外,文件扩展名中只能含有一个“.”。

以下主题说明了如何设置 Media Services 部署。

  1. 在用于 .NET 的 Media Services SDK 上进行开发的设置

    请注意,本主题中的代码使用 Azure Media Services .NET SDK Extensions。Media Services .NET SDK Extensions 是一组扩展方法和帮助器函数,可简化代码,并令使用 Media Services 进行开发变得更加容易。

  2. 使用 Media Services SDK 连接到 Media Services

可以传送(下载或流式处理)已经存储在 媒体服务 帐户中的资产(可通过 Assets 集合获取)。也可以下载或流式处理因运行编码或封装作业而生成的资产(可通过 OutputMediaAssets 集合获取)。本主题中的示例说明了如何访问和传送作业的输出媒体资产。

本部分中的代码执行以下操作:

  1. 创建新资产并从本地目录上载夹层 MP4 文件。

  2. 准备具有单个任务的作业以将之前的夹层资产转码成多码率资产。

  3. 提交该作业并等待其完成。

  4. 获取生成了编码作业的输出资产。

  5. 通过创建源定位符来发布输出资产。

  6. 生成用于自适应流式处理的平滑流、HLS 和 MPEG-DASH URL。

    要根据一组自适应比特率 MP4 文件流式处理您的内容,必须先至少获取一个点播流保留单元。有关详细信息,请参阅动态打包

  7. 创建用于渐进式下载并将文件下载到本地目录的 SAS 定位符。

    要渐进式下载的内容无法加密。

  8. 为资产中的每个 MP4 文件生成渐进式下载 URL。

  9. 将输出资产下载到本地文件夹。

note备注
此示例使用 Azure Media Services .NET SDK Extensions。SDK Extensions 隐藏了许多详细信息。如果要了解如何实施某些方法(例如,下列方法如何构建流 URL:GetSmoothStreamingUri(), GetHlsUri(), andGetMpegDashUri()),您可以浏览 github 上的代码:azure-sdk-for-media-services-extensions

确保更新该代码,以便指向输入 MP4 文件所在的文件夹。

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

private static readonly string _singleMP4File =
    Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure.MediaServices.Client;


namespace MediaServicesDeliveringContent 
{ 
    class Program 
    {
        private static readonly string _mediaFiles =
            Path.GetFullPath(@"../..\Media");

        private static readonly string _singleMP4File =
            Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

        private static MediaServicesCredentials _cachedCredentials = null;
        private static CloudMediaContext _context = null;

        // Media Services account information.
        private static readonly string _mediaServicesAccountName =
            ConfigurationManager.AppSettings["MediaServicesAccountName"];
        private static readonly string _mediaServicesAccountKey =
            ConfigurationManager.AppSettings["MediaServicesAccountKey"];
        
        static void Main(string[] args) 
        {
            _cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName,
                                                                                _mediaServicesAccountKey);
            _context = new CloudMediaContext(_cachedCredentials); 

            Console.WriteLine("Creating new asset from local file..."); 
 
            // Create a new asset and upload a mezzanine file from a local path. 
            IAsset inputAsset = _context.Assets.CreateFromFile(
                _singleMP4File, 
                AssetCreationOptions.None, 
                (af, p) => 
                { 
                    Console.WriteLine("Uploading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress); 
                }); 
 
            Console.WriteLine("Asset created."); 
 
            // Prepare a job with a single task to transcode the previous mezzanine asset 
            // into a multi-bitrate asset. 
            IJob job = _context.Jobs.CreateWithSingleTask( 
                MediaProcessorNames.WindowsAzureMediaEncoder, 
                MediaEncoderTaskPresetStrings.H264AdaptiveBitrateMP4Set720p, 
                inputAsset, 
                "Sample Adaptive Bitrate MP4", 
                AssetCreationOptions.None); 
 
            Console.WriteLine("Submitting transcoding job..."); 
 
            // Submit the job and wait until it is completed. 
            job.Submit(); 
            job = job.StartExecutionProgressTask( 
                j => 
                { 
                    Console.WriteLine("Job state: {0}", j.State); 
                    Console.WriteLine("Job progress: {0:0.##}%", j.GetOverallProgress()); 
                }, 
                CancellationToken.None).Result; 
 
            Console.WriteLine("Transcoding job finished."); 
             
            // The OutputMediaAssets[0] contains the first 
            // (and in this case the only) output asset 
            // produced by the encoding job.
            IAsset outputAsset = job.OutputMediaAssets[0]; 
 
            Console.WriteLine("Publishing output asset..."); 
 
            // Publish the output asset by creating an Origin locator.  
            // Define the Read only access policy and
            // specify that the asset can be accessed for 30 days.  
            _context.Locators.Create( 
                LocatorType.OnDemandOrigin, 
                outputAsset, 
                AccessPermissions.Read, 
                TimeSpan.FromDays(30));

            // Generate the Smooth Streaming, HLS and MPEG-DASH URLs for adaptive streaming.  
            Uri smoothStreamingUri = outputAsset.GetSmoothStreamingUri();
            Uri hlsUri = outputAsset.GetHlsUri();
            Uri mpegDashUri = outputAsset.GetMpegDashUri();

            // To stream your content based on the set of 
            // adaptive bitrate MP4 files, you must first get 
            // at least one On-demand Streaming reserved unit. 
            // For more information, see http://msdn.microsoft.com/en-us/library/jj889436.aspx.
            Console.WriteLine("Output is now available for adaptive streaming:");

            // Show the streaming URLs. 
            Console.WriteLine(smoothStreamingUri);
            Console.WriteLine(hlsUri);
            Console.WriteLine(mpegDashUri);
                
            //
            // Create a SAS locator that is used for progressive download
            // or to download files to a local directory.  
            // The content that you want to progressively download cannot be encrypted.
            _context.Locators.Create( 
                LocatorType.Sas, 
                outputAsset, 
                AccessPermissions.Read, 
                TimeSpan.FromDays(30)); 

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

            // Generate the Progressive Download URLs for each MP4. 
            List<Uri> mp4ProgressiveDownloadUris = 
                mp4AssetFiles.Select(af => af.GetSasUri()).ToList();

            Console.WriteLine("You can progressively download the following MP4 files:");
            mp4ProgressiveDownloadUris.ForEach(uri => Console.WriteLine(uri)); 
 
             string outputFolder = Path.Combine(_mediaFiles, @"job-output");

            if (!Directory.Exists(outputFolder)) 
            { 
                Directory.CreateDirectory(outputFolder); 
            } 
 
            Console.WriteLine("Downloading output asset files to local folder..."); 
 
            // Download the output asset to a local folder. 
            outputAsset.DownloadToFolder( 
                outputFolder, 
                (af, p) => 
                { 
                    Console.WriteLine("Downloading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress); 
                }); 
 
            Console.WriteLine("Downloaded files are located in '{0}'.", Path.GetFullPath(outputFolder)); 
        } 
    } 
} 

另请参阅

显示:
© 2014 Microsoft