How to get the duration of a video from the Azure media services?

我只是一个虾纸丫 提交于 2019-12-23 03:08:02

问题


I'm using the Windows Azure Media Services .NET SDK 3 to make use of the streaming services. I want to retrieve the duration of the video. How can I retrieve the duration of a video using the Windows Azure Media Services .NET SDK 3?


回答1:


Azure creates some metadata files (xml) which could be queried for the duration. These files be accessed using the media-service extension

https://github.com/Azure/azure-sdk-for-media-services-extensions

Under Get Assets Meta Data:

// The asset encoded with the Windows Media Services Encoder. Get a reference to it from the context.
IAsset asset = null;

// Get a SAS locator for the asset (make sure to create one first).
ILocator sasLocator = asset.Locators.Where(l => l.Type == LocatorType.Sas).First();

// Get one of the asset files.
IAssetFile assetFile = asset.AssetFiles.ToList().Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)).First();

// Get the metadata for the asset file.
AssetFileMetadata manifestAssetFile = assetFile.GetMetadata(sasLocator);

TimeSpan videoDuration = manifestAssetFile.Duration;



回答2:


In Azure Media Services SDK, we only provides the size of asset through contentFileSize (https://msdn.microsoft.com/en-us/library/azure/hh974275.aspx). However, we don't provide metadata of video (such as duration). When you get a streaming locator, the play would tell how long the video asset would be.

Cheers, Mingfei Yan




回答3:


If you're using AMSv3, the AdaptiveStreaming job generates a video_manifest.json file in the output asset. You can parse that to get the duration. Here's an example:

public async Task<TimeSpan> GetVideoDurationAsync(string encodedAssetName)
{
    var encodedAsset = await ams.Assets.GetAsync(config.ResourceGroup, config.AccountName, encodedAssetName);
    if(encodedAsset is null) throw new ArgumentException("An asset with that name doesn't exist.", nameof(encodedAssetName));
    var sas = GetSasForAssetFile("video_manifest.json", encodedAsset, DateTime.Now.AddMinutes(2));
    var responseMessage = await http.GetAsync(sas);
    var manifest = JsonConvert.DeserializeObject<Amsv3Manifest>(await responseMessage.Content.ReadAsStringAsync());
    var duration = manifest.AssetFile.First().Duration;
    return XmlConvert.ToTimeSpan(duration);
}

For the Amsv3Manifest model and a sample video_manifest.json file, see: https://app.quicktype.io/?share=pAhTMFSa3HVzInAET5k4

You may use the following definition of GetSasForAssetFile() to get started:

private string GetSasForAssetFile(string filename, Asset asset, DateTime expiry)
{
    var client = GetCloudBlobClient();
    var container = client.GetContainerReference(asset.Container);
    var blob = container.GetBlobReference(filename);

    var offset = TimeSpan.FromMinutes(10);
    var policy = new SharedAccessBlobPolicy
    {
        SharedAccessStartTime = DateTime.UtcNow.Subtract(offset),
        SharedAccessExpiryTime = expiry.Add(offset),
        Permissions = SharedAccessBlobPermissions.Read
    };
    var sas = blob.GetSharedAccessSignature(policy);
    return $"{blob.Uri.AbsoluteUri}{sas}";
}

private CloudBlobClient GetCloudBlobClient()
{
    if(CloudStorageAccount.TryParse(storageConfig.ConnectionString, out var storageAccount) is false)
    {
        throw new ArgumentException(message: "The storage configuration has an invalid connection string.", paramName: nameof(config));
    }

    return storageAccount.CreateCloudBlobClient();
}


来源:https://stackoverflow.com/questions/29296205/how-to-get-the-duration-of-a-video-from-the-azure-media-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!