Search code examples
c#.netazuresdkazure-media-services

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


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?


Solution

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

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

    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;