如何从Azure媒体服务获取视频的持续时间?

0yg35tkg  于 2023-02-25  发布在  其他
关注(0)|答案(4)|浏览(155)

我正在使用Windows Azure Media Services .NET SDK 3使用流服务。我希望检索视频的持续时间。如何使用Windows Azure Media Services .NET SDK 3检索视频的持续时间?

ht4b089n

ht4b089n1#

Azure创建一些元数据文件(xml),可以在持续时间内查询这些文件。可以使用媒体服务扩展访问这些文件
https://github.com/Azure/azure-sdk-for-media-services-extensions
在获取资产 meta数据下:

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

qlfbtfca2#

如果您使用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://app.quicktype.io/?share=pAhTMFSa3HVzInAET5k4
您可以使用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();
}
0wi1tuuw

0wi1tuuw3#

在Azure Media Services SDK中,我们仅通过contentFileSize(https://msdn.microsoft.com/en-us/library/azure/hh974275.aspx)提供资产的大小。但是,我们不提供视频的元数据(如持续时间)。当你获得流定位器时,播放将告知视频资产的长度。
干杯,燕明飞

oxcyiej7

oxcyiej74#

@galdin让我知道了他们的答案。由于一些事情发生了变化,我想添加一个使用Azure存储v12的快速示例。此外,我抓住了容器的SAS并从那里读取清单;看起来容易一点。为了我的目的,我需要总分钟数。
您可以通过复制清单JSON数据并使用Visual Studio中的选择性粘贴选项来快速创建Amsv3Manifest模型。

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

相关问题