簡體   English   中英

如何從 Azure 媒體服務獲取視頻的時長?

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

我正在使用 Windows Azure 媒體服務 .NET ZF20E3C5E54C0AB3D375D660B3F896 使用流媒體服務。 我想檢索視頻的持續時間。 如何使用 Windows Azure 媒體服務 .NET ZF20E3C5E54C0AB3D375AZ6660B6F 檢索視頻的持續時間?

Azure創建一些可以在持續時間內查詢的元數據文件(xml)。 使用媒體服務擴展名可以訪問這些文件

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

在獲取資產元數據下:

// 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;

在Azure媒體服務SDK中,我們僅通過contentFileSize( https://msdn.microsoft.com/zh-cn/library/azure/hh974275.aspx )提供資產的大小。 但是,我們不提供視頻的元數據(例如持續時間)。 當您找到流媒體定位器時,該播放將告訴您視頻資產將持續多長時間。

干杯,嚴明飛

如果您使用的是AMSv3,則AdaptiveStreaming作業會在輸出資產中生成video_manifest.json文件。 您可以對其進行解析以獲取持續時間。 這是一個例子:

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);
}

有關Amsv3Manifest模型和示例video_manifest.json文件,請參見: https : video_manifest.json = video_manifest.json

您可以使用以下GetSasForAssetFile()定義開始使用:

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();
}

@galdin 他們的回答讓我半途而廢。 由於發生了一些變化,我想添加一個使用 Azure 存儲 v12 的快速示例。 另外,我為容器抓取了 SAS 並從那里讀取清單; 這似乎更容易一些。 出於我的目的,我需要總分鍾數。

您可以通過復制清單 JSON 數據並使用 Visual Studio 中的特殊粘貼選項快速創建Amsv3Manifest model。

//todo: get duration from _manifest.json
HttpResponseMessage? responseMessage = null;
var roundedMinutes = 0;

// Use Media Services API to get back a response that contains
// SAS URL for the Asset container into which to upload blobs.
// That is where you would specify read-write permissions 
// and the expiration time for the SAS URL.
var durationAssetContainerSas = await _client.Assets.ListContainerSasAsync(
    config.ResourceGroup,
    config.AccountName,
    assetName,
    permissions: AssetContainerPermission.ReadWrite,
    expiryTime: DateTime.UtcNow.AddMinutes(10).ToUniversalTime());

var durationSasUri = new Uri(durationAssetContainerSas.AssetContainerSasUrls.First());

// Use Storage API to get a reference to the Asset container via
// Sas and then access the manifest file in the container.
// the manifest file starts with 32 characters from the video name
BlobContainerClient durBlobContainerClient = new BlobContainerClient(durationSasUri);
BlobClient durationBlobClient = durBlobContainerClient.GetBlobClient($"{name[..32]}_manifest.json");

try
{
    responseMessage = await new HttpClient().GetAsync(durationBlobClient.Uri);
}
catch (Exception e)
{
    logger.LogError(e.Message);
}

if (responseMessage != null)
{
    var manifest = 
        JsonConvert.DeserializeObject<Amsv3Manifest>(await responseMessage.Content.ReadAsStringAsync());
    var playDuration = XmlConvert.ToTimeSpan(manifest.AssetFile.First().Duration);
    roundedMinutes = (int)Math.Round(playDuration.TotalMinutes);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM