Use OpenAI’s Translation API to translate audio files to English

In this blog post I will show you how we can use the Translation API from OpenAI to translate any audio file into English using AI.

Sebastian Jensen
medialesson
3 min readDec 18, 2023

--

Introduction

In this blog post we will write a simple console application in C# to make a request to the Translation API to generate a translation into English of an audio file.

Prerequisites

You need to create an OpenAI account on this website. You have to pay to use the API so make sure that you add your payment information. After that, you can create an API Key here for further use.

Let’s code

I am using Visual Studio to create a new .NET console application using .NET 8. At the moment there is no official NuGet package for the OpenAI API communication. So we have to call the API by ourselfs.

Add the Spectre.Console NuGet package to the project and create a new folder called Models where we will add the request model.

Let’s start with the TranslationResponse class. This class contains the properties to deserialize the response from the API.

Now that we have the needed model available, we can open the Program.cs file to build the business logic.

First the console application is asking the user to provide the needed parameters. In this case we just need the OpenAI API key and the file path of a stored audio file.

We use the translations endpoint to post our properties to the API. The response is a JSON object containing the transcribed text. The console application will write the transcribed text to the console.

Sample

Let’s run the console application. You will see the header and the first input for the OpenAI API key.

Next we need to inser the file path to the stored audio file.

Here is the translated text of my German YouTube channel trailer.

Keep in mind that it is currently only possible to translate an audio file into English, but I’m pretty sure that in the future this will change and you can translate the audio file into different languages.

Conclusion

In this blog post I’ve written a simple .NET console application to use the published Translation API by OpenAI. So it is pretty simple to use the API, but keep in mind that you have to pay for each call against the API.

You can find the source code of the console aplication on my GitHub profile.

I’ve also published a blog post about the Text-To-Speech API and also a blog post about the Speech-To-Text API.

--

--

Sebastian Jensen
medialesson

Senior Software Developer & Team Lead @ medialesson GmbH