Chapter 20 · Hands-on: more projects
Turn speech into text on your computer
An open speech-recognition model can transcribe audio offline, keeping recordings private.
Chapter 20 · Hands-on: more projects
An open speech-recognition model can transcribe audio offline, keeping recordings private.
Recording goes in…
A short script that sends a recording to an open speech model on your own computer and saves the words, with times, as a text file and a subtitle file.
As in the speech recognition lesson, a speech model turns sound into text. Here you run one yourself. Once the model is downloaded, your recordings never leave your computer, which is a good fit for private meetings or interviews.
You need Python as in Set up Python and a virtual environment. This tutorial is for Windows, macOS and Linux desktops. A normal laptop CPU is enough for the small models; larger ones are slower and need more memory.
Open a terminal. Make a folder called speech-text, go into it and create a private Python box for this project, as in the Python setup lesson.
mkdir speech-text
cd speech-text
python -m venv .venv
.venv\Scripts\Activate.ps1 Check: Your prompt starts with (.venv).
pip installs Python packages. This one command installs faster-whisper, one popular open-source way to run Whisper-style speech models. It brings along what it needs to read common audio files, so you do not have to install anything else.
pip install faster-whisper Check: The command ends without red errors. Then python -c "import faster_whisper" prints nothing.
Record 30 to 60 seconds of clear speech with your phone’s voice recorder or any program, and copy the file into the speech-text folder. Common types such as .mp3, .wav, .m4a and .flac work. A short file makes your first test quick.
Check: The file is inside speech-text. A command to list files (ls or dir) shows its name.
Create a file called transcribe.py in the same folder and paste this in. In short: it loads a model, asks it to transcribe your file in pieces called segments (each with a start and end time), prints them, and saves a plain .txt and a subtitle .srt next to your audio.
int8 means the model’s numbers are stored in a small form, so it uses less memory on a CPU. vad_filter skips long silences.
import sys
from pathlib import Path
from faster_whisper import WhisperModel
MODEL_SIZE = "base" # "tiny" is fastest; larger sizes are more accurate but slower and need more memory
def stamp(seconds):
"""Turn 83.5 into the subtitle time format 00:01:23,500."""
ms = int(round(seconds * 1000))
h, ms = divmod(ms, 3_600_000)
m, ms = divmod(ms, 60_000)
s, ms = divmod(ms, 1000)
return f"{h:02}:{m:02}:{s:02},{ms:03}"
def main():
if len(sys.argv) < 2:
sys.exit("Give the audio file, for example: python transcribe.py meeting.mp3")
audio = Path(sys.argv[1])
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="int8")
segments, info = model.transcribe(str(audio), beam_size=5, vad_filter=True)
print(f"Language guessed: {info.language} (confidence {info.language_probability:.2f})")
lines, subtitles = [], []
for n, seg in enumerate(segments, start=1):
text = seg.text.strip()
print(f"[{seg.start:7.2f} -> {seg.end:7.2f}] {text}")
lines.append(text)
subtitles.append(f"{n}\n{stamp(seg.start)} --> {stamp(seg.end)}\n{text}\n")
audio.with_suffix(".txt").write_text("\n".join(lines) + "\n", encoding="utf-8")
audio.with_suffix(".srt").write_text("\n".join(subtitles), encoding="utf-8")
print("Saved", audio.with_suffix(".txt").name, "and", audio.with_suffix(".srt").name)
if __name__ == "__main__":
main() Check: The file is saved as transcribe.py, not transcribe.py.txt.
Type python transcribe.py, then your file name in quotes.
python transcribe.py "meeting.mp3" Check: You see Language guessed, then lines like [ 0.00 -> 4.20] Good morning…, then Saved … .txt and … .srt.
The .txt file has the words only, one segment per line. The .srt file is the subtitle format: a number, a time range, then the text. Most video players can load an .srt file next to a video. Read a few lines against the audio and note the mistakes: names and unusual words are the usual trouble.
Check: Both files sit next to your recording and open in any text editor.
If the text has too many mistakes, try a bigger model size. If speech is in another language, give the language code so the model does not have to guess. The helper below writes the exact lines to change. Run it again and compare.
MODEL_SIZE = "small"
segments, info = model.transcribe(str(audio), beam_size=5, vad_filter=True, language="en")Check: The new run takes a little longer and the text improves, or you see why it did not (noise, mumbling, overlapping voices).
Type your file name, pick a model size and language. This only writes text for you to copy; it does not run a model.
A good first choice on an ordinary laptop. Balanced speed and accuracy.
MODEL_SIZE = "base"
segments, info = model.transcribe(str(audio), beam_size=5, vad_filter=True)python transcribe.py "meeting.mp3"What do you see?
Why does the first run take longer than later runs?
Your transcript has many mistakes. What is a sensible first thing to try?
Which is true about recordings in this tutorial?