@torrent-tv/proxy 2.75.0 → 2.76.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +1509 -1461
  2. package/CLAUDE.md +11 -1
  3. package/biome.json +182 -1
  4. package/docs/container-architecture.md +2 -1
  5. package/docs/encode-run-state.md +1 -1
  6. package/knip.json +14 -0
  7. package/package.json +1 -1
  8. package/routes/api/sources/warm/post.js +1 -1
  9. package/routes/api/transcode-sessions/post.js +185 -185
  10. package/routes/api/transcode-sessions/progress/get.js +5 -1
  11. package/routes/transcode/audio-file/get.js +11 -1
  12. package/routes/transcode/audio-warm/get.js +11 -1
  13. package/routes/transcode/session-file/get.js +1 -1
  14. package/routes/transcode/variant-file/get.js +10 -1
  15. package/scripts/render-run-graph.js +2 -2
  16. package/server.js +25 -0
  17. package/services/audio-inventory.js +206 -201
  18. package/services/container/AviContainer.js +1 -1
  19. package/services/container/Container.js +33 -1
  20. package/services/container/MatroskaContainer.js +1 -1
  21. package/services/container/Mp4Container.js +1 -1
  22. package/services/container/SubtitleFileContainer.js +0 -1
  23. package/services/controllers/SubtitleController.js +128 -128
  24. package/services/demand/index.js +7 -10
  25. package/services/download/registry.js +0 -14
  26. package/services/encode/CoverageMap.js +281 -0
  27. package/services/encode/EncodePlan.js +255 -0
  28. package/services/encode/EncodeRun.js +587 -0
  29. package/services/encode/Encoder.js +84 -0
  30. package/services/encode/NvencEncoder.js +45 -0
  31. package/services/encode/QsvEncoder.js +47 -0
  32. package/services/encode/SegmentDemand.js +0 -0
  33. package/services/encode/SegmentStore.js +529 -0
  34. package/services/encode/SoftwareEncoder.js +111 -0
  35. package/services/encode/V4l2m2mEncoder.js +53 -0
  36. package/services/encode/VaapiEncoder.js +53 -0
  37. package/services/encode/args.js +200 -0
  38. package/services/{encode-exit.js → encode/encode-exit.js} +17 -0
  39. package/services/encode/index.js +9 -0
  40. package/services/encode/run-command.js +647 -0
  41. package/services/hls-session-manager.js +11100 -10711
  42. package/services/hwaccel.js +1688 -1992
  43. package/services/orchestrators/EncodeOrchestrator.js +359 -0
  44. package/services/output/LiveOutputs.js +213 -0
  45. package/services/output/Output.js +94 -0
  46. package/services/output/OutputSpec.js +195 -0
  47. package/services/output/Timeline.js +220 -0
  48. package/services/output/index.js +1 -0
  49. package/services/output/ladder.js +26 -0
  50. package/services/playback-planner.js +806 -775
  51. package/services/produced-index.js +222 -300
  52. package/services/source/SourceFile.js +346 -0
  53. package/services/{sidecar-files.js → torrent/files.js} +107 -11
  54. package/services/torrent/naming.js +619 -0
  55. package/services/torrent-worker/client.js +10 -0
  56. package/services/torrent-worker/container-tracks.js +71 -43
  57. package/services/torrent-worker/pool-adapter.js +18 -0
  58. package/services/torrent-worker/protocol.js +7 -0
  59. package/services/torrent-worker/subtitle-cues.js +549 -549
  60. package/services/torrent-worker/worker.js +18 -0
  61. package/services/tracks/TextSubtitleTrack.js +287 -287
  62. package/services/tracks/index.js +15 -14
  63. package/services/viewer/Viewer.js +145 -0
  64. package/services/viewer/Viewers.js +124 -0
  65. package/test/auto-quality-step.test.js +508 -506
  66. package/test/behind-head-repair.test.js +17 -7
  67. package/test/coverage-map.test.js +153 -0
  68. package/test/cut-times-timeline.test.js +6 -5
  69. package/test/cuts-follow-published-grid.test.js +4 -4
  70. package/test/decode-cost.test.js +31 -12
  71. package/test/encode-exit.test.js +1 -1
  72. package/test/encode-orchestrator.test.js +196 -0
  73. package/test/encode-plan.test.js +245 -0
  74. package/test/encode-run-state.test.js +2 -2
  75. package/test/encode-run.test.js +168 -0
  76. package/test/encoder-kinds.test.js +122 -0
  77. package/test/held-request-width.test.js +9 -3
  78. package/test/helpers/encode-run.js +128 -0
  79. package/test/keyframe-index-accuracy.test.js +19 -12
  80. package/test/keyframes-belong-to-the-file.test.js +132 -0
  81. package/test/orchestrator-wired.test.js +164 -0
  82. package/test/output-shape.test.js +68 -0
  83. package/test/output-spec.test.js +157 -0
  84. package/test/produced-copy-choice.test.js +58 -92
  85. package/test/produced-index.test.js +142 -188
  86. package/test/quality-variants.test.js +1079 -1075
  87. package/test/run-graph-drift.test.js +1 -1
  88. package/test/run-intervals.test.js +329 -0
  89. package/test/run-position-follows-published-grid.test.js +4 -4
  90. package/test/seek-landing.test.js +8 -8
  91. package/test/seek-target-not-superseded.test.js +21 -9
  92. package/test/segment-demand.test.js +82 -0
  93. package/test/segment-serve-wiring.test.js +47 -52
  94. package/test/segment-store.test.js +187 -0
  95. package/test/segments-are-shared.test.js +175 -0
  96. package/test/sidecar-naming.test.js +142 -0
  97. package/test/source-file.test.js +133 -0
  98. package/test/stale-request-after-seek.test.js +18 -12
  99. package/test/subtitle-language.test.js +252 -252
  100. package/test/timeline.test.js +95 -0
  101. package/test/{sidecar-files.test.js → torrent-files.test.js} +44 -1
  102. package/test/torrent-naming.test.js +255 -0
  103. package/test/tracks-begin-together.test.js +44 -32
  104. package/test/two-viewers-one-picture.test.js +347 -0
  105. package/test/viewer-outputs.test.js +273 -0
  106. package/test/viewer.test.js +91 -0
  107. package/utils/perf.js +1 -63
  108. package/services/container/index.js +0 -6
  109. package/services/controllers/index.js +0 -2
  110. package/services/download/index.js +0 -8
  111. package/services/orchestrators/index.js +0 -2
  112. /package/services/{encode-run-state.js → encode/encode-run-state.js} +0 -0
  113. /package/services/{language-detect.js → tracks/language-detect.js} +0 -0
@@ -30,6 +30,7 @@ import { readFragments, supplyFiguresFor } from "./piece-reader.js";
30
30
  import { cuesHeldFor, declaredSubtitleTracksOf, subtitleTracksOf, warmSubtitleCues } from "./subtitle-cues.js";
31
31
  import {
32
32
  CONTAINER_HEAD_BYTES,
33
+ containerKeyframesOf,
33
34
  containerMediaInfoOf,
34
35
  containerTracksOf,
35
36
  warmResumePosition
@@ -440,6 +441,23 @@ async function runCommand(command, params, id) {
440
441
  };
441
442
  }
442
443
 
444
+ case Command.CONTAINER_KEYFRAMES: {
445
+ const torrent = await requireTorrent(params.sourceKey);
446
+ return {
447
+ index: await containerKeyframesOf(torrent, params.fileIndex, params.sourceKey, {
448
+ // Both edges this time. A Matroska file's Cues sit at the END, behind
449
+ // a SeekHead in the head, so a read that has neither waits for the
450
+ // swarm twice over.
451
+ prefetchEdges: () =>
452
+ pool.prefetchFileEdges(torrent, params.fileIndex, {
453
+ headBytes: CONTAINER_HEAD_BYTES,
454
+ tailBytes: CONTAINER_HEAD_BYTES,
455
+ timeoutMs: 60_000
456
+ })
457
+ })
458
+ };
459
+ }
460
+
443
461
  case Command.WARM_POSITION: {
444
462
  const torrent = await requireTorrent(params.sourceKey);
445
463
  return {
@@ -1,287 +1,287 @@
1
- /**
2
- * @file A subtitle track whose cues are text, and everything that follows from
3
- * that being true.
4
- *
5
- * Matroska: `S_TEXT/UTF8`, `S_TEXT/ASS`, `S_TEXT/SSA`, `S_TEXT/WEBVTT`
6
- * (RFC 9559). MP4: `tx3g`, `text`, `wvtt` (`stpp`/TTML is XML and is not text
7
- * for this pipeline). A file beside the film is one of these too, with no
8
- * container behind it.
9
- *
10
- * Two axes meet on a subtitle cue and only ONE of them is here. How a cue's
11
- * bytes are FRAMED is stated by the container's specification and is answered
12
- * by `container/`: Matroska reorders an ASS dialogue row and drops its two
13
- * timing fields (`matroska.org/technical/subtitles.html`), a `.ass` file states
14
- * its own field order in the `Format:` line of `[Events]`, an MP4 carries each
15
- * cue as a sample with a length prefix. What the CODEC then puts inside that
16
- * text — override groups in braces, `\N` and `\n` for a break, `\h` for a hard
17
- * space — is a fact about ASS wherever it is stored, and that is this file,
18
- * along with writing the cues out as WebVTT.
19
- *
20
- * The two were one function until 2.72.1, which is what showed English
21
- * subtitles as `21,0,Default,,0000,0000,0000,,I am the powerful Demon King`:
22
- * it counted commas to guess which framing it held, expected the ten fields of
23
- * a row in a FILE, and a Matroska block carries nine. A function that has to
24
- * guess the shape of its input is being called by someone who knew and did not
25
- * say.
26
- */
27
-
28
- import { SubtitleTrack } from "./SubtitleTrack.js";
29
- import { detectLanguage } from "../language-detect.js";
30
-
31
- const TEXT_CODECS_MATROSKA = new Set(["S_TEXT/UTF8", "S_TEXT/ASS", "S_TEXT/SSA", "S_TEXT/WEBVTT"]);
32
- const TEXT_FORMATS_MP4 = new Set(["tx3g", "text", "wvtt"]);
33
-
34
- /** How a cue's text is marked up, once the container's framing is off. */
35
- export const MarkupKind = {
36
- /** Sub Station Alpha and its advanced form: `{\pos(…)}`, `\N`, `\h`. */
37
- ASS: "ass",
38
- /** Nothing to strip: the text is what is shown. */
39
- NONE: "none"
40
- };
41
-
42
- /**
43
- * Which markup a codec's cue text carries, by any of the codec's names.
44
- *
45
- * The keys are every name this proxy has for a text subtitle codec: Matroska
46
- * CodecIDs (RFC 9559 §5.1.4.1.28 and the codec mappings beside it), MP4 sample
47
- * entry types (ISO/IEC 14496-12 §12.6, plus Apple's `tx3g`), and the file
48
- * extensions a subtitle shipped beside the film uses. They are listed together
49
- * because the ANSWER is the same for all of them — ASS is ASS whether it sits
50
- * in a Matroska block, in a file, or nowhere yet — and keeping three tables
51
- * would mean three places to forget.
52
- *
53
- * @type {Map<string, string>}
54
- */
55
- const MARKUP_BY_CODEC = new Map([
56
- ["S_TEXT/ASS", MarkupKind.ASS],
57
- ["S_TEXT/SSA", MarkupKind.ASS],
58
- [".ass", MarkupKind.ASS],
59
- [".ssa", MarkupKind.ASS],
60
- ["S_TEXT/UTF8", MarkupKind.NONE],
61
- ["S_TEXT/WEBVTT", MarkupKind.NONE],
62
- [".srt", MarkupKind.NONE],
63
- [".vtt", MarkupKind.NONE],
64
- [".webvtt", MarkupKind.NONE],
65
- ["tx3g", MarkupKind.NONE],
66
- ["text", MarkupKind.NONE],
67
- ["wvtt", MarkupKind.NONE]
68
- ]);
69
-
70
- /** How long a cue with no stated end is shown, when no cue follows it. */
71
- const OPEN_ENDED_CUE_SECONDS = 4;
72
-
73
- export class TextSubtitleTrack extends SubtitleTrack {
74
- constructor(params) {
75
- super(params);
76
- this.textCodec = params.codecId ?? "";
77
- }
78
-
79
- isTextBased() {
80
- return true;
81
- }
82
-
83
- /**
84
- * Which markup a codec's cue text carries.
85
- *
86
- * An unknown codec is answered `NONE` rather than refused: the text is then
87
- * shown as it is, which is wrong only in so far as some markup stays visible,
88
- * where a refusal would show nothing at all.
89
- *
90
- * @param {string} codecId - Matroska CodecID, MP4 sample entry type, or a
91
- * file extension including the dot. Case is ignored for extensions, which
92
- * arrive from file names, and kept for the others, which a spec spells.
93
- * @returns {string} One of {@link MarkupKind}.
94
- */
95
- static markupKindOf(codecId) {
96
- const name = String(codecId ?? "");
97
- return MARKUP_BY_CODEC.get(name) ?? MARKUP_BY_CODEC.get(name.toLowerCase()) ?? MarkupKind.NONE;
98
- }
99
-
100
- /**
101
- * The visible text of one cue: its markup taken off, nothing else touched.
102
- *
103
- * @param {string} text - The cue's text FIELD, already out of the container's
104
- * framing. Handing a whole dialogue row to this is the mistake described at
105
- * the top of this file.
106
- * @param {string} codecId
107
- * @returns {string} Possibly empty — a cue whose text is only a drawing
108
- * command or a positioning group has nothing to show, and the caller drops
109
- * it.
110
- */
111
- static plainTextOf(text, codecId) {
112
- const raw = String(text ?? "");
113
- if (TextSubtitleTrack.markupKindOf(codecId) !== MarkupKind.ASS) {
114
- return raw.trim();
115
- }
116
- return raw
117
- // An override group. Any brace content is a directive, never dialogue:
118
- // drawing commands, karaoke timing, positioning, font changes.
119
- .replace(/\{[^}]*\}/g, "")
120
- // Both breaks reach a player as a break. ASS distinguishes them — `\N` is
121
- // always a break, `\n` only where the style does not wrap — and a WebVTT
122
- // cue has no way to express the difference, so it takes the break.
123
- .replace(/\\N/g, "\n")
124
- .replace(/\\n/g, "\n")
125
- // A space the renderer may not collapse.
126
- .replace(/\\h/g, " ")
127
- .trim();
128
- }
129
-
130
- /**
131
- * One cue's start or end as WebVTT writes it: `hh:mm:ss.mmm`.
132
- *
133
- * @param {number} seconds
134
- * @returns {string}
135
- */
136
- static vttTime(seconds) {
137
- const safe = Math.max(0, Number(seconds) || 0);
138
- const hours = Math.floor(safe / 3600);
139
- const minutes = Math.floor((safe % 3600) / 60);
140
- const rest = safe % 60;
141
- return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${rest.toFixed(3).padStart(6, "0")}`;
142
- }
143
-
144
- /**
145
- * Resolve what a cue is missing and take its codec's markup off, so what is
146
- * left is what a player shows.
147
- *
148
- * A cue with no duration — a Matroska SimpleBlock, which subtitles rarely use
149
- * — is given the time until the next one IN THIS LIST, and the last such cue
150
- * a few seconds. Not an invention about the film: it is what a player does
151
- * with an open-ended cue, made explicit so every consumer agrees on it.
152
- *
153
- * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
154
- * @param {string} codecId
155
- * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
156
- */
157
- static finalizeCues(cues, codecId) {
158
- const result = [];
159
- (Array.isArray(cues) ? cues : []).forEach((cue, index) => {
160
- const next = cues[index + 1];
161
- const endSeconds =
162
- cue.endSeconds ?? (next ? next.startSeconds : cue.startSeconds + OPEN_ENDED_CUE_SECONDS);
163
- const text = TextSubtitleTrack.plainTextOf(cue.text, codecId);
164
- if (!text) {
165
- return;
166
- }
167
- result.push({ startSeconds: cue.startSeconds, endSeconds, text });
168
- });
169
- return result;
170
- }
171
-
172
- /**
173
- * A WebVTT document from a list of cues — the one writer, used by every path
174
- * that produces subtitles: a file beside the film, a track inside it, a pull
175
- * and a push.
176
- *
177
- * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
178
- * @param {string} codecId
179
- * @returns {string}
180
- */
181
- static cuesToVtt(cues, codecId) {
182
- const lines = ["WEBVTT", ""];
183
- for (const cue of TextSubtitleTrack.finalizeCues(cues, codecId)) {
184
- lines.push(`${TextSubtitleTrack.vttTime(cue.startSeconds)} --> ${TextSubtitleTrack.vttTime(cue.endSeconds)}`);
185
- lines.push(cue.text);
186
- lines.push("");
187
- }
188
- return lines.join("\n");
189
- }
190
-
191
- /**
192
- * The words of a WebVTT document — what a viewer reads, with everything the
193
- * format puts around them removed.
194
- *
195
- * A WebVTT document is a series of blocks separated by blank lines. A block
196
- * that holds a timing line (`00:00:12.060 --> 00:00:13.270`) is a cue, and the
197
- * lines after that timing line are its text; the lines before it are the cue's
198
- * optional identifier. A block with NO timing line is the `WEBVTT` header or a
199
- * `NOTE` / `STYLE` / `REGION` block, and none of those is anybody's language.
200
- * That one rule removes the identifiers, the timings and the headers together.
201
- *
202
- * What is left can still carry WebVTT's own inline markup — `<v Speaker>`,
203
- * `<i>`, `<c.yellow>` — and character references. Both are dropped: a speaker
204
- * name and a class name are written in whatever language the releaser's tooling
205
- * used, which is not the language of the film.
206
- *
207
- * @param {string} vtt - A WebVTT document.
208
- * @returns {string} The cue text, blocks joined by newlines.
209
- */
210
- static cueTextOfVtt(vtt) {
211
- if (typeof vtt !== "string") {
212
- return "";
213
- }
214
- const blocks = vtt.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split(/\n{2,}/);
215
- const spoken = [];
216
- for (const block of blocks) {
217
- const lines = block.split("\n");
218
- const timingAt = lines.findIndex((line) => line.includes("-->"));
219
- if (timingAt < 0) {
220
- continue;
221
- }
222
- for (const line of lines.slice(timingAt + 1)) {
223
- spoken.push(line);
224
- }
225
- }
226
- return spoken
227
- .join("\n")
228
- .replace(/<[^>]*>/g, "")
229
- // A character reference stands for one character and never for a word, so a
230
- // space in its place keeps the neighbouring words apart and adds nothing.
231
- .replace(/&[a-z]+;|&#\d+;|&#x[0-9a-f]+;/gi, " ")
232
- .trim();
233
- }
234
-
235
- /**
236
- * Detect the language of a WebVTT document, reading only its cue text.
237
- *
238
- * @param {string} vtt - A WebVTT document.
239
- * @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
240
- */
241
- static detectLanguageFromVtt(vtt) {
242
- return detectLanguage(TextSubtitleTrack.cueTextOfVtt(vtt));
243
- }
244
-
245
- static isTextCodec(codecId) {
246
- return TEXT_CODECS_MATROSKA.has(codecId) || TEXT_FORMATS_MP4.has(codecId);
247
- }
248
-
249
- /** Which markup this track's cue text carries. */
250
- get markupKind() {
251
- return TextSubtitleTrack.markupKindOf(this.textCodec);
252
- }
253
-
254
- /**
255
- * The visible text of one of this track's cues.
256
- *
257
- * @param {string} textField - The cue's text field, already out of its
258
- * container's framing. This method knows the codec and not the container,
259
- * which is why it cannot be handed a whole dialogue row.
260
- * @returns {string}
261
- */
262
- plainText(textField) {
263
- return TextSubtitleTrack.plainTextOf(textField, this.textCodec);
264
- }
265
-
266
- /**
267
- * This track's cues, resolved and stripped, as a player reads them.
268
- *
269
- * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
270
- * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
271
- */
272
- finalize(cues) {
273
- return TextSubtitleTrack.finalizeCues(cues, this.textCodec);
274
- }
275
-
276
- /**
277
- * This track's cues as a WebVTT document.
278
- *
279
- * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
280
- * @returns {string}
281
- */
282
- toVtt(cues) {
283
- return TextSubtitleTrack.cuesToVtt(cues, this.textCodec);
284
- }
285
- }
286
-
287
- export { TEXT_CODECS_MATROSKA, TEXT_FORMATS_MP4 };
1
+ /**
2
+ * @file A subtitle track whose cues are text, and everything that follows from
3
+ * that being true.
4
+ *
5
+ * Matroska: `S_TEXT/UTF8`, `S_TEXT/ASS`, `S_TEXT/SSA`, `S_TEXT/WEBVTT`
6
+ * (RFC 9559). MP4: `tx3g`, `text`, `wvtt` (`stpp`/TTML is XML and is not text
7
+ * for this pipeline). A file beside the film is one of these too, with no
8
+ * container behind it.
9
+ *
10
+ * Two axes meet on a subtitle cue and only ONE of them is here. How a cue's
11
+ * bytes are FRAMED is stated by the container's specification and is answered
12
+ * by `container/`: Matroska reorders an ASS dialogue row and drops its two
13
+ * timing fields (`matroska.org/technical/subtitles.html`), a `.ass` file states
14
+ * its own field order in the `Format:` line of `[Events]`, an MP4 carries each
15
+ * cue as a sample with a length prefix. What the CODEC then puts inside that
16
+ * text — override groups in braces, `\N` and `\n` for a break, `\h` for a hard
17
+ * space — is a fact about ASS wherever it is stored, and that is this file,
18
+ * along with writing the cues out as WebVTT.
19
+ *
20
+ * The two were one function until 2.72.1, which is what showed English
21
+ * subtitles as `21,0,Default,,0000,0000,0000,,I am the powerful Demon King`:
22
+ * it counted commas to guess which framing it held, expected the ten fields of
23
+ * a row in a FILE, and a Matroska block carries nine. A function that has to
24
+ * guess the shape of its input is being called by someone who knew and did not
25
+ * say.
26
+ */
27
+
28
+ import { SubtitleTrack } from "./SubtitleTrack.js";
29
+ import { detectLanguage } from "./language-detect.js";
30
+
31
+ const TEXT_CODECS_MATROSKA = new Set(["S_TEXT/UTF8", "S_TEXT/ASS", "S_TEXT/SSA", "S_TEXT/WEBVTT"]);
32
+ const TEXT_FORMATS_MP4 = new Set(["tx3g", "text", "wvtt"]);
33
+
34
+ /** How a cue's text is marked up, once the container's framing is off. */
35
+ export const MarkupKind = {
36
+ /** Sub Station Alpha and its advanced form: `{\pos(…)}`, `\N`, `\h`. */
37
+ ASS: "ass",
38
+ /** Nothing to strip: the text is what is shown. */
39
+ NONE: "none"
40
+ };
41
+
42
+ /**
43
+ * Which markup a codec's cue text carries, by any of the codec's names.
44
+ *
45
+ * The keys are every name this proxy has for a text subtitle codec: Matroska
46
+ * CodecIDs (RFC 9559 §5.1.4.1.28 and the codec mappings beside it), MP4 sample
47
+ * entry types (ISO/IEC 14496-12 §12.6, plus Apple's `tx3g`), and the file
48
+ * extensions a subtitle shipped beside the film uses. They are listed together
49
+ * because the ANSWER is the same for all of them — ASS is ASS whether it sits
50
+ * in a Matroska block, in a file, or nowhere yet — and keeping three tables
51
+ * would mean three places to forget.
52
+ *
53
+ * @type {Map<string, string>}
54
+ */
55
+ const MARKUP_BY_CODEC = new Map([
56
+ ["S_TEXT/ASS", MarkupKind.ASS],
57
+ ["S_TEXT/SSA", MarkupKind.ASS],
58
+ [".ass", MarkupKind.ASS],
59
+ [".ssa", MarkupKind.ASS],
60
+ ["S_TEXT/UTF8", MarkupKind.NONE],
61
+ ["S_TEXT/WEBVTT", MarkupKind.NONE],
62
+ [".srt", MarkupKind.NONE],
63
+ [".vtt", MarkupKind.NONE],
64
+ [".webvtt", MarkupKind.NONE],
65
+ ["tx3g", MarkupKind.NONE],
66
+ ["text", MarkupKind.NONE],
67
+ ["wvtt", MarkupKind.NONE]
68
+ ]);
69
+
70
+ /** How long a cue with no stated end is shown, when no cue follows it. */
71
+ const OPEN_ENDED_CUE_SECONDS = 4;
72
+
73
+ export class TextSubtitleTrack extends SubtitleTrack {
74
+ constructor(params) {
75
+ super(params);
76
+ this.textCodec = params.codecId ?? "";
77
+ }
78
+
79
+ isTextBased() {
80
+ return true;
81
+ }
82
+
83
+ /**
84
+ * Which markup a codec's cue text carries.
85
+ *
86
+ * An unknown codec is answered `NONE` rather than refused: the text is then
87
+ * shown as it is, which is wrong only in so far as some markup stays visible,
88
+ * where a refusal would show nothing at all.
89
+ *
90
+ * @param {string} codecId - Matroska CodecID, MP4 sample entry type, or a
91
+ * file extension including the dot. Case is ignored for extensions, which
92
+ * arrive from file names, and kept for the others, which a spec spells.
93
+ * @returns {string} One of {@link MarkupKind}.
94
+ */
95
+ static markupKindOf(codecId) {
96
+ const name = String(codecId ?? "");
97
+ return MARKUP_BY_CODEC.get(name) ?? MARKUP_BY_CODEC.get(name.toLowerCase()) ?? MarkupKind.NONE;
98
+ }
99
+
100
+ /**
101
+ * The visible text of one cue: its markup taken off, nothing else touched.
102
+ *
103
+ * @param {string} text - The cue's text FIELD, already out of the container's
104
+ * framing. Handing a whole dialogue row to this is the mistake described at
105
+ * the top of this file.
106
+ * @param {string} codecId
107
+ * @returns {string} Possibly empty — a cue whose text is only a drawing
108
+ * command or a positioning group has nothing to show, and the caller drops
109
+ * it.
110
+ */
111
+ static plainTextOf(text, codecId) {
112
+ const raw = String(text ?? "");
113
+ if (TextSubtitleTrack.markupKindOf(codecId) !== MarkupKind.ASS) {
114
+ return raw.trim();
115
+ }
116
+ return raw
117
+ // An override group. Any brace content is a directive, never dialogue:
118
+ // drawing commands, karaoke timing, positioning, font changes.
119
+ .replace(/\{[^}]*\}/g, "")
120
+ // Both breaks reach a player as a break. ASS distinguishes them — `\N` is
121
+ // always a break, `\n` only where the style does not wrap — and a WebVTT
122
+ // cue has no way to express the difference, so it takes the break.
123
+ .replace(/\\N/g, "\n")
124
+ .replace(/\\n/g, "\n")
125
+ // A space the renderer may not collapse.
126
+ .replace(/\\h/g, " ")
127
+ .trim();
128
+ }
129
+
130
+ /**
131
+ * One cue's start or end as WebVTT writes it: `hh:mm:ss.mmm`.
132
+ *
133
+ * @param {number} seconds
134
+ * @returns {string}
135
+ */
136
+ static vttTime(seconds) {
137
+ const safe = Math.max(0, Number(seconds) || 0);
138
+ const hours = Math.floor(safe / 3600);
139
+ const minutes = Math.floor((safe % 3600) / 60);
140
+ const rest = safe % 60;
141
+ return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${rest.toFixed(3).padStart(6, "0")}`;
142
+ }
143
+
144
+ /**
145
+ * Resolve what a cue is missing and take its codec's markup off, so what is
146
+ * left is what a player shows.
147
+ *
148
+ * A cue with no duration — a Matroska SimpleBlock, which subtitles rarely use
149
+ * — is given the time until the next one IN THIS LIST, and the last such cue
150
+ * a few seconds. Not an invention about the film: it is what a player does
151
+ * with an open-ended cue, made explicit so every consumer agrees on it.
152
+ *
153
+ * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
154
+ * @param {string} codecId
155
+ * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
156
+ */
157
+ static finalizeCues(cues, codecId) {
158
+ const result = [];
159
+ (Array.isArray(cues) ? cues : []).forEach((cue, index) => {
160
+ const next = cues[index + 1];
161
+ const endSeconds =
162
+ cue.endSeconds ?? (next ? next.startSeconds : cue.startSeconds + OPEN_ENDED_CUE_SECONDS);
163
+ const text = TextSubtitleTrack.plainTextOf(cue.text, codecId);
164
+ if (!text) {
165
+ return;
166
+ }
167
+ result.push({ startSeconds: cue.startSeconds, endSeconds, text });
168
+ });
169
+ return result;
170
+ }
171
+
172
+ /**
173
+ * A WebVTT document from a list of cues — the one writer, used by every path
174
+ * that produces subtitles: a file beside the film, a track inside it, a pull
175
+ * and a push.
176
+ *
177
+ * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
178
+ * @param {string} codecId
179
+ * @returns {string}
180
+ */
181
+ static cuesToVtt(cues, codecId) {
182
+ const lines = ["WEBVTT", ""];
183
+ for (const cue of TextSubtitleTrack.finalizeCues(cues, codecId)) {
184
+ lines.push(`${TextSubtitleTrack.vttTime(cue.startSeconds)} --> ${TextSubtitleTrack.vttTime(cue.endSeconds)}`);
185
+ lines.push(cue.text);
186
+ lines.push("");
187
+ }
188
+ return lines.join("\n");
189
+ }
190
+
191
+ /**
192
+ * The words of a WebVTT document — what a viewer reads, with everything the
193
+ * format puts around them removed.
194
+ *
195
+ * A WebVTT document is a series of blocks separated by blank lines. A block
196
+ * that holds a timing line (`00:00:12.060 --> 00:00:13.270`) is a cue, and the
197
+ * lines after that timing line are its text; the lines before it are the cue's
198
+ * optional identifier. A block with NO timing line is the `WEBVTT` header or a
199
+ * `NOTE` / `STYLE` / `REGION` block, and none of those is anybody's language.
200
+ * That one rule removes the identifiers, the timings and the headers together.
201
+ *
202
+ * What is left can still carry WebVTT's own inline markup — `<v Speaker>`,
203
+ * `<i>`, `<c.yellow>` — and character references. Both are dropped: a speaker
204
+ * name and a class name are written in whatever language the releaser's tooling
205
+ * used, which is not the language of the film.
206
+ *
207
+ * @param {string} vtt - A WebVTT document.
208
+ * @returns {string} The cue text, blocks joined by newlines.
209
+ */
210
+ static cueTextOfVtt(vtt) {
211
+ if (typeof vtt !== "string") {
212
+ return "";
213
+ }
214
+ const blocks = vtt.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split(/\n{2,}/);
215
+ const spoken = [];
216
+ for (const block of blocks) {
217
+ const lines = block.split("\n");
218
+ const timingAt = lines.findIndex((line) => line.includes("-->"));
219
+ if (timingAt < 0) {
220
+ continue;
221
+ }
222
+ for (const line of lines.slice(timingAt + 1)) {
223
+ spoken.push(line);
224
+ }
225
+ }
226
+ return spoken
227
+ .join("\n")
228
+ .replace(/<[^>]*>/g, "")
229
+ // A character reference stands for one character and never for a word, so a
230
+ // space in its place keeps the neighbouring words apart and adds nothing.
231
+ .replace(/&[a-z]+;|&#\d+;|&#x[0-9a-f]+;/gi, " ")
232
+ .trim();
233
+ }
234
+
235
+ /**
236
+ * Detect the language of a WebVTT document, reading only its cue text.
237
+ *
238
+ * @param {string} vtt - A WebVTT document.
239
+ * @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
240
+ */
241
+ static detectLanguageFromVtt(vtt) {
242
+ return detectLanguage(TextSubtitleTrack.cueTextOfVtt(vtt));
243
+ }
244
+
245
+ static isTextCodec(codecId) {
246
+ return TEXT_CODECS_MATROSKA.has(codecId) || TEXT_FORMATS_MP4.has(codecId);
247
+ }
248
+
249
+ /** Which markup this track's cue text carries. */
250
+ get markupKind() {
251
+ return TextSubtitleTrack.markupKindOf(this.textCodec);
252
+ }
253
+
254
+ /**
255
+ * The visible text of one of this track's cues.
256
+ *
257
+ * @param {string} textField - The cue's text field, already out of its
258
+ * container's framing. This method knows the codec and not the container,
259
+ * which is why it cannot be handed a whole dialogue row.
260
+ * @returns {string}
261
+ */
262
+ plainText(textField) {
263
+ return TextSubtitleTrack.plainTextOf(textField, this.textCodec);
264
+ }
265
+
266
+ /**
267
+ * This track's cues, resolved and stripped, as a player reads them.
268
+ *
269
+ * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
270
+ * @returns {{ startSeconds: number, endSeconds: number, text: string }[]}
271
+ */
272
+ finalize(cues) {
273
+ return TextSubtitleTrack.finalizeCues(cues, this.textCodec);
274
+ }
275
+
276
+ /**
277
+ * This track's cues as a WebVTT document.
278
+ *
279
+ * @param {{ startSeconds: number, endSeconds?: number | null, text: string }[]} cues
280
+ * @returns {string}
281
+ */
282
+ toVtt(cues) {
283
+ return TextSubtitleTrack.cuesToVtt(cues, this.textCodec);
284
+ }
285
+ }
286
+
287
+ export { TEXT_CODECS_MATROSKA, TEXT_FORMATS_MP4 };
@@ -1,14 +1,15 @@
1
- export { ContainerTrack } from "./ContainerTrack.js";
2
- export { VideoTrack } from "./VideoTrack.js";
3
- export { AudioTrack } from "./AudioTrack.js";
4
- export { SubtitleTrack } from "./SubtitleTrack.js";
5
- export { TextSubtitleTrack, TEXT_CODECS_MATROSKA, TEXT_FORMATS_MP4 } from "./TextSubtitleTrack.js";
6
- export { ImageSubtitleTrack } from "./ImageSubtitleTrack.js";
7
- export { MarkupKind } from "./TextSubtitleTrack.js";
8
- // There is deliberately no class for a track that lives in a file of its own.
9
- // `<name>.mka` is a Matroska container holding an `AudioTrack`, and
10
- // `MatroskaContainer` reads it exactly as it reads the picture's — so "external"
11
- // is not a KIND of track, only the answer to where a track's bytes are. That
12
- // answer belongs to the application layer, which knows about torrents; this one
13
- // describes what a container declares and must not. `ExternalSubtitleFile`,
14
- // which asserted the opposite, was never used by anything and is gone.
1
+ // Only what is imported through this file, which is the one type five other
2
+ // modules name in their JSDoc. Every class beside it — `VideoTrack`,
3
+ // `AudioTrack`, `SubtitleTrack`, `TextSubtitleTrack`, `ImageSubtitleTrack` —
4
+ // and the tables `TEXT_CODECS_MATROSKA`, `TEXT_FORMATS_MP4` and `MarkupKind`
5
+ // were re-exported here and taken by nobody: whoever needs one imports the
6
+ // module that declares it.
7
+ export { ContainerTrack } from "./ContainerTrack.js";
8
+
9
+ // There is deliberately no class for a track that lives in a file of its own.
10
+ // `<name>.mka` is a Matroska container holding an `AudioTrack`, and
11
+ // `MatroskaContainer` reads it exactly as it reads the picture's so "external"
12
+ // is not a KIND of track, only the answer to where a track's bytes are. That
13
+ // answer belongs to the application layer, which knows about torrents; this one
14
+ // describes what a container declares and must not. `ExternalSubtitleFile`,
15
+ // which asserted the opposite, was never used by anything and is gone.