Skip to main content

suba

PyPI Tests License

Fix subtitle sync with Whisper, because h and g are for cowards.


You downloaded subtitles. The first line pops up during the studio logo. By the third act, characters are reacting to things that haven't happened yet. You've spent 20 minutes in VLC tapping keys and you're 90% sure you made it worse.

Suba rips a minute of audio from a dialogue-heavy part of your video, transcribes it with Whisper, matches the transcription against the subtitle file, and tells you the offset. Then it writes out a corrected SRT so you can stop thinking about this.

suba fixing German subtitles that drift against an English film

Install

You need ffmpeg and uv. Then:

uv tool install suba --with openai-whisper

Or with pip:

pip install suba openai-whisper

That's it. One command — no clone, no build step.

Use

suba subs.srt movie.mkv -o fixed.srt
suba <subtitles> <video> -o <output.srt|vtt>
Flag What
-o, --output Write corrected SRT/VTT
-m, --model Whisper model (base, small, medium, large)
-d, --duration Audio seconds to transcribe for text matching (default 60)
-t, --start Manual start time in seconds, for text matching
--threshold Minimum similarity to accept a match
-A, --audio-lang Language spoken in the video (en, english, eng)
-S, --sub-lang Language of the subtitle file (it, italian, ita)
--align Force a strategy: auto, text, translate, timing
--max-shift Largest offset to search for in timing mode (default 300 s)
--min-peak-ratio How clearly the winning offset must beat the runner-up (default 1.4)
--no-refine Don't sharpen a text match against the waveform
--no-rescale Don't test for PAL/NTSC frame-rate conversions, only a shift
--min-coverage Reject subtitles reaching less than this much of the runtime (default 0.7)

Different languages

English movie, Italian subtitles? Tell it so:

suba movie.ita.srt movie.mkv -A en -S it -o fixed.srt

You usually don't have to. Suba reads the language off the filename (movie.ita.srt), falls back to sniffing the subtitle text itself, and asks Whisper what the audio is. The flags are there for when it guesses wrong.

Depending on the pair, it picks one of three strategies:

Audio Subtitles Strategy How
en en text Transcribe and compare the words
it en translate Whisper translates the audio to English, then compare the words
en it timing Compare when people speak, ignoring what they say

The awkward case is the third one, because Whisper only translates into English — it can't produce Italian to match against. So suba stops reading and starts listening to the rhythm: it measures the energy of the waveform to find every stretch where somebody is talking, marks every stretch where a subtitle is on screen, and slides one over the other until the talking lines up with the talking and the silence lines up with the silence. Dialogue has a fingerprint, and translation doesn't change it.

Three things worth knowing about timing mode:

  • It reads the whole film, not a sample. A five-minute window is enough for clean audio, but a scored feature film needs everything: the pauses that carry the signal are rarer and noisier, and a short window reads as noise. On a 3h53m movie that costs about 30 seconds end to end, most of it ffmpeg.
  • It would rather say nothing than lie. A correct alignment beats every other candidate offset by a wide margin — measured at 5-8x on real films, against 1.01x for a subtitle file belonging to a different cut. When the best offset only edges out the runner-up, suba refuses instead of handing you a confidently wrong answer. --min-peak-ratio is the control that matters here; --threshold barely discriminates in this mode.
  • It's fast, because it never transcribes. Pass both languages and Whisper is skipped entirely.

Subtitles that drift

Some subtitles don't just start late — they run at the wrong speed. A track ripped from a PAL disc is timed for 25 fps, and against a 23.976 fps video it gains about four seconds every hundred. It starts nearly right and ends minutes out, and no single offset can fix it.

Suba tests the standard conversions alongside the plain shift and reports one if it fits better:

matched on speech modulation
frame-rate conversion detected: 25 → 23.976 fps (PAL)
SYNC OFFSET: +1.200 s

That's a real Italian track against a 2h30m film. Before: 33% of its lines landed within a second of the right moment, and the worst were fifteen seconds out. After: 99.6%, with the worst at 0.4s. Pass --no-rescale to only ever shift.

Half a subtitle file is not a subtitle file

Subtitle sites still carry tracks split across two discs, and a CD1 file looks exactly like a normal one until you notice its last line lands 90 minutes into a four-hour film. Suba can align that perfectly well — on the half it has — and hand you a file that dies at the intermission.

So it checks first and refuses:

error: these subtitles run out at 01:39:38.435, only 43% of the 03:53:15.014 film.
       That is usually one disc of a multi-disc rip. Syncing cannot fill in the
       missing part — find a complete file, or pass --min-coverage 0 to align
       the part that is there anyway.

The check runs before any audio is read, so it costs nothing. Ending at 90% is normal — that's end credits — and only gets a passing mention.

Finding the talking

Timing alignment lives or dies on knowing which parts of the audio are speech, and there are two ways to guess, neither of which wins everywhere:

  • Loudness in the speech band. Near-perfect on clean audio with real silence between lines.
  • Modulation — how much the speech-band energy wobbles around its own local average. Speech pulses at the syllable rate; an orchestra doesn't.

On a scored action film, loudness is worse than useless: the loudest frames are music and effects, so the mask tracks the score and anti-correlates with the dialogue. Measured on one, every candidate offset scored negative — there was no alignment to find at all. Modulation took the same film from -0.09 to +0.52.

But modulation smooths, and smoothing costs precision: on clean audio loudness scores 0.996 where modulation manages 0.69. So suba computes both, and picks whichever correlates better against your subtitles. It says which one won.

It's also precise, and that precision is now lent to the other two strategies. Matching text tells you which line is playing, but only as accurately as the Whisper segment it landed in — and those can run ten seconds long. So after a text or translate match, suba shifts the subtitles by its estimate and does a short correlation search to recover the leftover error:

Match similarity:  0.875
Refined on waveform: -0.314 s → -4.264 s (correlation 0.988)

That example is a real run against Italian audio with English subtitles, where the true offset was -4.250 s. Pass --no-refine to turn it off.

How it compares

ffsubsync is the established tool, and a good one. Both were run with default settings on Tears of Steel and its official subtitle tracks, broken in known ways so the right answer is exact:

Case Before suba ffsubsync
en +7.3 s 0% · 7.30 s 100% · 0.05 s 100% · 0.06 s
en -4.1 s 0% · 4.10 s 100% · 0.05 s 100% · 0.06 s
de +7.3 s 0% · 7.30 s 100% · 0.10 s 100% · 0.06 s
it -4.1 s 0% · 4.10 s 100% · 0.10 s 100% · 0.06 s
fr +12.0 s 0% · 12.00 s 100% · 0.10 s 100% · 0.06 s
ru +7.3 s 0% · 7.30 s 100% · 0.10 s 100% · 0.06 s
ja +7.3 s 0% · 7.30 s 100% · 0.10 s 100% · 0.06 s
en PAL drift 7% · 12.09 s 100% · 0.03 s 100% · 0.06 s
de PAL drift 7% · 12.09 s 100% · 0.09 s 100% · 0.06 s
es PAL drift +5 s 3% · 8.09 s 100% · 0.11 s 100% · 0.06 s
en +95 s 0% · 95.00 s 100% · 0.15 s 0% · 139.41 s
de +95 s 0% · 95.00 s 100% · 0.10 s 0% · 139.41 s
en +150 s 0% · 150.00 s 100% · 0.15 s 0% · 179.32 s
it +150 s 0% · 150.00 s 100% · 0.10 s 0% · 179.43 s
en +7.3 s, loud score 0% · 7.30 s 100% · 0.07 s 100% · 0.08 s
de +7.3 s, loud score 0% · 7.30 s 100% · 0.10 s 100% · 0.08 s
en +7.3 s, louder score 0% · 7.30 s 100% · 0.07 s 0% · 39.05 s
de +7.3 s, louder score 0% · 7.30 s 100% · 0.10 s 0% · 39.05 s
Sintel's subtitles (another film), en — refused wrote a wrong file
Sintel's subtitles (another film), as de — refused wrote a wrong file
made-up dialogue track, en — refused wrote a wrong file
first 43% of the track (CD1), en +7.3 s 0% · 7.30 s refused 100% · 0.09 s

Cells: share of lines within 0.5 s of the official timing · median error. Median run time: suba 13 s, ffsubsync 7 s.

Reading it honestly:

  • Ordinary shifts, other languages, PAL drift: a tie. Both land within a tenth of a second, below the precision of the official tracks themselves. ffsubsync is about twice as fast.
  • Offsets over a minute — a different intro, a recap, a studio logo: suba fixes them; ffsubsync makes the file worse. Raising its --max-offset-seconds to 300 didn't change that.
  • Music louder than the dialogue: both cope with a loud score. Louder still, ffsubsync's voice detection follows the music and lands 39 s out; suba's detector looks for the syllable rhythm of speech rather than loudness, and stays exact.
  • Subtitles for a different video: ffsubsync always writes a file. suba refuses — which in a library run over hundreds of files is the difference between a visible failure and silent damage.
  • Half a subtitle file: ffsubsync syncs the half it has. suba refuses by design, since the result dies mid-film; --min-coverage 0 makes it align the half too.

Reproduce it with bench/fetch.sh && python bench/run.py.

A whole library

suba batch /media/movies

Finds every subtitle file sitting next to its video (Film (2016).en.srt beside Film (2016).mkv, the way Jellyfin, Plex and Kodi name them), syncs each one in place, and keeps the original as Film (2016).en.srt.unsynced. Files that already have a backup are skipped, so running it again — from cron, say — only touches what's new. A file suba isn't sure about is left exactly as it was. --dry-run lists the pairs; any other flag is passed through to suba.

Docker

docker run --rm -v /media/movies:/media -v suba-cache:/cache \
  ghcr.io/xy3/suba batch /media

ffmpeg, Whisper and a CPU-only PyTorch are inside. The cache volume keeps Whisper's model between runs.

Bazarr

Bazarr can run suba on every subtitle it downloads. In Settings → Subtitles → Custom Post-Processing, set the command to:

suba "{{subtitles}}" "{{episode}}" -A {{episode_language_code2}} -S {{subtitles_language_code2}} -o "{{subtitles}}"

Turn off Bazarr's own sync (it runs ffsubsync first) so suba starts from the downloaded file. When suba refuses, the subtitle is left untouched.

How it works

  • Finds the densest 60 seconds of subtitles (more lines = more talking, probably)
  • Extracts that audio with ffmpeg
  • Transcribes it with Whisper
  • Matches every distinctive subtitle line against what Whisper heard, and only trusts an offset that at least three lines agree on — under a plain shift or a PAL/NTSC speed conversion
  • Sharpens that offset against the waveform, then shifts (and if needed rescales) every timestamp and writes a fixed SRT

When the subtitles aren't in the language being spoken, the middle steps change — see Different languages — but the last one doesn't. Your subtitle text is never rewritten, only re-timed.

🌟 Star this repository if you find it helpful!

Metadata

Release files for suba 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for suba 0.3.1
File Size Uploaded Provenance Actions
suba-0.3.1.tar.gz 27.3 kB Without provenance Details for suba-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for suba 0.3.1
File Interpreter ABI Platform Actions
suba-0.3.1-py3-none-any.whl Python 3 none any Details for suba-0.3.1-py3-none-any.whl

Total release size: 55.8 kB

Release files / suba-0.3.1.tar.gz

Download URL suba-0.3.1.tar.gz
Size 27.3 kB
Tags Source
SHA-256 checksum
How to use checksums
39f278f104a91f25d3dc4f0ad8622eb3ccc0b463b593f939935fd263ed13005e
BLAKE2b-256 checksum
How to use checksums
e1258490568e69c21e0b613d10d68660dd7fdbe48fa5b099d161cdd18091c296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / suba-0.3.1-py3-none-any.whl

Download URL suba-0.3.1-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edd45712b47a75adb3a5d9393fbe534abe6da6ce34e9badb53da0c7654e1f7d2
BLAKE2b-256 checksum
How to use checksums
7e120807afbbbc26c9c3007ab6a197216c56edcb8c740cc7dc33a9ddc4b38db0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for suba 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page