@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,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
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (
|
|
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
|
-
const
|
|
152
|
-
if (!
|
|
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
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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
|
+
}
|