Sub topic 5
Transcribing a File with the `openai-whisper` Package
This chapter assumes that you know basic Python, can install packages with pip, and understand common audio formats such as WAV and MP3. You should also be comfortable running commands in a terminal.
Whisper is an automatic speech recognition (ASR) model. ASR converts spoken audio into written text. The openai-whisper package provides Python code for downloading Whisper models and running them on audio files.
Install the package and audio decoder
Install the Python package with:
pip install -U openai-whisperWhisper uses ffmpeg to read many audio formats, including MP3, M4A, and video files. Install it separately if it is not already available.
On Ubuntu or Debian:
sudo apt update
sudo apt install ffmpegOn macOS with Homebrew:
brew install ffmpegCheck that the command is available:
ffmpeg -versionThe transcription workflow
The package performs several steps when you call transcribe:
The model converts the audio into internal features, processes those features, and generates text tokens. A token is a small unit of text, such as part of a word or punctuation. The method returns a dictionary containing the transcription and additional information.
Create a file named transcribe.py:
import whisper
model = whisper.load_model("base")
result = model.transcribe("audio.mp3")
print(result["language"])
print(result["text"])Run it with:
python transcribe.pyThe first call to load_model downloads the selected model and stores it in the local model cache. Later runs can reuse the downloaded files. The base model is a practical starting point for testing. Larger models can improve accuracy but need more memory and take longer to run.
The main result fields include:
| Field | Meaning |
|---|---|
| `text` | The complete transcription as one string |
| `language` | The detected language code |
| `segments` | A list of transcript sections with timing information |
You can inspect the segments like this:
import whisper
model = whisper.load_model("base")
result = model.transcribe("audio.mp3")
for segment in result["segments"]:
print(segment["start"], segment["end"], segment["text"])Each segment is a dictionary. Its start and end values are measured in seconds. Segment boundaries are useful for subtitles, search results, and reviewing a recording.
Common setup errors
ModuleNotFoundError: No module named 'whisper'usually means the package was installed into a different Python environment. Runpython -m pip install -U openai-whisper.- An error mentioning
ffmpegmeans the decoder is missing or is not on your systemPATH. - A memory error usually means that the selected model is too large for the available hardware. Try
tiny,base, orsmall. - An incorrect transcription may result from background noise, overlapping speakers, a strong accent, or an audio file with very low volume.
Exercise
Which expression prints the complete transcription returned by Whisper?
Show answer
print(result["text"]) prints the complete transcription as one string.