- Python 100%
transcribe.py #
Small CLI that sends an audio file to a speech-to-text gateway and writes the
transcript to a .txt file. Segment timestamps are rendered by default, and the
audio duration / cost are printed when the gateway reports them.
Two backends are built in:
--gateway |
Endpoint | Key |
|---|---|---|
openrouter (default) |
POST https://openrouter.ai/api/v1/audio/transcriptions |
OPENROUTER_API_KEY |
vercel |
POST https://ai-gateway.vercel.sh/v4/ai/transcription-model |
AI_GATEWAY_API_KEY |
Setup #
This repo is pinned with pixi (pixi.toml provides Python, ffmpeg and
requests), so no manual install is needed:
pixi run transcribe meeting.mp3
Plain Python works too:
python -m pip install -r requirements.txt
Then put a key in .env (see .env.example) or export one:
export OPENROUTER_API_KEY=sk-or-v1-...
# or
export AI_GATEWAY_API_KEY=...
ffmpeg is optional. It is only used to split files above 20 MB.
Usage #
Prefix commands with pixi run transcribe (or pixi run python transcribe.py)
if you use the pixi environment.
python transcribe.py meeting.mp3 # OpenRouter, timestamped + cost
python transcribe.py meeting.mp3 --gateway vercel # Vercel AI Gateway
python transcribe.py meeting.mp3 --no-timestamps # plain text only
python transcribe.py meeting.mp3 -o notes.txt
python transcribe.py talk.m4a -m openai/gpt-4o-mini-transcribe -l en
python transcribe.py *.mp3 # one .txt per file
python transcribe.py meeting.mp3 -o - > meeting.txt # transcript on stdout
| Flag | Meaning |
|---|---|
-g, --gateway |
openrouter (default) or vercel; env TRANSCRIBE_GATEWAY |
-m, --model |
Model slug; env TRANSCRIBE_MODEL; default is per gateway |
-l, --language |
ISO-639-1 code (en, ja, ...). Auto-detected if omitted |
-o, --output |
Output path, or - for stdout (single input only) |
--no-timestamps |
Plain text, without [start --> end] segment prefixes |
--no-split |
Do not split large files with ffmpeg |
--force-split |
Split even small files (long recordings that would time out) |
--chunk-seconds |
Chunk length used when splitting (default 600) |
--max-inline-mb |
Split above this size instead of the default 20 MB |
--no-usage |
Do not print audio duration and cost |
Default model: microsoft/mai-transcribe-2 on both gateways.
Exit code is non-zero if any file fails.
How the backends differ #
| OpenRouter | Vercel AI Gateway | |
|---|---|---|
| Model location | body model |
header ai-model-id |
| Audio | input_audio: {data, format: "mp3"} |
{audio, mediaType: "audio/mpeg"} |
| Timestamps | requested via verbose_json + timestamp_granularities |
no request knob; segments only if the provider returns them |
| Language | top-level language |
not supported over REST (-l is ignored with a warning) |
| Duration | usage.seconds |
durationInSeconds |
| Cost | usage.cost in USD |
not returned → prints cost n/a |
Both accept vendor/model slugs, so most model names work on either side.
Notes #
- 20 MB is the practical inline limit on OpenRouter; Vercel does not document
one. Bigger files are split with ffmpeg into mono 64 kbps mp3 chunks and
transcribed one by one, then joined. The mp3 re-encode keeps every chunk
independently decodable (a
-c copysplit can produce broken m4a/mp4 pieces). Use--max-inline-mbif a gateway rejects the default. - Long single requests can hit an upstream ~60 s timeout. For long recordings
that are still under the limit, use
--force-split --chunk-seconds 300. - On Vercel, cost is only visible in the gateway dashboard/observability, not in
the response. Some providers return no
segments, in which case the output falls back to plain text even with timestamps enabled. - Microsoft transcription models on Vercel accept WAV, MP3, FLAC, and Ogg/Opus, but not WebM or MP4.