In this tutorial, we’ll demonstrate how to translate a short Spanish audio clip into English text using OpenAI’s Whisper API. We’ll process a 20-second MP3 segment (up to 25 MB) extracted from an Easy Spanish YouTube video and send it to the API in one request.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- Python 3.7+
openaiPython SDK- An OpenAI API key
Ensure your MP3 file is under 25 MB. Whisper supports formats like MP3, WAV, and FLAC.
Translation Code Example
Step-by-Step Breakdown
| Step | Action | Code Snippet |
|---|---|---|
| 1 | Configure the OpenAI API key | openai.api_key = os.getenv("OPENAI_API_KEY") |
| 2 | Load and display the MP3 clip inline | ipd.display(ipd.Audio(file_name)) |
| 3 | Translate audio using whisper-1 | openai.Audio.translate("whisper-1", audio_file) |
| 4 | Print the translated English text | print(result.text) |
Keep your API key secure. Do not hard-code it in public repositories.