Azure AI Video Indexer

168 Views Asked by At

I am following the sample code at https://github.com/Azure-Samples/media-services-video-indexer/blob/master/API-Samples/C%23/Classic/sampleCode.cs to get started with Azure AI Video Indexer.

It has the following parameters to be setup in the code:

var apiKey = "API_KEY"; // replace with API key taken from https://aka.ms/viapi
var accountId = "ACCOUNT_ID"; // replace with your VI account id (guid)
var accountLocation = "trial"; // If you have a paid account the location will be different, named after the Azure region the account is in

Not sure which area Azure Video Indexer, I can get the Api Key and Account Id from.

Need some help in getting these information

1

There are 1 best solutions below

0
Venkatesan On

Not sure which area Azure Video Indexer, I can get the Api Key and Account ID from.

API Key:

You can get the API key from using this link https://aka.ms/viapi sign in with your Microsoft account and get the API key.

Portal:

enter image description here

Account ID:

You can get the Account ID using this https://www.videoindexer.ai/ link.

Portal:

enter image description here

Now, using the below code I can able to Upload video into the video indexer.

Code:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

class Program
{
    public static async Task Main(string[] args)
    {
        var apiUrl = "https://api.videoindexer.ai";
        var apiKey = "xxxxxx"; // replace with API key taken from https://aka.ms/viapi
        var accountId = "xxxxxxx"; // replace with your VI account id (guid)
        var accountLocation = "xxxxx"; // If you have a paid account the location will be different, named after the Azure region the account is in

        // TLS 1.2 (or above) is required to send requests
        var httpHandler = new SocketsHttpHandler();
        httpHandler.SslOptions.EnabledSslProtocols |= SslProtocols.Tls12;
        var client = new HttpClient(httpHandler);

        
        string queryParams = CreateQueryString(
            new Dictionary<string, string>()
            {
                    {"allowEdit", "true"},
            });

        var getAccountsRequest = new HttpRequestMessage(HttpMethod.Get, $"{apiUrl}/auth/{accountLocation}/Accounts/{accountId}/AccessToken?{queryParams}");
        getAccountsRequest.Headers.Add("Ocp-Apim-Subscription-Key", apiKey);
        getAccountsRequest.Headers.Add("x-ms-client-request-id", Guid.NewGuid().ToString()); 
        var result = await client.SendAsync(getAccountsRequest);
        Console.WriteLine("Response id to log: " + result.Headers.GetValues("x-ms-request-id").FirstOrDefault()); 
        var accessToken = (await result.Content.ReadAsStringAsync()).Trim('"'); 

        // Upload and index the video from URL - this is best practice and very robust compared to uploading a video from a local file. Uploading from an existing Azure Media Services asset is even better.
        var videoUrl = "your video url"; 
        queryParams = CreateQueryString(
            new Dictionary<string, string>()
            {
                    {"name", "Sample video name 1"},
                    {"videoUrl", videoUrl},
                    {"description", "This is a sample video description"},
                    {"privacy", "private"},
            });

        var uploadVideoRequest = new HttpRequestMessage(HttpMethod.Post, $"{apiUrl}/{accountLocation}/Accounts/{accountId}/Videos?{queryParams}");
        uploadVideoRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("\n", "")); // Access token is in JWT Bearer format
        uploadVideoRequest.Headers.Add("x-ms-client-request-id", Guid.NewGuid().ToString()); // Log the request id so you can include it in case you wish to report an issue for API errors or unexpected API behavior
        var uploadRequestResult = await client.SendAsync(uploadVideoRequest);
        Console.WriteLine("Response id to log: " + uploadRequestResult.Headers.GetValues("x-ms-request-id").FirstOrDefault());  
        var uploadResultJson = await uploadRequestResult.Content.ReadAsStringAsync();

        // Get the video ID from the upload result, will be used below.
        string videoId = JsonConvert.DeserializeObject<dynamic>(uploadResultJson).id;
        Console.WriteLine($"Uploaded, Account id: {accountId}, Video ID: {videoId}");

        while (true)
        {
            await Task.Delay(10000); 

            queryParams = CreateQueryString(
                new Dictionary<string, string>()
                {
                        {"accessToken", accessToken},
                        {"language", "English"},
                });

            var videoGetIndexRequestResult = await client.GetAsync($"{apiUrl}/{accountLocation}/Accounts/{accountId}/Videos/{videoId}/Index?{queryParams}");
            var videoGetIndexResult = await videoGetIndexRequestResult.Content.ReadAsStringAsync();

            var videoIndexData = JsonConvert.DeserializeObject<dynamic>(videoGetIndexResult);
            string processingState = videoIndexData?.state;

            Console.WriteLine("State: " + processingState);

            // Indexing completed
            if (processingState == "Processed" || processingState == "Failed")
            {
                Console.WriteLine("Full completed index JSON: ");
                Console.WriteLine(videoGetIndexResult);
                break;
            }
        }
    }

    static string CreateQueryString(IDictionary<string, string> parameters)
    {
        var queryParameters = HttpUtility.ParseQueryString(string.Empty);
        foreach (var parameter in parameters)
        {
            queryParameters[parameter.Key] = parameter.Value;
        }

        return queryParameters.ToString();
    }
}

Output:

Response id to log: 795xxx3
Response id to log: 667cfdd2xxxxe
Uploaded, Account id: xxx, Video ID: xxxx
State: Processing
State: Processing
State: Processing
State: Processing
State: Processing
State: Processing
State: Processing
State: Processing
State: Processing
State: Processed
Full completed.

Portal:

In my portal, the video is uploaded successfully.

enter image description here

Reference:

Use the Azure AI Video Indexer API | Microsoft Learn