@torrent-tv/proxy 2.73.0 → 2.74.0
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.
- package/CHANGELOG.md +1447 -1432
- package/CLAUDE.md +165 -160
- package/docs/container-architecture.md +192 -184
- package/package.json +1 -1
- package/routes/api/subtitles/get.js +205 -205
- package/services/container/Container.js +354 -135
- package/services/container/MatroskaContainer.js +1155 -516
- package/services/container/Mp4Container.js +858 -392
- package/services/container/SubtitleFileContainer.js +323 -261
- package/services/controllers/SubtitleController.js +128 -127
- package/services/delivery-probe.js +64 -6
- package/services/hls-session-manager.js +32 -35
- package/services/language-detect.js +174 -228
- package/services/orchestrators/SubtitleOrchestrator.js +64 -5
- package/services/playback-planner.js +747 -747
- package/services/produced-index.js +300 -0
- package/services/torrent-worker/subtitle-cues.js +582 -618
- package/services/tracks/TextSubtitleTrack.js +287 -47
- package/services/tracks/index.js +14 -14
- package/test/delivery-probe.test.js +67 -0
- package/test/matroska-blocks.test.js +0 -0
- package/test/mp4-subtitles.test.js +173 -127
- package/test/produced-index.test.js +188 -0
- package/test/subtitle-cue-framing.test.js +200 -202
- package/test/subtitle-cue-source.test.js +104 -0
- package/test/subtitle-cue-walk.test.js +369 -0
- package/test/subtitle-defaults.test.js +97 -97
- package/test/subtitle-language.test.js +252 -252
- package/test/subtitle-track-numbering.test.js +370 -370
- package/services/container-index/matroska-blocks.js +0 -202
- package/services/container-index/matroska-subtitles.js +0 -372
- package/services/container-index/mp4-subtitles.js +0 -404
- package/services/subtitle-convert.js +0 -144
- package/services/subtitle-defaults.js +0 -157
- package/services/tracks/subtitle-markup.js +0 -104
|
@@ -1,228 +1,174 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Content-based subtitle language detection (proxy side).
|
|
3
|
-
*
|
|
4
|
-
* Uses `franc` (n-gram / trigram frequency against per-language reference
|
|
5
|
-
* profiles — MIT). Runs on the proxy where the full subtitle text and
|
|
6
|
-
* node_modules live, so no detection model ships to the browser. Detection is
|
|
7
|
-
* restricted to a curated set of plausible subtitle languages via franc's
|
|
8
|
-
* `only` option: this both maps ISO 639-3 → ISO 639-1 + English name and
|
|
9
|
-
* avoids exotic false positives on short text (e.g. English mis-detected as
|
|
10
|
-
* Scots). Returns null when franc is not confident (too little text, or
|
|
11
|
-
* undetermined).
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { franc } from "franc";
|
|
15
|
-
|
|
16
|
-
/** ISO 639-3 (franc output) → { code: ISO 639-1 / BCP-47, name }. Curated allowlist. */
|
|
17
|
-
const LANG_3_TO_1 = {
|
|
18
|
-
eng: { code: "en", name: "English" },
|
|
19
|
-
rus: { code: "ru", name: "Russian" },
|
|
20
|
-
ukr: { code: "uk", name: "Ukrainian" },
|
|
21
|
-
bel: { code: "be", name: "Belarusian" },
|
|
22
|
-
jpn: { code: "ja", name: "Japanese" },
|
|
23
|
-
kor: { code: "ko", name: "Korean" },
|
|
24
|
-
cmn: { code: "zh", name: "Chinese" },
|
|
25
|
-
spa: { code: "es", name: "Spanish" },
|
|
26
|
-
fra: { code: "fr", name: "French" },
|
|
27
|
-
deu: { code: "de", name: "German" },
|
|
28
|
-
ita: { code: "it", name: "Italian" },
|
|
29
|
-
por: { code: "pt", name: "Portuguese" },
|
|
30
|
-
pol: { code: "pl", name: "Polish" },
|
|
31
|
-
nld: { code: "nl", name: "Dutch" },
|
|
32
|
-
arb: { code: "ar", name: "Arabic" },
|
|
33
|
-
tur: { code: "tr", name: "Turkish" },
|
|
34
|
-
vie: { code: "vi", name: "Vietnamese" },
|
|
35
|
-
tha: { code: "th", name: "Thai" },
|
|
36
|
-
hin: { code: "hi", name: "Hindi" },
|
|
37
|
-
ind: { code: "id", name: "Indonesian" },
|
|
38
|
-
zlm: { code: "ms", name: "Malay" },
|
|
39
|
-
ces: { code: "cs", name: "Czech" },
|
|
40
|
-
slk: { code: "sk", name: "Slovak" },
|
|
41
|
-
ron: { code: "ro", name: "Romanian" },
|
|
42
|
-
hun: { code: "hu", name: "Hungarian" },
|
|
43
|
-
srp: { code: "sr", name: "Serbian" },
|
|
44
|
-
hrv: { code: "hr", name: "Croatian" },
|
|
45
|
-
bul: { code: "bg", name: "Bulgarian" },
|
|
46
|
-
ell: { code: "el", name: "Greek" },
|
|
47
|
-
heb: { code: "he", name: "Hebrew" },
|
|
48
|
-
dan: { code: "da", name: "Danish" },
|
|
49
|
-
fin: { code: "fi", name: "Finnish" },
|
|
50
|
-
nob: { code: "no", name: "Norwegian" },
|
|
51
|
-
swe: { code: "sv", name: "Swedish" },
|
|
52
|
-
fas: { code: "fa", name: "Persian" }
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const ONLY = Object.keys(LANG_3_TO_1);
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The least text, in characters, that supports an answer of each language.
|
|
59
|
-
*
|
|
60
|
-
* MEASURED, not chosen — `research/franc-boundary-2026-09-02.md`. Method:
|
|
61
|
-
* Wikipedia extracts per language (deliberately NOT the UDHR, which is what
|
|
62
|
-
* franc's own profiles are built from and would read optimistically), 120
|
|
63
|
-
* windows cut at random from them at each length of a ladder from 40 to 1300
|
|
64
|
-
* characters, and the figure recorded is the shortest length from which franc
|
|
65
|
-
* answered correctly in at least 95 % of trials AND kept doing so at every
|
|
66
|
-
* longer length measured.
|
|
67
|
-
*
|
|
68
|
-
* Why it is per language rather than one number: the answer is not equally hard
|
|
69
|
-
* to reach, and the spread is fivefold. Greek and Korean settle at 40
|
|
70
|
-
* characters because their script settles it; English needs 130; Russian and
|
|
71
|
-
* Czech need 650, because each competes with neighbours in this very list for
|
|
72
|
-
* the same trigrams — Russian with Bulgarian, Serbian and Ukrainian, Czech with
|
|
73
|
-
* Slovak.
|
|
74
|
-
*
|
|
75
|
-
* Two languages are deliberately ABSENT. Swedish and Chinese did not settle
|
|
76
|
-
* anywhere in the ladder on the corpora collected, so no figure for them is
|
|
77
|
-
* measured and none is invented; they take the fallback below.
|
|
78
|
-
*
|
|
79
|
-
* A language with no entry gets the WORST measured figure. That is the
|
|
80
|
-
* conservative reading and it is still a measurement rather than a guess: it
|
|
81
|
-
* says "no better than the hardest language we have measured".
|
|
82
|
-
*
|
|
83
|
-
* @type {Record<string, number>}
|
|
84
|
-
*/
|
|
85
|
-
const LEAST_TEXT = {
|
|
86
|
-
bel: 100,
|
|
87
|
-
bul: 80,
|
|
88
|
-
ces: 650,
|
|
89
|
-
deu: 200,
|
|
90
|
-
ell: 40,
|
|
91
|
-
eng: 130,
|
|
92
|
-
fra: 80,
|
|
93
|
-
heb: 60,
|
|
94
|
-
ita: 160,
|
|
95
|
-
kor: 40,
|
|
96
|
-
nld: 130,
|
|
97
|
-
pol: 200,
|
|
98
|
-
por: 200,
|
|
99
|
-
rus: 650,
|
|
100
|
-
spa: 100,
|
|
101
|
-
srp: 100,
|
|
102
|
-
tur: 160,
|
|
103
|
-
ukr: 250
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/** The worst measured figure, used for any language not in the table. */
|
|
107
|
-
const LEAST_TEXT_WORST = Math.max(...Object.values(LEAST_TEXT), 0);
|
|
108
|
-
|
|
109
|
-
/** franc's own floor: below this it is not asked at all. */
|
|
110
|
-
const FRANC_FLOOR = 15;
|
|
111
|
-
|
|
112
|
-
/** One space between words, nothing else — the form every figure above is in. */
|
|
113
|
-
function normalise(text) {
|
|
114
|
-
return typeof text === "string" ? text.replace(/\s+/g, " ").trim() : "";
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* franc's answer for this text, or null when it has none.
|
|
119
|
-
*
|
|
120
|
-
* @param {string} text
|
|
121
|
-
* @returns {string | null} ISO 639-3.
|
|
122
|
-
*/
|
|
123
|
-
function ask(text) {
|
|
124
|
-
if (text.length < FRANC_FLOOR) {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
const iso3 = franc(text, { only: ONLY, minLength: FRANC_FLOOR });
|
|
128
|
-
return iso3 === "und" ? null : iso3;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Best-effort detect the language of subtitle text.
|
|
133
|
-
*
|
|
134
|
-
* **Give this the words a viewer reads and nothing else.** franc scores letter
|
|
135
|
-
* trigrams over the whole string it is handed, so anything around the words
|
|
136
|
-
* competes with them. An ASS file is markup by half: measured 2026-09-01 on
|
|
137
|
-
* `[HorribleSubs] Drifters - 03 [1080p].ass`, 5040 Latin characters of Aegisub
|
|
138
|
-
* headers, style and font names, `Format:`/`Dialogue:` field prefixes and
|
|
139
|
-
* `{\…}` override groups against 5983 Cyrillic characters of dialogue —
|
|
140
|
-
* `franc(the file) = eng`, `franc(the dialogue) = rus`. The proxy had already
|
|
141
|
-
* built the markup-free text and detected on the file anyway, so a Russian
|
|
142
|
-
* track was offered to the viewer as English.
|
|
143
|
-
*
|
|
144
|
-
* `detectLanguageFromVtt` below is the safe entry point for a whole document;
|
|
145
|
-
* this one is for text that is already only text.
|
|
146
|
-
*
|
|
147
|
-
* @param {string} text - Subtitle text with no markup left in it.
|
|
148
|
-
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
149
|
-
*/
|
|
150
|
-
export function detectLanguage(text) {
|
|
151
|
-
const words = normalise(text);
|
|
152
|
-
if (words.length < FRANC_FLOOR) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
const candidate = ask(words);
|
|
156
|
-
if (candidate === null) {
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
// Enough text to support THIS answer. The figure is the language's own,
|
|
160
|
-
// because the languages are not alike: Russian shares its trigrams with
|
|
161
|
-
// Bulgarian, Serbian and Ukrainian and needs several times what English does.
|
|
162
|
-
if (words.length < (LEAST_TEXT[candidate] ?? LEAST_TEXT_WORST)) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
// And an answer that does not survive losing half the text was an accident of
|
|
166
|
-
// where the text happened to stop, not a reading of it. Free — franc costs
|
|
167
|
-
// about 2 ms whatever the size, measured — and it needs no figure of its own,
|
|
168
|
-
// because the test is taken on the text in hand.
|
|
169
|
-
const middle = Math.floor(words.length / 2);
|
|
170
|
-
if (ask(words.slice(0, middle)) !== candidate || ask(words.slice(middle)) !== candidate) {
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
return LANG_3_TO_1[candidate] ?? null;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The words of a WebVTT document — what a viewer reads, with everything the
|
|
178
|
-
* format puts around them removed.
|
|
179
|
-
*
|
|
180
|
-
* A WebVTT document is a series of blocks separated by blank lines. A block
|
|
181
|
-
* that holds a timing line (`00:00:12.060 --> 00:00:13.270`) is a cue, and the
|
|
182
|
-
* lines after that timing line are its text; the lines before it are the cue's
|
|
183
|
-
* optional identifier. A block with NO timing line is the `WEBVTT` header or a
|
|
184
|
-
* `NOTE` / `STYLE` / `REGION` block, and none of those is anybody's language.
|
|
185
|
-
* That one rule removes the identifiers, the timings and the headers together.
|
|
186
|
-
*
|
|
187
|
-
* What is left can still carry WebVTT's own inline markup — `<v Speaker>`,
|
|
188
|
-
* `<i>`, `<c.yellow>` — and character references. Both are dropped: a speaker
|
|
189
|
-
* name and a class name are written in whatever language the releaser's tooling
|
|
190
|
-
* used, which is not the language of the film.
|
|
191
|
-
*
|
|
192
|
-
* @param {string} vtt - A WebVTT document.
|
|
193
|
-
* @returns {string} The cue text, blocks joined by newlines.
|
|
194
|
-
*/
|
|
195
|
-
export function cueTextOfVtt(vtt) {
|
|
196
|
-
if (typeof vtt !== "string") {
|
|
197
|
-
return "";
|
|
198
|
-
}
|
|
199
|
-
const blocks = vtt.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split(/\n{2,}/);
|
|
200
|
-
const spoken = [];
|
|
201
|
-
for (const block of blocks) {
|
|
202
|
-
const lines = block.split("\n");
|
|
203
|
-
const timingAt = lines.findIndex((line) => line.includes("-->"));
|
|
204
|
-
if (timingAt < 0) {
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
for (const line of lines.slice(timingAt + 1)) {
|
|
208
|
-
spoken.push(line);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return spoken
|
|
212
|
-
.join("\n")
|
|
213
|
-
.replace(/<[^>]*>/g, "")
|
|
214
|
-
// A character reference stands for one character and never for a word, so a
|
|
215
|
-
// space in its place keeps the neighbouring words apart and adds nothing.
|
|
216
|
-
.replace(/&[a-z]+;|&#\d+;|&#x[0-9a-f]+;/gi, " ")
|
|
217
|
-
.trim();
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Detect the language of a WebVTT document, reading only its cue text.
|
|
222
|
-
*
|
|
223
|
-
* @param {string} vtt - A WebVTT document.
|
|
224
|
-
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
225
|
-
*/
|
|
226
|
-
export function detectLanguageFromVtt(vtt) {
|
|
227
|
-
return detectLanguage(cueTextOfVtt(vtt));
|
|
228
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file Content-based subtitle language detection (proxy side).
|
|
3
|
+
*
|
|
4
|
+
* Uses `franc` (n-gram / trigram frequency against per-language reference
|
|
5
|
+
* profiles — MIT). Runs on the proxy where the full subtitle text and
|
|
6
|
+
* node_modules live, so no detection model ships to the browser. Detection is
|
|
7
|
+
* restricted to a curated set of plausible subtitle languages via franc's
|
|
8
|
+
* `only` option: this both maps ISO 639-3 → ISO 639-1 + English name and
|
|
9
|
+
* avoids exotic false positives on short text (e.g. English mis-detected as
|
|
10
|
+
* Scots). Returns null when franc is not confident (too little text, or
|
|
11
|
+
* undetermined).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { franc } from "franc";
|
|
15
|
+
|
|
16
|
+
/** ISO 639-3 (franc output) → { code: ISO 639-1 / BCP-47, name }. Curated allowlist. */
|
|
17
|
+
const LANG_3_TO_1 = {
|
|
18
|
+
eng: { code: "en", name: "English" },
|
|
19
|
+
rus: { code: "ru", name: "Russian" },
|
|
20
|
+
ukr: { code: "uk", name: "Ukrainian" },
|
|
21
|
+
bel: { code: "be", name: "Belarusian" },
|
|
22
|
+
jpn: { code: "ja", name: "Japanese" },
|
|
23
|
+
kor: { code: "ko", name: "Korean" },
|
|
24
|
+
cmn: { code: "zh", name: "Chinese" },
|
|
25
|
+
spa: { code: "es", name: "Spanish" },
|
|
26
|
+
fra: { code: "fr", name: "French" },
|
|
27
|
+
deu: { code: "de", name: "German" },
|
|
28
|
+
ita: { code: "it", name: "Italian" },
|
|
29
|
+
por: { code: "pt", name: "Portuguese" },
|
|
30
|
+
pol: { code: "pl", name: "Polish" },
|
|
31
|
+
nld: { code: "nl", name: "Dutch" },
|
|
32
|
+
arb: { code: "ar", name: "Arabic" },
|
|
33
|
+
tur: { code: "tr", name: "Turkish" },
|
|
34
|
+
vie: { code: "vi", name: "Vietnamese" },
|
|
35
|
+
tha: { code: "th", name: "Thai" },
|
|
36
|
+
hin: { code: "hi", name: "Hindi" },
|
|
37
|
+
ind: { code: "id", name: "Indonesian" },
|
|
38
|
+
zlm: { code: "ms", name: "Malay" },
|
|
39
|
+
ces: { code: "cs", name: "Czech" },
|
|
40
|
+
slk: { code: "sk", name: "Slovak" },
|
|
41
|
+
ron: { code: "ro", name: "Romanian" },
|
|
42
|
+
hun: { code: "hu", name: "Hungarian" },
|
|
43
|
+
srp: { code: "sr", name: "Serbian" },
|
|
44
|
+
hrv: { code: "hr", name: "Croatian" },
|
|
45
|
+
bul: { code: "bg", name: "Bulgarian" },
|
|
46
|
+
ell: { code: "el", name: "Greek" },
|
|
47
|
+
heb: { code: "he", name: "Hebrew" },
|
|
48
|
+
dan: { code: "da", name: "Danish" },
|
|
49
|
+
fin: { code: "fi", name: "Finnish" },
|
|
50
|
+
nob: { code: "no", name: "Norwegian" },
|
|
51
|
+
swe: { code: "sv", name: "Swedish" },
|
|
52
|
+
fas: { code: "fa", name: "Persian" }
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ONLY = Object.keys(LANG_3_TO_1);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The least text, in characters, that supports an answer of each language.
|
|
59
|
+
*
|
|
60
|
+
* MEASURED, not chosen — `research/franc-boundary-2026-09-02.md`. Method:
|
|
61
|
+
* Wikipedia extracts per language (deliberately NOT the UDHR, which is what
|
|
62
|
+
* franc's own profiles are built from and would read optimistically), 120
|
|
63
|
+
* windows cut at random from them at each length of a ladder from 40 to 1300
|
|
64
|
+
* characters, and the figure recorded is the shortest length from which franc
|
|
65
|
+
* answered correctly in at least 95 % of trials AND kept doing so at every
|
|
66
|
+
* longer length measured.
|
|
67
|
+
*
|
|
68
|
+
* Why it is per language rather than one number: the answer is not equally hard
|
|
69
|
+
* to reach, and the spread is fivefold. Greek and Korean settle at 40
|
|
70
|
+
* characters because their script settles it; English needs 130; Russian and
|
|
71
|
+
* Czech need 650, because each competes with neighbours in this very list for
|
|
72
|
+
* the same trigrams — Russian with Bulgarian, Serbian and Ukrainian, Czech with
|
|
73
|
+
* Slovak.
|
|
74
|
+
*
|
|
75
|
+
* Two languages are deliberately ABSENT. Swedish and Chinese did not settle
|
|
76
|
+
* anywhere in the ladder on the corpora collected, so no figure for them is
|
|
77
|
+
* measured and none is invented; they take the fallback below.
|
|
78
|
+
*
|
|
79
|
+
* A language with no entry gets the WORST measured figure. That is the
|
|
80
|
+
* conservative reading and it is still a measurement rather than a guess: it
|
|
81
|
+
* says "no better than the hardest language we have measured".
|
|
82
|
+
*
|
|
83
|
+
* @type {Record<string, number>}
|
|
84
|
+
*/
|
|
85
|
+
const LEAST_TEXT = {
|
|
86
|
+
bel: 100,
|
|
87
|
+
bul: 80,
|
|
88
|
+
ces: 650,
|
|
89
|
+
deu: 200,
|
|
90
|
+
ell: 40,
|
|
91
|
+
eng: 130,
|
|
92
|
+
fra: 80,
|
|
93
|
+
heb: 60,
|
|
94
|
+
ita: 160,
|
|
95
|
+
kor: 40,
|
|
96
|
+
nld: 130,
|
|
97
|
+
pol: 200,
|
|
98
|
+
por: 200,
|
|
99
|
+
rus: 650,
|
|
100
|
+
spa: 100,
|
|
101
|
+
srp: 100,
|
|
102
|
+
tur: 160,
|
|
103
|
+
ukr: 250
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** The worst measured figure, used for any language not in the table. */
|
|
107
|
+
const LEAST_TEXT_WORST = Math.max(...Object.values(LEAST_TEXT), 0);
|
|
108
|
+
|
|
109
|
+
/** franc's own floor: below this it is not asked at all. */
|
|
110
|
+
const FRANC_FLOOR = 15;
|
|
111
|
+
|
|
112
|
+
/** One space between words, nothing else — the form every figure above is in. */
|
|
113
|
+
function normalise(text) {
|
|
114
|
+
return typeof text === "string" ? text.replace(/\s+/g, " ").trim() : "";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* franc's answer for this text, or null when it has none.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} text
|
|
121
|
+
* @returns {string | null} ISO 639-3.
|
|
122
|
+
*/
|
|
123
|
+
function ask(text) {
|
|
124
|
+
if (text.length < FRANC_FLOOR) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const iso3 = franc(text, { only: ONLY, minLength: FRANC_FLOOR });
|
|
128
|
+
return iso3 === "und" ? null : iso3;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Best-effort detect the language of subtitle text.
|
|
133
|
+
*
|
|
134
|
+
* **Give this the words a viewer reads and nothing else.** franc scores letter
|
|
135
|
+
* trigrams over the whole string it is handed, so anything around the words
|
|
136
|
+
* competes with them. An ASS file is markup by half: measured 2026-09-01 on
|
|
137
|
+
* `[HorribleSubs] Drifters - 03 [1080p].ass`, 5040 Latin characters of Aegisub
|
|
138
|
+
* headers, style and font names, `Format:`/`Dialogue:` field prefixes and
|
|
139
|
+
* `{\…}` override groups against 5983 Cyrillic characters of dialogue —
|
|
140
|
+
* `franc(the file) = eng`, `franc(the dialogue) = rus`. The proxy had already
|
|
141
|
+
* built the markup-free text and detected on the file anyway, so a Russian
|
|
142
|
+
* track was offered to the viewer as English.
|
|
143
|
+
*
|
|
144
|
+
* `detectLanguageFromVtt` below is the safe entry point for a whole document;
|
|
145
|
+
* this one is for text that is already only text.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} text - Subtitle text with no markup left in it.
|
|
148
|
+
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
149
|
+
*/
|
|
150
|
+
export function detectLanguage(text) {
|
|
151
|
+
const words = normalise(text);
|
|
152
|
+
if (words.length < FRANC_FLOOR) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const candidate = ask(words);
|
|
156
|
+
if (candidate === null) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
// Enough text to support THIS answer. The figure is the language's own,
|
|
160
|
+
// because the languages are not alike: Russian shares its trigrams with
|
|
161
|
+
// Bulgarian, Serbian and Ukrainian and needs several times what English does.
|
|
162
|
+
if (words.length < (LEAST_TEXT[candidate] ?? LEAST_TEXT_WORST)) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
// And an answer that does not survive losing half the text was an accident of
|
|
166
|
+
// where the text happened to stop, not a reading of it. Free — franc costs
|
|
167
|
+
// about 2 ms whatever the size, measured — and it needs no figure of its own,
|
|
168
|
+
// because the test is taken on the text in hand.
|
|
169
|
+
const middle = Math.floor(words.length / 2);
|
|
170
|
+
if (ask(words.slice(0, middle)) !== candidate || ask(words.slice(middle)) !== candidate) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
return LANG_3_TO_1[candidate] ?? null;
|
|
174
|
+
}
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
|
|
14
14
|
import { containerOrchestrator } from "./ContainerOrchestrator.js";
|
|
15
15
|
import {
|
|
16
|
-
cuesHeldFor as domainCuesHeldFor,
|
|
17
16
|
warmSubtitleCues as domainWarm,
|
|
18
17
|
subtitleTracksOf,
|
|
19
18
|
declaredSubtitleTracksOf,
|
|
@@ -65,18 +64,78 @@ export class SubtitleOrchestrator {
|
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
/**
|
|
68
|
-
* Cues already downloaded for one track
|
|
67
|
+
* Cues already downloaded for one track — ASKED OF THE TORRENT WORKER, never
|
|
68
|
+
* walked here.
|
|
69
|
+
*
|
|
70
|
+
* The walk decides what it may read from `torrent.bitfield` and
|
|
71
|
+
* `torrent.pieceLength`, and a torrent stand-in on the main thread has
|
|
72
|
+
* neither: it carries `infoHash`, `name` and a `files` list whose reads go
|
|
73
|
+
* back across the boundary (`torrent-worker/client.js`). So the same code
|
|
74
|
+
* called here answers that nothing is downloaded, walks no clusters, and
|
|
75
|
+
* returns an empty document — which is not a failure anything reports,
|
|
76
|
+
* because "no cues held" is a legitimate answer.
|
|
77
|
+
*
|
|
78
|
+
* Field 2026-09-03, and it is the whole reason this method changed. An
|
|
79
|
+
* episode already downloaded from an earlier sitting had its cues found
|
|
80
|
+
* within a second and a half of the file being opened, and the first four
|
|
81
|
+
* pushes — everything before 81.7 s — went out before the browser had
|
|
82
|
+
* subscribed. The catch-up pull that exists for exactly that case answered
|
|
83
|
+
* `WEBVTT` and nothing else, with `x-subtitle-covered-clusters: 0` against
|
|
84
|
+
* 283 indexed, so the viewer watched the opening of the episode with no
|
|
85
|
+
* subtitles and the rest of it with them.
|
|
86
|
+
*
|
|
87
|
+
* There is a second reason, independent of the bitfield. The register of what
|
|
88
|
+
* has been walked, what has been found and in what ORDER lives in the module
|
|
89
|
+
* that does the walking, and the worker already keeps one — the push path
|
|
90
|
+
* fills it. Walking again on the main thread would build a SECOND register
|
|
91
|
+
* with its own `seq` counter, and the browser mixes the cursors from both
|
|
92
|
+
* paths (`#rememberCursor`): two counters would make a cursor from a pull and
|
|
93
|
+
* a cursor from a push incomparable. One register, one walk, one cursor.
|
|
94
|
+
*
|
|
95
|
+
* @param {{ getSubtitleCues?: Function }} pool - The torrent pool, which is
|
|
96
|
+
* what holds the channel to the worker.
|
|
69
97
|
* @param {object} torrent
|
|
70
98
|
* @param {number} fileIndex
|
|
71
99
|
* @param {string} sourceKey
|
|
72
100
|
* @param {number} trackNumber - Container trackNumber
|
|
101
|
+
* @returns {Promise<{ cues: object[], coveredClusters: number, indexedClusters: number, track: object | null }>}
|
|
73
102
|
*/
|
|
74
|
-
async getCues(torrent, fileIndex, sourceKey, trackNumber) {
|
|
103
|
+
async getCues(pool, torrent, fileIndex, sourceKey, trackNumber) {
|
|
104
|
+
// Built fresh on each of the three paths that need it. One shared literal
|
|
105
|
+
// returned by reference would hand every caller the same array, and a
|
|
106
|
+
// single one of them appending to it would change what the next caller
|
|
107
|
+
// reads — which in a method about cue registers not leaking into each
|
|
108
|
+
// other would be a poor thing to introduce.
|
|
109
|
+
const empty = () => ({ cues: [], coveredClusters: 0, indexedClusters: 0, track: null });
|
|
110
|
+
if (typeof pool?.getSubtitleCues !== "function") {
|
|
111
|
+
// Nothing here can read pieces, and answering an empty document would be
|
|
112
|
+
// indistinguishable from a file that genuinely holds no cues.
|
|
113
|
+
logger.warn(
|
|
114
|
+
"subtitle-orchestrator: the torrent pool cannot be asked for cues, " +
|
|
115
|
+
"so none can be served — the walk needs the thread that owns the torrent"
|
|
116
|
+
);
|
|
117
|
+
return empty();
|
|
118
|
+
}
|
|
75
119
|
try {
|
|
76
|
-
|
|
120
|
+
const answer = await pool.getSubtitleCues(torrent, fileIndex, trackNumber);
|
|
121
|
+
if (!answer) {
|
|
122
|
+
return empty();
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
cues: Array.isArray(answer.cues) ? answer.cues : [],
|
|
126
|
+
coveredClusters: answer.coveredClusters ?? 0,
|
|
127
|
+
indexedClusters: answer.indexedClusters ?? 0,
|
|
128
|
+
// The worker answers with the track's own fields flat, because a
|
|
129
|
+
// `ContainerTrack` is a class and only plain objects cross the boundary.
|
|
130
|
+
track: {
|
|
131
|
+
codecId: answer.codecId ?? "",
|
|
132
|
+
codecPrivate: answer.codecPrivate ?? "",
|
|
133
|
+
language: answer.language ?? ""
|
|
134
|
+
}
|
|
135
|
+
};
|
|
77
136
|
} catch (e) {
|
|
78
137
|
logger.warn(`subtitle-orchestrator: getCues failed: ${e?.message ?? e}`);
|
|
79
|
-
return
|
|
138
|
+
return empty();
|
|
80
139
|
}
|
|
81
140
|
}
|
|
82
141
|
|