@boy-offi9-inc/tidepull 0.4.2 → 0.6.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 -3
- package/dist/cliutil.d.ts +0 -8
- package/dist/cliutil.js +0 -10
- package/dist/cookies.d.ts +0 -5
- package/dist/cookies.js +0 -10
- package/dist/core/downloader.d.ts +9 -8
- package/dist/core/downloader.js +56 -14
- package/dist/core/errors.d.ts +0 -2
- package/dist/core/errors.js +0 -2
- package/dist/core/events.d.ts +0 -2
- package/dist/core/hls.d.ts +8 -0
- package/dist/core/hls.js +43 -5
- package/dist/core/http.d.ts +0 -13
- package/dist/core/http.js +0 -7
- package/dist/core/meter.d.ts +0 -1
- package/dist/core/meter.js +0 -1
- package/dist/core/pipeline.d.ts +1 -13
- package/dist/core/pipeline.js +16 -8
- package/dist/core/selector.d.ts +0 -10
- package/dist/core/selector.js +0 -1
- package/dist/core/template.d.ts +0 -1
- package/dist/core/template.js +0 -1
- package/dist/core/types.d.ts +7 -13
- package/dist/dash/index.js +1 -6
- package/dist/dash/parse.d.ts +0 -3
- package/dist/dash/parse.js +1 -3
- package/dist/dash/xml.js +0 -1
- package/dist/extractors/direct.js +0 -1
- package/dist/extractors/generic.d.ts +0 -1
- package/dist/extractors/generic.js +0 -2
- package/dist/extractors/hls.js +6 -3
- package/dist/extractors/index.d.ts +0 -1
- package/dist/extractors/index.js +0 -1
- package/dist/feeds.d.ts +0 -2
- package/dist/feeds.js +0 -9
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/loader.d.ts +0 -2
- package/dist/loader.js +0 -3
- package/dist/main.d.ts +0 -1
- package/dist/main.js +3 -8
- package/dist/playlist.d.ts +0 -18
- package/dist/playlist.js +0 -15
- package/dist/plugins.d.ts +0 -2
- package/dist/plugins.js +0 -2
- package/dist/tidepull.d.ts +0 -21
- package/dist/tidepull.js +0 -18
- package/dist/ui/banner.d.ts +0 -1
- package/dist/ui/banner.js +0 -2
- package/dist/ui/format.d.ts +0 -1
- package/dist/ui/format.js +0 -1
- package/dist/ui/log.d.ts +0 -2
- package/dist/ui/log.js +0 -2
- package/dist/ui/render.d.ts +0 -2
- package/dist/ui/render.js +0 -3
- package/dist/ui/spinner.d.ts +0 -5
- package/dist/ui/spinner.js +0 -5
- package/dist/ui/theme.d.ts +0 -5
- package/dist/ui/theme.js +0 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -183,14 +183,14 @@ 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: live streams, multi-period DASH (first period only), SAMPLE-AES,
|
|
186
|
+
- Not implemented yet: live streams, multi-period DASH (first period only), 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
|
|
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, separate audio renditions |
|
|
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
194
|
| 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`; encrypted (`ContentProtection`) representations are skipped, never decrypted |
|
|
195
195
|
| Generic page extractor | `og:video`, `<video>/<source>`, bare media URLs in HTML/JSON |
|
|
196
196
|
| Format selector | `b bv bv* ba ba* w wv wa`, `+` merge, `/` fallback, `()` groups, `[height<=720]` `[filesize<?50M]` `[ext=mp4]` |
|
|
@@ -205,7 +205,7 @@ The core does only what every user needs: extract → select → download, event
|
|
|
205
205
|
|
|
206
206
|
| Import | Files | Raw | Gzip | Minified (gzip) |
|
|
207
207
|
|---|---|---|---|---|
|
|
208
|
-
| `@boy-offi9-inc/tidepull` (core: direct, HLS, generic page, selector, templates, resume, merge, playlists contract) | 18 | 49.
|
|
208
|
+
| `@boy-offi9-inc/tidepull` (core: direct, HLS, generic page, selector, templates, resume, merge, playlists contract) | 18 | 49.6 KB | 13.7 KB | 23 KB (9.7 KB) |
|
|
209
209
|
| `@boy-offi9-inc/tidepull/dash` (adds XML + MPD parsing) | +6 | +14 KB | +4.7 KB | +6.7 KB (+3.1 KB) |
|
|
210
210
|
| `@boy-offi9-inc/tidepull/feeds` (RSS/Atom + .m3u; shares the XML parser with dash) | +5 | +9 KB | +3.3 KB | +3.8 KB (+1.9 KB) |
|
|
211
211
|
| `@boy-offi9-inc/tidepull/playlist` (`pullAll`, item selection) | +1 | +3.9 KB | +1.5 KB | +1.9 KB (+1.0 KB) |
|
package/dist/cliutil.d.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
import type { Info } from "./core/types.js";
|
|
2
|
-
/** "500K", "1.5M", "2G" or plain bytes per second. */
|
|
3
2
|
export declare function parseRate(s: string): number;
|
|
4
|
-
/** Splits a config line into arguments. Supports 'single' and "double" quotes and \" inside double quotes. */
|
|
5
3
|
export declare function splitArgs(line: string): string[];
|
|
6
|
-
/** One URL per line; blank lines and lines starting with # are ignored. "-" reads stdin. */
|
|
7
4
|
export declare function readUrlList(file: string): string[];
|
|
8
5
|
export declare function defaultConfigPath(env?: NodeJS.ProcessEnv): string;
|
|
9
|
-
/**
|
|
10
|
-
* Arguments from the config file: one or more options per line, # starts a comment.
|
|
11
|
-
* Placed before the command line so the command line wins. Returns [] when disabled or absent.
|
|
12
|
-
*/
|
|
13
6
|
export declare function configArgs(argv: string[], env?: NodeJS.ProcessEnv): string[];
|
|
14
|
-
/** Remembers finished downloads so later runs skip them. One key per line. */
|
|
15
7
|
export declare class Archive {
|
|
16
8
|
private file;
|
|
17
9
|
private keys;
|
package/dist/cliutil.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
/** CLI-only helpers: config file, batch input, download archive, rate parsing. Not part of the library entry point. */
|
|
2
1
|
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
3
2
|
import os from "node:os";
|
|
4
3
|
import path from "node:path";
|
|
5
4
|
import { TidepullError, errMsg } from "./core/errors.js";
|
|
6
|
-
/** "500K", "1.5M", "2G" or plain bytes per second. */
|
|
7
5
|
export function parseRate(s) {
|
|
8
6
|
const m = /^(\d+(?:\.\d+)?)([KMG])?$/i.exec(s.trim());
|
|
9
7
|
if (!m || !(Number(m[1]) > 0))
|
|
@@ -11,7 +9,6 @@ export function parseRate(s) {
|
|
|
11
9
|
const mult = { K: 1024, M: 1024 ** 2, G: 1024 ** 3 }[(m[2] ?? "").toUpperCase()] ?? 1;
|
|
12
10
|
return Math.round(Number(m[1]) * mult);
|
|
13
11
|
}
|
|
14
|
-
/** Splits a config line into arguments. Supports 'single' and "double" quotes and \" inside double quotes. */
|
|
15
12
|
export function splitArgs(line) {
|
|
16
13
|
const out = [];
|
|
17
14
|
let cur = "";
|
|
@@ -46,7 +43,6 @@ export function splitArgs(line) {
|
|
|
46
43
|
out.push(cur);
|
|
47
44
|
return out;
|
|
48
45
|
}
|
|
49
|
-
/** One URL per line; blank lines and lines starting with # are ignored. "-" reads stdin. */
|
|
50
46
|
export function readUrlList(file) {
|
|
51
47
|
let text;
|
|
52
48
|
try {
|
|
@@ -60,10 +56,6 @@ export function readUrlList(file) {
|
|
|
60
56
|
export function defaultConfigPath(env = process.env) {
|
|
61
57
|
return path.join(env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config"), "tidepull", "config");
|
|
62
58
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Arguments from the config file: one or more options per line, # starts a comment.
|
|
65
|
-
* Placed before the command line so the command line wins. Returns [] when disabled or absent.
|
|
66
|
-
*/
|
|
67
59
|
export function configArgs(argv, env = process.env) {
|
|
68
60
|
if (argv.includes("--no-config"))
|
|
69
61
|
return [];
|
|
@@ -87,9 +79,7 @@ export function configArgs(argv, env = process.env) {
|
|
|
87
79
|
}
|
|
88
80
|
return out;
|
|
89
81
|
}
|
|
90
|
-
/** Extractors whose ids are not globally unique (a file name, a playlist path): key them by URL instead. */
|
|
91
82
|
const BY_URL = new Set(["direct", "hls", "dash", "generic"]);
|
|
92
|
-
/** Remembers finished downloads so later runs skip them. One key per line. */
|
|
93
83
|
export class Archive {
|
|
94
84
|
file;
|
|
95
85
|
keys;
|
package/dist/cookies.d.ts
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
export interface Cookie {
|
|
2
2
|
domain: string;
|
|
3
|
-
/** also valid for subdomains of `domain` */
|
|
4
3
|
subdomains: boolean;
|
|
5
4
|
path: string;
|
|
6
5
|
secure: boolean;
|
|
7
|
-
/** seconds since epoch, 0 for a session cookie */
|
|
8
6
|
expires: number;
|
|
9
7
|
name: string;
|
|
10
8
|
value: string;
|
|
11
9
|
}
|
|
12
10
|
export declare function parseCookies(text: string): Cookie[];
|
|
13
|
-
/** The Cookie header value for `url`, or undefined when no cookie applies. */
|
|
14
11
|
export declare function cookieHeader(cookies: readonly Cookie[], url: URL, now?: number): string | undefined;
|
|
15
|
-
/** Plug into the HTTP client: `createTidepull({ http: { cookies: cookieJar(cookies) } })`. */
|
|
16
12
|
export declare const cookieJar: (cookies: readonly Cookie[]) => (url: URL) => string | undefined;
|
|
17
|
-
/** Reads a cookies.txt file. The warning (if any) says the file is readable by other users. */
|
|
18
13
|
export declare function loadCookieFile(file: string): {
|
|
19
14
|
cookies: Cookie[];
|
|
20
15
|
warning?: string;
|
package/dist/cookies.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Netscape cookies.txt support (opt-in: "@boy-offi9-inc/tidepull/cookies").
|
|
3
|
-
* Cookies are sent only to the hosts, paths and schemes they were issued for, and are re-evaluated on every redirect hop.
|
|
4
|
-
* Read-only: Set-Cookie responses are not stored. No public-suffix list: only a single-label domain guard.
|
|
5
|
-
*/
|
|
6
1
|
import { readFileSync, statSync } from "node:fs";
|
|
7
2
|
import { TidepullError, errMsg } from "./core/errors.js";
|
|
8
3
|
export function parseCookies(text) {
|
|
@@ -22,14 +17,12 @@ export function parseCookies(text) {
|
|
|
22
17
|
const bare = domain.replace(/^\./, "").toLowerCase();
|
|
23
18
|
if (!bare || !name)
|
|
24
19
|
continue;
|
|
25
|
-
// A domain without a dot ("com") must not match every site under it.
|
|
26
20
|
const subdomains = (flag.toUpperCase() === "TRUE" || domain.startsWith(".")) && bare.includes(".");
|
|
27
21
|
out.push({ domain: bare, subdomains, path: path || "/", secure: secure.toUpperCase() === "TRUE", expires: Number(expires) || 0, name, value: rest.join("\t") });
|
|
28
22
|
}
|
|
29
23
|
return out;
|
|
30
24
|
}
|
|
31
25
|
const pathMatch = (reqPath, cookiePath) => reqPath === cookiePath || (reqPath.startsWith(cookiePath) && (cookiePath.endsWith("/") || reqPath[cookiePath.length] === "/"));
|
|
32
|
-
/** The Cookie header value for `url`, or undefined when no cookie applies. */
|
|
33
26
|
export function cookieHeader(cookies, url, now = Date.now() / 1000) {
|
|
34
27
|
const host = url.hostname.toLowerCase();
|
|
35
28
|
const isIp = /^[\d.]+$/.test(host) || host.includes(":");
|
|
@@ -41,9 +34,7 @@ export function cookieHeader(cookies, url, now = Date.now() / 1000) {
|
|
|
41
34
|
return undefined;
|
|
42
35
|
return hits.sort((a, b) => b.path.length - a.path.length).map((c) => `${c.name}=${c.value}`).join("; ");
|
|
43
36
|
}
|
|
44
|
-
/** Plug into the HTTP client: `createTidepull({ http: { cookies: cookieJar(cookies) } })`. */
|
|
45
37
|
export const cookieJar = (cookies) => (url) => cookieHeader(cookies, url);
|
|
46
|
-
/** Reads a cookies.txt file. The warning (if any) says the file is readable by other users. */
|
|
47
38
|
export function loadCookieFile(file) {
|
|
48
39
|
let text;
|
|
49
40
|
try {
|
|
@@ -61,7 +52,6 @@ export function loadCookieFile(file) {
|
|
|
61
52
|
warning = `${file} is readable by other users; run: chmod 600 ${file}`;
|
|
62
53
|
}
|
|
63
54
|
catch {
|
|
64
|
-
/* permissions are advisory */
|
|
65
55
|
}
|
|
66
56
|
return { cookies, warning };
|
|
67
57
|
}
|
|
@@ -3,10 +3,8 @@ import { type HlsKey } from "./hls.js";
|
|
|
3
3
|
export interface Progress {
|
|
4
4
|
downloaded: number;
|
|
5
5
|
total?: number;
|
|
6
|
-
/** total is a guess (HLS: extrapolated from finished segments) */
|
|
7
6
|
estimated?: boolean;
|
|
8
7
|
}
|
|
9
|
-
/** Caps average throughput at `rate` bytes per second by delaying callers. */
|
|
10
8
|
export declare class RateLimiter {
|
|
11
9
|
private rate;
|
|
12
10
|
private start;
|
|
@@ -21,33 +19,36 @@ export interface DownloadOpts {
|
|
|
21
19
|
limiter?: RateLimiter;
|
|
22
20
|
onProgress: (p: Progress) => void;
|
|
23
21
|
}
|
|
24
|
-
/** Plain HTTP download. Resumes from `<dest>.part` with Range requests. */
|
|
25
22
|
export declare function downloadHttp(url: string, dest: string, o: DownloadOpts & {
|
|
26
23
|
resume: boolean;
|
|
27
24
|
}): Promise<void>;
|
|
28
25
|
export interface HlsOpts extends DownloadOpts {
|
|
29
26
|
concurrency: number;
|
|
30
27
|
}
|
|
31
|
-
/** A fragment to fetch. HLS segments and DASH fragments both reduce to this. */
|
|
32
28
|
export interface SegmentRef {
|
|
33
29
|
url: string;
|
|
34
30
|
seq: number;
|
|
35
31
|
key?: HlsKey;
|
|
36
|
-
/** init segment (fMP4); written whenever it changes */
|
|
37
32
|
map?: string;
|
|
33
|
+
range?: {
|
|
34
|
+
start: number;
|
|
35
|
+
length: number;
|
|
36
|
+
};
|
|
37
|
+
mapRange?: {
|
|
38
|
+
start: number;
|
|
39
|
+
length: number;
|
|
40
|
+
};
|
|
38
41
|
}
|
|
39
|
-
/** Fetches fragments in parallel, writes them in order (bounded memory), decrypts AES-128. Returns the container ext. */
|
|
40
42
|
export declare function downloadSegments(segments: SegmentRef[], dest: string, o: HlsOpts): Promise<{
|
|
41
43
|
ext: "ts" | "mp4";
|
|
42
44
|
}>;
|
|
43
|
-
/** HLS VOD: parse the media playlist, then hand the segments to the shared engine. */
|
|
44
45
|
export declare function downloadHls(url: string, dest: string, o: HlsOpts): Promise<{
|
|
45
46
|
ext: "ts" | "mp4";
|
|
46
47
|
}>;
|
|
47
|
-
/** DASH: fragments were already expanded by the extractor (format.init + format.fragments). */
|
|
48
48
|
export declare function downloadDash(f: {
|
|
49
49
|
init?: string;
|
|
50
50
|
fragments?: string[];
|
|
51
51
|
}, dest: string, o: HlsOpts): Promise<{
|
|
52
52
|
ext: "ts" | "mp4";
|
|
53
53
|
}>;
|
|
54
|
+
export declare function downloadHlsSubtitles(url: string, dest: string, o: HlsOpts): Promise<void>;
|
package/dist/core/downloader.js
CHANGED
|
@@ -6,7 +6,6 @@ import { Readable, Transform } from "node:stream";
|
|
|
6
6
|
import { pipeline } from "node:stream/promises";
|
|
7
7
|
import { TidepullError } from "./errors.js";
|
|
8
8
|
import { isMasterPlaylist, parseMedia } from "./hls.js";
|
|
9
|
-
/** Caps average throughput at `rate` bytes per second by delaying callers. */
|
|
10
9
|
export class RateLimiter {
|
|
11
10
|
rate;
|
|
12
11
|
start = Date.now();
|
|
@@ -21,7 +20,6 @@ export class RateLimiter {
|
|
|
21
20
|
await sleep(wait, undefined, { signal });
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
|
-
/** Plain HTTP download. Resumes from `<dest>.part` with Range requests. */
|
|
25
23
|
export async function downloadHttp(url, dest, o) {
|
|
26
24
|
const part = dest + ".part";
|
|
27
25
|
let offset = 0;
|
|
@@ -32,7 +30,6 @@ export async function downloadHttp(url, dest, o) {
|
|
|
32
30
|
headers.Range = `bytes=${offset}-`;
|
|
33
31
|
const res = await o.http.request(url, { headers, signal: o.signal });
|
|
34
32
|
if (res.status === 416) {
|
|
35
|
-
// Range not satisfiable: the .part is either complete or stale.
|
|
36
33
|
await res.body?.cancel();
|
|
37
34
|
const total = /\*\/(\d+)/.exec(res.headers.get("content-range") ?? "")?.[1];
|
|
38
35
|
if (total && Number(total) === offset) {
|
|
@@ -56,7 +53,7 @@ export async function downloadHttp(url, dest, o) {
|
|
|
56
53
|
total = m ? Number(m[2]) : undefined;
|
|
57
54
|
}
|
|
58
55
|
else {
|
|
59
|
-
offset = 0;
|
|
56
|
+
offset = 0;
|
|
60
57
|
const cl = res.headers.get("content-length");
|
|
61
58
|
total = cl ? Number(cl) : undefined;
|
|
62
59
|
}
|
|
@@ -86,7 +83,6 @@ export async function downloadHttp(url, dest, o) {
|
|
|
86
83
|
}
|
|
87
84
|
await rename(part, dest);
|
|
88
85
|
}
|
|
89
|
-
/** Fetches fragments in parallel, writes them in order (bounded memory), decrypts AES-128. Returns the container ext. */
|
|
90
86
|
export async function downloadSegments(segments, dest, o) {
|
|
91
87
|
if (!segments.length)
|
|
92
88
|
throw new TidepullError("No segments to download");
|
|
@@ -103,22 +99,29 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
103
99
|
}
|
|
104
100
|
return p;
|
|
105
101
|
};
|
|
106
|
-
const fetchBuf = async (u) => {
|
|
107
|
-
const
|
|
102
|
+
const fetchBuf = async (u, range) => {
|
|
103
|
+
const headers = { ...o.headers };
|
|
104
|
+
if (range)
|
|
105
|
+
headers.Range = `bytes=${range.start}-${range.start + range.length - 1}`;
|
|
106
|
+
const r = await o.http.request(u, { headers, signal: o.signal });
|
|
108
107
|
if (!r.ok) {
|
|
109
108
|
await r.body?.cancel();
|
|
110
109
|
throw new TidepullError(`HTTP ${r.status} for segment ${u}`);
|
|
111
110
|
}
|
|
112
|
-
|
|
111
|
+
const buf = Buffer.from(await r.arrayBuffer());
|
|
112
|
+
if (range && buf.length !== range.length) {
|
|
113
|
+
throw new TidepullError(`Segment byte range mismatch for ${u}: expected ${range.length} bytes, got ${buf.length}`);
|
|
114
|
+
}
|
|
115
|
+
return buf;
|
|
113
116
|
};
|
|
114
117
|
const fetchSeg = async (i) => {
|
|
115
118
|
const seg = segments[i];
|
|
116
|
-
let buf = await fetchBuf(seg.url);
|
|
119
|
+
let buf = await fetchBuf(seg.url, seg.range);
|
|
117
120
|
if (seg.key) {
|
|
118
121
|
const key = await getKey(seg.key.uri);
|
|
119
122
|
let iv = seg.key.iv;
|
|
120
123
|
if (!iv) {
|
|
121
|
-
iv = Buffer.alloc(16);
|
|
124
|
+
iv = Buffer.alloc(16);
|
|
122
125
|
iv.writeUInt32BE(seg.seq >>> 0, 12);
|
|
123
126
|
}
|
|
124
127
|
const d = createDecipheriv("aes-128-cbc", key, iv);
|
|
@@ -133,7 +136,7 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
133
136
|
const inflight = new Array(n);
|
|
134
137
|
const start = (i) => {
|
|
135
138
|
inflight[i] = fetchSeg(i);
|
|
136
|
-
inflight[i].catch(() => { });
|
|
139
|
+
inflight[i].catch(() => { });
|
|
137
140
|
};
|
|
138
141
|
let ext = "ts";
|
|
139
142
|
let lastMap;
|
|
@@ -148,7 +151,7 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
148
151
|
inflight[i] = undefined;
|
|
149
152
|
const map = segments[i].map;
|
|
150
153
|
if (map && map !== lastMap) {
|
|
151
|
-
const init = await fetchBuf(map);
|
|
154
|
+
const init = await fetchBuf(map, segments[i].mapRange);
|
|
152
155
|
await write(init);
|
|
153
156
|
bytes += init.length;
|
|
154
157
|
lastMap = map;
|
|
@@ -171,7 +174,6 @@ export async function downloadSegments(segments, dest, o) {
|
|
|
171
174
|
await rename(part, dest);
|
|
172
175
|
return { ext };
|
|
173
176
|
}
|
|
174
|
-
/** HLS VOD: parse the media playlist, then hand the segments to the shared engine. */
|
|
175
177
|
export async function downloadHls(url, dest, o) {
|
|
176
178
|
const { text, finalUrl } = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
177
179
|
if (isMasterPlaylist(text))
|
|
@@ -181,9 +183,49 @@ export async function downloadHls(url, dest, o) {
|
|
|
181
183
|
throw new TidepullError("Live HLS streams are not supported yet");
|
|
182
184
|
return downloadSegments(media.segments, dest, o);
|
|
183
185
|
}
|
|
184
|
-
/** DASH: fragments were already expanded by the extractor (format.init + format.fragments). */
|
|
185
186
|
export function downloadDash(f, dest, o) {
|
|
186
187
|
if (!f.fragments?.length)
|
|
187
188
|
throw new TidepullError("DASH format has no fragments");
|
|
188
189
|
return downloadSegments(f.fragments.map((url, seq) => ({ url, seq, map: f.init })), dest, o);
|
|
189
190
|
}
|
|
191
|
+
export async function downloadHlsSubtitles(url, dest, o) {
|
|
192
|
+
const { text, finalUrl } = await o.http.text(url, { headers: o.headers, signal: o.signal });
|
|
193
|
+
if (isMasterPlaylist(text))
|
|
194
|
+
throw new TidepullError("Expected a subtitle media playlist, got a master playlist");
|
|
195
|
+
const media = parseMedia(text, finalUrl);
|
|
196
|
+
if (!media.endList)
|
|
197
|
+
throw new TidepullError("Live subtitle streams are not supported yet");
|
|
198
|
+
const part = dest + ".part";
|
|
199
|
+
const out = createWriteStream(part);
|
|
200
|
+
const write = (s) => new Promise((res, rej) => out.write(s, (e) => (e ? rej(e) : res())));
|
|
201
|
+
try {
|
|
202
|
+
let header = "";
|
|
203
|
+
let lastMap;
|
|
204
|
+
for (const seg of media.segments) {
|
|
205
|
+
if (seg.map && seg.map !== lastMap) {
|
|
206
|
+
out.destroy();
|
|
207
|
+
await rm(part, { force: true });
|
|
208
|
+
throw new TidepullError("fMP4 subtitle tracks (EXT-X-MAP) are not supported yet; only WebVTT segment playlists are");
|
|
209
|
+
}
|
|
210
|
+
const r = await o.http.request(seg.url, { headers: o.headers, signal: o.signal });
|
|
211
|
+
if (!r.ok) {
|
|
212
|
+
await r.body?.cancel();
|
|
213
|
+
throw new TidepullError(`HTTP ${r.status} for subtitle segment ${seg.url}`);
|
|
214
|
+
}
|
|
215
|
+
const body = await r.text();
|
|
216
|
+
if (!header) {
|
|
217
|
+
header = body.slice(0, body.indexOf("\n\n") + 1);
|
|
218
|
+
await write(header);
|
|
219
|
+
}
|
|
220
|
+
const cues = body.startsWith("WEBVTT") ? body.slice(body.indexOf("\n\n") + 2) : body;
|
|
221
|
+
await write(cues.endsWith("\n") ? cues : cues + "\n");
|
|
222
|
+
}
|
|
223
|
+
await new Promise((res, rej) => out.end((e) => (e ? rej(e) : res())));
|
|
224
|
+
}
|
|
225
|
+
catch (e) {
|
|
226
|
+
out.destroy();
|
|
227
|
+
await rm(part, { force: true });
|
|
228
|
+
throw e;
|
|
229
|
+
}
|
|
230
|
+
await rename(part, dest);
|
|
231
|
+
}
|
package/dist/core/errors.d.ts
CHANGED
|
@@ -3,9 +3,7 @@ export declare class TidepullError extends Error {
|
|
|
3
3
|
constructor(message: string, exitCode?: number);
|
|
4
4
|
}
|
|
5
5
|
export declare const errMsg: (e: unknown) => string;
|
|
6
|
-
/** Thrown when a caller's AbortSignal fires. name === "AbortError" so it works with standard abort handling. */
|
|
7
6
|
export declare function abortError(): TidepullError;
|
|
8
|
-
/** Redirect policy violations. Never retried. */
|
|
9
7
|
export declare class RedirectError extends TidepullError {
|
|
10
8
|
constructor(message: string);
|
|
11
9
|
}
|
package/dist/core/errors.js
CHANGED
|
@@ -7,13 +7,11 @@ export class TidepullError extends Error {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
export const errMsg = (e) => (e instanceof Error ? e.message : String(e));
|
|
10
|
-
/** Thrown when a caller's AbortSignal fires. name === "AbortError" so it works with standard abort handling. */
|
|
11
10
|
export function abortError() {
|
|
12
11
|
const e = new TidepullError("Aborted", 130);
|
|
13
12
|
e.name = "AbortError";
|
|
14
13
|
return e;
|
|
15
14
|
}
|
|
16
|
-
/** Redirect policy violations. Never retried. */
|
|
17
15
|
export class RedirectError extends TidepullError {
|
|
18
16
|
constructor(message) {
|
|
19
17
|
super(message);
|
package/dist/core/events.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export type LogLevel = "debug" | "info" | "warn";
|
|
2
|
-
/** Everything the engine reports. Plain JSON-serializable objects, so they can be piped, logged or sent over IPC. */
|
|
3
2
|
export type TidepullEvent = {
|
|
4
3
|
type: "log";
|
|
5
4
|
level: LogLevel;
|
|
@@ -54,7 +53,6 @@ export type TidepullEvent = {
|
|
|
54
53
|
url?: string;
|
|
55
54
|
};
|
|
56
55
|
export type Emit = (e: TidepullEvent) => void;
|
|
57
|
-
/** Logging interface exposed to extractors. */
|
|
58
56
|
export interface Log {
|
|
59
57
|
debug(msg: string): void;
|
|
60
58
|
info(tag: string, msg: string): void;
|
package/dist/core/hls.d.ts
CHANGED
|
@@ -30,6 +30,14 @@ export interface HlsSegment {
|
|
|
30
30
|
seq: number;
|
|
31
31
|
key?: HlsKey;
|
|
32
32
|
map?: string;
|
|
33
|
+
range?: {
|
|
34
|
+
start: number;
|
|
35
|
+
length: number;
|
|
36
|
+
};
|
|
37
|
+
mapRange?: {
|
|
38
|
+
start: number;
|
|
39
|
+
length: number;
|
|
40
|
+
};
|
|
33
41
|
}
|
|
34
42
|
export interface HlsMedia {
|
|
35
43
|
segments: HlsSegment[];
|
package/dist/core/hls.js
CHANGED
|
@@ -67,6 +67,10 @@ export function parseMedia(text, base) {
|
|
|
67
67
|
let endList = false;
|
|
68
68
|
let key;
|
|
69
69
|
let map;
|
|
70
|
+
let mapRange;
|
|
71
|
+
let brLen;
|
|
72
|
+
let brOffset;
|
|
73
|
+
let lastEnd = 0;
|
|
70
74
|
const segments = [];
|
|
71
75
|
for (const line of ls) {
|
|
72
76
|
if (line.startsWith("#EXT-X-MEDIA-SEQUENCE:"))
|
|
@@ -75,8 +79,15 @@ export function parseMedia(text, base) {
|
|
|
75
79
|
dur = parseFloat(val(line)) || 0;
|
|
76
80
|
else if (line.startsWith("#EXT-X-ENDLIST"))
|
|
77
81
|
endList = true;
|
|
78
|
-
else if (line.startsWith("#EXT-X-BYTERANGE"))
|
|
79
|
-
|
|
82
|
+
else if (line.startsWith("#EXT-X-BYTERANGE")) {
|
|
83
|
+
const spec = val(line).split("@");
|
|
84
|
+
brLen = Number(spec[0]);
|
|
85
|
+
if (!brLen || brLen < 0)
|
|
86
|
+
throw new TidepullError(`Invalid EXT-X-BYTERANGE: ${val(line)}`);
|
|
87
|
+
brOffset = spec[1] !== undefined ? Number(spec[1]) : undefined;
|
|
88
|
+
if (spec[1] !== undefined && (Number.isNaN(brOffset) || brOffset < 0))
|
|
89
|
+
throw new TidepullError(`Invalid EXT-X-BYTERANGE offset: ${val(line)}`);
|
|
90
|
+
}
|
|
80
91
|
else if (line.startsWith("#EXT-X-KEY:")) {
|
|
81
92
|
const a = parseAttributes(val(line));
|
|
82
93
|
if (a.METHOD === "NONE")
|
|
@@ -89,12 +100,39 @@ export function parseMedia(text, base) {
|
|
|
89
100
|
}
|
|
90
101
|
else if (line.startsWith("#EXT-X-MAP:")) {
|
|
91
102
|
const a = parseAttributes(val(line));
|
|
92
|
-
if (a.BYTERANGE)
|
|
93
|
-
throw new TidepullError("HLS byte-range init segments are not supported yet");
|
|
94
103
|
map = a.URI ? new URL(a.URI, base).href : undefined;
|
|
104
|
+
if (a.BYTERANGE) {
|
|
105
|
+
const [len, off] = a.BYTERANGE.split("@");
|
|
106
|
+
const length = Number(len);
|
|
107
|
+
const start = Number(off ?? 0);
|
|
108
|
+
if (!length || start < 0)
|
|
109
|
+
throw new TidepullError(`Invalid EXT-X-MAP BYTERANGE: ${a.BYTERANGE}`);
|
|
110
|
+
mapRange = { start, length };
|
|
111
|
+
}
|
|
112
|
+
else
|
|
113
|
+
mapRange = undefined;
|
|
95
114
|
}
|
|
96
115
|
else if (!line.startsWith("#")) {
|
|
97
|
-
|
|
116
|
+
let range;
|
|
117
|
+
if (brLen !== undefined) {
|
|
118
|
+
const start = brOffset ?? lastEnd;
|
|
119
|
+
range = { start, length: brLen };
|
|
120
|
+
lastEnd = start + brLen;
|
|
121
|
+
brLen = undefined;
|
|
122
|
+
brOffset = undefined;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
lastEnd = 0;
|
|
126
|
+
}
|
|
127
|
+
segments.push({
|
|
128
|
+
url: new URL(line, base).href,
|
|
129
|
+
duration: dur,
|
|
130
|
+
seq: seq + segments.length,
|
|
131
|
+
key,
|
|
132
|
+
map,
|
|
133
|
+
...(map && mapRange ? { mapRange } : {}),
|
|
134
|
+
...(range ? { range } : {}),
|
|
135
|
+
});
|
|
98
136
|
dur = 0;
|
|
99
137
|
}
|
|
100
138
|
}
|
package/dist/core/http.d.ts
CHANGED
|
@@ -4,15 +4,7 @@ export interface HttpOptions {
|
|
|
4
4
|
retries?: number;
|
|
5
5
|
timeoutMs?: number;
|
|
6
6
|
maxRedirects?: number;
|
|
7
|
-
/**
|
|
8
|
-
* Scope for non-trivial custom headers (Authorization, Cookie, API tokens...).
|
|
9
|
-
* undefined = legacy/low-level mode: headers go to the origin each request starts at.
|
|
10
|
-
* string[] = scoped mode: they go ONLY to origins passed to `trust()` plus these host patterns
|
|
11
|
-
* ("cdn.example.com" or "*.example.com"). Tidepull uses scoped mode.
|
|
12
|
-
* Referer, Origin, User-Agent, Accept-* and Range are never considered secret and are always sent.
|
|
13
|
-
*/
|
|
14
7
|
headerHosts?: string[];
|
|
15
|
-
/** Returns the Cookie header value for a URL (see "@boy-offi9-inc/tidepull/cookies"). Called on every request and redirect hop. */
|
|
16
8
|
cookies?: (url: URL) => string | undefined;
|
|
17
9
|
}
|
|
18
10
|
export interface RequestInitLite {
|
|
@@ -21,9 +13,7 @@ export interface RequestInitLite {
|
|
|
21
13
|
signal?: AbortSignal;
|
|
22
14
|
}
|
|
23
15
|
export declare class Http {
|
|
24
|
-
/** non-secret defaults (UA, Accept-Language, Referer...), always sent */
|
|
25
16
|
headers: Record<string, string>;
|
|
26
|
-
/** custom headers that may be secret; sent only to trusted origins (scoped mode) */
|
|
27
17
|
private secret;
|
|
28
18
|
private scopedMode;
|
|
29
19
|
private hostPatterns;
|
|
@@ -34,12 +24,9 @@ export declare class Http {
|
|
|
34
24
|
maxRedirects: number;
|
|
35
25
|
private defaultSignal?;
|
|
36
26
|
constructor(o?: HttpOptions);
|
|
37
|
-
/** Allow scoped headers to be sent to this URL's origin (scoped mode only). Tidepull calls this for every URL you give it. */
|
|
38
27
|
trust(url: string | URL): void;
|
|
39
28
|
private headersFor;
|
|
40
|
-
/** A view of this client whose requests are cancelled by `signal` (so extractors don't need to pass it around). */
|
|
41
29
|
scoped(signal?: AbortSignal): Http;
|
|
42
|
-
/** fetch with manual, origin-aware redirects + retry (network errors, 5xx, 429) with backoff. The timeout covers headers only. */
|
|
43
30
|
request(url: string, init?: RequestInitLite): Promise<Response>;
|
|
44
31
|
private follow;
|
|
45
32
|
text(url: string, init?: RequestInitLite): Promise<{
|
package/dist/core/http.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { TidepullError, RedirectError, errMsg } from "./errors.js";
|
|
2
2
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
3
3
|
const REDIRECT = new Set([301, 302, 303, 307, 308]);
|
|
4
|
-
/** Sent to every host. All other custom headers are scoped (see HttpOptions.headerHosts). */
|
|
5
4
|
const OPEN_HEADERS = new Set(["user-agent", "accept", "accept-language", "accept-encoding", "range", "referer", "origin"]);
|
|
6
5
|
const hostMatches = (pattern, host) => pattern.startsWith("*.") ? host === pattern.slice(2) || host.endsWith(pattern.slice(1)) : host === pattern;
|
|
7
6
|
export class Http {
|
|
8
|
-
/** non-secret defaults (UA, Accept-Language, Referer...), always sent */
|
|
9
7
|
headers;
|
|
10
|
-
/** custom headers that may be secret; sent only to trusted origins (scoped mode) */
|
|
11
8
|
secret;
|
|
12
9
|
scopedMode;
|
|
13
10
|
hostPatterns;
|
|
@@ -36,7 +33,6 @@ export class Http {
|
|
|
36
33
|
this.timeoutMs = o.timeoutMs ?? 30_000;
|
|
37
34
|
this.maxRedirects = o.maxRedirects ?? 10;
|
|
38
35
|
}
|
|
39
|
-
/** Allow scoped headers to be sent to this URL's origin (scoped mode only). Tidepull calls this for every URL you give it. */
|
|
40
36
|
trust(url) {
|
|
41
37
|
this.trusted.add(new URL(url).origin);
|
|
42
38
|
}
|
|
@@ -44,7 +40,6 @@ export class Http {
|
|
|
44
40
|
const send = this.scopedMode ? this.trusted.has(u.origin) || this.hostPatterns.some((p) => hostMatches(p, u.hostname)) : u.origin === start.origin;
|
|
45
41
|
return send ? { ...this.headers, ...this.secret } : { ...this.headers };
|
|
46
42
|
}
|
|
47
|
-
/** A view of this client whose requests are cancelled by `signal` (so extractors don't need to pass it around). */
|
|
48
43
|
scoped(signal) {
|
|
49
44
|
if (!signal)
|
|
50
45
|
return this;
|
|
@@ -52,7 +47,6 @@ export class Http {
|
|
|
52
47
|
h.defaultSignal = signal;
|
|
53
48
|
return h;
|
|
54
49
|
}
|
|
55
|
-
/** fetch with manual, origin-aware redirects + retry (network errors, 5xx, 429) with backoff. The timeout covers headers only. */
|
|
56
50
|
async request(url, init = {}) {
|
|
57
51
|
init = { ...init, signal: init.signal ?? this.defaultSignal };
|
|
58
52
|
let lastErr;
|
|
@@ -81,7 +75,6 @@ export class Http {
|
|
|
81
75
|
const start = new URL(url);
|
|
82
76
|
let cur = start;
|
|
83
77
|
for (let hop = 0;; hop++) {
|
|
84
|
-
// per-request headers (Range, extractor-supplied Referer/Cookie...) only travel with the start origin; Range goes everywhere
|
|
85
78
|
const own = Object.fromEntries(Object.entries(init.headers ?? {}).filter(([k]) => cur.origin === start.origin || k.toLowerCase() === "range"));
|
|
86
79
|
const headers = { ...this.headersFor(cur, start), ...own };
|
|
87
80
|
const cookie = this.cookies?.(cur);
|
package/dist/core/meter.d.ts
CHANGED
package/dist/core/meter.js
CHANGED
package/dist/core/pipeline.d.ts
CHANGED
|
@@ -1,33 +1,21 @@
|
|
|
1
1
|
import type { Ctx, Info, Playlist } from "./types.js";
|
|
2
2
|
export declare const DEFAULT_OUTPUT = "%(title)s [%(id)s].%(ext)s";
|
|
3
3
|
export interface DownloadOptions {
|
|
4
|
-
/** format selector, default "bv*+ba/b" */
|
|
5
4
|
format: string;
|
|
6
|
-
/** output template. Default: "%(title)s [%(id)s].%(ext)s", or just the title when the id is identical (typical for direct files) */
|
|
7
5
|
output?: string;
|
|
8
|
-
/** base directory */
|
|
9
6
|
paths: string;
|
|
10
|
-
/** resume from .part files */
|
|
11
7
|
resume: boolean;
|
|
12
8
|
overwrite: boolean;
|
|
13
|
-
/** HLS segments fetched in parallel */
|
|
14
9
|
concurrency: number;
|
|
15
|
-
/** container used when merging video+audio */
|
|
16
10
|
mergeFormat: string;
|
|
17
11
|
remuxVideo?: string;
|
|
18
|
-
/** ffmpeg binary or its directory (default: search PATH) */
|
|
19
12
|
ffmpeg?: string;
|
|
20
|
-
/** extra request headers for the media downloads */
|
|
21
13
|
headers: Record<string, string>;
|
|
22
|
-
/** maximum average download speed in bytes per second (applies to each file) */
|
|
23
14
|
limitRate?: number;
|
|
24
|
-
/** extra output-template fields (playlist downloads set playlist, playlist_id, playlist_index, playlist_count) */
|
|
25
15
|
fields?: Record<string, string | number>;
|
|
16
|
+
writeSubs?: boolean;
|
|
26
17
|
}
|
|
27
18
|
export declare const isPlaylist: (x: Info | Playlist) => x is Playlist;
|
|
28
|
-
/** Resolves a URL with the first matching extractor: a single item (Info) or a Playlist. */
|
|
29
19
|
export declare function resolveUrl(rawUrl: string, ctx: Ctx): Promise<Info | Playlist>;
|
|
30
|
-
/** Like resolveUrl, but a playlist is an error (callers that need one item). */
|
|
31
20
|
export declare function extractInfo(rawUrl: string, ctx: Ctx): Promise<Info>;
|
|
32
|
-
/** Selects formats per opts.format, downloads them, merges/remuxes with ffmpeg if needed. Returns the final file path. */
|
|
33
21
|
export declare function downloadInfo(info: Info, opts: DownloadOptions, ctx: Ctx): Promise<string>;
|