Sub topic 7
Language detection and translating speech to English
This chapter assumes basic Python, pip, and familiarity with audio files. It also assumes that you have installed openai-whisper and that ffmpeg is available on your system.
Whisper can detect the language spoken in an audio file. It can then either transcribe the speech in its original language or translate it into English. These are different tasks controlled by the task option.
Detecting the spoken language
When you omit the language argument, Whisper analyzes the beginning of the audio and estimates the most likely language. The transcription result includes the detected language code:
import whisper
model = whisper.load_model("base")
result = model.transcribe("audio.mp3")
print("Detected language:", result["language"])
print("Text:", result["text"])A language code such as en, es, or fr identifies English, Spanish, or French. Detection is an estimate, not a guarantee. Short clips, background music, multiple speakers, and similar languages can produce an incorrect result.
Whisper also provides lower-level language probabilities. This is useful when you need to inspect uncertainty:
import whisper
model = whisper.load_model("base")
audio = whisper.load_audio("audio.mp3")
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probabilities = model.detect_language(mel)
for language, probability in sorted(
probabilities.items(), key=lambda item: item[1], reverse=True
)[:5]:
print(f"{language}: {probability:.3f}")The probabilities describe the model's preferences for the input window. They should not be treated as calibrated confidence scores.
Transcription versus translation
The default task is transcribe. Whisper writes the words in the language that was spoken:
import whisper
model = whisper.load_model("small")
result = model.transcribe(
"spanish_talk.mp3",
language="es",
task="transcribe",
)
print(result["text"])Setting language="es" avoids language detection and tells the decoder to expect Spanish. Providing the language is usually better when you know it, especially for short recordings.
Set task="translate" when you want English output from non-English speech:
import whisper
model = whisper.load_model("medium")
result = model.transcribe(
"spanish_talk.mp3",
language="es",
task="translate",
)
print(result["text"])The translation target is English. Whisper's standard translation task does not let you choose French, German, or another target language. For those targets, transcribe first and then use a separate text translation system.
You can omit language during translation, but explicit language selection is safer:
result = model.transcribe(
"meeting.mp3",
task="translate",
language="ja",
)The model still needs to understand the source language before it can translate it.
How the decision flows
Common errors
- A short recording may contain too little speech for reliable detection.
- Music, silence, or noise can cause a wrong language choice.
- A bilingual recording may be forced into one dominant language.
- Translation can mistranscribe a name even when the original transcription is correct.
- A language code must be valid, such as
en,es,de, orja.
Exercise
What is the difference between task="transcribe" and task="translate"?
Show answer
transcribe returns text in the spoken language. translate returns an English translation of non-English speech.