@boy-offi9-inc/tidepull 0.10.0 → 0.11.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/README.md +1 -1
- package/dist/core/downloader.d.ts +5 -1
- package/dist/core/downloader.js +42 -57
- package/dist/core/pipeline.d.ts +2 -0
- package/dist/core/pipeline.js +3 -2
- package/dist/core/subtitles.d.ts +4 -0
- package/dist/core/subtitles.js +45 -0
- package/dist/main.js +14 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -190,7 +190,7 @@ From a clone, `node src/cli.ts <args>` runs the TypeScript source directly (Node
|
|
|
190
190
|
| Area | Behavior |
|
|
191
191
|
|---|---|
|
|
192
192
|
| Direct files | resumable (`.part` + `Range`), retries with backoff |
|
|
193
|
-
| HLS VOD | master + media playlists, parallel segments written in order, AES-128 (explicit/default IV), fMP4 init maps, `EXT-X-BYTERANGE` segments and ranged init maps, separate audio renditions, `SUBTITLES` renditions with `--write-subs` (WebVTT segments joined into one `.vtt` per language) |
|
|
193
|
+
| HLS VOD | master + media playlists, parallel segments written in order, AES-128 (explicit/default IV), fMP4 init maps, `EXT-X-BYTERANGE` segments and ranged init maps, separate audio renditions, `SUBTITLES` renditions with `--write-subs` (WebVTT segments joined into one `.vtt` per language), time-range clipping with `--from SEC` / `--to SEC` (whole segments, partial overlaps included; DASH and live playlists are not clippable) |
|
|
194
194
|
| HLS live (`--live`) | playlist refresh + append by media sequence, stops on `EXT-X-ENDLIST` / `--max-duration SEC` / `--max-segments N` / Ctrl-C (Ctrl-C keeps what was captured); AES-128 + fMP4 init maps supported mid-stream |
|
|
195
195
|
| DASH VOD (`.mpd`), opt-in via `@boy-offi9-inc/tidepull/dash` (built into the CLI) | `SegmentTemplate` (`$Number%05d$`, `$Time$`, timelines, durations), `SegmentList`, single-file `SegmentBase`, nested `BaseURL`; multiple `Period`s merged by representation (fragments concatenated in order, init from the first period); encrypted (`ContentProtection`) representations are skipped, never decrypted |
|
|
196
196
|
| Generic page extractor | `og:video`, `<video>/<source>`, bare media URLs in HTML/JSON |
|
|
@@ -45,7 +45,12 @@ export interface HlsOpts extends DownloadOpts {
|
|
|
45
45
|
maxDuration?: number;
|
|
46
46
|
maxSegments?: number;
|
|
47
47
|
refreshMs?: number;
|
|
48
|
+
from?: number;
|
|
49
|
+
to?: number;
|
|
48
50
|
}
|
|
51
|
+
export declare function sliceByDuration(segments: Array<SegmentRef & {
|
|
52
|
+
duration?: number;
|
|
53
|
+
}>, from?: number, to?: number): SegmentRef[];
|
|
49
54
|
export declare function makeSegmentFetcher(o: HlsOpts): {
|
|
50
55
|
fetchBuf: (u: string, range?: {
|
|
51
56
|
start: number;
|
|
@@ -72,4 +77,3 @@ export declare function downloadDash(f: {
|
|
|
72
77
|
}, dest: string, o: HlsOpts): Promise<{
|
|
73
78
|
ext: "ts" | "mp4";
|
|
74
79
|
}>;
|
|
75
|
-
export declare function downloadHlsSubtitles(url: string, dest: string, o: HlsOpts): Promise<void>;
|
package/dist/core/downloader.js
CHANGED
|
@@ -83,6 +83,32 @@ export async function downloadHttp(url, dest, o) {
|
|
|
83
83
|
}
|
|
84
84
|
await rename(part, dest);
|
|
85
85
|
}
|
|
86
|
+
export function sliceByDuration(segments, from = 0, to = Infinity) {
|
|
87
|
+
if (from >= to)
|
|
88
|
+
throw new TidepullError(`--from (${from}) must be smaller than --to (${to})`);
|
|
89
|
+
const out = [];
|
|
90
|
+
let start = 0;
|
|
91
|
+
for (const s of segments) {
|
|
92
|
+
const dur = s.duration || 0;
|
|
93
|
+
if (start < to && start + dur > from)
|
|
94
|
+
out.push(s);
|
|
95
|
+
start += dur;
|
|
96
|
+
if (start >= to)
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (!out.length) {
|
|
100
|
+
throw new TidepullError(from > 0
|
|
101
|
+
? `--from/--to matched no segments (no EXTINF durations, or [${from}, ${to})s is outside the playlist)`
|
|
102
|
+
: `--to (${to})s is at or before the start of the playlist`);
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
function partWriter(dest) {
|
|
107
|
+
const out = createWriteStream(`${dest}.part`);
|
|
108
|
+
const write = (b) => new Promise((res, rej) => out.write(b, (e) => (e ? rej(e) : res())));
|
|
109
|
+
const finish = () => new Promise((res, rej) => out.end((e) => (e ? rej(e) : res())));
|
|
110
|
+
return { out, write, finish };
|
|
111
|
+
}
|
|
86
112
|
export function makeSegmentFetcher(o) {
|
|
87
113
|
const keyCache = new Map();
|
|
88
114
|
const getKey = (uri) => {
|
|
@@ -132,9 +158,7 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
132
158
|
if (!segments.length)
|
|
133
159
|
throw new TidepullError("No segments to download");
|
|
134
160
|
const { fetchBuf, fetchSeg } = makeSegmentFetcher(o);
|
|
135
|
-
const
|
|
136
|
-
const out = createWriteStream(part);
|
|
137
|
-
const write = (b) => new Promise((res, rej) => out.write(b, (e) => (e ? rej(e) : res())));
|
|
161
|
+
const { out, write, finish } = partWriter(dest);
|
|
138
162
|
const n = segments.length;
|
|
139
163
|
const inflight = new Array(n);
|
|
140
164
|
const start = (i) => {
|
|
@@ -167,14 +191,14 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
167
191
|
start(next++);
|
|
168
192
|
o.onProgress({ downloaded: bytes, total: Math.round((bytes / (i + 1)) * n), estimated: i + 1 < n });
|
|
169
193
|
}
|
|
170
|
-
await
|
|
194
|
+
await finish();
|
|
171
195
|
}
|
|
172
196
|
catch (e) {
|
|
173
197
|
out.destroy();
|
|
174
|
-
await rm(part
|
|
198
|
+
await rm(`${dest}.part`, { force: true });
|
|
175
199
|
throw e;
|
|
176
200
|
}
|
|
177
|
-
await rename(part
|
|
201
|
+
await rename(`${dest}.part`, dest);
|
|
178
202
|
return { ext };
|
|
179
203
|
}
|
|
180
204
|
export async function downloadHls(url, dest, o) {
|
|
@@ -183,11 +207,16 @@ export async function downloadHls(url, dest, o) {
|
|
|
183
207
|
throw new TidepullError("Expected a media playlist, got a master playlist");
|
|
184
208
|
const media = parseMedia(text, finalUrl);
|
|
185
209
|
if (!media.endList) {
|
|
210
|
+
if (o.from !== undefined || o.to !== undefined)
|
|
211
|
+
throw new TidepullError("Time-range clipping (--from/--to) only works on VOD playlists");
|
|
186
212
|
if (!o.live)
|
|
187
213
|
throw new TidepullError("Live HLS streams are not supported yet; pass live: true to record them");
|
|
188
214
|
return downloadHlsLive(url, dest, o);
|
|
189
215
|
}
|
|
190
|
-
|
|
216
|
+
const segments = o.from !== undefined || o.to !== undefined
|
|
217
|
+
? sliceByDuration(media.segments, o.from ?? 0, o.to ?? Infinity)
|
|
218
|
+
: media.segments;
|
|
219
|
+
return downloadSegments(segments, dest, o);
|
|
191
220
|
}
|
|
192
221
|
export async function downloadHlsLive(url, dest, o) {
|
|
193
222
|
const first = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
@@ -199,10 +228,7 @@ export async function downloadHlsLive(url, dest, o) {
|
|
|
199
228
|
return { ext, segments: current.segments.length, duration: current.segments.reduce((a, s) => a + s.duration, 0), bytes: 0 };
|
|
200
229
|
}
|
|
201
230
|
const { fetchBuf, fetchSeg } = makeSegmentFetcher(o);
|
|
202
|
-
const
|
|
203
|
-
const out = createWriteStream(part);
|
|
204
|
-
const write = (b) => new Promise((res, rej) => out.write(b, (e) => (e ? rej(e) : res())));
|
|
205
|
-
const done = () => new Promise((res, rej) => out.end((e) => (e ? rej(e) : res())));
|
|
231
|
+
const { out, write, finish } = partWriter(dest);
|
|
206
232
|
let lastSeq = -1;
|
|
207
233
|
let lastMap;
|
|
208
234
|
let ext = "ts";
|
|
@@ -242,22 +268,22 @@ export async function downloadHlsLive(url, dest, o) {
|
|
|
242
268
|
const next = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
243
269
|
current = parseMedia(next.text, next.finalUrl);
|
|
244
270
|
}
|
|
245
|
-
await
|
|
271
|
+
await finish();
|
|
246
272
|
}
|
|
247
273
|
catch (e) {
|
|
248
274
|
if (o.signal?.aborted) {
|
|
249
275
|
try {
|
|
250
|
-
await
|
|
276
|
+
await finish();
|
|
251
277
|
}
|
|
252
278
|
catch { }
|
|
253
|
-
await rename(part
|
|
279
|
+
await rename(`${dest}.part`, dest);
|
|
254
280
|
return { ext, segments: count, duration, bytes };
|
|
255
281
|
}
|
|
256
282
|
out.destroy();
|
|
257
|
-
await rm(part
|
|
283
|
+
await rm(`${dest}.part`, { force: true });
|
|
258
284
|
throw e;
|
|
259
285
|
}
|
|
260
|
-
await rename(part
|
|
286
|
+
await rename(`${dest}.part`, dest);
|
|
261
287
|
return { ext, segments: count, duration, bytes };
|
|
262
288
|
}
|
|
263
289
|
export function downloadDash(f, dest, o) {
|
|
@@ -265,44 +291,3 @@ export function downloadDash(f, dest, o) {
|
|
|
265
291
|
throw new TidepullError("DASH format has no fragments");
|
|
266
292
|
return downloadSegments(f.fragments.map((url, seq) => ({ url, seq, map: f.init })), dest, o);
|
|
267
293
|
}
|
|
268
|
-
export async function downloadHlsSubtitles(url, dest, o) {
|
|
269
|
-
const { text, finalUrl } = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
270
|
-
if (isMasterPlaylist(text))
|
|
271
|
-
throw new TidepullError("Expected a subtitle media playlist, got a master playlist");
|
|
272
|
-
const media = parseMedia(text, finalUrl);
|
|
273
|
-
if (!media.endList)
|
|
274
|
-
throw new TidepullError("Live subtitle streams are not supported yet");
|
|
275
|
-
const part = dest + ".part";
|
|
276
|
-
const out = createWriteStream(part);
|
|
277
|
-
const write = (s) => new Promise((res, rej) => out.write(s, (e) => (e ? rej(e) : res())));
|
|
278
|
-
try {
|
|
279
|
-
let header = "";
|
|
280
|
-
let lastMap;
|
|
281
|
-
for (const seg of media.segments) {
|
|
282
|
-
if (seg.map && seg.map !== lastMap) {
|
|
283
|
-
out.destroy();
|
|
284
|
-
await rm(part, { force: true });
|
|
285
|
-
throw new TidepullError("fMP4 subtitle tracks (EXT-X-MAP) are not supported yet; only WebVTT segment playlists are");
|
|
286
|
-
}
|
|
287
|
-
const r = await o.http.request(seg.url, { headers: o.headers, signal: o.signal });
|
|
288
|
-
if (!r.ok) {
|
|
289
|
-
await r.body?.cancel();
|
|
290
|
-
throw new TidepullError(`HTTP ${r.status} for subtitle segment ${seg.url}`);
|
|
291
|
-
}
|
|
292
|
-
const body = await r.text();
|
|
293
|
-
if (!header) {
|
|
294
|
-
header = body.slice(0, body.indexOf("\n\n") + 1);
|
|
295
|
-
await write(header);
|
|
296
|
-
}
|
|
297
|
-
const cues = body.startsWith("WEBVTT") ? body.slice(body.indexOf("\n\n") + 2) : body;
|
|
298
|
-
await write(cues.endsWith("\n") ? cues : cues + "\n");
|
|
299
|
-
}
|
|
300
|
-
await new Promise((res, rej) => out.end((e) => (e ? rej(e) : res())));
|
|
301
|
-
}
|
|
302
|
-
catch (e) {
|
|
303
|
-
out.destroy();
|
|
304
|
-
await rm(part, { force: true });
|
|
305
|
-
throw e;
|
|
306
|
-
}
|
|
307
|
-
await rename(part, dest);
|
|
308
|
-
}
|
package/dist/core/pipeline.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface DownloadOptions {
|
|
|
18
18
|
live?: boolean;
|
|
19
19
|
maxDuration?: number;
|
|
20
20
|
maxSegments?: number;
|
|
21
|
+
from?: number;
|
|
22
|
+
to?: number;
|
|
21
23
|
}
|
|
22
24
|
export declare const isPlaylist: (x: Info | Playlist) => x is Playlist;
|
|
23
25
|
export declare function resolveUrl(rawUrl: string, ctx: Ctx): Promise<Info | Playlist>;
|
package/dist/core/pipeline.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mkdir, rename, rm, stat } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { downloadDash, downloadHls,
|
|
3
|
+
import { downloadDash, downloadHls, downloadHttp, RateLimiter } from "./downloader.js";
|
|
4
4
|
import { TidepullError } from "./errors.js";
|
|
5
5
|
import { findFfmpeg, mergeStreams, remux, extractAudio } from "./ffmpeg.js";
|
|
6
6
|
import { Meter } from "./meter.js";
|
|
@@ -94,7 +94,7 @@ export async function downloadInfo(info, opts, ctx) {
|
|
|
94
94
|
const common = { http: ctx.http, headers: { ...f.http_headers, ...opts.headers }, signal: ctx.signal, limiter, onProgress };
|
|
95
95
|
let ext = f.ext;
|
|
96
96
|
if (f.protocol === "hls")
|
|
97
|
-
({ ext } = await downloadHls(f.url, dest, { ...common, concurrency: opts.concurrency, live: opts.live, maxDuration: opts.maxDuration, maxSegments: opts.maxSegments }));
|
|
97
|
+
({ ext } = await downloadHls(f.url, dest, { ...common, concurrency: opts.concurrency, live: opts.live, maxDuration: opts.maxDuration, maxSegments: opts.maxSegments, from: opts.from, to: opts.to }));
|
|
98
98
|
else if (f.protocol === "dash")
|
|
99
99
|
({ ext } = await downloadDash(f, dest, { ...common, concurrency: opts.concurrency }));
|
|
100
100
|
else
|
|
@@ -149,6 +149,7 @@ export async function downloadInfo(info, opts, ctx) {
|
|
|
149
149
|
target = out;
|
|
150
150
|
}
|
|
151
151
|
if (opts.writeSubs && info.subtitles?.length) {
|
|
152
|
+
const { downloadHlsSubtitles } = await import("./subtitles.js");
|
|
152
153
|
const stemBase = target.slice(0, target.length - path.extname(target).length);
|
|
153
154
|
for (const sub of info.subtitles) {
|
|
154
155
|
const dest = `${stemBase}.${sub.lang.replace(/\W+/g, "_")}.vtt`;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createWriteStream } from "node:fs";
|
|
2
|
+
import { rename, rm } from "node:fs/promises";
|
|
3
|
+
import { TidepullError } from "./errors.js";
|
|
4
|
+
import { isMasterPlaylist, parseMedia } from "./hls.js";
|
|
5
|
+
export async function downloadHlsSubtitles(url, dest, o) {
|
|
6
|
+
const { text, finalUrl } = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
7
|
+
if (isMasterPlaylist(text))
|
|
8
|
+
throw new TidepullError("Expected a subtitle media playlist, got a master playlist");
|
|
9
|
+
const media = parseMedia(text, finalUrl);
|
|
10
|
+
if (!media.endList)
|
|
11
|
+
throw new TidepullError("Live subtitle streams are not supported yet");
|
|
12
|
+
const part = `${dest}.part`;
|
|
13
|
+
const out = createWriteStream(part);
|
|
14
|
+
const write = (s) => new Promise((res, rej) => out.write(s, (e) => (e ? rej(e) : res())));
|
|
15
|
+
try {
|
|
16
|
+
let header = "";
|
|
17
|
+
let lastMap;
|
|
18
|
+
for (const seg of media.segments) {
|
|
19
|
+
if (seg.map && seg.map !== lastMap) {
|
|
20
|
+
out.destroy();
|
|
21
|
+
await rm(part, { force: true });
|
|
22
|
+
throw new TidepullError("fMP4 subtitle tracks (EXT-X-MAP) are not supported yet; only WebVTT segment playlists are");
|
|
23
|
+
}
|
|
24
|
+
const r = await o.http.request(seg.url, { headers: o.headers, signal: o.signal });
|
|
25
|
+
if (!r.ok) {
|
|
26
|
+
await r.body?.cancel();
|
|
27
|
+
throw new TidepullError(`HTTP ${r.status} for subtitle segment ${seg.url}`);
|
|
28
|
+
}
|
|
29
|
+
const body = await r.text();
|
|
30
|
+
if (!header) {
|
|
31
|
+
header = body.slice(0, body.indexOf("\n\n") + 1);
|
|
32
|
+
await write(header);
|
|
33
|
+
}
|
|
34
|
+
const cues = body.startsWith("WEBVTT") ? body.slice(body.indexOf("\n\n") + 2) : body;
|
|
35
|
+
await write(cues.endsWith("\n") ? cues : cues + "\n");
|
|
36
|
+
}
|
|
37
|
+
await new Promise((res, rej) => out.end((e) => (e ? rej(e) : res())));
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
out.destroy();
|
|
41
|
+
await rm(part, { force: true });
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
await rename(part, dest);
|
|
45
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -64,6 +64,8 @@ Network:
|
|
|
64
64
|
--live Record live HLS streams (stops when the stream ends or on Ctrl-C, keeping what was captured)
|
|
65
65
|
--max-duration SEC Live recording: stop after SEC seconds of media
|
|
66
66
|
--max-segments N Live recording: stop after N segments
|
|
67
|
+
--from SEC VOD HLS only: download from SEC seconds into the media (whole segments)
|
|
68
|
+
--to SEC VOD HLS only: stop at SEC seconds into the media (whole segments)
|
|
67
69
|
--cookies FILE Send cookies from a Netscape cookies.txt file, only to the hosts they belong to
|
|
68
70
|
-N, --concurrent-fragments N HLS segments fetched in parallel (default: 4)
|
|
69
71
|
-R, --retries N Retries per request (default: 3)
|
|
@@ -162,6 +164,8 @@ export async function main(argv) {
|
|
|
162
164
|
live: { type: "boolean" },
|
|
163
165
|
"max-duration": { type: "string" },
|
|
164
166
|
"max-segments": { type: "string" },
|
|
167
|
+
from: { type: "string" },
|
|
168
|
+
to: { type: "string" },
|
|
165
169
|
cookies: { type: "string" },
|
|
166
170
|
"config-location": { type: "string" },
|
|
167
171
|
"no-config": { type: "boolean" },
|
|
@@ -290,7 +294,17 @@ export async function main(argv) {
|
|
|
290
294
|
live: !!values.live,
|
|
291
295
|
maxDuration: values["max-duration"] ? num(values["max-duration"], 0) || undefined : undefined,
|
|
292
296
|
maxSegments: values["max-segments"] ? num(values["max-segments"], 0) || undefined : undefined,
|
|
297
|
+
from: values.from !== undefined ? num(values.from, -1) : undefined,
|
|
298
|
+
to: values.to !== undefined ? num(values.to, -1) : undefined,
|
|
293
299
|
};
|
|
300
|
+
if (values.from !== undefined && !(opts.from >= 0)) {
|
|
301
|
+
log.error("--from must be a number of seconds >= 0");
|
|
302
|
+
return 2;
|
|
303
|
+
}
|
|
304
|
+
if (values.to !== undefined && !(opts.to > 0)) {
|
|
305
|
+
log.error("--to must be a number of seconds > 0");
|
|
306
|
+
return 2;
|
|
307
|
+
}
|
|
294
308
|
for (const k of Object.keys(opts))
|
|
295
309
|
if (opts[k] === undefined)
|
|
296
310
|
delete opts[k];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boy-offi9-inc/tidepull",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "A minimal, from-scratch media downloader library: extractor plugins, yt-dlp-style format selection, resumable downloads.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json && node scripts/fix-dts.mjs",
|
|
26
26
|
"prepack": "npm run build",
|
|
27
27
|
"prepublishOnly": "npm run typecheck && npm test",
|
|
28
|
-
"size": "node scripts/size.mjs dist/index.js --budget=
|
|
28
|
+
"size": "node scripts/size.mjs dist/index.js --budget=54",
|
|
29
29
|
"lint:imports": "node scripts/lint-imports.mjs"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|