Transcribe audio and video to text and SRT subtitles with faster-whisper
DEV Community

Transcribe audio and video to text and SRT subtitles with faster-whisper

OpenAI's Whisper models are open, and faster-whisper runs them on CTranslate2, which makes them usable on a plain CPU.

You do not need a GPU to transcribe a podcast, and you do not need to install ffmpeg: faster-whisper decodes audio through PyAV, which ships its own FFmpeg libraries. Video files work too, since only the audio track is read.

The script

pip install faster-whisper
import sys
from faster_whisper import WhisperModel

model = WhisperModel("base", device="cpu", compute_type="int8")

def timestamp(seconds):
    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 transcribe(path, language=None):
    segments, info = model.transcribe(path, language=language, vad_filter=True, beam_size=1)
    segments = list(segments) # the generator does the actual work
    text = " ".join(s.text.strip() for s in segments)
    srt = "\n".join(
        f"{i}\n{timestamp(s.start)} --> {timestamp(s.end)}\n{s.text.strip()}\n"
        for i, s in enumerate(segments, 1)
    )
    return info.language, info.duration, text, srt

if __name__ == "__main__":
    language, duration, text, srt = transcribe(sys.argv[1])
    print(f"{language}, {duration:.0f} s")
    print(text)
    print(srt)
$ python transcribe.py jfk.flac
en, 11 s
And so my fellow Americans ask not what your country can do for you, ask what you can do for your country.
1
00:00:00,000 --> 00:00:11,000
And so my fellow Americans ask not what your country can do for you, ask what you can do for your country.

The model is downloaded on first use and cached. The language is detected from the first 30 seconds unless you pass language="de".

The settings that matter

  • compute_type="int8" quantizes the weights to 8 bits, which cuts memory and speeds up CPU inference. It is what I run in production.
  • vad_filter=True runs a voice activity detector first and skips silence. It does two things: long pauses and music stop costing time, and Whisper stops inventing text over silence, which it otherwise does ("Thanks for watching!" at the end of a file that never says it).
  • beam_size is the accuracy and speed dial. 1 is greedy decoding and fastest. 5 is the library default: slower, and better with names and unclear words.
  • Model size. base is fine for clear speech in major languages. small is visibly better with accents, background noise and less common languages. Above that the CPU cost climbs quickly.

The generator is lazy. model.transcribe() returns immediately. The work happens when you iterate over segments, so that is the line to time and the line to wrap in a progress bar.

Speed, measured

On a single cloud CPU core with int8:

Model Beam Speed
base 1 about 11x real time: an hour of audio in 5 to 6 minutes
small 5 about 4x real time: an hour of audio in 15 minutes

More cores help: cpu_threads controls how many the model uses.

A dependency pin that saved me an evening

faster-whisper 1.2.1 crashed on decode for me with PyAV 19. Pinning it fixed that:

faster-whisper==1.2.1
av<16

If your transcription dies inside av with no useful message, check the version before anything else.

Other things worth knowing

  • SRT wants commas. 00:00:07,600, not 00:00:07.600. WebVTT is the one with dots, plus a WEBVTT header line.
  • No speaker labels. Whisper does not know who is talking. That takes a separate diarization model.
  • Segment length. Segments follow pauses and can run long for subtitles. Pass word_timestamps=True and regroup the words if you need short lines.

If you do not want to run it yourself

I host this as an Apify Actor, Audio & Video Transcriber: you send direct file URLs and get text, timestamps, SRT and VTT back as JSON. It costs $0.006 per audio minute in fast mode and $0.015 in accurate mode, and failed files are not charged. I built it and I earn from it. Python, Node and curl examples are in this repository.

curl -s -X POST \
  "https://api.apify.com/v2/acts/picdev~audio-video-transcriber-speech-to-text/run-sync-get-dataset-items" \
  -H "Authorization: Bearer $APIFY_TOKEN" -H "Content-Type: application/json" \
  -d '{"urls": ["https://github.com/openai/whisper/raw/main/tests/jfk.flac"], "includeSrt": true}'

For a few files on your own machine, the script at the top is all you need.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.