Prerequisites
- Make sure you have your Azure Video Indexer account API key and locate your account ID, location, and videos URL. You'll need these for authentication and accessing the API.
Integrate Azure Cognitive Services Video Indexer SDK
- Start by adding the necessary NuGet packages for accessing the Azure Video Indexer API. Use NuGet package manager or the package manager console to install:
Install-Package Azure.Media.VideoAnalyzer.Edge
This package consists of tools for interacting with video analytics and processing video content through Azure.
Set Up Authentication
- To authenticate and access the Video Indexer API, use Azure Active Directory Bearer Token. Here’s a sample function to get the token:
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<string> GetAccessTokenAsync(string clientId, string clientSecret, string tenantId)
{
HttpClient client = new HttpClient();
var url = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";
var content = new StringContent($"grant_type=client_credentials&client_id={clientId}&client_secret={clientSecret}&resource=https://management.azure.com/",
Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
return payload["access_token"].ToString();
}
Invoke Video Indexer Operations
- Set up a method to upload video for analysis. First, set base URL and authentication headers:
using System;
using System.IO;
using System.Net.Http.Headers;
public static async Task<string> UploadVideoAsync(string accessToken, string accountId, string location, string videoFilePath)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// API url to upload video
var url = $"https://{location}.api.videoindexer.ai/{location}/Accounts/{accountId}/Videos?name={Path.GetFileNameWithoutExtension(videoFilePath)}";
byte[] videoData = File.ReadAllBytes(videoFilePath);
ByteArrayContent content = new ByteArrayContent(videoData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
Here, replace the placeholders with your account specific data. The method returns the response from the Video Indexer API.
Handle Video Analysis Results
- Once the video is indexed, retrieve the analysis results like transcripts, insights, etc.
public static async Task<string> GetVideoInsightsAsync(string accessToken, string accountId, string location, string videoId)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var url = $"https://{location}.api.videoindexer.ai/{location}/Accounts/{accountId}/Videos/{videoId}/Index";
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
This method fetches insights using the video ID obtained from the upload response. Deserialize the JSON response to work with insights in your application.
Best Practices and Considerations
- Ensure that authentication and access token management are securely handled.
- You'll likely want to retry failed API requests, particularly ones involving network calls, to handle transient issues gracefully.
- Consider implementing logging for both successful and unsuccessful API interactions to maintain a robust integration and debugging capability.