when subtitles disagree, vote again.
README.md

requor #

requor — when subtitles disagree, vote again.

re·quor — v.t. — to run the quorum again; to resync subtitles by letting every cue's neighbourhood vote on the local timing offset.

Cut-tolerant subtitle resynchronization. Fixes subtitles whose offset to the video steps during the episode (different cut, recap added/removed, broadcast edits) — cases where global-shift tools and audio-based syncers (alass, ffsubsync) give up or lock onto the wrong alignment.

The name is the method (re + quorum): every subtitle cue's neighbourhood votes on the local timing offset, and the dominant offset — the quorum — wins. Wrong pairings scatter their votes; the true alignment lands nearly every one. When subtitles disagree, vote again.

How it works #

  1. Local-voting time matching — instead of pairing each cue with the nearest reference cue (ambiguous when the offset is a large fraction of the cue spacing), every cue's neighbourhood votes on the local offset. The true offset collects nearly every vote; accidental pairings scatter.
  2. Cut-aware rolling median — the correction follows step changes cleanly instead of ramping across them, and mismatched/outlier lines are rejected.
  3. Self-tuning — multiple drift windows are tried; the most self-consistent alignment wins (finds 45s recap shifts unaided).

Install (editable, for development) #

python3 -m venv .venv && source .venv/bin/activate
pip install -e .

Usage #

The easy way — point at the video itself. requor extracts every embedded text track, scores each against your subtitle, and syncs:

requor --video episode.mkv broken.pt.srt fixed.pt.srt

How the reference is chosen and used, smartest first:

  1. Text twin — when one of the embedded tracks carries the same translation as your file (common: your own language track bundled in the container), cues are paired by identical text. Every paired cue then carries its own measured offset, so ANY offset structure comes out exact — including several multi-second steps in quick succession and 25s scene displacements that defeat every time-matching approach. A twin must pair MOST cues (~98% on real files): a different translation still shares short lines ("Não.", names) — about 9% on a real pt-PT vs pt-BR pair — and that coincidence rate must not be trusted.
  2. Time voting — no text twin (your file is a different language than every embedded track): the local-voting matcher syncs against the track with the strongest time-match quorum.
  3. Audio calibration — embedded track groups are routinely mistimed by a round constant (measured on a real file: 25 of 26 tracks exactly 1s early). After matching, requor measures every track's lead over detected speech onsets and re-times the result onto the best-synced one — a single constant; the matched structure is never re-scrambled. No usable embedded track at all: audio-only sync (--audio-only forces it, --no-audio disables all audio use). By default requor uses Silero VAD, a small neural voice detector that ignores music and effects — the ~0.6MB model ships inside the package. It measures alignment by cross-correlation and applies a drift model, so it catches what no shift-only tool can: subtitles timed for a different frame rate (a ~0.1% mismatch drifts seconds over an episode), plus a bounded per-cue snap that fixes per-line jitter. --no-ml reverts to plain energy-based silence detection for environments where onnxruntime misbehaves.

Automation mode: --lang and --json #

The --video pipeline is built to run unattended, where nobody can check the result by eye. Language tags are routinely missing, so requor identifies every embedded track from its text — including European vs Brazilian Portuguese (está a fazer vs está fazendo, tu/você, facto/equipa/comboio vs ônibus/celular/time). With

requor --video episode.mkv --lang pt-PT --json report.json broken.pt.srt fixed.pt.srt

the tool applies a strict policy:

  • An embedded track confidently in the requested variety and verified in sync against the audio is exported as the output, overwriting the downloaded subtitle's role. Verification is measured by per-block correlation anchors — a pt-PT track that turns out to sit 1s early is refused, not trusted.
  • pt-BR is never exported for a pt-PT request, and a confidently pt-BR input is rejected outright (exit 2, nothing written) — no subtitle at all beats a wrong-variety one. A track whose variety cannot be determined is never exported either.
  • An input that is already in sync (correlation-verified against the voice onsets) is written through unchanged — syncing an already-synced file is a no-op, not a re-match onto track noise.
  • The result is verified at the end: exit code 0 = written and verified in sync, 1 = written but NOT verified (check by eye), 2 = nothing written (no alignment possible, or language refused).

--json FILE writes the full machine-readable report: every track (tag + detected language + measured lead), the input's detected language, each pass with its timing segments, and the verification result. --json - prints it to stdout instead (with --quiet the output is pure JSON) — a background pipeline can log it rather than accumulate files, or skip --json entirely and rely on the exit code.

You need ffmpeg/ffprobe on PATH. Inspect what a file contains (identified languages included):

requor --video episode.mkv --list-tracks
requor --video episode.mkv --track 1 broken.pt.srt fixed.pt.srt
requor --video episode.mkv --lang pt-PT broken.pt.srt fixed.pt.srt

Image-based tracks (PGS, VobSub) are listed but skipped — they hold no timed text without OCR.

Or do it by hand — extract a reference subtitle yourself, then resync:

# reference = embedded track of the SAME video (guaranteed same cut)
ffmpeg -i episode.mkv -map 0:s:0 embedded.srt
requor embedded.srt broken.pt.srt fixed.pt.srt

Or without installing:

python3 -m requor.cli --video episode.mkv broken.pt.srt fixed.pt.srt

Options: --max-drift SECONDS (default 30), --window CUES (default 15), --shift-only SECONDS (constant shift, no matching), --quiet; with --video: --track N (which embedded track, 0-based as in 0:s:N), --lang LANG (the language you want, e.g. pt-PT — enables the strict variety/export policy above), --json FILE (machine-readable report), --list-tracks, --audio-only, --no-audio, --no-ml (revert to silence detection), --silence-db DB.

The diagnostic report prints each timing segment found and its shift, so you can sanity-check where the cuts were detected. If matching fails outright you'll see: requor: no quorum reached (the quorum is the story, the command is requor).

Tests #

pip install pytest
pytest -q

The test suite generates synthetic "episodes" (step offsets at cuts, merged lines, hearing-impaired noise cues, recap-sized shifts, CRLF/dot-millis files) and asserts the correction lands within tolerance.

Roadmap ideas #

  • --semantic: LaBSE multilingual embeddings for meaning-based matching
  • batch mode over a library directory
  • --dry-run: the JSON report without writing the output