@torrent-tv/proxy 2.73.1 → 2.74.1

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.
Files changed (34) hide show
  1. package/CHANGELOG.md +1453 -1437
  2. package/CLAUDE.md +165 -160
  3. package/docs/container-architecture.md +192 -184
  4. package/package.json +1 -1
  5. package/routes/api/subtitles/get.js +205 -205
  6. package/services/container/Container.js +400 -135
  7. package/services/container/ContainerFactory.js +55 -31
  8. package/services/container/MatroskaContainer.js +1166 -516
  9. package/services/container/Mp4Container.js +898 -392
  10. package/services/container/SubtitleFileContainer.js +323 -261
  11. package/services/controllers/SubtitleController.js +128 -127
  12. package/services/delivery-probe.js +64 -6
  13. package/services/hls-session-manager.js +32 -35
  14. package/services/language-detect.js +174 -228
  15. package/services/playback-planner.js +747 -747
  16. package/services/produced-index.js +300 -0
  17. package/services/torrent-worker/subtitle-cues.js +549 -633
  18. package/services/tracks/TextSubtitleTrack.js +287 -47
  19. package/services/tracks/index.js +14 -14
  20. package/test/delivery-probe.test.js +67 -0
  21. package/test/matroska-blocks.test.js +0 -0
  22. package/test/mp4-subtitles.test.js +173 -127
  23. package/test/produced-index.test.js +188 -0
  24. package/test/subtitle-cue-framing.test.js +200 -202
  25. package/test/subtitle-cue-walk.test.js +369 -0
  26. package/test/subtitle-defaults.test.js +97 -97
  27. package/test/subtitle-language.test.js +252 -252
  28. package/test/subtitle-track-numbering.test.js +370 -370
  29. package/services/container-index/matroska-blocks.js +0 -202
  30. package/services/container-index/matroska-subtitles.js +0 -372
  31. package/services/container-index/mp4-subtitles.js +0 -404
  32. package/services/subtitle-convert.js +0 -144
  33. package/services/subtitle-defaults.js +0 -157
  34. package/services/tracks/subtitle-markup.js +0 -104
@@ -1,261 +1,323 @@
1
- /**
2
- * @file A file of subtitles as a container of its own — `.srt`, `.ass`, `.ssa`,
3
- * `.vtt` shipped beside the film.
4
- *
5
- * It belongs in this folder for the same reason `.mka` does: "a file of its
6
- * own" is not a KIND of track, only the answer to where a track's bytes are,
7
- * and the question this folder answers is how a format frames what it carries.
8
- * SubRip frames a cue as an ordinal, a timing line and the lines under it; ASS
9
- * frames one as a `Dialogue:` row whose FIELD ORDER the file itself states in
10
- * `[Events]`; WebVTT is already what a browser reads.
11
- *
12
- * That last point about ASS is what makes the file different from Matroska and
13
- * the reason both need their own answer. Matroska fixes the order in its own
14
- * specification, so eight fields always stand before the text. A file does not:
15
- * `Format:` may list the columns in any order, and the specification is that
16
- * the reader obeys it. Two framings of one subtitle format, each stated by
17
- * whoever stores it.
18
- *
19
- * What this file does NOT do is take off ASS's own markup — `{\pos(…)}`, `\N`,
20
- * `\h`. That is the same wherever ASS is stored and lives in
21
- * `tracks/subtitle-markup.js`.
22
- */
23
-
24
- import { Container } from "./Container.js";
25
- import { TextSubtitleTrack } from "../tracks/TextSubtitleTrack.js";
26
-
27
- /** The extensions this reads. `.vtt` is included and passes through unparsed. */
28
- const EXTENSIONS = new Set([".srt", ".ass", ".ssa", ".vtt", ".webvtt"]);
29
-
30
- /**
31
- * A SubRip timing line. The specification writes a comma before the
32
- * milliseconds; WebVTT writes a dot, which is the only difference between the
33
- * two lines.
34
- */
35
- const SRT_TIMING = /^(\d{1,2}):(\d{2}):(\d{2})[,.](\d{1,3})\s*-->\s*(\d{1,2}):(\d{2}):(\d{2})[,.](\d{1,3})/;
36
-
37
- /** An ASS timing field: `h:mm:ss.cc`, centiseconds. */
38
- const ASS_TIMING = /^(\d+):(\d{2}):(\d{2})\.(\d{1,2})$/;
39
-
40
- /**
41
- * Seconds from a SubRip timing line's four captured parts.
42
- *
43
- * @param {string[]} parts - [hours, minutes, seconds, fraction]
44
- * @returns {number}
45
- */
46
- function srtSeconds([hours, minutes, seconds, fraction]) {
47
- const ms = Number(String(fraction).padEnd(3, "0"));
48
- return Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds) + ms / 1000;
49
- }
50
-
51
- /**
52
- * Seconds from an ASS timing field.
53
- *
54
- * @param {string} field
55
- * @returns {number | null} Null when the field is not a timing at all, which is
56
- * a malformed row and not a cue at zero.
57
- */
58
- function assSeconds(field) {
59
- const match = ASS_TIMING.exec(String(field ?? "").trim());
60
- if (!match) {
61
- return null;
62
- }
63
- const centiseconds = Number(String(match[4]).padEnd(2, "0"));
64
- return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]) + centiseconds / 100;
65
- }
66
-
67
- export class SubtitleFileContainer extends Container {
68
- /**
69
- * @param {object} params
70
- * @param {string} params.extension - Lowercase, including the dot.
71
- * @param {string} [params.label]
72
- */
73
- constructor({ extension, label = "" }) {
74
- // A subtitle file is read whole — it is kilobytes — so there is no range
75
- // reading and no size to bound it by. `readTracks` and `readCues` take the
76
- // decoded text directly, which is why the base's `readRange` is unused.
77
- super({ readRange: null, fileSize: 0, label });
78
- this.extension = String(extension ?? "").toLowerCase();
79
- /** Column order from `[Events]`'s `Format:`, once a file has been read. */
80
- this.eventColumns = null;
81
- }
82
-
83
- get formatName() {
84
- return "subtitle-file";
85
- }
86
-
87
- /**
88
- * @param {string} extension - Lowercase, including the dot.
89
- * @returns {boolean}
90
- */
91
- static detect(extension) {
92
- return EXTENSIONS.has(String(extension ?? "").toLowerCase());
93
- }
94
-
95
- /**
96
- * The single track a subtitle file carries.
97
- *
98
- * Its `codecId` is the extension, which is the only thing the file says about
99
- * its own format, and `subtitle-markup.js` accepts extensions alongside
100
- * container codec names for exactly this reason.
101
- *
102
- * @returns {Promise<TextSubtitleTrack[]>}
103
- */
104
- async readTracks() {
105
- return [new TextSubtitleTrack({
106
- trackNumber: 0,
107
- declaredIndex: 0,
108
- codecId: this.extension,
109
- language: "",
110
- languageBcp47: "",
111
- name: this.label,
112
- isEnabled: true,
113
- isDefault: false,
114
- declaresDefault: false
115
- })];
116
- }
117
-
118
- /**
119
- * The text field of one ASS `Dialogue:` row, by the order the file declared.
120
- *
121
- * Not static, unlike the other containers': the order is read out of the
122
- * file's own header, so the answer depends on which file this is. A row
123
- * arriving before any `Format:` line has been seen has no declared order and
124
- * is not guessed at.
125
- *
126
- * @param {string} row - The row after `Dialogue:`.
127
- * @param {string} [codecId]
128
- * @returns {string}
129
- */
130
- cueTextOf(row, codecId = this.extension) {
131
- if (codecId !== ".ass" && codecId !== ".ssa") {
132
- return String(row ?? "");
133
- }
134
- const at = this.eventColumns ? this.eventColumns.indexOf("text") : -1;
135
- if (at < 0) {
136
- return "";
137
- }
138
- // The text field is last by the specification and may hold commas, so
139
- // everything from its column on is joined back together.
140
- return String(row ?? "").split(",").slice(at).join(",");
141
- }
142
-
143
- /**
144
- * Every cue in the file, with its markup still in place.
145
- *
146
- * @param {string} text - The file, already decoded to characters.
147
- * @returns {{ startSeconds: number, endSeconds: number, text: string }[] | null}
148
- * Null for WebVTT, which is not parsed: it is already what a browser reads,
149
- * and taking it apart to write it back would drop its styles, its regions
150
- * and its cue identifiers for nothing.
151
- */
152
- readCues(text) {
153
- const lines = String(text ?? "").replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
154
- if (this.extension === ".ass" || this.extension === ".ssa") {
155
- return this.#assCues(lines);
156
- }
157
- if (this.extension === ".srt") {
158
- return this.#srtCues(lines);
159
- }
160
- return null;
161
- }
162
-
163
- /**
164
- * @param {string[]} lines
165
- * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
166
- */
167
- #assCues(lines) {
168
- const cues = [];
169
- let inEvents = false;
170
- this.eventColumns = null;
171
- for (const line of lines) {
172
- const trimmed = line.trim();
173
- if (/^\[.*\]$/.test(trimmed)) {
174
- inEvents = trimmed === "[Events]";
175
- continue;
176
- }
177
- if (!inEvents) {
178
- continue;
179
- }
180
- if (trimmed.startsWith("Format:")) {
181
- this.eventColumns = trimmed
182
- .slice("Format:".length)
183
- .split(",")
184
- .map((column) => column.trim().toLowerCase());
185
- continue;
186
- }
187
- if (!trimmed.startsWith("Dialogue:") || !this.eventColumns) {
188
- continue;
189
- }
190
- const row = trimmed.slice("Dialogue:".length);
191
- const fields = row.split(",");
192
- const startAt = this.eventColumns.indexOf("start");
193
- const endAt = this.eventColumns.indexOf("end");
194
- if (startAt < 0 || endAt < 0) {
195
- continue;
196
- }
197
- const startSeconds = assSeconds(fields[startAt]);
198
- const endSeconds = assSeconds(fields[endAt]);
199
- const cueText = this.cueTextOf(row);
200
- if (startSeconds === null || endSeconds === null || !cueText) {
201
- continue;
202
- }
203
- cues.push({ startSeconds, endSeconds, text: cueText });
204
- }
205
- return cues;
206
- }
207
-
208
- /**
209
- * @param {string[]} lines
210
- * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
211
- */
212
- #srtCues(lines) {
213
- const cues = [];
214
- /** @type {{ startSeconds: number, endSeconds: number, text: string[] } | null} */
215
- let open = null;
216
- const close = () => {
217
- if (!open) {
218
- return;
219
- }
220
- // A file with no blank line between cues leaves the next cue's ordinal as
221
- // the last line of this one. It is SubRip's own numbering, never
222
- // dialogue, so it goes rather than being shown.
223
- while (open.text.length > 0 && /^\d+$/.test(open.text[open.text.length - 1].trim())) {
224
- open.text.pop();
225
- }
226
- const text = open.text.join("\n").trim();
227
- if (text) {
228
- cues.push({ startSeconds: open.startSeconds, endSeconds: open.endSeconds, text });
229
- }
230
- open = null;
231
- };
232
- for (const line of lines) {
233
- const timing = SRT_TIMING.exec(line.trim());
234
- if (timing) {
235
- // A timing line opens a cue and closes the one before it. The ordinal
236
- // above it is SubRip's own numbering and carries nothing a player needs,
237
- // so it is dropped rather than carried into the cue's text — which is
238
- // what taking the lines between timings would do.
239
- close();
240
- open = {
241
- startSeconds: srtSeconds(timing.slice(1, 5)),
242
- endSeconds: srtSeconds(timing.slice(5, 9)),
243
- text: []
244
- };
245
- continue;
246
- }
247
- if (!open) {
248
- continue;
249
- }
250
- if (line.trim() === "") {
251
- close();
252
- continue;
253
- }
254
- open.text.push(line);
255
- }
256
- close();
257
- return cues;
258
- }
259
- }
260
-
261
- export { EXTENSIONS as SUBTITLE_FILE_EXTENSIONS };
1
+ /**
2
+ * @file A file of subtitles as a container of its own — `.srt`, `.ass`, `.ssa`,
3
+ * `.vtt` shipped beside the film.
4
+ *
5
+ * It belongs in this folder for the same reason `.mka` does: "a file of its
6
+ * own" is not a KIND of track, only the answer to where a track's bytes are,
7
+ * and the question this folder answers is how a format frames what it carries.
8
+ * SubRip frames a cue as an ordinal, a timing line and the lines under it; ASS
9
+ * frames one as a `Dialogue:` row whose FIELD ORDER the file itself states in
10
+ * `[Events]`; WebVTT is already what a browser reads.
11
+ *
12
+ * That last point about ASS is what makes the file different from Matroska and
13
+ * the reason both need their own answer. Matroska fixes the order in its own
14
+ * specification, so eight fields always stand before the text. A file does not:
15
+ * `Format:` may list the columns in any order, and the specification is that
16
+ * the reader obeys it. Two framings of one subtitle format, each stated by
17
+ * whoever stores it.
18
+ *
19
+ * What this file does NOT do is take off ASS's own markup — `{\pos(…)}`, `\N`,
20
+ * `\h`. That is the same wherever ASS is stored and lives in
21
+ * `TextSubtitleTrack`, because it belongs to the codec rather than the store.
22
+ */
23
+
24
+ import { Container } from "./Container.js";
25
+ import { TextSubtitleTrack } from "../tracks/TextSubtitleTrack.js";
26
+
27
+ /** A leading UTF-8 BOM, which must never reach the WEBVTT signature or a cue. */
28
+ function stripBom(text) {
29
+ return typeof text === "string" && text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
30
+ }
31
+
32
+ /** The extensions this reads. `.vtt` is included and passes through unparsed. */
33
+ const EXTENSIONS = new Set([".srt", ".ass", ".ssa", ".vtt", ".webvtt"]);
34
+
35
+ /**
36
+ * A SubRip timing line. The specification writes a comma before the
37
+ * milliseconds; WebVTT writes a dot, which is the only difference between the
38
+ * two lines.
39
+ */
40
+ const SRT_TIMING = /^(\d{1,2}):(\d{2}):(\d{2})[,.](\d{1,3})\s*-->\s*(\d{1,2}):(\d{2}):(\d{2})[,.](\d{1,3})/;
41
+
42
+ /** An ASS timing field: `h:mm:ss.cc`, centiseconds. */
43
+ const ASS_TIMING = /^(\d+):(\d{2}):(\d{2})\.(\d{1,2})$/;
44
+
45
+ /**
46
+ * Seconds from a SubRip timing line's four captured parts.
47
+ *
48
+ * @param {string[]} parts - [hours, minutes, seconds, fraction]
49
+ * @returns {number}
50
+ */
51
+ function srtSeconds([hours, minutes, seconds, fraction]) {
52
+ const ms = Number(String(fraction).padEnd(3, "0"));
53
+ return Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds) + ms / 1000;
54
+ }
55
+
56
+ /**
57
+ * Seconds from an ASS timing field.
58
+ *
59
+ * @param {string} field
60
+ * @returns {number | null} Null when the field is not a timing at all, which is
61
+ * a malformed row and not a cue at zero.
62
+ */
63
+ function assSeconds(field) {
64
+ const match = ASS_TIMING.exec(String(field ?? "").trim());
65
+ if (!match) {
66
+ return null;
67
+ }
68
+ const centiseconds = Number(String(match[4]).padEnd(2, "0"));
69
+ return Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]) + centiseconds / 100;
70
+ }
71
+
72
+ export class SubtitleFileContainer extends Container {
73
+ /**
74
+ * @param {object} params
75
+ * @param {string} params.extension - Lowercase, including the dot.
76
+ * @param {string} [params.label]
77
+ */
78
+ constructor({ extension, label = "" }) {
79
+ // A subtitle file is read whole it is kilobytes so there is no range
80
+ // reading and no size to bound it by. `readTracks` and `readCues` take the
81
+ // decoded text directly, which is why the base's `readRange` is unused.
82
+ super({ readRange: null, fileSize: 0, label });
83
+ this.extension = String(extension ?? "").toLowerCase();
84
+ /** Column order from `[Events]`'s `Format:`, once a file has been read. */
85
+ this.eventColumns = null;
86
+ }
87
+
88
+ get formatName() {
89
+ return "subtitle-file";
90
+ }
91
+
92
+ /**
93
+ * @param {string} extension - Lowercase, including the dot.
94
+ * @returns {boolean}
95
+ */
96
+
97
+ /**
98
+ * Decode a subtitle file's bytes to text.
99
+ *
100
+ * UTF-8 is preferred and a BOM settles it. Where the UTF-8 decode produces
101
+ * many replacement characters the bytes are read again as Windows-1251, which
102
+ * is what most Russian `.srt` files are written in — otherwise both what the
103
+ * viewer reads and what the language detector reads would be mojibake.
104
+ *
105
+ * @param {Buffer | Uint8Array} bytes
106
+ * @returns {string}
107
+ */
108
+ static decodeBytes(bytes) {
109
+ const buffer = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
110
+ if (buffer.length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
111
+ return new TextDecoder("utf-8").decode(buffer);
112
+ }
113
+ const utf8 = new TextDecoder("utf-8").decode(buffer);
114
+ const replacements = (utf8.match(/�/g) || []).length;
115
+ // More than half a per cent of the text unreadable is not UTF-8 at all.
116
+ if (replacements > Math.max(2, utf8.length * 0.005)) {
117
+ try {
118
+ return new TextDecoder("windows-1251").decode(buffer);
119
+ } catch {
120
+ // The decoder needs a full-ICU build; fall back to the UTF-8 attempt.
121
+ }
122
+ }
123
+ return utf8;
124
+ }
125
+
126
+ /**
127
+ * A subtitle file as a WebVTT document, or null for a format that cannot be
128
+ * converted in place — image-based `.sup`, ambiguous `.sub`, `.ttml`.
129
+ *
130
+ * The reading is this class's, because the file states how its own cues are
131
+ * framed; the writing is the track's, because what is inside a cue's text
132
+ * belongs to the codec.
133
+ *
134
+ * @param {string} text
135
+ * @param {string} extension - Lowercase, including the dot.
136
+ * @returns {string | null}
137
+ */
138
+ static toVtt(text, extension) {
139
+ const clean = stripBom(text);
140
+ const ext = String(extension ?? "").toLowerCase();
141
+ if (ext === ".vtt" || ext === ".webvtt") {
142
+ return clean.trimStart().startsWith("WEBVTT") ? clean : `WEBVTT
143
+
144
+ ${clean}`;
145
+ }
146
+ if (!SubtitleFileContainer.detect(ext)) {
147
+ return null;
148
+ }
149
+ const cues = new SubtitleFileContainer({ extension: ext }).readCues(clean);
150
+ return cues === null ? null : TextSubtitleTrack.cuesToVtt(cues, ext);
151
+ }
152
+
153
+ static detect(extension) {
154
+ return EXTENSIONS.has(String(extension ?? "").toLowerCase());
155
+ }
156
+
157
+ /**
158
+ * The single track a subtitle file carries.
159
+ *
160
+ * Its `codecId` is the extension, which is the only thing the file says about
161
+ * its own format, and the markup table accepts extensions alongside
162
+ * container codec names for exactly this reason.
163
+ *
164
+ * @returns {Promise<TextSubtitleTrack[]>}
165
+ */
166
+ async readTracks() {
167
+ return [new TextSubtitleTrack({
168
+ trackNumber: 0,
169
+ declaredIndex: 0,
170
+ codecId: this.extension,
171
+ language: "",
172
+ languageBcp47: "",
173
+ name: this.label,
174
+ isEnabled: true,
175
+ isDefault: false,
176
+ declaresDefault: false
177
+ })];
178
+ }
179
+
180
+ /**
181
+ * The text field of one ASS `Dialogue:` row, by the order the file declared.
182
+ *
183
+ * Not static, unlike the other containers': the order is read out of the
184
+ * file's own header, so the answer depends on which file this is. A row
185
+ * arriving before any `Format:` line has been seen has no declared order and
186
+ * is not guessed at.
187
+ *
188
+ * @param {string} row - The row after `Dialogue:`.
189
+ * @param {string} [codecId]
190
+ * @returns {string}
191
+ */
192
+ cueTextOf(row, codecId = this.extension) {
193
+ if (codecId !== ".ass" && codecId !== ".ssa") {
194
+ return String(row ?? "");
195
+ }
196
+ const at = this.eventColumns ? this.eventColumns.indexOf("text") : -1;
197
+ if (at < 0) {
198
+ return "";
199
+ }
200
+ // The text field is last by the specification and may hold commas, so
201
+ // everything from its column on is joined back together.
202
+ return String(row ?? "").split(",").slice(at).join(",");
203
+ }
204
+
205
+ /**
206
+ * Every cue in the file, with its markup still in place.
207
+ *
208
+ * @param {string} text - The file, already decoded to characters.
209
+ * @returns {{ startSeconds: number, endSeconds: number, text: string }[] | null}
210
+ * Null for WebVTT, which is not parsed: it is already what a browser reads,
211
+ * and taking it apart to write it back would drop its styles, its regions
212
+ * and its cue identifiers for nothing.
213
+ */
214
+ readCues(text) {
215
+ const lines = String(text ?? "").replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
216
+ if (this.extension === ".ass" || this.extension === ".ssa") {
217
+ return this.#assCues(lines);
218
+ }
219
+ if (this.extension === ".srt") {
220
+ return this.#srtCues(lines);
221
+ }
222
+ return null;
223
+ }
224
+
225
+ /**
226
+ * @param {string[]} lines
227
+ * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
228
+ */
229
+ #assCues(lines) {
230
+ const cues = [];
231
+ let inEvents = false;
232
+ this.eventColumns = null;
233
+ for (const line of lines) {
234
+ const trimmed = line.trim();
235
+ if (/^\[.*\]$/.test(trimmed)) {
236
+ inEvents = trimmed === "[Events]";
237
+ continue;
238
+ }
239
+ if (!inEvents) {
240
+ continue;
241
+ }
242
+ if (trimmed.startsWith("Format:")) {
243
+ this.eventColumns = trimmed
244
+ .slice("Format:".length)
245
+ .split(",")
246
+ .map((column) => column.trim().toLowerCase());
247
+ continue;
248
+ }
249
+ if (!trimmed.startsWith("Dialogue:") || !this.eventColumns) {
250
+ continue;
251
+ }
252
+ const row = trimmed.slice("Dialogue:".length);
253
+ const fields = row.split(",");
254
+ const startAt = this.eventColumns.indexOf("start");
255
+ const endAt = this.eventColumns.indexOf("end");
256
+ if (startAt < 0 || endAt < 0) {
257
+ continue;
258
+ }
259
+ const startSeconds = assSeconds(fields[startAt]);
260
+ const endSeconds = assSeconds(fields[endAt]);
261
+ const cueText = this.cueTextOf(row);
262
+ if (startSeconds === null || endSeconds === null || !cueText) {
263
+ continue;
264
+ }
265
+ cues.push({ startSeconds, endSeconds, text: cueText });
266
+ }
267
+ return cues;
268
+ }
269
+
270
+ /**
271
+ * @param {string[]} lines
272
+ * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
273
+ */
274
+ #srtCues(lines) {
275
+ const cues = [];
276
+ /** @type {{ startSeconds: number, endSeconds: number, text: string[] } | null} */
277
+ let open = null;
278
+ const close = () => {
279
+ if (!open) {
280
+ return;
281
+ }
282
+ // A file with no blank line between cues leaves the next cue's ordinal as
283
+ // the last line of this one. It is SubRip's own numbering, never
284
+ // dialogue, so it goes rather than being shown.
285
+ while (open.text.length > 0 && /^\d+$/.test(open.text[open.text.length - 1].trim())) {
286
+ open.text.pop();
287
+ }
288
+ const text = open.text.join("\n").trim();
289
+ if (text) {
290
+ cues.push({ startSeconds: open.startSeconds, endSeconds: open.endSeconds, text });
291
+ }
292
+ open = null;
293
+ };
294
+ for (const line of lines) {
295
+ const timing = SRT_TIMING.exec(line.trim());
296
+ if (timing) {
297
+ // A timing line opens a cue and closes the one before it. The ordinal
298
+ // above it is SubRip's own numbering and carries nothing a player needs,
299
+ // so it is dropped rather than carried into the cue's text — which is
300
+ // what taking the lines between timings would do.
301
+ close();
302
+ open = {
303
+ startSeconds: srtSeconds(timing.slice(1, 5)),
304
+ endSeconds: srtSeconds(timing.slice(5, 9)),
305
+ text: []
306
+ };
307
+ continue;
308
+ }
309
+ if (!open) {
310
+ continue;
311
+ }
312
+ if (line.trim() === "") {
313
+ close();
314
+ continue;
315
+ }
316
+ open.text.push(line);
317
+ }
318
+ close();
319
+ return cues;
320
+ }
321
+ }
322
+
323
+ export { EXTENSIONS as SUBTITLE_FILE_EXTENSIONS };