[英]How to properly upload video files to Azure Media Services from angularjs
[英]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.