使用媒体服务 .NET SDK 创建筛选器Creating Filters with Media Services .NET SDK

从 2.17 版开始,可使用媒体服务为资产定义筛选器。Starting with 2.17 release, Media Services enables you to define filters for your assets. 这些筛选器是服务器端规则,可让客户选择运行如下操作:只播放一段视频(而非播放完整视频),或只指定客户设备可以处理的一部分音频和视频再现内容(而非与该资产相关的所有再现内容)。These filters are server-side rules that allow your customers to choose to do things like: playback only a section of a video (instead of playing the whole video), or specify only a subset of audio and video renditions that your customer's device can handle (instead of all the renditions that are associated with the asset). 通过按客户请求创建的动态清单可以实现对资产进行这种筛选,并基于指定的筛选器流式传输视频。This filtering of your assets is achieved through Dynamic Manifests that are created upon your customer's request to stream a video based on specified filter(s).

有关与筛选器和动态清单相关的更多详细信息,请参阅动态清单概述For more detailed information related to filters and Dynamic Manifest, see Dynamic manifests overview.

本文介绍如何使用媒体服务 .NET SDK 创建、更新和删除筛选器。This article shows how to use Media Services .NET SDK to create, update, and delete filters.

请注意,如果更新筛选器,则流式处理终结点需要两分钟的时间来刷新规则。Note if you update a filter, it can take up to two minutes for streaming endpoint to refresh the rules. 如果内容是通过使用此筛选器提供的(并在代理中缓存),则更新此筛选器会导致播放器失败。If the content was served using this filter (and cached in proxies), updating this filter can result in player failures. 请始终在更新筛选器之后清除缓存。Always clear the cache after updating the filter. 如果此选项不可用,请考虑使用其他筛选器。If this option is not possible, consider using a different filter.

用于创建筛选器的类型Types used to create filters

创建筛选器时会使用以下类型:The following types are used when creating filters:

创建/更新/读取/删除全局筛选器Create/Update/Read/Delete global filters

以下代码演示如何使用 .NET 创建、更新、读取和删除资产筛选器。The following code shows how to use .NET to create, update, read, and delete asset filters.

    string filterName = "GlobalFilter_" + Guid.NewGuid().ToString();

    List<FilterTrackSelectStatement> filterTrackSelectStatements = new List<FilterTrackSelectStatement>();

    FilterTrackSelectStatement filterTrackSelectStatement = new FilterTrackSelectStatement();
    filterTrackSelectStatement.PropertyConditions = new List<IFilterTrackPropertyCondition>();
    filterTrackSelectStatement.PropertyConditions.Add(new FilterTrackNameCondition("Track Name", FilterTrackCompareOperator.NotEqual));
    filterTrackSelectStatement.PropertyConditions.Add(new FilterTrackBitrateRangeCondition(new FilterTrackBitrateRange(0, 1), FilterTrackCompareOperator.NotEqual));
    filterTrackSelectStatement.PropertyConditions.Add(new FilterTrackTypeCondition(FilterTrackType.Audio, FilterTrackCompareOperator.NotEqual));
    filterTrackSelectStatements.Add(filterTrackSelectStatement);

    // Create
    IStreamingFilter filter = _context.Filters.Create(filterName, new PresentationTimeRange(), filterTrackSelectStatements);

    // Update
    filter.PresentationTimeRange = new PresentationTimeRange(timescale: 500);
    filter.Update();

    // Read
    var filterUpdated = _context.Filters.FirstOrDefault();
    Console.WriteLine(filterUpdated.Name);

    // Delete
    filter.Delete();

创建/更新/读取/删除资产筛选器Create/Update/Read/Delete asset filters

以下代码演示如何使用 .NET 创建、更新、读取和删除资产筛选器。The following code shows how to use .NET to create, update, read, and delete asset filters.

    string assetName = "AssetFilter_" + Guid.NewGuid().ToString();
    var asset = _context.Assets.Create(assetName, AssetCreationOptions.None);

    string filterName = "AssetFilter_" + Guid.NewGuid().ToString();


    // Create
    IStreamingAssetFilter filter = asset.AssetFilters.Create(filterName,
                                        new PresentationTimeRange(), 
                                        new List<FilterTrackSelectStatement>());

    // Update
    filter.PresentationTimeRange = 
            new PresentationTimeRange(start: 6000000000, end: 72000000000);

    filter.Update();

    // Read
    asset = _context.Assets.Where(c => c.Id == asset.Id).FirstOrDefault();
    var filterUpdated = asset.AssetFilters.FirstOrDefault();
    Console.WriteLine(filterUpdated.Name);

    // Delete
    filterUpdated.Delete();

生成使用筛选器的流 URLBuild streaming URLs that use filters

有关如何发布和传送资产的信息,请参阅将内容传送到客户概述For information on how to publish and deliver your assets, see Delivering Content to Customers Overview.

以下示例演示了如何将筛选器添加到流 URL。The following examples show how to add filters to your streaming URLs.

MPEG DASHMPEG DASH

http://testendpoint-testaccount.streaming.mediaservices.chinacloudapi.cn/fecebb23-46f6-490d-8b70-203e86b0df58/BigBuckBunny.ism/Manifest(format=mpd-time-csf, filter=MyFilter)

Apple HTTP Live Streaming (HLS) V4Apple HTTP Live Streaming (HLS) V4

http://testendpoint-testaccount.streaming.mediaservices.chinacloudapi.cn/fecebb23-46f6-490d-8b70-203e86b0df58/BigBuckBunny.ism/Manifest(format=m3u8-aapl, filter=MyFilter)

Apple HTTP Live Streaming (HLS) V3Apple HTTP Live Streaming (HLS) V3

http://testendpoint-testaccount.streaming.mediaservices.chinacloudapi.cn/fecebb23-46f6-490d-8b70-203e86b0df58/BigBuckBunny.ism/Manifest(format=m3u8-aapl-v3, filter=MyFilter)

平滑流式处理Smooth Streaming

http://testendpoint-testaccount.streaming.mediaservices.chinacloudapi.cn/fecebb23-46f6-490d-8b70-203e86b0df58/BigBuckBunny.ism/Manifest(filter=MyFilter)

媒体服务学习路径Media Services learning paths

媒体服务 v3(最新版本)Media Services v3 (latest)

查看最新版本的 Azure 媒体服务!Check out the latest version of Azure Media Services!

媒体服务 v2(旧版)Media Services v2 (legacy)

另请参阅See Also

动态清单概述Dynamic manifests overview