@torrent-tv/proxy 2.73.1 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +1447 -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 +354 -135
  7. package/services/container/MatroskaContainer.js +1155 -516
  8. package/services/container/Mp4Container.js +858 -392
  9. package/services/container/SubtitleFileContainer.js +323 -261
  10. package/services/controllers/SubtitleController.js +128 -127
  11. package/services/delivery-probe.js +64 -6
  12. package/services/hls-session-manager.js +32 -35
  13. package/services/language-detect.js +174 -228
  14. package/services/playback-planner.js +747 -747
  15. package/services/produced-index.js +300 -0
  16. package/services/torrent-worker/subtitle-cues.js +582 -633
  17. package/services/tracks/TextSubtitleTrack.js +287 -47
  18. package/services/tracks/index.js +14 -14
  19. package/test/delivery-probe.test.js +67 -0
  20. package/test/matroska-blocks.test.js +0 -0
  21. package/test/mp4-subtitles.test.js +173 -127
  22. package/test/produced-index.test.js +188 -0
  23. package/test/subtitle-cue-framing.test.js +200 -202
  24. package/test/subtitle-cue-walk.test.js +369 -0
  25. package/test/subtitle-defaults.test.js +97 -97
  26. package/test/subtitle-language.test.js +252 -252
  27. package/test/subtitle-track-numbering.test.js +370 -370
  28. package/services/container-index/matroska-blocks.js +0 -202
  29. package/services/container-index/matroska-subtitles.js +0 -372
  30. package/services/container-index/mp4-subtitles.js +0 -404
  31. package/services/subtitle-convert.js +0 -144
  32. package/services/subtitle-defaults.js +0 -157
  33. package/services/tracks/subtitle-markup.js +0 -104
@@ -1,392 +1,858 @@
1
- /**
2
- * @file MP4/MOV container — ISO/IEC 14496-12.
3
- *
4
- * Parses moov for all track types in one walk:
5
- * - tkhd: track_ID, flags track_enabled (0x000001), alternate_group, width/height
6
- * - mdhd: timescale, language (packed 5-bit), version handling
7
- * - hdlr: handler_type (vide/soun/text/sbtl/subt/subp/clcp)
8
- * - elng: extendedLanguage BCP47 (when present, replaces mdhd language per spec)
9
- * - stsd: sample entry format (avc1/hev1/mp4a/tx3g/wvtt/stpp)
10
- * - stbl tables for subtitle cue ranges (stts/stsz/stsc/stco/co64) and video keyframes (stss/stts/ctts/elst)
11
- *
12
- * Delegates keyframe and subtitle sample reading to existing mp4.js / mp4-subtitles.js
13
- * but centralizes track creation so FlagEnabled / LanguageBCP47 / alternate_group
14
- * are handled once for every media type.
15
- */
16
-
17
- import { Container } from "./Container.js";
18
- import { isMp4, readMp4KeyframeTimes } from "../container-index/mp4.js";
19
- import { decodeSubtitleSample, readMp4SubtitlePlan } from "../container-index/mp4-subtitles.js";
20
- import { VideoTrack } from "../tracks/VideoTrack.js";
21
- import { AudioTrack } from "../tracks/AudioTrack.js";
22
- import { TextSubtitleTrack, TEXT_FORMATS_MP4 } from "../tracks/TextSubtitleTrack.js";
23
- import { ImageSubtitleTrack } from "../tracks/ImageSubtitleTrack.js";
24
-
25
- /**
26
- * One box header, per ISO/IEC 14496-12 §4.2.
27
- *
28
- * @param {Buffer} buf
29
- * @param {number} off
30
- * @returns {{ type: string, size: number, dataOffset: number, end: number } | null}
31
- */
32
- function readBox(buf, off) {
33
- if (off + 8 > buf.length) return null;
34
- let sz = buf.readUInt32BE(off);
35
- const tp = buf.toString("latin1", off + 4, off + 8);
36
- let hb = 8;
37
- if (sz === 1) { if (off + 16 > buf.length) return null; sz = Number(buf.readBigUInt64BE(off + 8)); hb = 16; }
38
- if (sz < hb) return null;
39
- return { type: tp, size: sz, dataOffset: off + hb, end: off + sz };
40
- }
41
-
42
- /**
43
- * Every direct child box of the given type.
44
- *
45
- * @param {Buffer} buf
46
- * @param {number} start
47
- * @param {number} end
48
- * @param {string} type
49
- * @returns {Array<{ type: string, size: number, dataOffset: number, end: number }>}
50
- */
51
- function childrenOf(buf, start, end, type) {
52
- const out = [];
53
- let p = start;
54
- while (p + 8 <= end) {
55
- const b = readBox(buf, p);
56
- if (!b) break;
57
- if (b.type === type) out.push(b);
58
- p = b.end;
59
- }
60
- return out;
61
- }
62
-
63
- /**
64
- * The first direct child box of the given type, or null.
65
- *
66
- * @param {Buffer} buf
67
- * @param {number} s
68
- * @param {number} e
69
- * @param {string} t
70
- * @returns {{ type: string, size: number, dataOffset: number, end: number } | null}
71
- */
72
- function childOf(buf, s, e, t) {
73
- return childrenOf(buf, s, e, t)[0] ?? null;
74
- }
75
-
76
- export class Mp4Container extends Container {
77
- get formatName() {
78
- return "mp4";
79
- }
80
-
81
- static detect(head) {
82
- return isMp4(head);
83
- }
84
-
85
- async readTracks() {
86
- const head = await this.readRange(0, Math.min(64 - 1, this.fileSize - 1));
87
- if (!head || !isMp4(head)) return [];
88
-
89
- // Use the subtitle plan reader's moov parsing as source for subtitle tracks,
90
- // and a direct moov walk for video/audio to collect tkhd/mdhd/hdlr/elng uniformly.
91
- // For simplicity, delegate entirely to readMp4SubtitlePlan for subtitles and
92
- // do a lightweight moov walk for video/audio here — then merge.
93
- const subtitlePlan = await readMp4SubtitlePlan(this.readRange, this.fileSize).catch(() => null);
94
- const subtitleByDecl = new Map();
95
- if (subtitlePlan?.tracks) {
96
- for (const t of subtitlePlan.tracks) subtitleByDecl.set(t.declaredIndex, t);
97
- }
98
-
99
- // Minimal moov walk for video/audio: reuse isMp4 + findMoov logic by reading via existing helper
100
- // Instead of duplicating, parse tracks via a second full moov read that collects vide/soun.
101
- // We read moov box directly to extract video/audio tracks.
102
- const tracks = await this.#readVideoAudioTracks();
103
- // Append subtitle tracks from plan, converting to domain objects
104
- if (subtitlePlan?.tracks) {
105
- for (const s of subtitlePlan.tracks) {
106
- const isText = TEXT_FORMATS_MP4.has(s.format);
107
- const Cls = isText ? TextSubtitleTrack : ImageSubtitleTrack;
108
- tracks.push(new Cls({
109
- trackNumber: s.trackId,
110
- declaredIndex: s.declaredIndex,
111
- codecId: s.format,
112
- language: s.language,
113
- languageBcp47: "",
114
- name: "",
115
- isEnabled: true,
116
- isDefault: s.declaredIndex === 0,
117
- declaresDefault: false,
118
- codecPrivateB64: "",
119
- isForced: false,
120
- isHearingImpaired: false,
121
- clusterPositions: [],
122
- samples: s.samples
123
- }));
124
- }
125
- // Count non-text subtitle handlers (subp/clcp/stpp) for declaredIndex correctness — they are already
126
- // accounted for in subtitlePlan's declaredIndex via SUBTITLE_HANDLERS, but we didn't create objects for
127
- // them above when they were stpp (non-text not in plan's tracks). The plan already excludes stpp from tracks
128
- // but increments declaredIndex, so alignment holds: we don't need extra placeholders.
129
- }
130
- return tracks;
131
- }
132
-
133
- /**
134
- * The `moov` box, read whole.
135
- *
136
- * Held on the instance because every question this class answers is inside
137
- * it, and the box can be tens of megabytes off a torrent — reading it once
138
- * per file is the difference between one fetch and one per question.
139
- *
140
- * @returns {Promise<{ moov: Buffer, header: number } | null>}
141
- */
142
- async #moovBuffer() {
143
- if (this.moovHeld !== undefined) {
144
- return this.moovHeld;
145
- }
146
- const PROBE = 64;
147
- const MAX_MOOV = 32 * 1024 * 1024;
148
- let at = 0;
149
- let moovBox = null;
150
- while (at < this.fileSize) {
151
- const probe = await this.readRange(at, Math.min(this.fileSize - 1, at + PROBE - 1));
152
- if (!probe || probe.length < 8) break;
153
- let size = probe.readUInt32BE(0);
154
- const type = probe.toString("latin1", 4, 8);
155
- let header = 8;
156
- if (size === 1) {
157
- if (probe.length < 16) break;
158
- size = Number(probe.readBigUInt64BE(8));
159
- header = 16;
160
- }
161
- if (size <= 0) break;
162
- if (type === "moov") { moovBox = { offset: at, size, header }; break; }
163
- at += size;
164
- }
165
- if (!moovBox || moovBox.size > MAX_MOOV) {
166
- this.moovHeld = null;
167
- return null;
168
- }
169
- const moov = await this.readRange(moovBox.offset, Math.min(this.fileSize - 1, moovBox.offset + moovBox.size - 1));
170
- this.moovHeld = moov ? { moov, header: moovBox.header } : null;
171
- return this.moovHeld;
172
- }
173
-
174
- /**
175
- * Duration from `mvhd` and the presentation offset from the first track's
176
- * edit list, per ISO/IEC 14496-12 §8.2.2 and §8.6.6.
177
- *
178
- * An edit entry whose `media_time` is -1 is an EMPTY edit: it presents
179
- * nothing for `segment_duration`, which shifts everything after it later by
180
- * that much. That shift is what a player reports as the file's start, and it
181
- * is the only way an MP4 states one — a file without such an edit begins at
182
- * zero, which is a declaration, not an absence.
183
- *
184
- * @returns {Promise<import("./Container.js").ContainerMediaInfo>}
185
- */
186
- async readMediaInfo() {
187
- if (this.mediaInfo) {
188
- return this.mediaInfo;
189
- }
190
- /** @type {import("./Container.js").ContainerMediaInfo} */
191
- const info = { format: this.formatName, durationSeconds: null, startTimeSeconds: null };
192
- this.mediaInfo = info;
193
- const held = await this.#moovBuffer();
194
- if (!held) {
195
- return info;
196
- }
197
- const { moov, header } = held;
198
- const mvhd = childOf(moov, header, moov.length, "mvhd");
199
- let movieTimescale = 0;
200
- if (mvhd) {
201
- const version = moov[mvhd.dataOffset];
202
- // version 0: creation(4) modification(4) timescale(4) duration(4)
203
- // version 1: creation(8) modification(8) timescale(4) duration(8)
204
- const at = version === 1 ? mvhd.dataOffset + 20 : mvhd.dataOffset + 12;
205
- if (at + 8 <= moov.length) {
206
- movieTimescale = moov.readUInt32BE(at);
207
- const duration = version === 1 ? Number(moov.readBigUInt64BE(at + 4)) : moov.readUInt32BE(at + 4);
208
- if (movieTimescale > 0 && duration > 0) {
209
- info.durationSeconds = duration / movieTimescale;
210
- }
211
- }
212
- }
213
- info.startTimeSeconds = movieTimescale > 0
214
- ? Mp4Container.#emptyEditSeconds(moov, header, movieTimescale)
215
- : null;
216
- return info;
217
- }
218
-
219
- /**
220
- * The presentation shift of the first empty edit, in seconds; 0 when no track
221
- * declares one.
222
- *
223
- * @param {Buffer} moov
224
- * @param {number} header
225
- * @param {number} movieTimescale
226
- * @returns {number}
227
- */
228
- static #emptyEditSeconds(moov, header, movieTimescale) {
229
- let shift = 0;
230
- for (const trak of childrenOf(moov, header, moov.length, "trak")) {
231
- const edts = childOf(moov, trak.dataOffset, trak.end, "edts");
232
- const elst = edts && childOf(moov, edts.dataOffset, edts.end, "elst");
233
- if (!elst) {
234
- continue;
235
- }
236
- const version = moov[elst.dataOffset];
237
- const count = moov.readUInt32BE(elst.dataOffset + 4);
238
- if (count < 1) {
239
- continue;
240
- }
241
- const entry = elst.dataOffset + 8;
242
- const segmentDuration = version === 1
243
- ? Number(moov.readBigUInt64BE(entry))
244
- : moov.readUInt32BE(entry);
245
- const mediaTime = version === 1
246
- ? Number(moov.readBigInt64BE(entry + 8))
247
- : moov.readInt32BE(entry + 4);
248
- if (mediaTime === -1 && segmentDuration > 0) {
249
- shift = Math.max(shift, segmentDuration / movieTimescale);
250
- }
251
- }
252
- return shift;
253
- }
254
-
255
- async #readVideoAudioTracks() {
256
- const held = await this.#moovBuffer();
257
- if (!held) return [];
258
- const { moov, header: moovHeader } = held;
259
-
260
- const result = [];
261
- let videoIdx = -1;
262
- let audioIdx = -1;
263
-
264
- const moovContentStart = moovHeader;
265
- const moovEnd = moov.length;
266
- for (const trak of childrenOf(moov, moovContentStart, moovEnd, "trak")) {
267
- const mdia = childOf(moov, trak.dataOffset, trak.end, "mdia");
268
- if (!mdia) continue;
269
- const hdlr = childOf(moov, mdia.dataOffset, mdia.end, "hdlr");
270
- const handler = hdlr ? moov.toString("latin1", hdlr.dataOffset + 8, hdlr.dataOffset + 12) : "";
271
- const mdhd = childOf(moov, mdia.dataOffset, mdia.end, "mdhd");
272
- const tkhd = childOf(moov, trak.dataOffset, trak.end, "tkhd");
273
- let language = "";
274
- if (mdhd) {
275
- const ver = moov[mdhd.dataOffset];
276
- const langAt = ver === 1 ? mdhd.dataOffset + 32 : mdhd.dataOffset + 20;
277
- if (langAt + 2 <= mdhd.end) {
278
- const packed = moov.readUInt16BE(langAt);
279
- language = [10, 5, 0].map((s) => String.fromCharCode(((packed >> s) & 0x1f) + 0x60)).join("").replace(/[^a-z]/g, "");
280
- }
281
- }
282
- // elng overrides mdhd language per spec §8.4.6
283
- let languageBcp47 = "";
284
- const elng = childOf(moov, mdia.dataOffset, mdia.end, "elng");
285
- if (elng && elng.end - elng.dataOffset >= 4) {
286
- languageBcp47 = moov.toString("utf8", elng.dataOffset + 4, elng.end).replace(/\0+$/, "");
287
- }
288
- let trackId = 0;
289
- let isEnabled = true;
290
- let alternateGroup = 0;
291
- let width = null;
292
- let height = null;
293
- if (tkhd) {
294
- const ver = moov[tkhd.dataOffset];
295
- const flags = moov.readUInt32BE(tkhd.dataOffset + 1) & 0xffffff; // 3 bytes after version
296
- isEnabled = (flags & 0x000001) !== 0;
297
- trackId = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 20 : tkhd.dataOffset + 12);
298
- alternateGroup = moov.readUInt16BE(ver === 1 ? tkhd.dataOffset + 26 : tkhd.dataOffset + 18);
299
- // width/height are 16.16 fixed point at end of tkhd
300
- if (tkhd.end - tkhd.dataOffset >= 84) {
301
- const w = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 76 : tkhd.dataOffset + 68);
302
- const h = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 80 : tkhd.dataOffset + 72);
303
- width = w / 65536;
304
- height = h / 65536;
305
- }
306
- }
307
- const resolvedLang = languageBcp47 || language;
308
- if (handler === "vide") {
309
- videoIdx += 1;
310
- // stsd format for codecId
311
- let codecId = "";
312
- const minf = childOf(moov, mdia.dataOffset, mdia.end, "minf");
313
- const stbl = minf && childOf(moov, minf.dataOffset, minf.end, "stbl");
314
- const stsd = stbl && childOf(moov, stbl.dataOffset, stbl.end, "stsd");
315
- if (stsd) {
316
- const first = readBox(moov, stsd.dataOffset + 8);
317
- if (first) codecId = first.type;
318
- }
319
- result.push(new VideoTrack({
320
- trackNumber: trackId,
321
- declaredIndex: videoIdx,
322
- codecId,
323
- language: resolvedLang,
324
- languageBcp47,
325
- name: "",
326
- isEnabled,
327
- isDefault: true,
328
- declaresDefault: false,
329
- codecPrivateB64: "",
330
- alternateGroup,
331
- width,
332
- height
333
- }));
334
- } else if (handler === "soun") {
335
- audioIdx += 1;
336
- let codecId = "";
337
- const minf = childOf(moov, mdia.dataOffset, mdia.end, "minf");
338
- const stbl = minf && childOf(moov, minf.dataOffset, minf.end, "stbl");
339
- const stsd = stbl && childOf(moov, stbl.dataOffset, stbl.end, "stsd");
340
- if (stsd) {
341
- const first = readBox(moov, stsd.dataOffset + 8);
342
- if (first) codecId = first.type;
343
- }
344
- result.push(new AudioTrack({
345
- trackNumber: trackId,
346
- declaredIndex: audioIdx,
347
- codecId,
348
- language: resolvedLang,
349
- languageBcp47,
350
- name: "",
351
- isEnabled,
352
- isDefault: true,
353
- declaresDefault: false,
354
- codecPrivateB64: "",
355
- alternateGroup,
356
- isOriginal: false,
357
- isCommentary: false,
358
- isVisualImpaired: false
359
- }));
360
- }
361
- }
362
- return result;
363
- }
364
-
365
- /**
366
- * The text field of one cue as MP4 frames it.
367
- *
368
- * A `tx3g`/`text` sample is a 16-bit big-endian length followed by that many
369
- * bytes of UTF-8 (ISO/IEC 14496-12 §12.6.3 and Apple's text sample format); a
370
- * `wvtt` sample is a sequence of boxes whose `vttc`/`payl` holds the cue text
371
- * (§12.6.3.2). Neither carries the subtitle format's own markup, so the
372
- * markup step that follows has nothing to take off — it is applied all the
373
- * same, because which step applies is decided by the codec and not here.
374
- *
375
- * The byte reading itself stays in `container-index/mp4-subtitles.js`,
376
- * alongside the sample-table walk that found the range.
377
- *
378
- * @param {Buffer} payload - The sample's own bytes.
379
- * @param {string} codecId - Sample entry type: `tx3g`, `text` or `wvtt`.
380
- * @returns {string}
381
- */
382
- static cueTextOf(payload, codecId) {
383
- return decodeSubtitleSample(payload, codecId);
384
- }
385
-
386
- async readKeyframeIndex() {
387
- const r = await readMp4KeyframeTimes(this.readRange, this.fileSize);
388
- if (!r) return null;
389
- if (Array.isArray(r)) return { times: r, tolerance: 0 };
390
- return r;
391
- }
392
- }
1
+ /**
2
+ * @file MP4/MOV container — ISO/IEC 14496-12.
3
+ *
4
+ * Parses moov for all track types in one walk:
5
+ * - tkhd: track_ID, flags track_enabled (0x000001), alternate_group, width/height
6
+ * - mdhd: timescale, language (packed 5-bit), version handling
7
+ * - hdlr: handler_type (vide/soun/text/sbtl/subt/subp/clcp)
8
+ * - elng: extendedLanguage BCP47 (when present, replaces mdhd language per spec)
9
+ * - stsd: sample entry format (avc1/hev1/mp4a/tx3g/wvtt/stpp)
10
+ * - stbl tables for subtitle cue ranges (stts/stsz/stsc/stco/co64) and video keyframes (stss/stts/ctts/elst)
11
+ *
12
+ * Keyframe reading is delegated to mp4.js. The subtitle sample table is read in
13
+ * this module, because every rule in it is a statement of ISO/IEC 14496-12 about
14
+ * this container. Track creation is centralised here so that track_enabled,
15
+ * `elng` and alternate_group are handled once for every media type.
16
+ */
17
+
18
+ import { Container } from "./Container.js";
19
+ import { isMp4, readMp4KeyframeTimes } from "../container-index/mp4.js";
20
+ import { VideoTrack } from "../tracks/VideoTrack.js";
21
+ import { AudioTrack } from "../tracks/AudioTrack.js";
22
+ import { TextSubtitleTrack, TEXT_FORMATS_MP4 } from "../tracks/TextSubtitleTrack.js";
23
+ import { ImageSubtitleTrack } from "../tracks/ImageSubtitleTrack.js";
24
+
25
+ /** A box header is eight bytes, or sixteen when the size field says 1 (§4.2). */
26
+ const HEADER_BYTES = 8;
27
+ const LARGE_SIZE_MARKER = 1;
28
+ const LARGE_HEADER_BYTES = 16;
29
+
30
+ /**
31
+ * One box header, per ISO/IEC 14496-12 §4.2.
32
+ *
33
+ * @param {Buffer} buf
34
+ * @param {number} off
35
+ * @returns {{ type: string, size: number, dataOffset: number, end: number } | null}
36
+ */
37
+ function readBox(buffer, offset) {
38
+ if (offset + HEADER_BYTES > buffer.length) {
39
+ return null;
40
+ }
41
+ let size = buffer.readUInt32BE(offset);
42
+ const type = buffer.toString("latin1", offset + 4, offset + 8);
43
+ let headerBytes = HEADER_BYTES;
44
+ // A size of 1 means the real size is the 64-bit value after the type.
45
+ if (size === LARGE_SIZE_MARKER) {
46
+ if (offset + LARGE_HEADER_BYTES > buffer.length) {
47
+ return null;
48
+ }
49
+ size = Number(buffer.readBigUInt64BE(offset + HEADER_BYTES));
50
+ headerBytes = LARGE_HEADER_BYTES;
51
+ }
52
+ if (size < headerBytes) {
53
+ return null;
54
+ }
55
+ return { type, size, dataOffset: offset + headerBytes, end: offset + size };
56
+ }
57
+
58
+ /**
59
+ * Every direct child box of the given type.
60
+ *
61
+ * @param {Buffer} buf
62
+ * @param {number} start
63
+ * @param {number} end
64
+ * @param {string} type
65
+ * @returns {Array<{ type: string, size: number, dataOffset: number, end: number }>}
66
+ */
67
+ function childrenOf(buf, start, end, type) {
68
+ const out = [];
69
+ let p = start;
70
+ while (p + 8 <= end) {
71
+ const b = readBox(buf, p);
72
+ if (!b) break;
73
+ if (b.type === type) out.push(b);
74
+ p = b.end;
75
+ }
76
+ return out;
77
+ }
78
+
79
+ /**
80
+ * The first direct child box of the given type, or null.
81
+ *
82
+ * @param {Buffer} buf
83
+ * @param {number} s
84
+ * @param {number} e
85
+ * @param {string} t
86
+ * @returns {{ type: string, size: number, dataOffset: number, end: number } | null}
87
+ */
88
+ function childOf(buf, s, e, t) {
89
+ return childrenOf(buf, s, e, t)[0] ?? null;
90
+ }
91
+
92
+ export class Mp4Container extends Container {
93
+ get formatName() {
94
+ return "mp4";
95
+ }
96
+
97
+ static detect(head) {
98
+ return isMp4(head);
99
+ }
100
+
101
+ /**
102
+ * This container's text subtitle tracks, with every cue's time and byte
103
+ * range ISO/IEC 14496-12 §8.5 and §8.7.
104
+ *
105
+ * @param {(start:number,end:number)=>Promise<Buffer|null>} readRange
106
+ * @param {number} fileSize
107
+ * @returns {Promise<{ tracks: object[] } | null>}
108
+ */
109
+ static readSubtitlePlan(readRange, fileSize) {
110
+ return readMp4SubtitlePlan(readRange, fileSize);
111
+ }
112
+
113
+ /**
114
+ * The same reading, over the file this container was built on.
115
+ *
116
+ * The static form exists for a caller that has bytes and no container; this
117
+ * is the one to use otherwise, because the reader is already here.
118
+ *
119
+ * @returns {Promise<object|null>}
120
+ */
121
+ readSubtitlePlan() {
122
+ return Mp4Container.readSubtitlePlan(this.readRange, this.fileSize);
123
+ }
124
+
125
+
126
+ /**
127
+ * Read the samples of one subtitle track that are HELD now.
128
+ *
129
+ * An MP4 states every sample's byte range in its own table, so a cue costs its
130
+ * own few dozen bytes rather than the cluster around it — which is why this
131
+ * reads per sample where Matroska reads per cluster.
132
+ *
133
+ * Nothing is fetched: a sample whose bytes are not downloaded is left for the
134
+ * next call.
135
+ *
136
+ * @param {{ codecId: string, samples: {offset: number, size: number, startSeconds: number, endSeconds: number}[] }} track
137
+ * @param {Set<number>} harvested - Sample offsets already read; added to.
138
+ * @returns {Promise<{startSeconds: number, endSeconds: number, text: string}[]>}
139
+ * The cues found in THIS pass.
140
+ */
141
+ async readHeldSamples(track, harvested) {
142
+ const found = [];
143
+ for (const sample of track?.samples ?? []) {
144
+ if (harvested.has(sample.offset)) {
145
+ continue;
146
+ }
147
+ const last = Math.min(this.fileSize - 1, sample.offset + sample.size - 1);
148
+ if (!this.isHeld(sample.offset, last)) {
149
+ continue;
150
+ }
151
+ const bytes = await this.readHeld(sample.offset, last);
152
+ if (!bytes) {
153
+ continue;
154
+ }
155
+ harvested.add(sample.offset);
156
+ // The MP4 has framed this cue and is the one that unframes it.
157
+ const text = Mp4Container.cueTextOf(bytes, track.codecId);
158
+ if (text) {
159
+ found.push({ startSeconds: sample.startSeconds, endSeconds: sample.endSeconds, text });
160
+ }
161
+ }
162
+ return found;
163
+ }
164
+
165
+ async readTracks() {
166
+ const head = await this.readRange(0, Math.min(64 - 1, this.fileSize - 1));
167
+ if (!head || !isMp4(head)) return [];
168
+
169
+ // Use the subtitle plan reader's moov parsing as source for subtitle tracks,
170
+ // and a direct moov walk for video/audio to collect tkhd/mdhd/hdlr/elng uniformly.
171
+ // For simplicity, delegate entirely to readMp4SubtitlePlan for subtitles and
172
+ // do a lightweight moov walk for video/audio here — then merge.
173
+ const subtitlePlan = await readMp4SubtitlePlan(this.readRange, this.fileSize).catch(() => null);
174
+ const subtitleByDecl = new Map();
175
+ if (subtitlePlan?.tracks) {
176
+ for (const t of subtitlePlan.tracks) subtitleByDecl.set(t.declaredIndex, t);
177
+ }
178
+
179
+ // Minimal moov walk for video/audio: reuse isMp4 + findMoov logic by reading via existing helper
180
+ // Instead of duplicating, parse tracks via a second full moov read that collects vide/soun.
181
+ // We read moov box directly to extract video/audio tracks.
182
+ const tracks = await this.#readVideoAudioTracks();
183
+ // Append subtitle tracks from plan, converting to domain objects
184
+ if (subtitlePlan?.tracks) {
185
+ for (const s of subtitlePlan.tracks) {
186
+ const isText = TEXT_FORMATS_MP4.has(s.format);
187
+ const Cls = isText ? TextSubtitleTrack : ImageSubtitleTrack;
188
+ tracks.push(new Cls({
189
+ trackNumber: s.trackId,
190
+ declaredIndex: s.declaredIndex,
191
+ codecId: s.format,
192
+ language: s.language,
193
+ languageBcp47: "",
194
+ name: "",
195
+ isEnabled: true,
196
+ isDefault: s.declaredIndex === 0,
197
+ declaresDefault: false,
198
+ codecPrivateB64: "",
199
+ // The sample entry's own words, read below. Either
200
+ // bit is enough: a file that sets only "all samples are forced" is
201
+ // saying what a well-formed one says twice.
202
+ isForced: s.someSamplesForced === true || s.allSamplesForced === true,
203
+ isHearingImpaired: false,
204
+ clusterPositions: [],
205
+ samples: s.samples
206
+ }));
207
+ }
208
+ // Count non-text subtitle handlers (subp/clcp/stpp) for declaredIndex correctness they are already
209
+ // accounted for in subtitlePlan's declaredIndex via SUBTITLE_HANDLERS, but we didn't create objects for
210
+ // them above when they were stpp (non-text not in plan's tracks). The plan already excludes stpp from tracks
211
+ // but increments declaredIndex, so alignment holds: we don't need extra placeholders.
212
+ }
213
+ return tracks;
214
+ }
215
+
216
+ /**
217
+ * The `moov` box, read whole.
218
+ *
219
+ * Held on the instance because every question this class answers is inside
220
+ * it, and the box can be tens of megabytes off a torrent reading it once
221
+ * per file is the difference between one fetch and one per question.
222
+ *
223
+ * @returns {Promise<{ moov: Buffer, header: number } | null>}
224
+ */
225
+ async #moovBuffer() {
226
+ if (this.moovHeld !== undefined) {
227
+ return this.moovHeld;
228
+ }
229
+ const PROBE = 64;
230
+ const MAX_MOOV = 32 * 1024 * 1024;
231
+ let at = 0;
232
+ let moovBox = null;
233
+ while (at < this.fileSize) {
234
+ const probe = await this.readRange(at, Math.min(this.fileSize - 1, at + PROBE - 1));
235
+ if (!probe || probe.length < 8) break;
236
+ let size = probe.readUInt32BE(0);
237
+ const type = probe.toString("latin1", 4, 8);
238
+ let header = 8;
239
+ if (size === 1) {
240
+ if (probe.length < 16) break;
241
+ size = Number(probe.readBigUInt64BE(8));
242
+ header = 16;
243
+ }
244
+ if (size <= 0) break;
245
+ if (type === "moov") { moovBox = { offset: at, size, header }; break; }
246
+ at += size;
247
+ }
248
+ if (!moovBox || moovBox.size > MAX_MOOV) {
249
+ this.moovHeld = null;
250
+ return null;
251
+ }
252
+ const moov = await this.readRange(moovBox.offset, Math.min(this.fileSize - 1, moovBox.offset + moovBox.size - 1));
253
+ this.moovHeld = moov ? { moov, header: moovBox.header } : null;
254
+ return this.moovHeld;
255
+ }
256
+
257
+ /**
258
+ * Duration from `mvhd` and the presentation offset from the first track's
259
+ * edit list, per ISO/IEC 14496-12 §8.2.2 and §8.6.6.
260
+ *
261
+ * An edit entry whose `media_time` is -1 is an EMPTY edit: it presents
262
+ * nothing for `segment_duration`, which shifts everything after it later by
263
+ * that much. That shift is what a player reports as the file's start, and it
264
+ * is the only way an MP4 states one — a file without such an edit begins at
265
+ * zero, which is a declaration, not an absence.
266
+ *
267
+ * @returns {Promise<import("./Container.js").ContainerMediaInfo>}
268
+ */
269
+ async readMediaInfo() {
270
+ if (this.mediaInfo) {
271
+ return this.mediaInfo;
272
+ }
273
+ /** @type {import("./Container.js").ContainerMediaInfo} */
274
+ const info = { format: this.formatName, durationSeconds: null, startTimeSeconds: null };
275
+ this.mediaInfo = info;
276
+ const held = await this.#moovBuffer();
277
+ if (!held) {
278
+ return info;
279
+ }
280
+ const { moov, header } = held;
281
+ const mvhd = childOf(moov, header, moov.length, "mvhd");
282
+ let movieTimescale = 0;
283
+ if (mvhd) {
284
+ const version = moov[mvhd.dataOffset];
285
+ // version 0: creation(4) modification(4) timescale(4) duration(4)
286
+ // version 1: creation(8) modification(8) timescale(4) duration(8)
287
+ const at = version === 1 ? mvhd.dataOffset + 20 : mvhd.dataOffset + 12;
288
+ if (at + 8 <= moov.length) {
289
+ movieTimescale = moov.readUInt32BE(at);
290
+ const duration = version === 1 ? Number(moov.readBigUInt64BE(at + 4)) : moov.readUInt32BE(at + 4);
291
+ if (movieTimescale > 0 && duration > 0) {
292
+ info.durationSeconds = duration / movieTimescale;
293
+ }
294
+ }
295
+ }
296
+ info.startTimeSeconds = movieTimescale > 0
297
+ ? Mp4Container.#emptyEditSeconds(moov, header, movieTimescale)
298
+ : null;
299
+ return info;
300
+ }
301
+
302
+ /**
303
+ * The presentation shift of the first empty edit, in seconds; 0 when no track
304
+ * declares one.
305
+ *
306
+ * @param {Buffer} moov
307
+ * @param {number} header
308
+ * @param {number} movieTimescale
309
+ * @returns {number}
310
+ */
311
+ static #emptyEditSeconds(moov, header, movieTimescale) {
312
+ let shift = 0;
313
+ for (const trak of childrenOf(moov, header, moov.length, "trak")) {
314
+ const edts = childOf(moov, trak.dataOffset, trak.end, "edts");
315
+ const elst = edts && childOf(moov, edts.dataOffset, edts.end, "elst");
316
+ if (!elst) {
317
+ continue;
318
+ }
319
+ const version = moov[elst.dataOffset];
320
+ const count = moov.readUInt32BE(elst.dataOffset + 4);
321
+ if (count < 1) {
322
+ continue;
323
+ }
324
+ const entry = elst.dataOffset + 8;
325
+ const segmentDuration = version === 1
326
+ ? Number(moov.readBigUInt64BE(entry))
327
+ : moov.readUInt32BE(entry);
328
+ const mediaTime = version === 1
329
+ ? Number(moov.readBigInt64BE(entry + 8))
330
+ : moov.readInt32BE(entry + 4);
331
+ if (mediaTime === -1 && segmentDuration > 0) {
332
+ shift = Math.max(shift, segmentDuration / movieTimescale);
333
+ }
334
+ }
335
+ return shift;
336
+ }
337
+
338
+ async #readVideoAudioTracks() {
339
+ const held = await this.#moovBuffer();
340
+ if (!held) return [];
341
+ const { moov, header: moovHeader } = held;
342
+
343
+ const result = [];
344
+ let videoIdx = -1;
345
+ let audioIdx = -1;
346
+
347
+ const moovContentStart = moovHeader;
348
+ const moovEnd = moov.length;
349
+ for (const trak of childrenOf(moov, moovContentStart, moovEnd, "trak")) {
350
+ const mdia = childOf(moov, trak.dataOffset, trak.end, "mdia");
351
+ if (!mdia) continue;
352
+ const hdlr = childOf(moov, mdia.dataOffset, mdia.end, "hdlr");
353
+ const handler = hdlr ? moov.toString("latin1", hdlr.dataOffset + 8, hdlr.dataOffset + 12) : "";
354
+ const mdhd = childOf(moov, mdia.dataOffset, mdia.end, "mdhd");
355
+ const tkhd = childOf(moov, trak.dataOffset, trak.end, "tkhd");
356
+ let language = "";
357
+ if (mdhd) {
358
+ const ver = moov[mdhd.dataOffset];
359
+ const langAt = ver === 1 ? mdhd.dataOffset + 32 : mdhd.dataOffset + 20;
360
+ if (langAt + 2 <= mdhd.end) {
361
+ const packed = moov.readUInt16BE(langAt);
362
+ language = [10, 5, 0].map((s) => String.fromCharCode(((packed >> s) & 0x1f) + 0x60)).join("").replace(/[^a-z]/g, "");
363
+ }
364
+ }
365
+ // elng overrides mdhd language per spec §8.4.6
366
+ let languageBcp47 = "";
367
+ const elng = childOf(moov, mdia.dataOffset, mdia.end, "elng");
368
+ if (elng && elng.end - elng.dataOffset >= 4) {
369
+ languageBcp47 = moov.toString("utf8", elng.dataOffset + 4, elng.end).replace(/\0+$/, "");
370
+ }
371
+ let trackId = 0;
372
+ let isEnabled = true;
373
+ let alternateGroup = 0;
374
+ let width = null;
375
+ let height = null;
376
+ if (tkhd) {
377
+ const ver = moov[tkhd.dataOffset];
378
+ const flags = moov.readUInt32BE(tkhd.dataOffset + 1) & 0xffffff; // 3 bytes after version
379
+ isEnabled = (flags & 0x000001) !== 0;
380
+ trackId = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 20 : tkhd.dataOffset + 12);
381
+ alternateGroup = moov.readUInt16BE(ver === 1 ? tkhd.dataOffset + 26 : tkhd.dataOffset + 18);
382
+ // width/height are 16.16 fixed point at end of tkhd
383
+ if (tkhd.end - tkhd.dataOffset >= 84) {
384
+ const w = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 76 : tkhd.dataOffset + 68);
385
+ const h = moov.readUInt32BE(ver === 1 ? tkhd.dataOffset + 80 : tkhd.dataOffset + 72);
386
+ width = w / 65536;
387
+ height = h / 65536;
388
+ }
389
+ }
390
+ const resolvedLang = languageBcp47 || language;
391
+ if (handler === "vide") {
392
+ videoIdx += 1;
393
+ // stsd format for codecId
394
+ let codecId = "";
395
+ const minf = childOf(moov, mdia.dataOffset, mdia.end, "minf");
396
+ const stbl = minf && childOf(moov, minf.dataOffset, minf.end, "stbl");
397
+ const stsd = stbl && childOf(moov, stbl.dataOffset, stbl.end, "stsd");
398
+ if (stsd) {
399
+ const first = readBox(moov, stsd.dataOffset + 8);
400
+ if (first) codecId = first.type;
401
+ }
402
+ result.push(new VideoTrack({
403
+ trackNumber: trackId,
404
+ declaredIndex: videoIdx,
405
+ codecId,
406
+ language: resolvedLang,
407
+ languageBcp47,
408
+ name: "",
409
+ isEnabled,
410
+ isDefault: true,
411
+ declaresDefault: false,
412
+ codecPrivateB64: "",
413
+ alternateGroup,
414
+ width,
415
+ height
416
+ }));
417
+ } else if (handler === "soun") {
418
+ audioIdx += 1;
419
+ let codecId = "";
420
+ const minf = childOf(moov, mdia.dataOffset, mdia.end, "minf");
421
+ const stbl = minf && childOf(moov, minf.dataOffset, minf.end, "stbl");
422
+ const stsd = stbl && childOf(moov, stbl.dataOffset, stbl.end, "stsd");
423
+ if (stsd) {
424
+ const first = readBox(moov, stsd.dataOffset + 8);
425
+ if (first) codecId = first.type;
426
+ }
427
+ result.push(new AudioTrack({
428
+ trackNumber: trackId,
429
+ declaredIndex: audioIdx,
430
+ codecId,
431
+ language: resolvedLang,
432
+ languageBcp47,
433
+ name: "",
434
+ isEnabled,
435
+ isDefault: true,
436
+ declaresDefault: false,
437
+ codecPrivateB64: "",
438
+ alternateGroup,
439
+ isOriginal: false,
440
+ isCommentary: false,
441
+ isVisualImpaired: false
442
+ }));
443
+ }
444
+ }
445
+ return result;
446
+ }
447
+
448
+ /**
449
+ * The text field of one cue as MP4 frames it.
450
+ *
451
+ * A `tx3g`/`text` sample is a 16-bit big-endian length followed by that many
452
+ * bytes of UTF-8 (ISO/IEC 14496-12 §12.6.3 and Apple's text sample format); a
453
+ * `wvtt` sample is a sequence of boxes whose `vttc`/`payl` holds the cue text
454
+ * (§12.6.3.2). Neither carries the subtitle format's own markup, so the
455
+ * markup step that follows has nothing to take off — it is applied all the
456
+ * same, because which step applies is decided by the codec and not here.
457
+ *
458
+ * The byte reading itself is this module's,
459
+ * alongside the sample-table walk that found the range.
460
+ *
461
+ * @param {Buffer} payload - The sample's own bytes.
462
+ * @param {string} codecId - Sample entry type: `tx3g`, `text` or `wvtt`.
463
+ * @returns {string}
464
+ */
465
+ static cueTextOf(payload, codecId) {
466
+ return decodeSubtitleSample(payload, codecId);
467
+ }
468
+
469
+ async readKeyframeIndex() {
470
+ const r = await readMp4KeyframeTimes(this.readRange, this.fileSize);
471
+ if (!r) return null;
472
+ if (Array.isArray(r)) return { times: r, tolerance: 0 };
473
+ return r;
474
+ }
475
+ }
476
+
477
+ // ---------------------------------------------------------------------------
478
+ // The MP4's own reading of its subtitle sample table. Here because every rule
479
+ // in it is ISO/IEC 14496-12 speaking about this container, and the class is
480
+ // the only way in.
481
+ // ---------------------------------------------------------------------------
482
+ /**
483
+ * @file The text subtitle tracks of an MP4, and where each cue's bytes are.
484
+ *
485
+ * The same rule as the Matroska side: nothing is extracted with ffmpeg and
486
+ * nothing is fetched for its own sake. Here it is cheaper still. Matroska hides
487
+ * its subtitle blocks inside clusters shared with the picture, so a cue costs
488
+ * whatever cluster holds it; an MP4 states every sample's offset and length in
489
+ * the sample table, so a cue costs its own bytes and nothing more — usually a
490
+ * few dozen of them.
491
+ *
492
+ * The tables, from ISO/IEC 14496-12:
493
+ *
494
+ * stsd — what the samples are (`tx3g` timed text, `wvtt` WebVTT, `stpp` TTML)
495
+ * stts — how long each sample lasts, run-length encoded (§8.6.1.2)
496
+ * stsz — how long each sample is, in bytes (§8.7.3)
497
+ * stsc — how samples are grouped into chunks (§8.7.4)
498
+ * stco / co64 — where each chunk begins in the file (§8.7.5)
499
+ *
500
+ * Together they give, for sample N: when it starts, how long it stays, and the
501
+ * exact byte range holding it. That is everything needed to show a cue without
502
+ * reading anything else.
503
+ */
504
+
505
+ const PROBE_BYTES = 64;
506
+ const MAX_MOOV_BYTES = 32 * 1024 * 1024;
507
+
508
+ /** Handlers that mean "this track is text on screen". */
509
+ const TEXT_HANDLERS = new Set(["text", "sbtl", "subt"]);
510
+ /**
511
+ * Every handler ffmpeg's mov demuxer turns into a SUBTITLE stream, whether or
512
+ * not this file can read it — `subp` is a DVD subpicture and `clcp` closed
513
+ * captions, both pictures or caption data rather than text. They are counted
514
+ * because `declaredIndex` has to equal ffmpeg's `0:s:N`, and a track left out
515
+ * of the count shifts every text track after it, which is the very defect
516
+ * `declaredIndex` exists to remove.
517
+ */
518
+ const SUBTITLE_HANDLERS = new Set([...TEXT_HANDLERS, "subp", "clcp"]);
519
+ /** Sample formats this can turn into cues. `stpp` (TTML) is XML and is not one. */
520
+ const TEXT_FORMATS = new Set(["tx3g", "text", "wvtt"]);
521
+
522
+
523
+
524
+
525
+ /**
526
+ * Walk the top level of the file to find `moov`, reading only box headers.
527
+ *
528
+ * @param {(start: number, end: number) => Promise<Buffer | null>} readRange
529
+ * @param {number} fileSize
530
+ * @returns {Promise<{ offset: number, size: number } | null>}
531
+ */
532
+ async function findMoov(readRange, fileSize) {
533
+ let at = 0;
534
+ while (at < fileSize) {
535
+ const probe = await readRange(at, Math.min(fileSize - 1, at + PROBE_BYTES - 1));
536
+ if (!probe || probe.length < HEADER_BYTES) {
537
+ return null;
538
+ }
539
+ const box = readBox(probe, 0);
540
+ if (!box) {
541
+ return null;
542
+ }
543
+ if (box.type === "moov") {
544
+ return { offset: at, size: box.size };
545
+ }
546
+ at += box.size;
547
+ }
548
+ return null;
549
+ }
550
+
551
+ /**
552
+ * Sample durations, expanded from the run-length table.
553
+ *
554
+ * @param {Buffer} moov
555
+ * @param {{ dataOffset: number, end: number }} stts
556
+ * @param {number} total - How many samples the size table declares.
557
+ * @returns {number[]} Ticks each sample lasts.
558
+ */
559
+ function sampleDurations(moov, stts, total) {
560
+ const durations = new Array(total).fill(0);
561
+ const entries = moov.readUInt32BE(stts.dataOffset + 4);
562
+ let at = stts.dataOffset + 8;
563
+ let sample = 0;
564
+ for (let entry = 0; entry < entries && at + 8 <= stts.end && sample < total; entry += 1, at += 8) {
565
+ const count = moov.readUInt32BE(at);
566
+ const delta = moov.readUInt32BE(at + 4);
567
+ for (let index = 0; index < count && sample < total; index += 1, sample += 1) {
568
+ durations[sample] = delta;
569
+ }
570
+ }
571
+ return durations;
572
+ }
573
+
574
+ /**
575
+ * Sample sizes, whether the table states one for all or one for each.
576
+ *
577
+ * @param {Buffer} moov
578
+ * @param {{ dataOffset: number, end: number }} stsz
579
+ * @returns {number[]}
580
+ */
581
+ function sampleSizes(moov, stsz) {
582
+ const uniform = moov.readUInt32BE(stsz.dataOffset + 4);
583
+ const count = moov.readUInt32BE(stsz.dataOffset + 8);
584
+ if (uniform > 0) {
585
+ return new Array(count).fill(uniform);
586
+ }
587
+ const sizes = new Array(count).fill(0);
588
+ let at = stsz.dataOffset + 12;
589
+ for (let index = 0; index < count && at + 4 <= stsz.end; index += 1, at += 4) {
590
+ sizes[index] = moov.readUInt32BE(at);
591
+ }
592
+ return sizes;
593
+ }
594
+
595
+ /**
596
+ * Where every sample of a track begins in the file.
597
+ *
598
+ * The sample-to-chunk table says how many samples each run of chunks holds, and
599
+ * the chunk-offset table says where each chunk starts; a sample's own offset is
600
+ * its chunk's start plus the sizes of the samples before it in that chunk.
601
+ *
602
+ * @param {Buffer} moov
603
+ * @param {{ dataOffset: number, end: number }} stsc
604
+ * @param {number[]} chunkOffsets
605
+ * @param {number[]} sizes
606
+ * @returns {number[]}
607
+ */
608
+ function sampleOffsets(moov, stsc, chunkOffsets, sizes) {
609
+ const offsets = new Array(sizes.length).fill(0);
610
+ const entries = moov.readUInt32BE(stsc.dataOffset + 4);
611
+ /** @type {{ firstChunk: number, perChunk: number }[]} */
612
+ const runs = [];
613
+ let at = stsc.dataOffset + 8;
614
+ for (let entry = 0; entry < entries && at + 12 <= stsc.end; entry += 1, at += 12) {
615
+ runs.push({ firstChunk: moov.readUInt32BE(at), perChunk: moov.readUInt32BE(at + 4) });
616
+ }
617
+ let sample = 0;
618
+ for (let run = 0; run < runs.length && sample < sizes.length; run += 1) {
619
+ const from = runs[run].firstChunk;
620
+ const to = run + 1 < runs.length ? runs[run + 1].firstChunk - 1 : chunkOffsets.length;
621
+ for (let chunk = from; chunk <= to && sample < sizes.length; chunk += 1) {
622
+ let inChunk = chunkOffsets[chunk - 1];
623
+ if (inChunk === undefined) {
624
+ break;
625
+ }
626
+ for (let index = 0; index < runs[run].perChunk && sample < sizes.length; index += 1, sample += 1) {
627
+ offsets[sample] = inChunk;
628
+ inChunk += sizes[sample];
629
+ }
630
+ }
631
+ }
632
+ return offsets;
633
+ }
634
+
635
+ /**
636
+ * @typedef {object} Mp4SubtitleSample
637
+ * @property {number} startSeconds
638
+ * @property {number} endSeconds
639
+ * @property {number} offset - Where the sample's bytes are in the file.
640
+ * @property {number} size
641
+ */
642
+
643
+ /**
644
+ * @typedef {object} Mp4SubtitleTrack
645
+ * @property {number} trackId
646
+ * @property {number} declaredIndex - Its position among ALL of the file's
647
+ * subtitle tracks, including the ones whose sample format this cannot turn
648
+ * into cues (`stpp` TTML). That is the number ffmpeg gives the same stream in
649
+ * `0:s:N`, which is the number the browser names; counting only the readable
650
+ * ones would shift every track after a TTML one.
651
+ * @property {string} format - `tx3g`, `text` or `wvtt`.
652
+ * @property {string} language - Three letters, as the file declares them.
653
+ * @property {boolean} someSamplesForced - The sample entry says at least one
654
+ * cue carries a forced (`frcd`) atom.
655
+ * @property {boolean} allSamplesForced - The sample entry says every cue is to
656
+ * be treated as forced, whether or not it carries that atom.
657
+ * @property {Mp4SubtitleSample[]} samples - In time order.
658
+ */
659
+
660
+ /**
661
+ * The text subtitle tracks of an MP4, with every cue's time and byte range.
662
+ *
663
+ * @param {(start: number, end: number) => Promise<Buffer | null>} readRange
664
+ * @param {number} fileSize
665
+ * @returns {Promise<{ tracks: Mp4SubtitleTrack[] } | null>}
666
+ */
667
+ async function readMp4SubtitlePlan(readRange, fileSize) {
668
+ const found = await findMoov(readRange, fileSize);
669
+ if (!found || found.size > MAX_MOOV_BYTES) {
670
+ return null;
671
+ }
672
+ const moov = await readRange(found.offset, Math.min(fileSize - 1, found.offset + found.size - 1));
673
+ if (!moov || moov.length < HEADER_BYTES) {
674
+ return null;
675
+ }
676
+ const moovBox = readBox(moov, 0);
677
+ if (!moovBox) {
678
+ return null;
679
+ }
680
+
681
+ /** @type {Mp4SubtitleTrack[]} */
682
+ const tracks = [];
683
+ // Counts every subtitle track the file has, whether or not this can read it,
684
+ // so the number handed out matches ffmpeg's `0:s:N`. See `declaredIndex`.
685
+ let declaredIndex = -1;
686
+ for (const trak of childrenOf(moov, moovBox.dataOffset, moov.length, "trak")) {
687
+ const mdia = childOf(moov, trak.dataOffset, trak.end, "mdia");
688
+ if (!mdia) {
689
+ continue;
690
+ }
691
+ const hdlr = childOf(moov, mdia.dataOffset, mdia.end, "hdlr");
692
+ const handler = hdlr ? moov.toString("latin1", hdlr.dataOffset + 8, hdlr.dataOffset + 12) : "";
693
+ if (!SUBTITLE_HANDLERS.has(handler)) {
694
+ continue;
695
+ }
696
+ // Counted before the readability checks below, and before the handler is
697
+ // narrowed to the text ones: this number is the track's place in the file,
698
+ // not its place among the tracks this code can turn into cues.
699
+ declaredIndex += 1;
700
+ if (!TEXT_HANDLERS.has(handler)) {
701
+ continue;
702
+ }
703
+ const mdhd = childOf(moov, mdia.dataOffset, mdia.end, "mdhd");
704
+ if (!mdhd) {
705
+ continue;
706
+ }
707
+ const version = moov[mdhd.dataOffset];
708
+ const timescale = moov.readUInt32BE(version === 1 ? mdhd.dataOffset + 20 : mdhd.dataOffset + 12);
709
+ if (!timescale) {
710
+ continue;
711
+ }
712
+ // The language is five bits per letter, offset from 0x60, packed into two
713
+ // bytes after the times (ISO/IEC 14496-12 §8.4.2.3).
714
+ const languageAt = version === 1 ? mdhd.dataOffset + 32 : mdhd.dataOffset + 20;
715
+ let language = "";
716
+ if (languageAt + 2 <= mdhd.end) {
717
+ const packed = moov.readUInt16BE(languageAt);
718
+ language = [10, 5, 0]
719
+ .map((shift) => String.fromCharCode(((packed >> shift) & 0x1f) + 0x60))
720
+ .join("")
721
+ .replace(/[^a-z]/g, "");
722
+ }
723
+
724
+ const tkhd = childOf(moov, trak.dataOffset, trak.end, "tkhd");
725
+ const trackId = tkhd
726
+ ? moov.readUInt32BE(moov[tkhd.dataOffset] === 1 ? tkhd.dataOffset + 20 : tkhd.dataOffset + 12)
727
+ : tracks.length + 1;
728
+
729
+ const minf = childOf(moov, mdia.dataOffset, mdia.end, "minf");
730
+ const stbl = minf && childOf(moov, minf.dataOffset, minf.end, "stbl");
731
+ if (!stbl) {
732
+ continue;
733
+ }
734
+ const stsd = childOf(moov, stbl.dataOffset, stbl.end, "stsd");
735
+ const first = stsd && readBox(moov, stsd.dataOffset + 8);
736
+ const format = first ? first.type : "";
737
+ if (!TEXT_FORMATS.has(format)) {
738
+ continue;
739
+ }
740
+ // Whether the file itself says this track is forced — subtitles shown even
741
+ // to a viewer who did not ask for subtitles, because the dialogue on screen
742
+ // is in a language the soundtrack is not.
743
+ //
744
+ // Apple's QuickTime File Format, "Display flags" under Subtitle sample
745
+ // description, defines the two bits read here: `0x40000000` "Some samples
746
+ // are forced" ("at least one sample contains a forced (`frcd`) atom") and
747
+ // `0x80000000` "All samples are forced" ("the subtitle media handler treats
748
+ // all samples as forced subtitles, regardless of the presence or absence of
749
+ // a `frcd` atom"), with the note that setting the second requires the first
750
+ // — the pair together being `0xC0000000`. We honour that requirement rather
751
+ // than trusting a writer to have met it: either bit alone is enough to call
752
+ // the track forced, because a file that sets only `0x80000000` is saying
753
+ // exactly what a well-formed one would say twice.
754
+ //
755
+ // The field sits at a fixed place in the sample entry. `dataOffset` is
756
+ // already past the box header, and every sample entry opens with 6 reserved
757
+ // bytes and a 2-byte data reference index (ISO/IEC 14496-12 §8.5.2.2), so
758
+ // `displayFlags` is the 32 bits eight bytes in.
759
+ let someSamplesForced = false;
760
+ let allSamplesForced = false;
761
+ if (format === "tx3g" && first && first.dataOffset + 8 + 4 <= first.end) {
762
+ const displayFlags = moov.readUInt32BE(first.dataOffset + 8);
763
+ someSamplesForced = (displayFlags & 0x40000000) !== 0;
764
+ allSamplesForced = (displayFlags & 0x80000000) !== 0;
765
+ }
766
+ const stts = childOf(moov, stbl.dataOffset, stbl.end, "stts");
767
+ const stsz = childOf(moov, stbl.dataOffset, stbl.end, "stsz");
768
+ const stsc = childOf(moov, stbl.dataOffset, stbl.end, "stsc");
769
+ const stco = childOf(moov, stbl.dataOffset, stbl.end, "stco");
770
+ const co64 = childOf(moov, stbl.dataOffset, stbl.end, "co64");
771
+ if (!stts || !stsz || !stsc || (!stco && !co64)) {
772
+ continue;
773
+ }
774
+
775
+ const sizes = sampleSizes(moov, stsz);
776
+ const durations = sampleDurations(moov, stts, sizes.length);
777
+ const chunkOffsets = [];
778
+ if (stco) {
779
+ const count = moov.readUInt32BE(stco.dataOffset + 4);
780
+ let at = stco.dataOffset + 8;
781
+ for (let index = 0; index < count && at + 4 <= stco.end; index += 1, at += 4) {
782
+ chunkOffsets.push(moov.readUInt32BE(at));
783
+ }
784
+ } else {
785
+ const count = moov.readUInt32BE(co64.dataOffset + 4);
786
+ let at = co64.dataOffset + 8;
787
+ for (let index = 0; index < count && at + 8 <= co64.end; index += 1, at += 8) {
788
+ chunkOffsets.push(Number(moov.readBigUInt64BE(at)));
789
+ }
790
+ }
791
+ const offsets = sampleOffsets(moov, stsc, chunkOffsets, sizes);
792
+
793
+ /** @type {Mp4SubtitleSample[]} */
794
+ const samples = [];
795
+ let ticks = 0;
796
+ for (let index = 0; index < sizes.length; index += 1) {
797
+ const start = ticks / timescale;
798
+ ticks += durations[index];
799
+ // An empty sample is a gap between cues, which the format uses to say
800
+ // "nothing on screen"; it is not a cue and would show as a blank line.
801
+ if (sizes[index] > 2) {
802
+ samples.push({
803
+ startSeconds: start,
804
+ endSeconds: ticks / timescale,
805
+ offset: offsets[index],
806
+ size: sizes[index]
807
+ });
808
+ }
809
+ }
810
+ tracks.push({
811
+ trackId,
812
+ declaredIndex,
813
+ format,
814
+ language,
815
+ someSamplesForced,
816
+ allSamplesForced,
817
+ samples
818
+ });
819
+ }
820
+ return { tracks };
821
+ }
822
+
823
+ /**
824
+ * The text of one sample.
825
+ *
826
+ * `tx3g` is a two-byte length followed by UTF-8; anything after that is styling
827
+ * boxes, which this deliberately drops. `wvtt` is a sequence of boxes, and the
828
+ * text lives in the `payl` inside a `vttc`.
829
+ *
830
+ * @param {Buffer} bytes
831
+ * @param {string} format
832
+ * @returns {string}
833
+ */
834
+ function decodeSubtitleSample(bytes, format) {
835
+ if (format === "wvtt") {
836
+ let at = 0;
837
+ const parts = [];
838
+ while (at + HEADER_BYTES <= bytes.length) {
839
+ const box = readBox(bytes, at);
840
+ if (!box) {
841
+ break;
842
+ }
843
+ if (box.type === "vttc") {
844
+ const payl = childOf(bytes, box.dataOffset, Math.min(bytes.length, box.end), "payl");
845
+ if (payl) {
846
+ parts.push(bytes.toString("utf8", payl.dataOffset, Math.min(bytes.length, payl.end)));
847
+ }
848
+ }
849
+ at = box.end;
850
+ }
851
+ return parts.join("\n").trim();
852
+ }
853
+ if (bytes.length < 2) {
854
+ return "";
855
+ }
856
+ const length = bytes.readUInt16BE(0);
857
+ return bytes.toString("utf8", 2, Math.min(bytes.length, 2 + length)).trim();
858
+ }