我需要实现一个能够在 Azure 媒体服务上上传 .mp4 视频的应用程序。视频应该以ProgressiveDownload流格式发布,并且应该在静态时加密。
研究媒体服务文档,我尝试实现一个控制台应用程序。
static void Main(string[] args)
{
try
{
var tokenCredentials = new AzureAdTokenCredentials(_AADTenantDomain, AzureEnvironments.AzureCloudEnvironment);
var tokenProvider = new AzureAdTokenProvider(tokenCredentials);
_context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
// Add calls to methods defined in this section.
// Make sure to update the file name and path to where you have your media file.
IAsset inputAsset =
UploadFile(_videoPath, AssetCreationOptions.StorageEncrypted);
IAsset encodedAsset =
EncodeToAdaptiveBitrateMP4s(inputAsset, AssetCreationOptions.StorageEncrypted);
PublishAssetGetURLs(encodedAsset);
}
catch (Exception exception)
{
// Parse the XML error message in the Media Services response and create a new
// exception with its content.
exception = MediaServicesExceptionParser.Parse(exception);
Console.Error.WriteLine(exception.Message);
}
finally
{
Console.ReadLine();
}
}
static public IAsset EncodeToAdaptiveBitrateMP4s(IAsset asset, AssetCreationOptions options)
{
// Prepare a job with a single task to transcode the specified asset
// into a multi-bitrate asset.
IJob job = _context.Jobs.CreateWithSingleTask(
"Media Encoder Standard",
"Adaptive Streaming",
asset,
"Adaptive Bitrate MP4",
options);
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.");
IAsset outputAsset = job.OutputMediaAssets[0];
return outputAsset;
}
static public void PublishAssetGetURLs(IAsset asset)
{
// Publish the output asset by creating an Origin locator for adaptive streaming,
// and a SAS locator for progressive download.
IAssetDeliveryPolicy policy =
_context.AssetDeliveryPolicies.Create("Clear Policy",
AssetDeliveryPolicyType.NoDynamicEncryption,
AssetDeliveryProtocol.ProgressiveDownload | AssetDeliveryProtocol.HLS | AssetDeliveryProtocol.SmoothStreaming | AssetDeliveryProtocol.Dash,
null);
asset.DeliveryPolicies.Add(policy);
_context.Locators.Create(
LocatorType.OnDemandOrigin,
asset,
AccessPermissions.Read,
TimeSpan.FromDays(30));
_context.Locators.Create(
LocatorType.Sas,
asset,
AccessPermissions.Read,
TimeSpan.FromDays(30));
IEnumerable<IAssetFile> mp4AssetFiles = asset
.AssetFiles
.ToList()
.Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase));
// Get the Smooth Streaming, HLS and MPEG-DASH URLs for adaptive streaming,
// and the Progressive Download URL.
Uri smoothStreamingUri = asset.GetSmoothStreamingUri();
Uri hlsUri = asset.GetHlsUri();
Uri mpegDashUri = asset.GetMpegDashUri();
// Get the URls for progressive download for each MP4 file that was generated as a result
// of encoding.
List<Uri> mp4ProgressiveDownloadUris = mp4AssetFiles.Select(af => af.GetSasUri()).ToList();
}
当我添加部分以管理静态加密时,此代码停止工作。更准确地说,当我:
- 替换
UploadFile(_videoPath, AssetCreationOptions.None);
为UploadFile(_videoPath, AssetCreationOptions.StorageEncrypted);
- 替换
EncodeToAdaptiveBitrateMP4s(inputAsset, AssetCreationOptions.None);
为EncodeToAdaptiveBitrateMP4s(inputAsset, AssetCreationOptions.StorageEncrypted);
PublishAssetGetURLs
在方法中添加了以下代码IAssetDeliveryPolicy 策略 = _context.AssetDeliveryPolicies.Create("清除策略", AssetDeliveryPolicyType.NoDynamicEncryption, AssetDeliveryProtocol.ProgressiveDownload | AssetDeliveryProtocol.HLS | AssetDeliveryProtocol.SmoothStreaming | AssetDeliveryProtocol.Dash, null);
asset.DeliveryPolicies.Add(policy);
问题是视频已正确上传,但是当我尝试在 Azure 门户内播放视频时,我得到一个通用的 0x0 错误。