@boy-offi9-inc/tidepull 0.8.1 → 0.10.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 +3 -2
- package/dist/core/downloader.d.ts +21 -0
- package/dist/core/downloader.js +85 -8
- package/dist/core/pipeline.d.ts +3 -0
- package/dist/core/pipeline.js +1 -1
- package/dist/main.js +9 -0
- package/dist/playlist.d.ts +1 -0
- package/dist/playlist.js +79 -13
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ const files = await pullAll(g, "https://example.com/podcast.rss", { items: "1-3,
|
|
|
62
62
|
// "01 - Episode title.mp3", "02 - ...": the number and the title come from the playlist
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
`g.resolve(url)` returns an `Info` or a `Playlist` (`isPlaylist(x)` tells you which). `g.extract(url)` still returns a single `Info` and refuses a playlist with a clear error. A failing item is thrown by default; pass `onError` to report it and continue. Template fields for playlist items: `%(playlist)s`, `%(playlist_index)02d`, `%(playlist_count)s`, `%(playlist_id)s`.
|
|
65
|
+
`g.resolve(url)` returns an `Info` or a `Playlist` (`isPlaylist(x)` tells you which). `g.extract(url)` still returns a single `Info` and refuses a playlist with a clear error. A failing item is thrown by default; pass `onError` to report it and continue. Template fields for playlist items: `%(playlist)s`, `%(playlist_index)02d`, `%(playlist_count)s`, `%(playlist_id)s`. To download several items at once, pass `concurrency` (an integer ≥ 1, default 1); only the downloads run in parallel — items are still extracted in order and the returned files keep item order.
|
|
66
66
|
|
|
67
67
|
## Example
|
|
68
68
|
```console
|
|
@@ -183,7 +183,7 @@ From a clone, `node src/cli.ts <args>` runs the TypeScript source directly (Node
|
|
|
183
183
|
- No site-specific extractors are built in. Platforms such as YouTube hide their media behind scripts and tokens and need a plugin
|
|
184
184
|
- No DRM: protected streams (Widevine, PlayReady, FairPlay, encrypted DASH) are not supported and will not be
|
|
185
185
|
- No circumvention: it cannot get past logins, paywalls or geo-blocks. With `--cookies` it only fetches what your own session can already see
|
|
186
|
-
- Not implemented yet:
|
|
186
|
+
- Not implemented yet: SAMPLE-AES, fMP4 subtitle tracks (WebVTT segment playlists only), playlists from video sites (they need a plugin), a proxy option
|
|
187
187
|
- Cookies are read-only: `Set-Cookie` responses are not stored
|
|
188
188
|
|
|
189
189
|
### Details
|
|
@@ -191,6 +191,7 @@ From a clone, `node src/cli.ts <args>` runs the TypeScript source directly (Node
|
|
|
191
191
|
|---|---|
|
|
192
192
|
| Direct files | resumable (`.part` + `Range`), retries with backoff |
|
|
193
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) |
|
|
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 |
|
|
194
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 |
|
|
195
196
|
| Generic page extractor | `og:video`, `<video>/<source>`, bare media URLs in HTML/JSON |
|
|
196
197
|
| Format selector | `b bv bv* ba ba* w wv wa`, `+` merge, `/` fallback, `()` groups, `[height<=720]` `[filesize<?50M]` `[ext=mp4]` |
|
|
@@ -39,12 +39,33 @@ export interface SegmentRef {
|
|
|
39
39
|
length: number;
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
+
export interface HlsOpts extends DownloadOpts {
|
|
43
|
+
concurrency: number;
|
|
44
|
+
live?: boolean;
|
|
45
|
+
maxDuration?: number;
|
|
46
|
+
maxSegments?: number;
|
|
47
|
+
refreshMs?: number;
|
|
48
|
+
}
|
|
49
|
+
export declare function makeSegmentFetcher(o: HlsOpts): {
|
|
50
|
+
fetchBuf: (u: string, range?: {
|
|
51
|
+
start: number;
|
|
52
|
+
length: number;
|
|
53
|
+
}) => Promise<Buffer>;
|
|
54
|
+
fetchSeg: (seg: SegmentRef) => Promise<Buffer>;
|
|
55
|
+
};
|
|
42
56
|
export declare function downloadSegments(segments: SegmentRef[], dest: string, o: HlsOpts): Promise<{
|
|
43
57
|
ext: "ts" | "mp4";
|
|
44
58
|
}>;
|
|
45
59
|
export declare function downloadHls(url: string, dest: string, o: HlsOpts): Promise<{
|
|
46
60
|
ext: "ts" | "mp4";
|
|
47
61
|
}>;
|
|
62
|
+
export interface LiveResult {
|
|
63
|
+
ext: "ts" | "mp4";
|
|
64
|
+
segments: number;
|
|
65
|
+
duration: number;
|
|
66
|
+
bytes: number;
|
|
67
|
+
}
|
|
68
|
+
export declare function downloadHlsLive(url: string, dest: string, o: HlsOpts): Promise<LiveResult>;
|
|
48
69
|
export declare function downloadDash(f: {
|
|
49
70
|
init?: string;
|
|
50
71
|
fragments?: string[];
|
package/dist/core/downloader.js
CHANGED
|
@@ -83,9 +83,7 @@ export async function downloadHttp(url, dest, o) {
|
|
|
83
83
|
}
|
|
84
84
|
await rename(part, dest);
|
|
85
85
|
}
|
|
86
|
-
export
|
|
87
|
-
if (!segments.length)
|
|
88
|
-
throw new TidepullError("No segments to download");
|
|
86
|
+
export function makeSegmentFetcher(o) {
|
|
89
87
|
const keyCache = new Map();
|
|
90
88
|
const getKey = (uri) => {
|
|
91
89
|
let p = keyCache.get(uri);
|
|
@@ -114,8 +112,7 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
114
112
|
}
|
|
115
113
|
return buf;
|
|
116
114
|
};
|
|
117
|
-
const fetchSeg = async (
|
|
118
|
-
const seg = segments[i];
|
|
115
|
+
const fetchSeg = async (seg) => {
|
|
119
116
|
let buf = await fetchBuf(seg.url, seg.range);
|
|
120
117
|
if (seg.key) {
|
|
121
118
|
const key = await getKey(seg.key.uri);
|
|
@@ -129,13 +126,19 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
129
126
|
}
|
|
130
127
|
return buf;
|
|
131
128
|
};
|
|
129
|
+
return { fetchBuf, fetchSeg };
|
|
130
|
+
}
|
|
131
|
+
export async function downloadSegments(segments, dest, o) {
|
|
132
|
+
if (!segments.length)
|
|
133
|
+
throw new TidepullError("No segments to download");
|
|
134
|
+
const { fetchBuf, fetchSeg } = makeSegmentFetcher(o);
|
|
132
135
|
const part = dest + ".part";
|
|
133
136
|
const out = createWriteStream(part);
|
|
134
137
|
const write = (b) => new Promise((res, rej) => out.write(b, (e) => (e ? rej(e) : res())));
|
|
135
138
|
const n = segments.length;
|
|
136
139
|
const inflight = new Array(n);
|
|
137
140
|
const start = (i) => {
|
|
138
|
-
inflight[i] = fetchSeg(i);
|
|
141
|
+
inflight[i] = fetchSeg(segments[i]);
|
|
139
142
|
inflight[i].catch(() => { });
|
|
140
143
|
};
|
|
141
144
|
let ext = "ts";
|
|
@@ -179,10 +182,84 @@ export async function downloadHls(url, dest, o) {
|
|
|
179
182
|
if (isMasterPlaylist(text))
|
|
180
183
|
throw new TidepullError("Expected a media playlist, got a master playlist");
|
|
181
184
|
const media = parseMedia(text, finalUrl);
|
|
182
|
-
if (!media.endList)
|
|
183
|
-
|
|
185
|
+
if (!media.endList) {
|
|
186
|
+
if (!o.live)
|
|
187
|
+
throw new TidepullError("Live HLS streams are not supported yet; pass live: true to record them");
|
|
188
|
+
return downloadHlsLive(url, dest, o);
|
|
189
|
+
}
|
|
184
190
|
return downloadSegments(media.segments, dest, o);
|
|
185
191
|
}
|
|
192
|
+
export async function downloadHlsLive(url, dest, o) {
|
|
193
|
+
const first = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
194
|
+
if (isMasterPlaylist(first.text))
|
|
195
|
+
throw new TidepullError("Expected a media playlist, got a master playlist");
|
|
196
|
+
let current = parseMedia(first.text, first.finalUrl);
|
|
197
|
+
if (current.endList) {
|
|
198
|
+
const { ext } = await downloadSegments(current.segments, dest, o);
|
|
199
|
+
return { ext, segments: current.segments.length, duration: current.segments.reduce((a, s) => a + s.duration, 0), bytes: 0 };
|
|
200
|
+
}
|
|
201
|
+
const { fetchBuf, fetchSeg } = makeSegmentFetcher(o);
|
|
202
|
+
const part = dest + ".part";
|
|
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())));
|
|
206
|
+
let lastSeq = -1;
|
|
207
|
+
let lastMap;
|
|
208
|
+
let ext = "ts";
|
|
209
|
+
let bytes = 0;
|
|
210
|
+
let duration = 0;
|
|
211
|
+
let count = 0;
|
|
212
|
+
const reached = () => (o.maxSegments !== undefined && count >= o.maxSegments) ||
|
|
213
|
+
(o.maxDuration !== undefined && duration >= o.maxDuration);
|
|
214
|
+
try {
|
|
215
|
+
while (true) {
|
|
216
|
+
o.signal?.throwIfAborted();
|
|
217
|
+
for (const seg of current.segments) {
|
|
218
|
+
if (seg.seq <= lastSeq || reached())
|
|
219
|
+
continue;
|
|
220
|
+
if (seg.map && seg.map !== lastMap) {
|
|
221
|
+
const init = await fetchBuf(seg.map, seg.mapRange);
|
|
222
|
+
await write(init);
|
|
223
|
+
bytes += init.length;
|
|
224
|
+
lastMap = seg.map;
|
|
225
|
+
ext = "mp4";
|
|
226
|
+
}
|
|
227
|
+
const buf = await fetchSeg(seg);
|
|
228
|
+
await o.limiter?.take(buf.length, o.signal);
|
|
229
|
+
await write(buf);
|
|
230
|
+
bytes += buf.length;
|
|
231
|
+
duration += seg.duration;
|
|
232
|
+
count++;
|
|
233
|
+
lastSeq = seg.seq;
|
|
234
|
+
o.onProgress({ downloaded: bytes });
|
|
235
|
+
}
|
|
236
|
+
if (reached() || current.endList)
|
|
237
|
+
break;
|
|
238
|
+
const durations = current.segments.map((s) => s.duration).filter((d) => d > 0);
|
|
239
|
+
const target = durations.length ? Math.min(...durations) : 6;
|
|
240
|
+
const wait = o.refreshMs ?? Math.max(1000, Math.min(10000, target * 500));
|
|
241
|
+
await sleep(wait, undefined, { signal: o.signal });
|
|
242
|
+
const next = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
243
|
+
current = parseMedia(next.text, next.finalUrl);
|
|
244
|
+
}
|
|
245
|
+
await done();
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
if (o.signal?.aborted) {
|
|
249
|
+
try {
|
|
250
|
+
await done();
|
|
251
|
+
}
|
|
252
|
+
catch { }
|
|
253
|
+
await rename(part, dest);
|
|
254
|
+
return { ext, segments: count, duration, bytes };
|
|
255
|
+
}
|
|
256
|
+
out.destroy();
|
|
257
|
+
await rm(part, { force: true });
|
|
258
|
+
throw e;
|
|
259
|
+
}
|
|
260
|
+
await rename(part, dest);
|
|
261
|
+
return { ext, segments: count, duration, bytes };
|
|
262
|
+
}
|
|
186
263
|
export function downloadDash(f, dest, o) {
|
|
187
264
|
if (!f.fragments?.length)
|
|
188
265
|
throw new TidepullError("DASH format has no fragments");
|
package/dist/core/pipeline.d.ts
CHANGED
|
@@ -15,6 +15,9 @@ export interface DownloadOptions {
|
|
|
15
15
|
limitRate?: number;
|
|
16
16
|
fields?: Record<string, string | number>;
|
|
17
17
|
writeSubs?: boolean;
|
|
18
|
+
live?: boolean;
|
|
19
|
+
maxDuration?: number;
|
|
20
|
+
maxSegments?: number;
|
|
18
21
|
}
|
|
19
22
|
export declare const isPlaylist: (x: Info | Playlist) => x is Playlist;
|
|
20
23
|
export declare function resolveUrl(rawUrl: string, ctx: Ctx): Promise<Info | Playlist>;
|
package/dist/core/pipeline.js
CHANGED
|
@@ -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 }));
|
|
97
|
+
({ ext } = await downloadHls(f.url, dest, { ...common, concurrency: opts.concurrency, live: opts.live, maxDuration: opts.maxDuration, maxSegments: opts.maxSegments }));
|
|
98
98
|
else if (f.protocol === "dash")
|
|
99
99
|
({ ext } = await downloadDash(f, dest, { ...common, concurrency: opts.concurrency }));
|
|
100
100
|
else
|
package/dist/main.js
CHANGED
|
@@ -61,6 +61,9 @@ Config:
|
|
|
61
61
|
Network:
|
|
62
62
|
--limit-rate RATE Maximum download speed per file, e.g. 500K or 1.5M
|
|
63
63
|
--write-subs Download subtitle tracks (HLS SUBTITLES renditions) as .vtt next to the media
|
|
64
|
+
--live Record live HLS streams (stops when the stream ends or on Ctrl-C, keeping what was captured)
|
|
65
|
+
--max-duration SEC Live recording: stop after SEC seconds of media
|
|
66
|
+
--max-segments N Live recording: stop after N segments
|
|
64
67
|
--cookies FILE Send cookies from a Netscape cookies.txt file, only to the hosts they belong to
|
|
65
68
|
-N, --concurrent-fragments N HLS segments fetched in parallel (default: 4)
|
|
66
69
|
-R, --retries N Retries per request (default: 3)
|
|
@@ -156,6 +159,9 @@ export async function main(argv) {
|
|
|
156
159
|
"download-archive": { type: "string" },
|
|
157
160
|
"limit-rate": { type: "string" },
|
|
158
161
|
"write-subs": { type: "boolean" },
|
|
162
|
+
live: { type: "boolean" },
|
|
163
|
+
"max-duration": { type: "string" },
|
|
164
|
+
"max-segments": { type: "string" },
|
|
159
165
|
cookies: { type: "string" },
|
|
160
166
|
"config-location": { type: "string" },
|
|
161
167
|
"no-config": { type: "boolean" },
|
|
@@ -281,6 +287,9 @@ export async function main(argv) {
|
|
|
281
287
|
headers,
|
|
282
288
|
limitRate,
|
|
283
289
|
writeSubs: !!values["write-subs"],
|
|
290
|
+
live: !!values.live,
|
|
291
|
+
maxDuration: values["max-duration"] ? num(values["max-duration"], 0) || undefined : undefined,
|
|
292
|
+
maxSegments: values["max-segments"] ? num(values["max-segments"], 0) || undefined : undefined,
|
|
284
293
|
};
|
|
285
294
|
for (const k of Object.keys(opts))
|
|
286
295
|
if (opts[k] === undefined)
|
package/dist/playlist.d.ts
CHANGED
|
@@ -18,5 +18,6 @@ export declare function resolveItems(tp: Tidepull, source: string | Info | Playl
|
|
|
18
18
|
export interface PullAllOptions extends Partial<DownloadOptions>, CallOptions {
|
|
19
19
|
items?: string;
|
|
20
20
|
onError?: (error: Error, item: PlaylistItem) => void;
|
|
21
|
+
concurrency?: number;
|
|
21
22
|
}
|
|
22
23
|
export declare function pullAll(tp: Tidepull, source: string, o?: PullAllOptions): Promise<string[]>;
|
package/dist/playlist.js
CHANGED
|
@@ -58,19 +58,85 @@ export async function* resolveItems(tp, source, o = {}) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
export async function pullAll(tp, source, o = {}) {
|
|
61
|
-
const { items, onError, ...rest } = o;
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
const { items, onError, concurrency = 1, ...rest } = o;
|
|
62
|
+
const limit = concurrency === undefined ? 1 : Math.floor(concurrency);
|
|
63
|
+
if (limit !== concurrency || limit < 1) {
|
|
64
|
+
throw new TidepullError(`Invalid playlist concurrency ${concurrency} (an integer >= 1)`, 2);
|
|
65
|
+
}
|
|
66
|
+
if (limit === 1) {
|
|
67
|
+
const files = [];
|
|
68
|
+
for await (const item of resolveItems(tp, source, { items, signal: o.signal, trust: o.trust })) {
|
|
69
|
+
try {
|
|
70
|
+
if (item.error)
|
|
71
|
+
throw item.error;
|
|
72
|
+
files.push(await tp.download(item.info, { ...rest, fields: { ...rest.fields, ...playlistFields(item) } }));
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
if (o.signal?.aborted || !onError)
|
|
76
|
+
throw e;
|
|
77
|
+
onError(e, item);
|
|
78
|
+
}
|
|
73
79
|
}
|
|
80
|
+
return files;
|
|
74
81
|
}
|
|
75
|
-
|
|
82
|
+
const iterator = resolveItems(tp, source, { items, signal: o.signal, trust: o.trust });
|
|
83
|
+
const slots = [];
|
|
84
|
+
let nextSlot = 0;
|
|
85
|
+
let firstFailure;
|
|
86
|
+
let inFlight = 0;
|
|
87
|
+
let exhausted = false;
|
|
88
|
+
await new Promise((resolve, reject) => {
|
|
89
|
+
let settled = false;
|
|
90
|
+
const finish = () => {
|
|
91
|
+
if (settled)
|
|
92
|
+
return;
|
|
93
|
+
settled = true;
|
|
94
|
+
if (firstFailure)
|
|
95
|
+
reject(firstFailure);
|
|
96
|
+
else
|
|
97
|
+
resolve();
|
|
98
|
+
};
|
|
99
|
+
const launch = (item) => {
|
|
100
|
+
const slot = nextSlot++;
|
|
101
|
+
inFlight++;
|
|
102
|
+
void (async () => {
|
|
103
|
+
try {
|
|
104
|
+
if (item.error)
|
|
105
|
+
throw item.error;
|
|
106
|
+
slots[slot] = await tp.download(item.info, { ...rest, fields: { ...rest.fields, ...playlistFields(item) } });
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
if (o.signal?.aborted || !onError)
|
|
110
|
+
firstFailure ??= e;
|
|
111
|
+
else
|
|
112
|
+
onError(e, item);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
inFlight--;
|
|
116
|
+
pump();
|
|
117
|
+
}
|
|
118
|
+
})();
|
|
119
|
+
};
|
|
120
|
+
const pump = () => {
|
|
121
|
+
void (async () => {
|
|
122
|
+
try {
|
|
123
|
+
while (!exhausted && !firstFailure && !o.signal?.aborted && inFlight < limit) {
|
|
124
|
+
const step = await iterator.next();
|
|
125
|
+
if (step.done) {
|
|
126
|
+
exhausted = true;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
launch(step.value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
firstFailure ??= e;
|
|
134
|
+
}
|
|
135
|
+
if (inFlight === 0 && (exhausted || firstFailure || o.signal?.aborted))
|
|
136
|
+
finish();
|
|
137
|
+
})();
|
|
138
|
+
};
|
|
139
|
+
pump();
|
|
140
|
+
});
|
|
141
|
+
return slots.filter((f) => f !== undefined);
|
|
76
142
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boy-offi9-inc/tidepull",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|
|
@@ -78,4 +78,4 @@
|
|
|
78
78
|
"library",
|
|
79
79
|
"typescript"
|
|
80
80
|
]
|
|
81
|
-
}
|
|
81
|
+
}
|