Sub-align
Align .srt / .lrc / .txt subtitles (or generate them) to audio/video with
WhisperX forced alignment — so each cue can
move independently instead of only applying one global timeline shift. Optional
--diarization adds pyannote speaker labels ([SPEAKER_00]: …) to cues.
Why not only a global offset?
Tools like ffsubsync typically find a constant offset (or stretch) between speech activity and subtitle “on” times. That works well for whole-track drift, but leading/trailing silence or local timing errors can still leave lines early or late.
sub-align picks a strategy from the input type, then runs WhisperX
phoneme / word-level forced alignment so each line is refined against the
audio:
| Input | Strategy |
|---|---|
| Media only | Whisper ASR → word-align → split into timed cues |
.txt script |
ASR only for search windows → forced-align original lines |
.srt / .lrc |
Optional global offset → expand windows by --margin → forced-align |
Limitations: subtitle text must roughly match spoken content (no
translation). Refine can still nudge already-good cues; very short lines may be
merged with close neighbors (large pauses stay alone); VAD only blocks refine
starts pulled earlier into silence. Pass --diarization to add speaker labels
(opt-in). See docs/pipeline.md § Limitations.
More detail: docs/pipeline.md · scenarios & flags: docs/usage.md · speaker diarization: docs/diarization.md
Install
Requires Python 3.10+ and ffmpeg on PATH. First run
downloads WhisperX alignment models (disk/RAM).
pip install 'sub-align[align]'
# or
uv pip install 'sub-align[align]'
Extras [align], [cpu], and [gpu] all install WhisperX. Install a matching
PyTorch build first when you need a specific CPU/CUDA wheel:
# CPU
uv pip install torch --index-url https://download.pytorch.org/whl/cpu
uv pip install 'sub-align[cpu]'
# CUDA (example: cu124)
uv pip install torch --index-url https://download.pytorch.org/whl/cu124
uv pip install 'sub-align[gpu]'
Development
uv venv
uv sync --group dev # unit tests / lint (no WhisperX)
uv sync --group dev --extra align # full local alignment
uv run pytest
uv run ruff check src tests
CI runs the same unit tests without the align extra. Path A/B/C coverage
also uses checked-in fixtures under tests/fixtures/ (timed WAVs + recorded
WhisperX ASR/align JSON), replayed via mocks in tests/test_fixture_replay.py.
To regenerate those fixtures locally (needs ffmpeg; recording needs WhisperX):
# 1) Timed TTS clips (clip 1 = main coverage, clip 2 = dash dialogue)
uv run --with edge-tts --with numpy --with soundfile \
scripts/generate_clip1_tts.py --clip 1
uv run --with edge-tts --with numpy --with soundfile \
scripts/generate_clip1_tts.py --clip 2
# 2) Record ASR + word-align JSON for replay tests
uv run --extra align scripts/record_whisperx_fixtures.py \
tests/fixtures/clip1_timed.wav tests/fixtures/clip2_timed.wav
Companion subtitle inputs (clip1_script.txt, clip1_drift.srt,
clip2_dialogue.srt) live beside the WAVs; edit those if the cue sheet changes.
Usage
# Timed subtitles: auto global offset + per-cue refine
sub-align media.mp4 subs.srt --language zh -o out.srt
# Lyrics (LRC): same strategy as SRT
sub-align audio.wav lyrics.lrc --language en --margin 1.0
# Untimed script: ASR windows, then force-align original lines
sub-align media.mkv script.txt --language en --model small
# Skip podcast intro/outro before aligning a script
sub-align media.mp3 script.txt --language en --trim-start 13 --trim-end 5
# Known whole-track shift (skips auto-offset ASR)
sub-align media.mkv subs.srt --language en --offset 12.5
# Audio only: transcribe + word-align into an SRT
sub-align lecture.mp4 --language en -o lecture.asr.srt
# Speaker diarization (first Hub run: add --hf-token; see docs/diarization.md)
sub-align media.mp4 subs.srt --language en --diarization --skip-force-align
# Non-default or local pipeline dir only (--diarization-model); token if needed
sub-align media.mp4 subs.srt --language en --diarization \
--diarization-model /path/to/pyannote/model
Always pass --language (e.g. en, zh) or --detect-language.
See docs/usage.md for when to use --model, --margin,
--offset, --fill-gaps, --trim-*, audio-only line limits, and a Whisper
model size / VRAM cheat sheet. Speaker diarization setup (Hugging Face token,
gated models): docs/diarization.md.
Python API
from sub_align import align_file
align_file(
media="a.mp4",
subtitle="a.srt", # omit for audio-only transcription
output="a.aligned.srt",
language="zh",
device="auto",
)
How it works (short)
- Load media as 16 kHz mono audio (via WhisperX / ffmpeg); optional
--trim-start/--trim-end. - Resolve language (
--languageor tiny-model detection). - Build search windows by input type (ASR token match for
.txt; offset + margin refine for.srt/.lrc; full ASR for media-only). - Run WhisperX forced alignment; remap word times onto original cues; trim
overlaps; optional
--fill-gaps; write.srtor.lrc. - Optional
--diarization— pyannote speaker labels by cue overlap (see docs/diarization.md).
Full diagram and tech notes: docs/pipeline.md.
Build and publish
uv build
uv publish # requires PyPI credentials / UV_PUBLISH_TOKEN
GitHub Actions publishes on tags matching v* (see .github/workflows/publish.yml).
Configure Trusted Publishing on PyPI or repository secret UV_PUBLISH_TOKEN.
License
MIT
Metadata
Release files for sub-align 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sub_align-0.3.0.tar.gz | 589.0 kB | Without provenance | Details for sub_align-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sub_align-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for sub_align-0.3.0-py3-none-any.whl |
Total release size: 622.9 kB
Release files / sub_align-0.3.0.tar.gz
| Download URL | sub_align-0.3.0.tar.gz |
|---|---|
| Size | 589.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0670c6cce933291121cb4262d2f12cf20d644ef92d7b5ed044b71d66ad46f685
|
|
BLAKE2b-256 checksum How to use checksums |
f86993b13f4e6cea98bf4fd42125b708c64e06cdc8f6a27d26085fcb7d9b13e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / sub_align-0.3.0-py3-none-any.whl
| Download URL | sub_align-0.3.0-py3-none-any.whl |
|---|---|
| Size | 33.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fddd8b91eacb6c20226cbf5ab54a1e8280fe3b2faef0120416e9c3de3a0d4195
|
|
BLAKE2b-256 checksum How to use checksums |
c0e975fbaf51adea155686a09bec2c822751e7fec97a15795990148fddb1f59e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for sub-align 0.3.0
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.