Home > Uncategorized > Automatically translate #Udemy captions using #Microsoft #Azure #Cognitive services.

Automatically translate #Udemy captions using #Microsoft #Azure #Cognitive services.

captions editor

Udemy has a great system for captioning videos, but perhaps you want to appeal to an international audience, and include captions in multiple languages. The best solution, of course it is to have them professionally translated, but if you want a cheap solution (and poor-quality), then you can run them through an automated translator like Azure cognitive services.

The code here is in C#, and is available on Github here; https://github.com/infiniteloopltd/vtt-translate

You download the VTT file (which is a bit like an SRT), and run it through this code;

private static string FromString(string english, string to)
{
const string host = “https://api.cognitive.microsofttranslator.com”;
var route = “/translate?api-version=3.0&from=en&to=” + to;
var subscriptionKey = ConfigurationManager.AppSettings[“subscription”];
var body = new object[] { new { Text = english } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(host + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, “application/json”);
request.Headers.Add(“Ocp-Apim-Subscription-Key”, subscriptionKey);
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var jResponse = JArray.Parse(jsonResponse);
foreach (var translation in jResponse[0][“translations”])
{
var strText = translation[“text”].ToString();
return strText;
}
}
return null;
}

Obviously, you’ll need your own subscription key from azure, but the free tier covers 2M chars.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment