@torrent-tv/proxy 2.74.0 → 2.75.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 +14 -0
- package/CLAUDE.md +8 -5
- package/docs/container-architecture.md +25 -5
- package/package.json +1 -1
- package/services/audio-inventory.js +201 -411
- package/services/container/AviContainer.js +266 -81
- package/services/container/Container.js +294 -79
- package/services/container/ContainerFactory.js +122 -31
- package/services/container/MatroskaContainer.js +337 -7
- package/services/container/Mp4Container.js +414 -28
- package/services/hls-session-manager.js +2 -2
- package/services/playback-planner.js +43 -15
- package/services/torrent-worker/pool-adapter.js +352 -333
- package/services/torrent-worker/subtitle-cues.js +42 -75
- package/services/tracks/AudioTrack.js +131 -40
- package/test/audio-inventory.test.js +176 -177
- package/test/matroska-cues-track.test.js +192 -192
- package/test/mp4-composition-times.test.js +0 -0
- package/test/video-facts.test.js +102 -0
- package/services/container-index/avi.js +0 -167
- package/services/container-index/index.js +0 -118
- package/services/container-index/matroska.js +0 -336
- package/services/container-index/mp4.js +0 -358
- /package/services/{container-index → container}/ebml-reader.js +0 -0
|
@@ -1,81 +1,266 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file AVI container — RIFF.
|
|
3
|
-
*
|
|
4
|
-
* Minimal: only keyframe index via idx1 (AVIIF_KEYFRAME). Tracks are not
|
|
5
|
-
* used by current product beyond video — expose a single VideoTrack if needed.
|
|
6
|
-
* Spec: RIFF AVI, idx1 chunk at file end, OpenDML may lack idx1 → no index.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { Container } from "./Container.js";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
return
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file AVI container — RIFF.
|
|
3
|
+
*
|
|
4
|
+
* Minimal: only keyframe index via idx1 (AVIIF_KEYFRAME). Tracks are not
|
|
5
|
+
* used by current product beyond video — expose a single VideoTrack if needed.
|
|
6
|
+
* Spec: RIFF AVI, idx1 chunk at file end, OpenDML may lack idx1 → no index.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Container } from "./Container.js";
|
|
10
|
+
import { VideoTrack } from "../tracks/VideoTrack.js";
|
|
11
|
+
|
|
12
|
+
export class AviContainer extends Container {
|
|
13
|
+
get formatName() {
|
|
14
|
+
return "avi";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static detect(head) {
|
|
18
|
+
return isAvi(head);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The keyframe times this container's own index states, in ascending seconds.
|
|
23
|
+
*
|
|
24
|
+
* Static so a caller that has bytes and no container can ask; the instance
|
|
25
|
+
* form is {@link Container#readKeyframeIndex}.
|
|
26
|
+
*
|
|
27
|
+
* @param {(start:number,end:number)=>Promise<Buffer|null>} readRange
|
|
28
|
+
* @param {number} fileSize
|
|
29
|
+
* @returns {Promise<number[]|null>} Null where the container has no index.
|
|
30
|
+
*/
|
|
31
|
+
static readKeyframeTimes(readRange, fileSize) {
|
|
32
|
+
return readAviKeyframeTimes(readRange, fileSize);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async readTracks() {
|
|
36
|
+
const head = await this.readRange(0, Math.min(4095, this.fileSize - 1));
|
|
37
|
+
if (!head || !isAvi(head)) return [];
|
|
38
|
+
// AVI track table is minimal — expose one video track for uniformity.
|
|
39
|
+
return [new VideoTrack({
|
|
40
|
+
trackNumber: 1,
|
|
41
|
+
declaredIndex: 0,
|
|
42
|
+
codecId: "",
|
|
43
|
+
language: "",
|
|
44
|
+
languageBcp47: "",
|
|
45
|
+
name: "",
|
|
46
|
+
isEnabled: true,
|
|
47
|
+
isDefault: true,
|
|
48
|
+
declaresDefault: false
|
|
49
|
+
})];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Duration from the main AVI header, per the RIFF AVI specification: the
|
|
54
|
+
* header states microseconds per frame and the total number of frames, and
|
|
55
|
+
* their product is the length.
|
|
56
|
+
*
|
|
57
|
+
* An AVI has no edit list and no timeline offset of any kind, so its start is
|
|
58
|
+
* zero — a declaration of the format itself, not an absence.
|
|
59
|
+
*
|
|
60
|
+
* @returns {Promise<import("./Container.js").ContainerMediaInfo>}
|
|
61
|
+
*/
|
|
62
|
+
async readMediaInfo() {
|
|
63
|
+
if (this.mediaInfo) {
|
|
64
|
+
return this.mediaInfo;
|
|
65
|
+
}
|
|
66
|
+
/** @type {import("./Container.js").ContainerMediaInfo} */
|
|
67
|
+
const info = { format: this.formatName, durationSeconds: null, startTimeSeconds: 0 };
|
|
68
|
+
this.mediaInfo = info;
|
|
69
|
+
const head = await this.readRange(0, Math.min(4095, this.fileSize - 1));
|
|
70
|
+
if (!head || !isAvi(head)) {
|
|
71
|
+
return info;
|
|
72
|
+
}
|
|
73
|
+
// RIFF("AVI ") -> LIST("hdrl") -> avih. The avih chunk's payload begins with
|
|
74
|
+
// dwMicroSecPerFrame and its fifth field is dwTotalFrames.
|
|
75
|
+
const at = head.indexOf("avih", 0, "latin1");
|
|
76
|
+
if (at < 0 || at + 8 + 20 > head.length) {
|
|
77
|
+
return info;
|
|
78
|
+
}
|
|
79
|
+
const payload = at + 8;
|
|
80
|
+
const microsecondsPerFrame = head.readUInt32LE(payload);
|
|
81
|
+
const totalFrames = head.readUInt32LE(payload + 16);
|
|
82
|
+
if (microsecondsPerFrame > 0 && totalFrames > 0) {
|
|
83
|
+
info.durationSeconds = (microsecondsPerFrame * totalFrames) / 1e6;
|
|
84
|
+
}
|
|
85
|
+
return info;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async readKeyframeIndex() {
|
|
89
|
+
const r = await readAviKeyframeTimes(this.readRange, this.fileSize);
|
|
90
|
+
if (!r) return null;
|
|
91
|
+
if (Array.isArray(r)) return { times: r, tolerance: 0 };
|
|
92
|
+
return r;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// RIFF speaking about AVI: the idx1 index and its keyframe flag.
|
|
98
|
+
// Here because the class is the only way in.
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
/**
|
|
101
|
+
* @file Keyframe index for AVI, read without downloading the file.
|
|
102
|
+
*
|
|
103
|
+
* AVI ends with an `idx1` chunk: one fixed-size entry per stream chunk, each
|
|
104
|
+
* carrying a flags word whose keyframe bit says whether that chunk starts a
|
|
105
|
+
* keyframe. Frame number times the video stream's frame duration gives the
|
|
106
|
+
* time, so the index alone is enough — no media has to be read.
|
|
107
|
+
*
|
|
108
|
+
* `idx1` lives at the end of the file and the top-level chunk headers state
|
|
109
|
+
* their sizes, so it is reached by stepping over headers (typically two hops:
|
|
110
|
+
* `LIST hdrl`, `LIST movi`), not by scanning.
|
|
111
|
+
*
|
|
112
|
+
* Still relevant despite the format's age: older releases are largely XviD in
|
|
113
|
+
* AVI, and those are exactly the files that get copied rather than re-encoded.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
const HEADER_BYTES = 8;
|
|
117
|
+
const PROBE_BYTES = 4096;
|
|
118
|
+
// Keyframe flag in an idx1 entry's flags word (AVIIF_KEYFRAME).
|
|
119
|
+
const KEYFRAME_FLAG = 0x10;
|
|
120
|
+
const IDX1_ENTRY_BYTES = 16;
|
|
121
|
+
// Cap on the idx1 read. One entry per chunk, 16 bytes each — a long film runs
|
|
122
|
+
// to a few MB; beyond this is not a normal index.
|
|
123
|
+
const MAX_IDX1_BYTES = 64 * 1024 * 1024;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Whether this looks like AVI: a RIFF container whose form type is `AVI `.
|
|
127
|
+
*
|
|
128
|
+
* @param {Buffer} head
|
|
129
|
+
* @returns {boolean}
|
|
130
|
+
*/
|
|
131
|
+
function isAvi(head) {
|
|
132
|
+
return (
|
|
133
|
+
head.length >= 12 &&
|
|
134
|
+
head.toString("latin1", 0, 4) === "RIFF" &&
|
|
135
|
+
head.toString("latin1", 8, 12) === "AVI "
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Microseconds per frame and the video stream's chunk id prefix, from the main
|
|
141
|
+
* header. Both live in the `hdrl` list near the file start.
|
|
142
|
+
*
|
|
143
|
+
* @param {Buffer} head
|
|
144
|
+
* @returns {{ microsecondsPerFrame: number } | null}
|
|
145
|
+
*/
|
|
146
|
+
function readMainHeader(head) {
|
|
147
|
+
// Top-level: "RIFF" size "AVI " then chunks. `avih` sits inside `LIST hdrl`.
|
|
148
|
+
let offset = 12;
|
|
149
|
+
while (offset + HEADER_BYTES <= head.length) {
|
|
150
|
+
const id = head.toString("latin1", offset, offset + 4);
|
|
151
|
+
const size = head.readUInt32LE(offset + 4);
|
|
152
|
+
if (size <= 0) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (id === "LIST") {
|
|
156
|
+
// Descend: list type follows the header, then its own chunks.
|
|
157
|
+
const listType = head.toString("latin1", offset + 8, offset + 12);
|
|
158
|
+
if (listType === "hdrl") {
|
|
159
|
+
let inner = offset + 12;
|
|
160
|
+
while (inner + HEADER_BYTES <= Math.min(head.length, offset + 8 + size)) {
|
|
161
|
+
const innerId = head.toString("latin1", inner, inner + 4);
|
|
162
|
+
const innerSize = head.readUInt32LE(inner + 4);
|
|
163
|
+
if (innerSize <= 0) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
if (innerId === "avih" && inner + 8 + 4 <= head.length) {
|
|
167
|
+
return { microsecondsPerFrame: head.readUInt32LE(inner + 8) };
|
|
168
|
+
}
|
|
169
|
+
inner += HEADER_BYTES + innerSize + (innerSize % 2);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
offset += HEADER_BYTES + 4 + (size - 4) + ((size - 4) % 2);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
offset += HEADER_BYTES + size + (size % 2);
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Step over top-level chunks to find `idx1`.
|
|
182
|
+
*
|
|
183
|
+
* @param {(start: number, end: number) => Promise<Buffer | null>} readRange
|
|
184
|
+
* @param {number} fileSize
|
|
185
|
+
* @returns {Promise<{ offset: number, size: number } | null>}
|
|
186
|
+
*/
|
|
187
|
+
async function findIdx1(readRange, fileSize) {
|
|
188
|
+
let offset = 12; // Past "RIFF" size "AVI ".
|
|
189
|
+
while (offset + HEADER_BYTES < fileSize) {
|
|
190
|
+
const probe = await readRange(offset, Math.min(fileSize - 1, offset + HEADER_BYTES - 1));
|
|
191
|
+
if (!probe || probe.length < HEADER_BYTES) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
const id = probe.toString("latin1", 0, 4);
|
|
195
|
+
const size = probe.readUInt32LE(4);
|
|
196
|
+
if (size <= 0) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (id === "idx1") {
|
|
200
|
+
return { offset: offset + HEADER_BYTES, size };
|
|
201
|
+
}
|
|
202
|
+
// Chunks are word-aligned; a LIST carries its type inside the payload, so
|
|
203
|
+
// the same size arithmetic covers both cases.
|
|
204
|
+
offset += HEADER_BYTES + size + (size % 2);
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Read the keyframe times of an AVI file.
|
|
211
|
+
*
|
|
212
|
+
* @param {(start: number, end: number) => Promise<Buffer | null>} readRange
|
|
213
|
+
* @param {number} fileSize
|
|
214
|
+
* @returns {Promise<number[] | null>} Ascending seconds, or null when the file
|
|
215
|
+
* has no `idx1` (OpenDML-only index, interrupted write, damaged upload).
|
|
216
|
+
*/
|
|
217
|
+
async function readAviKeyframeTimes(readRange, fileSize) {
|
|
218
|
+
const head = await readRange(0, Math.min(PROBE_BYTES - 1, fileSize - 1));
|
|
219
|
+
if (!head || !isAvi(head)) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
const mainHeader = readMainHeader(head);
|
|
223
|
+
if (!mainHeader || !mainHeader.microsecondsPerFrame) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const idx1 = await findIdx1(readRange, fileSize);
|
|
228
|
+
if (!idx1 || idx1.size > MAX_IDX1_BYTES) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const table = await readRange(idx1.offset, Math.min(fileSize - 1, idx1.offset + idx1.size - 1));
|
|
233
|
+
if (!table || table.length < IDX1_ENTRY_BYTES) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const secondsPerFrame = mainHeader.microsecondsPerFrame / 1e6;
|
|
238
|
+
const times = [];
|
|
239
|
+
let videoFrame = 0;
|
|
240
|
+
for (let at = 0; at + IDX1_ENTRY_BYTES <= table.length; at += IDX1_ENTRY_BYTES) {
|
|
241
|
+
const chunkId = table.toString("latin1", at, at + 4);
|
|
242
|
+
// Video chunks are "##db" (uncompressed) or "##dc" (compressed); audio is
|
|
243
|
+
// "##wb" and must not advance the frame counter.
|
|
244
|
+
const isVideo = chunkId.endsWith("db") || chunkId.endsWith("dc");
|
|
245
|
+
if (!isVideo) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const flags = table.readUInt32LE(at + 4);
|
|
249
|
+
if ((flags & KEYFRAME_FLAG) !== 0) {
|
|
250
|
+
times.push(videoFrame * secondsPerFrame);
|
|
251
|
+
}
|
|
252
|
+
videoFrame += 1;
|
|
253
|
+
}
|
|
254
|
+
if (times.length === 0) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
// AVI names a keyframe by its FRAME NUMBER, and the time above is that number
|
|
258
|
+
// multiplied by the frame duration the header declares. The frames are the
|
|
259
|
+
// right ones — measured 2026-08-21 against the files themselves, 1196 index
|
|
260
|
+
// entries against 1196 real keyframes and 901 against 901, exactly — but the
|
|
261
|
+
// names are 10-44 ms away from the presentation times the demuxer computes,
|
|
262
|
+
// always under one frame. So the caller is told how far a time here may be
|
|
263
|
+
// from the instant it refers to, and can ask for a seek late enough that it
|
|
264
|
+
// still lands on the frame rather than on the one before it.
|
|
265
|
+
return { times, tolerance: secondsPerFrame };
|
|
266
|
+
}
|