azureazure-resource-managerazure-media-services

Get Input Asset or Output Asset from JobId or SmoothStreamingUrl - Azure Media Services


In our database we have a list of JobIds (From Azure Media Services encoding) and SmoothStreamingUrls like the below:

What I want is to use either of these pieces of information to get the input/output asset name so I can record where the video file is stored within the Storage Account.

Currently I have been using the Azure.ResourceManager.Media nuget package and attempting to try and get this information but so far to no success.

Can anyone offer any help or advice please?


Solution

  • So I figured out how to get the output asset mainly using Azure.ResourceManager.Media Nuget. Not sure if this is the best way but works for me. Only works on the streaming urls created in V2 for the ones created it V3 it provides the output asset name which you can then use .GetMediaAsset() from the Resource Manager nuget to get the container.

        var mediaServiceAccountId = MediaServicesAccountResource.CreateResourceIdentifier(
            subscriptionId: {subscriptionId},
            resourceGroupName: {resourceGroupName},
            accountName: {accountName});
        
        var credential = new ClientSecretCredential({tenant}, {clientId}, {clientSecret});
        var armClient = new ArmClient(credential);
        
        var mediaServicesAccount = armClient.GetMediaServicesAccountResource(mediaServiceAccountId);
        
        //Gets results from table
        var videosOldEncoding = Repository.GetAllVideosOldEncoding();
        
        var locators = mediaServicesAccount.GetStreamingLocators();
        
        foreach (var locator in locators)
        {
            var streamingEndpoint = (mediaServicesAccount.GetStreamingEndpoints().Get("default")).Value;
        
            var streamingName = PrintStreamingUrlsAsync(locator, streamingEndpoint);
        
            foreach (var video in videosOldEncoding)
            {
                if (video.SmoothStreamingUrl != null)
                {
                    string[] items = video.SmoothStreamingUrl.Split("/", StringSplitOptions.RemoveEmptyEntries);
                    string[] idStrings = { items[2], items[3] };
                    var streamingId = String.Join("/", idStrings);
        
                    if (streamingName.Contains(streamingId))
                    {
                        Repository.UpdateAsset(video.VideoId, String.Concat("asset-", locator.Data.AssetName));
                    }
                }
            }
        }
    
    static string PrintStreamingUrlsAsync(
               StreamingLocatorResource locator,
               StreamingEndpointResource streamingEndpoint)
    {
        var paths = locator.GetStreamingPaths();
    
        foreach (StreamingPath path in paths.Value.StreamingPaths)
        {
            //Console.WriteLine($"The following formats are available for {path.StreamingProtocol.ToString().ToUpper()}:");
    
            if (path.StreamingProtocol == StreamingPolicyStreamingProtocol.SmoothStreaming)
            {
                string streamingFormatPath = path.Paths.FirstOrDefault();
    
                var uriBuilder = new UriBuilder()
                {
                    Scheme = "https",
                    Host = streamingEndpoint.Data.HostName,
                    Path = streamingFormatPath
                };
    
                return uriBuilder.ToString();
            }
        }
    
        return null;
    }