suba 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
suba-0.2.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
suba-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,202 @@
1
+ Metadata-Version: 2.5
2
+ Name: suba
3
+ Version: 0.2.0
4
+ Summary: Fix out-of-sync subtitles automatically: Whisper text matching, cross-language speech-timing alignment, and PAL/NTSC drift correction
5
+ Project-URL: Homepage, https://github.com/xy3/suba
6
+ Project-URL: Issues, https://github.com/xy3/suba/issues
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Keywords: alignment,ffmpeg,srt,subtitles,sync,vtt,whisper
10
+ Classifier: Environment :: Console
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: Multimedia :: Video
13
+ Requires-Python: >=3.12
14
+ Requires-Dist: numpy
15
+ Provides-Extra: whisper
16
+ Requires-Dist: openai-whisper; extra == 'whisper'
17
+ Description-Content-Type: text/markdown
18
+
19
+ # suba
20
+
21
+ [![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)
22
+
23
+ > Fix subtitle sync with Whisper, because `h` and `g` are for cowards.
24
+
25
+ ---
26
+
27
+ 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.
28
+
29
+ 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.
30
+
31
+ ## Install
32
+
33
+ You need `ffmpeg` and [`uv`](https://docs.astral.sh/uv/). Then:
34
+
35
+ ```bash
36
+ uv tool install suba --with openai-whisper
37
+ ```
38
+
39
+ Or with pip:
40
+
41
+ ```bash
42
+ pip install suba openai-whisper
43
+ ```
44
+
45
+ That's it. One command — no clone, no build step.
46
+
47
+ ## Use
48
+
49
+ ```bash
50
+ suba subs.srt movie.mkv -o fixed.srt
51
+ ```
52
+
53
+ ```bash
54
+ suba <subtitles> <video> -o <output.srt|vtt>
55
+ ```
56
+
57
+ | Flag | What |
58
+ |------|------|
59
+ | `-o`, `--output` | Write corrected SRT/VTT |
60
+ | `-m`, `--model` | Whisper model (`base`, `small`, `medium`, `large`) |
61
+ | `-d`, `--duration` | Audio seconds to transcribe for text matching (default 60) |
62
+ | `-t`, `--start` | Manual start time in seconds, for text matching |
63
+ | `--threshold` | Minimum similarity to accept a match |
64
+ | `-A`, `--audio-lang` | Language spoken in the video (`en`, `english`, `eng`) |
65
+ | `-S`, `--sub-lang` | Language of the subtitle file (`it`, `italian`, `ita`) |
66
+ | `--align` | Force a strategy: `auto`, `text`, `translate`, `timing` |
67
+ | `--max-shift` | Largest offset to search for in timing mode (default 60 s) |
68
+ | `--min-peak-ratio` | How clearly the winning offset must beat the runner-up (default 1.4) |
69
+ | `--no-refine` | Don't sharpen a text match against the waveform |
70
+ | `--no-rescale` | Don't test for PAL/NTSC frame-rate conversions, only a shift |
71
+ | `--min-coverage` | Reject subtitles reaching less than this much of the runtime (default 0.7) |
72
+
73
+ ## Different languages
74
+
75
+ English movie, Italian subtitles? Tell it so:
76
+
77
+ ```bash
78
+ suba movie.ita.srt movie.mkv -A en -S it -o fixed.srt
79
+ ```
80
+
81
+ You usually don't have to. Suba reads the language off the filename
82
+ (`movie.ita.srt`), falls back to sniffing the subtitle text itself, and asks
83
+ Whisper what the audio is. The flags are there for when it guesses wrong.
84
+
85
+ Depending on the pair, it picks one of three strategies:
86
+
87
+ | Audio | Subtitles | Strategy | How |
88
+ |-------|-----------|----------|-----|
89
+ | en | en | `text` | Transcribe and compare the words |
90
+ | it | en | `translate` | Whisper translates the audio to English, then compare the words |
91
+ | en | it | `timing` | Compare *when* people speak, ignoring what they say |
92
+
93
+ The awkward case is the third one, because Whisper only translates **into**
94
+ English — it can't produce Italian to match against. So suba stops reading and
95
+ starts listening to the rhythm: it measures the energy of the waveform to find
96
+ every stretch where somebody is talking, marks every stretch where a subtitle is
97
+ on screen, and slides one over the other until the talking lines up with the
98
+ talking *and* the silence lines up with the silence. Dialogue has a fingerprint,
99
+ and translation doesn't change it.
100
+
101
+ Three things worth knowing about timing mode:
102
+
103
+ - **It reads the whole film**, not a sample. A five-minute window is enough for
104
+ clean audio, but a scored feature film needs everything: the pauses that carry
105
+ the signal are rarer and noisier, and a short window reads as noise. On a
106
+ 3h53m movie that costs about 30 seconds end to end, most of it ffmpeg.
107
+ - **It would rather say nothing than lie.** A correct alignment beats every
108
+ other candidate offset by a wide margin — measured at 5-8x on real films,
109
+ against 1.01x for a subtitle file belonging to a different cut. When the best
110
+ offset only edges out the runner-up, suba refuses instead of handing you a
111
+ confidently wrong answer. `--min-peak-ratio` is the control that matters here;
112
+ `--threshold` barely discriminates in this mode.
113
+ - **It's fast**, because it never transcribes. Pass both languages and Whisper
114
+ is skipped entirely.
115
+
116
+ ## Subtitles that drift
117
+
118
+ Some subtitles don't just start late — they run at the wrong *speed*. A track
119
+ ripped from a PAL disc is timed for 25 fps, and against a 23.976 fps video it
120
+ gains about four seconds every hundred. It starts nearly right and ends minutes
121
+ out, and no single offset can fix it.
122
+
123
+ Suba tests the standard conversions alongside the plain shift and reports one if
124
+ it fits better:
125
+
126
+ ```
127
+ matched on speech modulation
128
+ frame-rate conversion detected: 25 → 23.976 fps (PAL)
129
+ SYNC OFFSET: +1.200 s
130
+ ```
131
+
132
+ That's a real Italian track against a 2h30m film. Before: 33% of its lines
133
+ landed within a second of the right moment, and the worst were fifteen seconds
134
+ out. After: 99.6%, with the worst at 0.4s. Pass `--no-rescale` to only ever
135
+ shift.
136
+
137
+ ## Half a subtitle file is not a subtitle file
138
+
139
+ Subtitle sites still carry tracks split across two discs, and a `CD1` file looks
140
+ exactly like a normal one until you notice its last line lands 90 minutes into a
141
+ four-hour film. Suba can align that perfectly well — on the half it has — and
142
+ hand you a file that dies at the intermission.
143
+
144
+ So it checks first and refuses:
145
+
146
+ ```
147
+ error: these subtitles run out at 01:39:38.435, only 43% of the 03:53:15.014 film.
148
+ That is usually one disc of a multi-disc rip. Syncing cannot fill in the
149
+ missing part — find a complete file, or pass --min-coverage 0 to align
150
+ the part that is there anyway.
151
+ ```
152
+
153
+ The check runs before any audio is read, so it costs nothing. Ending at 90% is
154
+ normal — that's end credits — and only gets a passing mention.
155
+
156
+ ## Finding the talking
157
+
158
+ Timing alignment lives or dies on knowing which parts of the audio are speech,
159
+ and there are two ways to guess, neither of which wins everywhere:
160
+
161
+ - **Loudness** in the speech band. Near-perfect on clean audio with real silence
162
+ between lines.
163
+ - **Modulation** — how much the speech-band energy wobbles around its own local
164
+ average. Speech pulses at the syllable rate; an orchestra doesn't.
165
+
166
+ On a scored action film, loudness is worse than useless: the loudest frames are
167
+ music and effects, so the mask tracks the score and *anti*-correlates with the
168
+ dialogue. Measured on one, every candidate offset scored negative — there was no
169
+ alignment to find at all. Modulation took the same film from -0.09 to +0.52.
170
+
171
+ But modulation smooths, and smoothing costs precision: on clean audio loudness
172
+ scores 0.996 where modulation manages 0.69. So suba computes both, and picks
173
+ whichever correlates better against your subtitles. It says which one won.
174
+
175
+ It's also *precise*, and that precision is now lent to the other two strategies.
176
+ Matching text tells you which line is playing, but only as accurately as the
177
+ Whisper segment it landed in — and those can run ten seconds long. So after a
178
+ text or translate match, suba shifts the subtitles by its estimate and does a
179
+ short correlation search to recover the leftover error:
180
+
181
+ ```
182
+ Match similarity: 0.875
183
+ Refined on waveform: -0.314 s → -4.264 s (correlation 0.988)
184
+ ```
185
+
186
+ That example is a real run against Italian audio with English subtitles, where
187
+ the true offset was -4.250 s. Pass `--no-refine` to turn it off.
188
+
189
+ ## How it works
190
+
191
+ - Finds the densest 60 seconds of subtitles (more lines = more talking, probably)
192
+ - Extracts that audio with ffmpeg
193
+ - Transcribes it with Whisper
194
+ - Slides windows of the transcription over each candidate subtitle line, scoring by text similarity and timestamp proximity
195
+ - Shifts all timestamps by the detected offset and writes a fixed SRT
196
+
197
+ When the subtitles aren't in the language being spoken, the middle steps change
198
+ — see [Different languages](#different-languages) — but the last one doesn't.
199
+ Your subtitle text is never rewritten, only re-timed.
200
+
201
+
202
+ **🌟 Star this repository if you find it helpful!**
suba-0.2.0/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # suba
2
+
3
+ [![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)
4
+
5
+ > Fix subtitle sync with Whisper, because `h` and `g` are for cowards.
6
+
7
+ ---
8
+
9
+ 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.
10
+
11
+ 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.
12
+
13
+ ## Install
14
+
15
+ You need `ffmpeg` and [`uv`](https://docs.astral.sh/uv/). Then:
16
+
17
+ ```bash
18
+ uv tool install suba --with openai-whisper
19
+ ```
20
+
21
+ Or with pip:
22
+
23
+ ```bash
24
+ pip install suba openai-whisper
25
+ ```
26
+
27
+ That's it. One command — no clone, no build step.
28
+
29
+ ## Use
30
+
31
+ ```bash
32
+ suba subs.srt movie.mkv -o fixed.srt
33
+ ```
34
+
35
+ ```bash
36
+ suba <subtitles> <video> -o <output.srt|vtt>
37
+ ```
38
+
39
+ | Flag | What |
40
+ |------|------|
41
+ | `-o`, `--output` | Write corrected SRT/VTT |
42
+ | `-m`, `--model` | Whisper model (`base`, `small`, `medium`, `large`) |
43
+ | `-d`, `--duration` | Audio seconds to transcribe for text matching (default 60) |
44
+ | `-t`, `--start` | Manual start time in seconds, for text matching |
45
+ | `--threshold` | Minimum similarity to accept a match |
46
+ | `-A`, `--audio-lang` | Language spoken in the video (`en`, `english`, `eng`) |
47
+ | `-S`, `--sub-lang` | Language of the subtitle file (`it`, `italian`, `ita`) |
48
+ | `--align` | Force a strategy: `auto`, `text`, `translate`, `timing` |
49
+ | `--max-shift` | Largest offset to search for in timing mode (default 60 s) |
50
+ | `--min-peak-ratio` | How clearly the winning offset must beat the runner-up (default 1.4) |
51
+ | `--no-refine` | Don't sharpen a text match against the waveform |
52
+ | `--no-rescale` | Don't test for PAL/NTSC frame-rate conversions, only a shift |
53
+ | `--min-coverage` | Reject subtitles reaching less than this much of the runtime (default 0.7) |
54
+
55
+ ## Different languages
56
+
57
+ English movie, Italian subtitles? Tell it so:
58
+
59
+ ```bash
60
+ suba movie.ita.srt movie.mkv -A en -S it -o fixed.srt
61
+ ```
62
+
63
+ You usually don't have to. Suba reads the language off the filename
64
+ (`movie.ita.srt`), falls back to sniffing the subtitle text itself, and asks
65
+ Whisper what the audio is. The flags are there for when it guesses wrong.
66
+
67
+ Depending on the pair, it picks one of three strategies:
68
+
69
+ | Audio | Subtitles | Strategy | How |
70
+ |-------|-----------|----------|-----|
71
+ | en | en | `text` | Transcribe and compare the words |
72
+ | it | en | `translate` | Whisper translates the audio to English, then compare the words |
73
+ | en | it | `timing` | Compare *when* people speak, ignoring what they say |
74
+
75
+ The awkward case is the third one, because Whisper only translates **into**
76
+ English — it can't produce Italian to match against. So suba stops reading and
77
+ starts listening to the rhythm: it measures the energy of the waveform to find
78
+ every stretch where somebody is talking, marks every stretch where a subtitle is
79
+ on screen, and slides one over the other until the talking lines up with the
80
+ talking *and* the silence lines up with the silence. Dialogue has a fingerprint,
81
+ and translation doesn't change it.
82
+
83
+ Three things worth knowing about timing mode:
84
+
85
+ - **It reads the whole film**, not a sample. A five-minute window is enough for
86
+ clean audio, but a scored feature film needs everything: the pauses that carry
87
+ the signal are rarer and noisier, and a short window reads as noise. On a
88
+ 3h53m movie that costs about 30 seconds end to end, most of it ffmpeg.
89
+ - **It would rather say nothing than lie.** A correct alignment beats every
90
+ other candidate offset by a wide margin — measured at 5-8x on real films,
91
+ against 1.01x for a subtitle file belonging to a different cut. When the best
92
+ offset only edges out the runner-up, suba refuses instead of handing you a
93
+ confidently wrong answer. `--min-peak-ratio` is the control that matters here;
94
+ `--threshold` barely discriminates in this mode.
95
+ - **It's fast**, because it never transcribes. Pass both languages and Whisper
96
+ is skipped entirely.
97
+
98
+ ## Subtitles that drift
99
+
100
+ Some subtitles don't just start late — they run at the wrong *speed*. A track
101
+ ripped from a PAL disc is timed for 25 fps, and against a 23.976 fps video it
102
+ gains about four seconds every hundred. It starts nearly right and ends minutes
103
+ out, and no single offset can fix it.
104
+
105
+ Suba tests the standard conversions alongside the plain shift and reports one if
106
+ it fits better:
107
+
108
+ ```
109
+ matched on speech modulation
110
+ frame-rate conversion detected: 25 → 23.976 fps (PAL)
111
+ SYNC OFFSET: +1.200 s
112
+ ```
113
+
114
+ That's a real Italian track against a 2h30m film. Before: 33% of its lines
115
+ landed within a second of the right moment, and the worst were fifteen seconds
116
+ out. After: 99.6%, with the worst at 0.4s. Pass `--no-rescale` to only ever
117
+ shift.
118
+
119
+ ## Half a subtitle file is not a subtitle file
120
+
121
+ Subtitle sites still carry tracks split across two discs, and a `CD1` file looks
122
+ exactly like a normal one until you notice its last line lands 90 minutes into a
123
+ four-hour film. Suba can align that perfectly well — on the half it has — and
124
+ hand you a file that dies at the intermission.
125
+
126
+ So it checks first and refuses:
127
+
128
+ ```
129
+ error: these subtitles run out at 01:39:38.435, only 43% of the 03:53:15.014 film.
130
+ That is usually one disc of a multi-disc rip. Syncing cannot fill in the
131
+ missing part — find a complete file, or pass --min-coverage 0 to align
132
+ the part that is there anyway.
133
+ ```
134
+
135
+ The check runs before any audio is read, so it costs nothing. Ending at 90% is
136
+ normal — that's end credits — and only gets a passing mention.
137
+
138
+ ## Finding the talking
139
+
140
+ Timing alignment lives or dies on knowing which parts of the audio are speech,
141
+ and there are two ways to guess, neither of which wins everywhere:
142
+
143
+ - **Loudness** in the speech band. Near-perfect on clean audio with real silence
144
+ between lines.
145
+ - **Modulation** — how much the speech-band energy wobbles around its own local
146
+ average. Speech pulses at the syllable rate; an orchestra doesn't.
147
+
148
+ On a scored action film, loudness is worse than useless: the loudest frames are
149
+ music and effects, so the mask tracks the score and *anti*-correlates with the
150
+ dialogue. Measured on one, every candidate offset scored negative — there was no
151
+ alignment to find at all. Modulation took the same film from -0.09 to +0.52.
152
+
153
+ But modulation smooths, and smoothing costs precision: on clean audio loudness
154
+ scores 0.996 where modulation manages 0.69. So suba computes both, and picks
155
+ whichever correlates better against your subtitles. It says which one won.
156
+
157
+ It's also *precise*, and that precision is now lent to the other two strategies.
158
+ Matching text tells you which line is playing, but only as accurately as the
159
+ Whisper segment it landed in — and those can run ten seconds long. So after a
160
+ text or translate match, suba shifts the subtitles by its estimate and does a
161
+ short correlation search to recover the leftover error:
162
+
163
+ ```
164
+ Match similarity: 0.875
165
+ Refined on waveform: -0.314 s → -4.264 s (correlation 0.988)
166
+ ```
167
+
168
+ That example is a real run against Italian audio with English subtitles, where
169
+ the true offset was -4.250 s. Pass `--no-refine` to turn it off.
170
+
171
+ ## How it works
172
+
173
+ - Finds the densest 60 seconds of subtitles (more lines = more talking, probably)
174
+ - Extracts that audio with ffmpeg
175
+ - Transcribes it with Whisper
176
+ - Slides windows of the transcription over each candidate subtitle line, scoring by text similarity and timestamp proximity
177
+ - Shifts all timestamps by the detected offset and writes a fixed SRT
178
+
179
+ When the subtitles aren't in the language being spoken, the middle steps change
180
+ — see [Different languages](#different-languages) — but the last one doesn't.
181
+ Your subtitle text is never rewritten, only re-timed.
182
+
183
+
184
+ **🌟 Star this repository if you find it helpful!**