Metadata-Version: 2.5
Name: suba
Version: 0.4.1
Summary: Fix out-of-sync subtitles automatically: Whisper text matching, cross-language speech-timing alignment, and PAL/NTSC drift correction
Project-URL: Homepage, https://github.com/xy3/suba
Project-URL: Issues, https://github.com/xy3/suba/issues
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: alignment,ffmpeg,srt,subtitles,sync,vtt,whisper
Classifier: Environment :: Console
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Multimedia :: Video
Requires-Python: >=3.12
Requires-Dist: numpy
Requires-Dist: webrtcvad-wheels
Provides-Extra: whisper
Requires-Dist: openai-whisper; extra == 'whisper'
Description-Content-Type: text/markdown

# suba

[![PyPI](https://img.shields.io/pypi/v/suba)](https://pypi.org/project/suba/) [![Tests](https://github.com/xy3/suba/actions/workflows/test.yml/badge.svg)](https://github.com/xy3/suba/actions/workflows/test.yml) [![License](https://img.shields.io/github/license/xy3/suba)](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](https://raw.githubusercontent.com/xy3/suba/main/docs/demo.gif)

## Install

You need `ffmpeg` and [`uv`](https://docs.astral.sh/uv/). Then:

```bash
uv tool install suba --with openai-whisper
```

Or with pip:

```bash
pip install suba openai-whisper
```

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

Whisper is optional. Without it (`pip install suba`, no PyTorch), suba aligns
every subtitle on speech timing alone; with it, same-language subtitles also
get checked word by word.

## Use

```bash
suba subs.srt movie.mkv -o fixed.srt
```

```bash
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` | Skip the final sharpening against the film's voice track |
| `--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:

```bash
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.

Both detectors are coarse: they place speech to within a few
tenths of a second, not a few hundredths. So once suba has its answer — from
text or from timing — it takes one last step with a sharp detector: the
[WebRTC voice detector](https://github.com/wiseman/py-webrtcvad), the one
ffsubsync uses, run over the whole film. It may only move the answer by up to
two seconds:

```
  matched on speech modulation (40% of frames look like speech)
  sharpened on voice activity: +7.550 s → +7.400 s
```

The order matters. Searching a film with a sharp voice detector alone is what
fails under a loud score — it starts hearing the orchestra. Used only to nudge
an answer already found, it brings the precision without the risk. Over the
whole film it also settles which frame-rate conversion fits, where five
minutes of dialogue can't tell 25→24 fps from 25→23.976 fps. Pass
`--no-refine` to skip it.

## How it compares

[ffsubsync](https://github.com/smacke/ffsubsync) is the established tool, and a
good one. Both were run with default settings, first on
[Tears of Steel](https://mango.blender.org/) 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.06 s | 100% · 0.06 s |
| en -4.1 s | 0% · 4.10 s | 100% · 0.06 s | 100% · 0.06 s |
| de +7.3 s | 0% · 7.30 s | 100% · 0.06 s | 100% · 0.06 s |
| it -4.1 s | 0% · 4.10 s | 100% · 0.06 s | 100% · 0.06 s |
| fr +12.0 s | 0% · 12.00 s | 100% · 0.06 s | 100% · 0.06 s |
| ru +7.3 s | 0% · 7.30 s | 100% · 0.06 s | 100% · 0.06 s |
| ja +7.3 s | 0% · 7.30 s | 100% · 0.06 s | 100% · 0.06 s |
| en PAL drift | 7% · 12.09 s | 100% · 0.06 s | 100% · 0.06 s |
| de PAL drift | 7% · 12.09 s | 100% · 0.06 s | 100% · 0.06 s |
| es PAL drift +5 s | 3% · 8.09 s | 100% · 0.06 s | 100% · 0.06 s |
| en +95 s | 0% · 95.00 s | 100% · 0.06 s | 0% · 139.41 s |
| de +95 s | 0% · 95.00 s | 100% · 0.06 s | 0% · 139.41 s |
| en +150 s | 0% · 150.00 s | 100% · 0.06 s | 0% · 179.32 s |
| it +150 s | 0% · 150.00 s | 100% · 0.06 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.07 s | 100% · 0.08 s |
| en +7.3 s, louder score | 0% · 7.30 s | 100% · 0.04 s | 0% · 39.05 s |
| de +7.3 s, louder score | 0% · 7.30 s | 100% · 0.04 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 14 s, ffsubsync 7 s.

### Two full-length films

A 12-minute short proves little, so the same test was run on two features:
[Charade](https://commons.wikimedia.org/wiki/File:Charade_(1963).webm)
(1963, public domain), with volunteer subtitle tracks timed to that exact file,
and [Sita Sings the Blues](https://archive.org/details/Sita_Sings_the_Blues)
(2008, CC-BY-SA), an animated musical whose dialogue is largely 1920s jazz
recordings, with its community Portuguese translation. On the untouched tracks
both tools agreed with the stated timing to within 0.3 s.

| Case | Before | suba | ffsubsync |
|------|--------|------|-----------|
| Charade, en +7.3 s | 0% · 7.30 s | 100% · 0.29 s | 100% · 0.29 s |
| Charade, en PAL drift | 0% · 146.59 s | 100% · 0.30 s | 100% · 0.29 s |
| Charade, en +95 s | 0% · 95.00 s | 100% · 0.29 s | 0% · 37.42 s |
| Charade, en +150 s | 0% · 150.00 s | 100% · 0.29 s | 0% · 67.27 s |
| Charade, es +7.3 s | 0% · 7.30 s | 100% · 0.24 s | 100% · 0.25 s |
| Charade, es PAL drift | 0% · 143.48 s | 100% · 0.24 s | 100% · 0.25 s |
| Charade, es +95 s | 0% · 95.00 s | 100% · 0.24 s | 0% · 36.64 s |
| Charade, es +150 s | 0% · 150.00 s | 100% · 0.24 s | 0% · 64.06 s |
| Charade, fr +7.3 s | 0% · 7.30 s | 100% · 0.10 s | 100% · 0.10 s |
| Charade, fr PAL drift | 0% · 145.42 s | 100% · 0.10 s | 100% · 0.11 s |
| Charade, fr +95 s | 0% · 95.00 s | 100% · 0.10 s | 0% · 37.04 s |
| Charade, fr +150 s | 0% · 150.00 s | 100% · 0.10 s | 1% · 66.69 s |
| Sita, pt +7.3 s | 0% · 7.30 s | 100% · 0.04 s | 100% · 0.04 s |
| Sita, pt PAL drift | 0% · 77.86 s | 100% · 0.05 s | 100% · 0.05 s |
| Sita, pt +95 s | 0% · 95.00 s | 100% · 0.04 s | 1% · 34.51 s |
| Night of the Living Dead's subtitles on Charade, en | — | refused | **wrote a wrong file** |
| Night of the Living Dead's subtitles on Charade, as es | — | refused | **wrote a wrong file** |

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

Reading both tables honestly:

- **Ordinary shifts, other languages, PAL drift:** a tie. Every line lands
  within half a second either way, and the median errors agree to within
  0.01 s. Where both tools say 0.25–0.3 s on Charade, the volunteer track
  itself is probably that far off. On a short film ffsubsync is about twice as
  fast; on a feature the two take about a minute each.
- **Offsets over a minute** — a different intro, a recap, a studio logo: suba
  fixed all eleven such cases across three films; ffsubsync left at most 1% of
  lines right in each, usually further off than before. 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 finds the offset by the syllable rhythm of speech rather than loudness,
  and stays exact. (Whisper hears little under that score, so on some runs the
  text match can't decide and suba falls back to timing alignment.)
- **Subtitles for a different video**, including another feature's real
  subtitles: ffsubsync always writes a file. suba refused all five — 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 them with `bench/fetch.sh && python bench/run.py` and
`bench/fetch_features.sh && python bench/features.py`.

## A whole library

```bash
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

```bash
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.

### Bazarr in Docker

Bazarr's container doesn't include suba, but it's a one-line addition. The
[linuxserver.io image](https://docs.linuxserver.io/images/docker-bazarr/)
already has everything suba needs (Python, numpy, ffmpeg and the voice
detector, since ffsubsync uses them too), so add to its `environment:`

```yaml
- DOCKER_MODS=linuxserver/mods:universal-package-install
- INSTALL_PIP_PACKAGES=suba
```

and restart. suba is installed at every start, and the post-processing command
above works as-is.

Inside the container, suba runs without Whisper: it aligns on speech timing,
which needs no transcription. On the benchmark that gives the same results —
every same-language case fixed at the same precision, every wrong-film
subtitle refused. Whisper adds a second check on the words; it needs PyTorch,
which is too heavy for that image.

## 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 whole film's voice track, 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](#different-languages) — but the last one doesn't.
Your subtitle text is never rewritten, only re-timed.


## Development note

Much of suba's recent development — the cross-language alignment, the
benchmark and the library mode — was done with the help of an AI coding
agent, reviewed and released by the maintainer.

**🌟 Star this repository if you find it helpful!**
