Translate Audio to Multiple Languages
OpenAI cookbook using GPT-4o's audio-in/audio-out modality to dub audio into another language in one API call.
Why it matters
Translate audio content from one language to another, making it accessible to a global audience. This asset leverages advanced AI to perform voice-to-voice translation in a single step.
Outcomes
What it gets done
Transcribe source audio to text.
Dub audio directly to a target language.
Evaluate translation quality using benchmarks.
Improve translation by adjusting prompts.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-voicetranslationintodifferentlanguagesusinggpt-4o | bash Steps
Steps in the chain
Overview
Voice Translation Into Different Languages Using Gpt 4O
OpenAI cookbook using GPT-4o's audio-in/audio-out modality to dub audio from one language to another in a single API call, then benchmark translation quality with BLEU and ROUGE scores via back-translation. Use when dubbing or translating audio content - podcasts, training videos, film - into another language with a single-call voice-to-voice pipeline.
What it does
This OpenAI cookbook shows how to translate and dub an English audio file into Hindi using GPT-4o's audio-in and audio-out modality, replacing the previous multi-step process of transcribing audio to text, translating that text, and converting it back to audio with a single voice-to-voice API call. It first clarifies a distinction the task depends on: language (a spoken/written communication system, like Hindi or English) versus script (the character set used to write it, like Devanagari or Latin) - since some languages share a script and some don't. The workflow has four steps. Step 1 (optional, skippable if you already have a source transcription) transcribes the source audio into its own language's script using a process_audio_with_gpt_4o function that sends a base64-encoded audio file, desired output modalities, and a system prompt to the chat/completions endpoint with model gpt-4o-audio-preview, requesting only ["text"] output and instructing the model to ignore background noise like applause - this transcript becomes the ground truth for later benchmarking. Step 2 dubs the audio directly from source to target language in one call by setting output modality to ["text", "audio"], returning both the target-language transcription (mixing scripts naturally, e.g. Devanagari for Hindi words and Latin for retained English terms, for correct pronunciation) and the dubbed audio itself, played back with the pydub module. Step 3 benchmarks translation quality with BLEU (n-gram overlap, 0-100 scale) and ROUGE (n-gram and longest-common-subsequence overlap, commonly used for summarization) by transcribing the dubbed output audio back into the original language with GPT-4o and comparing it to the Step 1 ground truth, since a genuine human reference translation is often impractical to obtain. Step 4 interprets those scores against reference scales (e.g. BLEU 40-50 = very good, 50+ = excellent; ROUGE-1 0.5-0.6 and ROUGE-L 0.4-0.5 = good) and, if scores are unsatisfactory, offers concrete fixes: supply a glossary of easily-confused terms if transcription is inaccurate, insert a text post-processing/grammar-correction step (then switch to GPT-4o's text-in/audio-out modality) if the source audio has grammatical errors, or maintain a glossary_of_terms_to_keep_in_original_language for terms better left untranslated.
When to use - and when NOT to
Use this cookbook when dubbing or translating audio content - podcasts, training videos, films - into another language and you want a single-call voice-to-voice pipeline instead of chaining separate transcription, translation, and text-to-speech services. It is not a guide for text-only translation (no audio involved) or for languages/use cases where BLEU/ROUGE benchmarking against a back-translated transcript isn't a meaningful substitute for genuine human-reference evaluation.
Inputs and outputs
Input is a base64-encoded source-language audio file (and optionally an existing source transcription). Output is a target-language transcription plus dubbed audio from GPT-4o, along with BLEU/ROUGE scores benchmarking translation quality against the source transcript via back-translation.
Integrations
Built on OpenAI's GPT-4o audio API (gpt-4o-audio-preview model, chat/completions endpoint, audio-in/audio-out and text-in/audio-out modalities), the pydub Python library for audio playback, and BLEU/ROUGE for translation-quality benchmarking.
Who it's for
Developers and content teams dubbing or translating audio content across languages - podcasts, training material, film - who want a single-API-call voice-to-voice pipeline with a built-in way to benchmark and iteratively improve translation quality.
Source README
Voice Translation of Audio Files into Different Languages Using Gpt-4o
Have you ever wanted to translate a podcast into your native language? Translating and dubbing audio content can make it more accessible to audiences worldwide. With GPT-4o's new audio-in and audio-out modality, this process is now easier than ever.
This guide will walk you through translating an English audio file into Hindi using OpenAI's GPT-4o audio modality API.
GPT-4o simplifies the dubbing process for audio content. Previously, you had to convert the audio to text and then translate the text into the target language before converting it back into audio. Now, with GPT-4o’s voice-to-voice capability, you can achieve this in a single step with audio input and output.
A note on semantics used in this Cookbook regarding Language and written Script. These words are generally used interchangeably, though it's important to understand the distinction, given the task at hand.
- Language refers to the spoken or written system of communication. For instance, Hindi and Marathi are different languages, but both use the Devanagari script. Similarly, English and French are different languages, but are written in Latin script.
- Script refers to the set of characters or symbols used to write the language. For example, Serbian language traditionally written in Cyrillic Script, is also written in Latin script.
GPT-4o audio-in and audio-out modality makes it easier to dub the audio from one language to another with one API call.
1. Transcribe the source audio file into source language script using GPT-4o. This is an optional step that can be skipped if you already have the transcription of source audio content.
2. Dub the audio file from source language directly to the target langauge.
3. Obtain Translation Benchmarks using BLEU or ROUGE.
4. Interpret and improve scores by adjusting prompting parameters in steps 1-3 as needed.
Before we get started, make sure you have your OpenAI API key configured as an environment variable, and necessary packages installed as outlined in the code cells below.
Step 1: Transcribe the Audio to Source Language Script using GPT-4o
Let's start by creating a function that sends an audio file to OpenAI's GPT-4o API for processing, using the chat completions API endpoint.
The function process_audio_with_gpt_4o takes three inputs:
- A base64-encoded audio file (base64_encoded_audio) that will be sent to the GPT-4o model.
- Desired output modalities (such as text, or both text and audio).
- A system prompt that instructs the model on how to process the input.
The function sends an API request to OpenAI's chat/completions endpoint. The request headers include the API key for authorization. The data payload contains the model type (gpt-4o-audio-preview), the selected output modalities, and audio details, such as the voice type and format (in this case, "alloy" and "wav"). It also includes the system prompt and the base64-encoded audio file as part of the "user" message. If the API request is successful (HTTP status 200), the response is returned as JSON. If an error occurs (non-200 status), it prints the error code and message.
This function enables audio processing through OpenAI's GPT-4o API, allowing tasks like dubbing, transcription, or translation to be performed based on the input provided.
Using the function process_audio_with_gpt_4o, we will first get an English transcription of the source audio. You can skip this step if you already have a transcription in the source language.
In this step, we:
- Read the WAV file and convert it into base64 encoding.
- Set the output modality to ["text"], as we only need a text transcription.
- Provide a system prompt to instruct the model to focus on transcribing the speech and to ignore background noises like applause.
- Call the process_audio_with_gpt_4o function to process the audio and return the transcription.
This English transcript will serve as our ground truth as we benchmark the Hindi language dubbing of the audio in Step 3.
Step 2. Dub the Audio from the Source Language to the Target Language using GPT-4o
With GPT-4o, we can directly dub the audio file from English to Hindi and get the Hindi transcription of the audio in one API call. For this, we set the output modality to ["text", "audio"]
In the following code snippet, we will retrieve both the Hindi transcription and the dubbed audio from the GPT-4o response. Previously, this would have been a multistep process, involving several API calls to first transcribe, then translate, and finally produce the audio in the target language. With GPT-4o, we can now accomplish this in a single API call.
The transcribed text is a combination of Hindi and English, represented in their respective scripts: Devanagari for Hindi and Latin for English. This approach ensures more natural-sounding speech with the correct pronunciation of both languages' words. We will use the pydub module to play the audio as demonstrated in the code below.
Step 3. Obtain Translation Benchmarks (e.g., BLEU or ROUGE)
We can assess the quality of the translated text by comparing it to a reference translation using evaluation metrics like BLEU and ROUGE.
BLEU (Bilingual Evaluation Understudy): Measures the overlap of n-grams between the candidate and reference translations. Scores range from 0 to 100, with higher scores indicating better quality.
ROUGE (Recall-Oriented Understudy for Gisting Evaluation): Commonly used for summarization evaluation. Measures the overlap of n-grams and the longest common subsequence between the candidate and reference texts.
Ideally, a reference translation (a human-translated version) of the original text is needed for an accurate evaluation. However, developing such evaluations can be challenging, as it requires time and effort from bilingual humans proficient in both languages.
An alternative is to transcribe the output audio file from the target language back into the original language to assess the quality of the translation using GPT-4o.
With the text transcribed back into English language script from the Hindi audio, we can run the evaluation metrics by comparing it to the original English transcription.
Step 4. Interpret and improve scores by adjusting prompting parameters in steps 1-3 as needed
In this example, both BLEU and ROUGE scores indicate that the quality of the voice translation is between very good and excellent.
Interpreting BLEU Scores: While there is no universally accepted scale, some interpretations suggest:
0 to 10: Poor quality translation; significant errors and lack of fluency.
10 to 20: Low quality; understandable in parts but contains many errors.
20 to 30: Fair quality; conveys the general meaning but lacks precision and fluency.
30 to 40: Good quality; understandable and relatively accurate with minor errors.
40 to 50: Very good quality; accurate and fluent with very few errors.
50 and above: Excellent quality; closely resembles human translation.
Interpreting ROUGE scores: The interpretation of a "good" ROUGE score can vary depending on the task, dataset, and domain. The following guidelines indicate a good outcome:
ROUGE-1 (unigram overlap): Scores between 0.5 to 0.6 are generally considered good for abstractive summarization tasks.
ROUGE-L (Longest Common Subsequence): Scores around 0.4 to 0.5 are often regarded as good, reflecting the model's ability to capture the structure of the reference text.
If the score for your translation is unsatisfactory, consider the following questions:
1. Is the source audio accurately transcribed?
If the transcription contains errors, such as confusing similar-sounding words, you can provide a glossary of such terms in the system prompt during step 1. This helps the model avoid misinterpretations and ensures accurate transcription of specific terms.
2. Is the source audio free of grammatical errors?
If the source audio contains grammatical errors, consider using a post-processing step with the GPT model to refine the transcription by removing grammatical mistakes and adding appropriate punctuation. After this, instead of using GPT-4o’s audio-in and audio-out modality, you can use the corrected transcription with GPT-4o’s text-in and audio-out modality to generate the audio in the target language.
3. Are there words that make sense to keep in the original language?
Certain terms or concepts may not have a suitable translation in the target language or may be better understood in their original form. Revisit your glossary_of_terms_to_keep_in_original_language and include any such terms to maintain clarity and context.
Conclusion
In summary, this cookbook offers a clear, step-by-step process for translating and dubbing audio, making content more accessible to a global audience. Using GPT-4o’s audio input and output capabilities, translating and dubbing audio files from one language to another becomes much simpler. Our example focused on translating an audio file from English to Hindi.
The process can be broken down into the following steps:
1. Transcription: Obtain transcription of the source language audio into source language script using GPT-4o text modality.
2. Dub: Directly dub the audio file into the target language using GPT-4o's audio modality.
3. Benchmark Translation Quality: Evaluate the translation’s accuracy using BLEU or ROUGE scores compared to reference text.
4. Optimize the Process: If needed, adjust the prompting parameters to improve the transcription and dubbing results.
This guide also highlights the crucial distinction between "language" and "script"-terms that are often confused but are essential in translation work. Language refers to the system of communication, either spoken or written, while script is the set of characters used to write a language. Grasping this difference is vital for effective translation and dubbing.
By following the techniques in this cookbook, you can translate and dub a wide range of content-from podcasts and training videos to full-length films-into multiple languages. This method applies across industries such as entertainment, education, business, and global communication, empowering creators to extend their reach to diverse linguistic audiences.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.