@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,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
|
-
* `
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
import { Container } from "./Container.js";
|
|
25
|
-
import { TextSubtitleTrack } from "../tracks/TextSubtitleTrack.js";
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
* @
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
* @
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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 };
|