如何从 Azure 媒体服务获取视频的时长?
How to get the duration of a video from the Azure media services?
我正在使用 Windows Azure 媒体服务 .NET SDK 3 来使用流媒体服务。我想检索视频的持续时间。如何使用 Windows Azure 媒体服务 .NET SDK 3 检索视频的持续时间?
在 Azure 媒体服务 SDK 中,我们仅通过 contentFileSize (https://msdn.microsoft.com/en-us/library/azure/hh974275.aspx) 提供资产的大小。但是,我们不提供视频的元数据(例如持续时间)。当您获得流媒体定位器时,播放会告诉您视频资产的时长。
干杯,
闫明飞
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;
如果您使用的是 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();
}
我正在使用 Windows Azure 媒体服务 .NET SDK 3 来使用流媒体服务。我想检索视频的持续时间。如何使用 Windows Azure 媒体服务 .NET SDK 3 检索视频的持续时间?
在 Azure 媒体服务 SDK 中,我们仅通过 contentFileSize (https://msdn.microsoft.com/en-us/library/azure/hh974275.aspx) 提供资产的大小。但是,我们不提供视频的元数据(例如持续时间)。当您获得流媒体定位器时,播放会告诉您视频资产的时长。
干杯, 闫明飞
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;
如果您使用的是 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();
}