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

Related searches

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?

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

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;

How to get uploded Video length/Duration- C# - MSDN, How to get uploded Video length/Duration- C# - After/Before encoding process. RRS feed � Archived Forums. > Azure Media Services. Migrate Smarter Using the Best Cloud Migration Tools From AWS. With More Than 165 Services, AWS Helps You Go From An Idea To Implementation In Seconds.

In Azure Media Services SDK, we only provides the size of asset through contentFileSize ( 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

Analyze video and audio files, Azure Media Services v3 lets you extract insights from your video and and continues with the selected language for the duration of the file. Search for Azure Cloud Service. The Last Information Now! Search for Azure cloud service. Find it with us!

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:

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

How to get an encoded video's duration? � Issue #5 � southworks , southworks / azure-sdk-for-media-services-java-samples � Watch 11 Is there any way I can get the duration of an encoded media content? TheAnswerHub is a top destination for finding answers online. Browse our content today! Find azure cloud platform on

Using Azure Media Services to get metadata from a media file , I have found that regardless of the video size or video length, this process takes an average of 40 seconds (if you have a media encoder unit� Search Azure Cloud Platform. Compare Results on Find Information Related to Azure Cloud Platform

Azure Media Player, Azure Media Player is a web video player built to playback media content from Microsoft to get basic playback of media content from your Azure Media Services account. The clip duration in seconds, cannot extend past the end of the asset� In Azure Media Services SDK, we only provides the size of asset through contentFileSize ( 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

The videos are hosted on Azure Media Services and downloading isn't enabled. Since I didn't know the environment, I wanted a back up plan and Microsoft provides a lot of content on their website . These videos explain the basic functionality in a short and clear way, but I wanted to have them offline.

  • This appears to not work anymore with the latest api and I cannot figure out how to get to the meta data any more. Any ideas?
  • Thanks for this piece of code, however assetFile.GetMetadata() returns a IEnumerable<AssetFileMetadata> (at least in v4.2.0), and should be treated as such.
  • You used to provide the duration until the most recent update of the api. Where did this go?