@boy-offi9-inc/tidepull 0.5.0 → 0.7.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 +5 -5
- 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 +1 -10
- package/dist/core/downloader.js +44 -9
- 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 +0 -2
- package/dist/core/hls.js +2 -3
- 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 +43 -9
- package/dist/dash/parse.d.ts +1 -3
- package/dist/dash/parse.js +83 -83
- 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/dist/core/template.d.ts
CHANGED
package/dist/core/template.js
CHANGED
package/dist/core/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Emit, Log } from "./events.js";
|
|
2
2
|
import type { Http } from "./http.js";
|
|
3
3
|
export type Protocol = "https" | "hls" | "dash";
|
|
4
|
-
/** One downloadable stream. */
|
|
5
4
|
export interface Format {
|
|
6
5
|
format_id: string;
|
|
7
6
|
url: string;
|
|
@@ -10,50 +9,46 @@ export interface Format {
|
|
|
10
9
|
width?: number;
|
|
11
10
|
height?: number;
|
|
12
11
|
fps?: number;
|
|
13
|
-
/** "none" = no video track. "unknown" = present but codec not known. */
|
|
14
12
|
vcodec: string;
|
|
15
|
-
/** "none" = no audio track. */
|
|
16
13
|
acodec: string;
|
|
17
|
-
/** total bitrate, kbit/s */
|
|
18
14
|
tbr?: number;
|
|
19
|
-
/** audio bitrate, kbit/s */
|
|
20
15
|
abr?: number;
|
|
21
16
|
filesize?: number;
|
|
22
17
|
format_note?: string;
|
|
23
18
|
http_headers?: Record<string, string>;
|
|
24
|
-
/** dash: init segment URL (written first) */
|
|
25
19
|
init?: string;
|
|
26
|
-
/** dash: media segment URLs in order */
|
|
27
20
|
fragments?: string[];
|
|
28
21
|
}
|
|
29
|
-
|
|
22
|
+
export interface Subtitle {
|
|
23
|
+
lang: string;
|
|
24
|
+
url: string;
|
|
25
|
+
protocol: "hls";
|
|
26
|
+
isDefault?: boolean;
|
|
27
|
+
}
|
|
30
28
|
export interface Info {
|
|
31
29
|
id: string;
|
|
32
30
|
title: string;
|
|
33
31
|
extractor: string;
|
|
34
32
|
webpage_url: string;
|
|
35
33
|
formats: Format[];
|
|
34
|
+
subtitles?: Subtitle[];
|
|
36
35
|
thumbnail?: string;
|
|
37
36
|
duration?: number;
|
|
38
37
|
uploader?: string;
|
|
39
38
|
upload_date?: string;
|
|
40
39
|
[key: string]: unknown;
|
|
41
40
|
}
|
|
42
|
-
/** Passed to every extractor call. */
|
|
43
41
|
export interface Ctx {
|
|
44
42
|
http: Http;
|
|
45
43
|
log: Log;
|
|
46
44
|
emit: Emit;
|
|
47
|
-
/** Ordered extractor list (plugins, built-ins, generic last). */
|
|
48
45
|
extractors: readonly Extractor[];
|
|
49
46
|
signal?: AbortSignal;
|
|
50
47
|
}
|
|
51
|
-
/** One item of a playlist. It is extracted on its own when it is downloaded. */
|
|
52
48
|
export interface PlaylistEntry {
|
|
53
49
|
url: string;
|
|
54
50
|
title?: string;
|
|
55
51
|
}
|
|
56
|
-
/** A list of items found at one URL (a feed, a playlist file). */
|
|
57
52
|
export interface Playlist {
|
|
58
53
|
kind: "playlist";
|
|
59
54
|
id: string;
|
|
@@ -62,7 +57,6 @@ export interface Playlist {
|
|
|
62
57
|
webpage_url: string;
|
|
63
58
|
entries: PlaylistEntry[];
|
|
64
59
|
}
|
|
65
|
-
/** The extractor contract. One file per site. */
|
|
66
60
|
export interface Extractor {
|
|
67
61
|
readonly name: string;
|
|
68
62
|
suitable(url: URL): boolean;
|
package/dist/dash/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Opt-in DASH (VOD) support.
|
|
3
|
-
* import dash from "@boy-offi9-inc/tidepull/dash";
|
|
4
|
-
* createTidepull({ plugins: [dash] });
|
|
5
|
-
*/
|
|
6
1
|
import { TidepullError } from "../core/errors.js";
|
|
7
2
|
import { extOf, splitCodecs, stem } from "../extractors/util.js";
|
|
8
3
|
import { definePlugin } from "../plugins.js";
|
|
@@ -21,10 +16,9 @@ export async function extractDash(url, ctx) {
|
|
|
21
16
|
const mpd = parseMpd(text, finalUrl);
|
|
22
17
|
if (mpd.dynamic)
|
|
23
18
|
throw new TidepullError("Live DASH streams are not supported yet");
|
|
24
|
-
if (mpd.periods > 1)
|
|
25
|
-
ctx.log.warn(`Manifest has ${mpd.periods} periods; only the first is used`);
|
|
26
19
|
for (const s of mpd.skipped)
|
|
27
20
|
ctx.log.warn(`Skipping representation ${s}`);
|
|
21
|
+
const reps = mergePeriods(mpd.reps, mpd.periods, ctx);
|
|
28
22
|
const seen = new Set();
|
|
29
23
|
const uniq = (id) => {
|
|
30
24
|
let out = id;
|
|
@@ -34,9 +28,9 @@ export async function extractDash(url, ctx) {
|
|
|
34
28
|
return out;
|
|
35
29
|
};
|
|
36
30
|
const formats = [];
|
|
37
|
-
for (const r of
|
|
31
|
+
for (const r of reps) {
|
|
38
32
|
if (r.kind === "other")
|
|
39
|
-
continue;
|
|
33
|
+
continue;
|
|
40
34
|
const { v, a } = splitCodecs(r.codecs);
|
|
41
35
|
const video = r.kind === "video";
|
|
42
36
|
const f = {
|
|
@@ -66,5 +60,45 @@ export async function extractDash(url, ctx) {
|
|
|
66
60
|
name = url.pathname.split("/").filter(Boolean).slice(-2, -1)[0] ?? url.hostname;
|
|
67
61
|
return { id: name || url.hostname, title: name || url.hostname, extractor: "dash", webpage_url: url.href, formats };
|
|
68
62
|
}
|
|
63
|
+
function mergePeriods(reps, periods, ctx) {
|
|
64
|
+
if (periods <= 1)
|
|
65
|
+
return reps;
|
|
66
|
+
const byId = new Map();
|
|
67
|
+
for (const r of reps) {
|
|
68
|
+
const list = byId.get(r.id) ?? [];
|
|
69
|
+
list.push(r);
|
|
70
|
+
byId.set(r.id, list);
|
|
71
|
+
}
|
|
72
|
+
const merged = [];
|
|
73
|
+
for (const [, list] of byId) {
|
|
74
|
+
if (list.length !== periods) {
|
|
75
|
+
ctx.log.warn(`Skipping representation ${list[0].id}: it does not appear in every period (${list.length}/${periods})`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
list.sort((a, b) => a.period - b.period);
|
|
79
|
+
const [first, ...rest] = list;
|
|
80
|
+
if (rest.some((r) => r.mime !== first.mime || r.codecs !== first.codecs)) {
|
|
81
|
+
ctx.log.warn(`Skipping representation ${first.id}: codecs or mime type change between periods`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (first.single) {
|
|
85
|
+
ctx.log.warn(`Skipping representation ${first.id}: whole-file representations cannot span periods`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const fragments = [];
|
|
89
|
+
for (const r of list) {
|
|
90
|
+
if (!r.fragments?.length || !r.init) {
|
|
91
|
+
ctx.log.warn(`Skipping representation ${first.id}: a period has no fragment list`);
|
|
92
|
+
fragments.length = 0;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
fragments.push(...r.fragments);
|
|
96
|
+
}
|
|
97
|
+
if (!fragments.length)
|
|
98
|
+
continue;
|
|
99
|
+
merged.push({ ...first, fragments, init: first.init });
|
|
100
|
+
}
|
|
101
|
+
return merged;
|
|
102
|
+
}
|
|
69
103
|
export { parseMpd };
|
|
70
104
|
export default definePlugin({ name: "dash", apiVersion: 1, extractors: [dashExtractor] });
|
package/dist/dash/parse.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export interface DashRep {
|
|
2
2
|
id: string;
|
|
3
|
+
period: number;
|
|
3
4
|
kind: "video" | "audio" | "other";
|
|
4
5
|
mime: string;
|
|
5
6
|
codecs?: string;
|
|
@@ -8,10 +9,8 @@ export interface DashRep {
|
|
|
8
9
|
height?: number;
|
|
9
10
|
fps?: number;
|
|
10
11
|
lang?: string;
|
|
11
|
-
/** fragmented: init segment + media segments */
|
|
12
12
|
init?: string;
|
|
13
13
|
fragments?: string[];
|
|
14
|
-
/** whole-file representation (SegmentBase / plain BaseURL) */
|
|
15
14
|
single?: string;
|
|
16
15
|
}
|
|
17
16
|
export interface DashManifest {
|
|
@@ -21,7 +20,6 @@ export interface DashManifest {
|
|
|
21
20
|
skipped: string[];
|
|
22
21
|
}
|
|
23
22
|
export declare function parseIsoDuration(s?: string): number | undefined;
|
|
24
|
-
/** $RepresentationID$, $Number$, $Bandwidth$, $Time$ (+ %0Nd width), $$ */
|
|
25
23
|
export declare function fillTemplate(tpl: string, v: {
|
|
26
24
|
RepresentationID: string;
|
|
27
25
|
Bandwidth: number;
|
package/dist/dash/parse.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** Static (VOD) MPEG-DASH: SegmentTemplate (number / timeline), SegmentList, single-file SegmentBase. No DRM, no live. */
|
|
2
1
|
import { TidepullError } from "../core/errors.js";
|
|
3
2
|
import { child, kids, parseXml } from "./xml.js";
|
|
4
3
|
const MAX_FRAGMENTS = 50_000;
|
|
@@ -23,7 +22,6 @@ function resolve(base, rel) {
|
|
|
23
22
|
throw new TidepullError(`Refusing non-http(s) URL in manifest: ${u.protocol}`);
|
|
24
23
|
return u.href;
|
|
25
24
|
}
|
|
26
|
-
/** $RepresentationID$, $Number$, $Bandwidth$, $Time$ (+ %0Nd width), $$ */
|
|
27
25
|
export function fillTemplate(tpl, v) {
|
|
28
26
|
return tpl.replace(/\$(RepresentationID|Number|Bandwidth|Time)(?:%0(\d+)d)?\$|\$\$/g, (m, key, width) => {
|
|
29
27
|
if (m === "$$")
|
|
@@ -44,95 +42,97 @@ export function parseMpd(text, manifestUrl) {
|
|
|
44
42
|
const out = { reps: [], dynamic, periods: periods.length, skipped: [] };
|
|
45
43
|
if (dynamic || !periods.length)
|
|
46
44
|
return out;
|
|
47
|
-
const period = periods[0];
|
|
48
45
|
const mpdDur = parseIsoDuration(root.attrs.mediaPresentationDuration);
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
for (const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const tplNode = child(rep, "SegmentTemplate") ?? child(aset, "SegmentTemplate");
|
|
76
|
-
const listNode = child(rep, "SegmentList") ?? child(aset, "SegmentList");
|
|
77
|
-
if (tplNode) {
|
|
78
|
-
const tpl = { ...child(aset, "SegmentTemplate")?.attrs, ...child(rep, "SegmentTemplate")?.attrs };
|
|
79
|
-
if (!tpl.media)
|
|
80
|
-
throw new TidepullError("SegmentTemplate without @media");
|
|
81
|
-
if (tpl.initialization)
|
|
82
|
-
r.init = resolve(baseR, fillTemplate(tpl.initialization, vars));
|
|
83
|
-
const timescale = Number(tpl.timescale ?? 1) || 1;
|
|
84
|
-
let n = Number(tpl.startNumber ?? 1);
|
|
85
|
-
const frags = [];
|
|
86
|
-
const push = (num, time) => {
|
|
87
|
-
if (frags.length >= MAX_FRAGMENTS)
|
|
88
|
-
throw new TidepullError(`Too many fragments (>${MAX_FRAGMENTS})`);
|
|
89
|
-
frags.push(resolve(baseR, fillTemplate(tpl.media, { ...vars, Number: num, Time: time })));
|
|
46
|
+
const baseMpd = resolve(manifestUrl, child(root, "BaseURL")?.text);
|
|
47
|
+
for (const [pi, period] of periods.entries()) {
|
|
48
|
+
const periodDur = parseIsoDuration(period.attrs.duration) ?? (periods.length === 1 ? mpdDur : undefined);
|
|
49
|
+
const baseP = resolve(baseMpd, child(period, "BaseURL")?.text);
|
|
50
|
+
for (const aset of kids(period, "AdaptationSet")) {
|
|
51
|
+
const baseA = resolve(baseP, child(aset, "BaseURL")?.text);
|
|
52
|
+
for (const rep of kids(aset, "Representation")) {
|
|
53
|
+
const id = rep.attrs.id ?? String(out.reps.length);
|
|
54
|
+
const mime = rep.attrs.mimeType ?? aset.attrs.mimeType ?? "";
|
|
55
|
+
const ctype = aset.attrs.contentType ?? rep.attrs.contentType ?? mime.split("/")[0];
|
|
56
|
+
const kind = ctype === "video" ? "video" : ctype === "audio" ? "audio" : "other";
|
|
57
|
+
if (child(aset, "ContentProtection") || child(rep, "ContentProtection")) {
|
|
58
|
+
out.skipped.push(`${id}: encrypted (ContentProtection), cannot be downloaded`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const baseR = resolve(baseA, child(rep, "BaseURL")?.text);
|
|
63
|
+
const bandwidth = Number(rep.attrs.bandwidth ?? 0);
|
|
64
|
+
const r = {
|
|
65
|
+
id, period: pi, kind, mime,
|
|
66
|
+
codecs: rep.attrs.codecs ?? aset.attrs.codecs,
|
|
67
|
+
bandwidth,
|
|
68
|
+
width: Number(rep.attrs.width ?? aset.attrs.width) || undefined,
|
|
69
|
+
height: Number(rep.attrs.height ?? aset.attrs.height) || undefined,
|
|
70
|
+
fps: parseRate(rep.attrs.frameRate ?? aset.attrs.frameRate),
|
|
71
|
+
lang: aset.attrs.lang,
|
|
90
72
|
};
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
73
|
+
const vars = { RepresentationID: id, Bandwidth: bandwidth };
|
|
74
|
+
const tplNode = child(rep, "SegmentTemplate") ?? child(aset, "SegmentTemplate");
|
|
75
|
+
const listNode = child(rep, "SegmentList") ?? child(aset, "SegmentList");
|
|
76
|
+
if (tplNode) {
|
|
77
|
+
const tpl = { ...child(aset, "SegmentTemplate")?.attrs, ...child(rep, "SegmentTemplate")?.attrs };
|
|
78
|
+
if (!tpl.media)
|
|
79
|
+
throw new TidepullError("SegmentTemplate without @media");
|
|
80
|
+
if (tpl.initialization)
|
|
81
|
+
r.init = resolve(baseR, fillTemplate(tpl.initialization, vars));
|
|
82
|
+
const timescale = Number(tpl.timescale ?? 1) || 1;
|
|
83
|
+
let n = Number(tpl.startNumber ?? 1);
|
|
84
|
+
const frags = [];
|
|
85
|
+
const push = (num, time) => {
|
|
86
|
+
if (frags.length >= MAX_FRAGMENTS)
|
|
87
|
+
throw new TidepullError(`Too many fragments (>${MAX_FRAGMENTS})`);
|
|
88
|
+
frags.push(resolve(baseR, fillTemplate(tpl.media, { ...vars, Number: num, Time: time })));
|
|
89
|
+
};
|
|
90
|
+
const timeline = child(tplNode, "SegmentTimeline") ?? child(aset, "SegmentTimeline");
|
|
91
|
+
if (timeline) {
|
|
92
|
+
let t = 0;
|
|
93
|
+
for (const s of kids(timeline, "S")) {
|
|
94
|
+
if (s.attrs.t !== undefined)
|
|
95
|
+
t = Number(s.attrs.t);
|
|
96
|
+
const d = Number(s.attrs.d);
|
|
97
|
+
if (!(d > 0))
|
|
98
|
+
throw new TidepullError("SegmentTimeline entry without a positive @d");
|
|
99
|
+
let rep_ = Number(s.attrs.r ?? 0);
|
|
100
|
+
if (rep_ < 0) {
|
|
101
|
+
if (!periodDur)
|
|
102
|
+
throw new TidepullError("Open-ended SegmentTimeline repeat needs a known duration");
|
|
103
|
+
rep_ = Math.ceil((periodDur * timescale - t) / d) - 1;
|
|
104
|
+
}
|
|
105
|
+
for (let i = 0; i <= rep_; i++, n++, t += d)
|
|
106
|
+
push(n, t);
|
|
105
107
|
}
|
|
106
|
-
for (let i = 0; i <= rep_; i++, n++, t += d)
|
|
107
|
-
push(n, t);
|
|
108
108
|
}
|
|
109
|
+
else if (tpl.duration) {
|
|
110
|
+
if (!periodDur)
|
|
111
|
+
throw new TidepullError("Duration-based SegmentTemplate needs a known presentation duration");
|
|
112
|
+
const count = Math.ceil(periodDur / (Number(tpl.duration) / timescale));
|
|
113
|
+
for (let i = 0; i < count; i++)
|
|
114
|
+
push(n + i);
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
throw new TidepullError("SegmentTemplate has neither SegmentTimeline nor @duration");
|
|
118
|
+
r.fragments = frags;
|
|
109
119
|
}
|
|
110
|
-
else if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
120
|
+
else if (listNode) {
|
|
121
|
+
const init = child(listNode, "Initialization");
|
|
122
|
+
if (init?.attrs.range || kids(listNode, "SegmentURL").some((u) => u.attrs.mediaRange))
|
|
123
|
+
throw new TidepullError("Byte-range SegmentList is not supported yet");
|
|
124
|
+
if (init?.attrs.sourceURL)
|
|
125
|
+
r.init = resolve(baseR, init.attrs.sourceURL);
|
|
126
|
+
r.fragments = kids(listNode, "SegmentURL").map((u) => resolve(baseR, u.attrs.media));
|
|
116
127
|
}
|
|
117
|
-
else
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
else if (listNode) {
|
|
122
|
-
const init = child(listNode, "Initialization");
|
|
123
|
-
if (init?.attrs.range || kids(listNode, "SegmentURL").some((u) => u.attrs.mediaRange))
|
|
124
|
-
throw new TidepullError("Byte-range SegmentList is not supported yet");
|
|
125
|
-
if (init?.attrs.sourceURL)
|
|
126
|
-
r.init = resolve(baseR, init.attrs.sourceURL);
|
|
127
|
-
r.fragments = kids(listNode, "SegmentURL").map((u) => resolve(baseR, u.attrs.media));
|
|
128
|
+
else {
|
|
129
|
+
r.single = baseR;
|
|
130
|
+
}
|
|
131
|
+
out.reps.push(r);
|
|
128
132
|
}
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
catch (e) {
|
|
134
|
+
out.skipped.push(`${id}: ${e instanceof Error ? e.message : String(e)}`);
|
|
131
135
|
}
|
|
132
|
-
out.reps.push(r);
|
|
133
|
-
}
|
|
134
|
-
catch (e) {
|
|
135
|
-
out.skipped.push(`${id}: ${e instanceof Error ? e.message : String(e)}`);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
}
|
package/dist/dash/xml.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** Minimal XML parser: elements, attributes and text. DTDs and custom entities are not expanded; namespace prefixes are dropped. */
|
|
2
1
|
import { TidepullError } from "../core/errors.js";
|
|
3
2
|
const ENT = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
4
3
|
const decode = (s) => s.replace(/&(?:#x([0-9a-f]+)|#(\d+)|(\w+));/gi, (m, hex, dec, name) => {
|
|
@@ -9,7 +9,6 @@ export const directExtractor = {
|
|
|
9
9
|
};
|
|
10
10
|
export async function extractDirect(url, ctx) {
|
|
11
11
|
const ext = extOf(url.pathname);
|
|
12
|
-
// Try HEAD first; some servers reject it, so fall back to a 1-byte ranged GET.
|
|
13
12
|
let res = await ctx.http.request(url.href, { method: "HEAD" });
|
|
14
13
|
let size = res.ok ? Number(res.headers.get("content-length")) || undefined : undefined;
|
|
15
14
|
if (!res.ok) {
|
|
@@ -33,7 +33,6 @@ function findCandidates(html, base) {
|
|
|
33
33
|
found.push(new URL(decodeEntities(u).trim(), base).href);
|
|
34
34
|
}
|
|
35
35
|
catch {
|
|
36
|
-
/* ignore bad URLs */
|
|
37
36
|
}
|
|
38
37
|
};
|
|
39
38
|
for (const m of html.matchAll(/<meta\b[^>]*>/gi)) {
|
|
@@ -58,7 +57,6 @@ async function extractOne(u, ctx) {
|
|
|
58
57
|
throw new TidepullError(`${u.href} is a playlist, not a single media item`);
|
|
59
58
|
return result;
|
|
60
59
|
}
|
|
61
|
-
/** Fallback: look at the page itself (media content-type, og:video, <video>, bare media URLs). */
|
|
62
60
|
export const genericExtractor = {
|
|
63
61
|
name: "generic",
|
|
64
62
|
suitable: () => true,
|
package/dist/extractors/hls.js
CHANGED
|
@@ -11,8 +11,9 @@ export async function extractHls(url, ctx) {
|
|
|
11
11
|
if (!text.trimStart().startsWith("#EXTM3U"))
|
|
12
12
|
throw new TidepullError(`Not an HLS playlist: ${url.href}`);
|
|
13
13
|
const formats = [];
|
|
14
|
+
let master;
|
|
14
15
|
if (isMasterPlaylist(text)) {
|
|
15
|
-
|
|
16
|
+
master = parseMaster(text, finalUrl);
|
|
16
17
|
const audioGroups = new Map();
|
|
17
18
|
for (const r of master.renditions)
|
|
18
19
|
if (r.type === "AUDIO" && r.uri)
|
|
@@ -28,7 +29,6 @@ export async function extractHls(url, ctx) {
|
|
|
28
29
|
let audioCodec;
|
|
29
30
|
for (const v of master.variants) {
|
|
30
31
|
const { v: vc, a: ac } = splitCodecs(v.codecs);
|
|
31
|
-
// Variant points at an external audio group and lists no audio codec => video-only stream.
|
|
32
32
|
const external = !!v.audioGroup && audioGroups.has(v.audioGroup) && !ac && !!v.codecs;
|
|
33
33
|
if (ac)
|
|
34
34
|
audioCodec ??= ac;
|
|
@@ -64,9 +64,12 @@ export async function extractHls(url, ctx) {
|
|
|
64
64
|
else {
|
|
65
65
|
formats.push({ format_id: "hls", url: finalUrl, ext: "ts", protocol: "hls", vcodec: "unknown", acodec: "unknown" });
|
|
66
66
|
}
|
|
67
|
+
const subtitles = master?.renditions
|
|
68
|
+
.filter((r) => r.type === "SUBTITLES" && r.uri)
|
|
69
|
+
.map((r) => ({ lang: r.language || r.name, url: r.uri, protocol: "hls", isDefault: r.isDefault }));
|
|
67
70
|
let name = stem(url.pathname);
|
|
68
71
|
if (/^(index|master|playlist|manifest|stream|main)$/i.test(name)) {
|
|
69
72
|
name = url.pathname.split("/").filter(Boolean).slice(-2, -1)[0] ?? url.hostname;
|
|
70
73
|
}
|
|
71
|
-
return { id: name || url.hostname, title: name || url.hostname, extractor: "hls", webpage_url: url.href, formats };
|
|
74
|
+
return { id: name || url.hostname, title: name || url.hostname, extractor: "hls", webpage_url: url.href, formats, ...(subtitles?.length ? { subtitles } : {}) };
|
|
72
75
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Extractor } from "../core/types.js";
|
|
2
2
|
import type { TidepullPlugin } from "../plugins.js";
|
|
3
3
|
export declare const builtinExtractors: readonly Extractor[];
|
|
4
|
-
/** First `suitable()` match wins: plugin extractors (in order), then built-ins, with the generic fallback always last. */
|
|
5
4
|
export declare function buildRegistry(plugins?: readonly TidepullPlugin[]): Extractor[];
|
package/dist/extractors/index.js
CHANGED
|
@@ -2,7 +2,6 @@ import { directExtractor } from "./direct.js";
|
|
|
2
2
|
import { genericExtractor } from "./generic.js";
|
|
3
3
|
import { hlsExtractor } from "./hls.js";
|
|
4
4
|
export const builtinExtractors = [hlsExtractor, directExtractor, genericExtractor];
|
|
5
|
-
/** First `suitable()` match wins: plugin extractors (in order), then built-ins, with the generic fallback always last. */
|
|
6
5
|
export function buildRegistry(plugins = []) {
|
|
7
6
|
const specific = builtinExtractors.filter((e) => e !== genericExtractor);
|
|
8
7
|
return [...plugins.flatMap((p) => p.extractors ?? []), ...specific, genericExtractor];
|
package/dist/feeds.d.ts
CHANGED
|
@@ -3,9 +3,7 @@ export interface ParsedList {
|
|
|
3
3
|
title: string;
|
|
4
4
|
entries: PlaylistEntry[];
|
|
5
5
|
}
|
|
6
|
-
/** Items with a media enclosure from an RSS 2.0, RSS 1.0 or Atom feed. URLs are resolved against `base`. */
|
|
7
6
|
export declare function parseFeed(text: string, base: string): ParsedList;
|
|
8
|
-
/** Entries of an .m3u playlist file (#EXTINF titles, #PLAYLIST name). Only http(s) URLs are kept. */
|
|
9
7
|
export declare function parseM3u(text: string, base: string): ParsedList;
|
|
10
8
|
export declare const rssExtractor: {
|
|
11
9
|
name: string;
|
package/dist/feeds.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Playlist sources (opt-in: "@boy-offi9-inc/tidepull/feeds"): RSS/Atom feeds with media enclosures, and .m3u playlist files.
|
|
3
|
-
* import feeds from "@boy-offi9-inc/tidepull/feeds";
|
|
4
|
-
* createTidepull({ plugins: [feeds] });
|
|
5
|
-
* Each media URL becomes one playlist entry, extracted on its own when it is downloaded.
|
|
6
|
-
*/
|
|
7
1
|
import { TidepullError } from "./core/errors.js";
|
|
8
2
|
import { child, kids, parseXml } from "./dash/xml.js";
|
|
9
3
|
import { extOf, stem } from "./extractors/util.js";
|
|
@@ -11,7 +5,6 @@ import { definePlugin } from "./plugins.js";
|
|
|
11
5
|
const MAX_BYTES = 10 * 1024 * 1024;
|
|
12
6
|
const MAX_ENTRIES = 10_000;
|
|
13
7
|
const isMedia = (type) => !type || /^(audio|video)\//i.test(type) || /octet-stream/i.test(type);
|
|
14
|
-
/** The media URL of an RSS item or Atom entry: <enclosure>, an Atom rel="enclosure" link, or Media RSS <media:content>. */
|
|
15
8
|
function mediaUrl(item) {
|
|
16
9
|
const candidates = [
|
|
17
10
|
...kids(item, "enclosure").map((e) => ({ url: e.attrs.url, type: e.attrs.type })),
|
|
@@ -38,7 +31,6 @@ function toEntry(raw, title, base, seen) {
|
|
|
38
31
|
seen.add(url);
|
|
39
32
|
return title ? { url, title } : { url };
|
|
40
33
|
}
|
|
41
|
-
/** Items with a media enclosure from an RSS 2.0, RSS 1.0 or Atom feed. URLs are resolved against `base`. */
|
|
42
34
|
export function parseFeed(text, base) {
|
|
43
35
|
const root = parseXml(text);
|
|
44
36
|
let title;
|
|
@@ -63,7 +55,6 @@ export function parseFeed(text, base) {
|
|
|
63
55
|
}
|
|
64
56
|
return { title: title.trim(), entries };
|
|
65
57
|
}
|
|
66
|
-
/** Entries of an .m3u playlist file (#EXTINF titles, #PLAYLIST name). Only http(s) URLs are kept. */
|
|
67
58
|
export function parseM3u(text, base) {
|
|
68
59
|
let title = "";
|
|
69
60
|
let pending;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** Public library API. Everything not exported here is internal. */
|
|
2
1
|
export { Tidepull, createTidepull } from "./tidepull.js";
|
|
3
2
|
export type { TidepullOptions, CallOptions, Listener } from "./tidepull.js";
|
|
4
3
|
export { definePlugin, PLUGIN_API_VERSION } from "./plugins.js";
|
package/dist/index.js
CHANGED
package/dist/loader.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { type TidepullPlugin } from "./plugins.js";
|
|
2
2
|
export declare function validatePlugin(p: unknown, source: string): TidepullPlugin;
|
|
3
|
-
/** SECURITY: this executes the file's code with your privileges. Only load plugins you trust. */
|
|
4
3
|
export declare function loadPlugin(file: string): Promise<TidepullPlugin>;
|
|
5
|
-
/** Each path may be a plugin file, or a directory of plugin files and/or `<name>/index.*` folders (not recursive). */
|
|
6
4
|
export declare function loadPlugins(paths: string[]): Promise<TidepullPlugin[]>;
|
|
7
5
|
export declare function defaultPluginDir(env?: NodeJS.ProcessEnv): string;
|
package/dist/loader.js
CHANGED
|
@@ -6,7 +6,6 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
-
/** Loads plugins from disk (Node only). Opt-in: `import { loadPlugins } from "@boy-offi9-inc/tidepull/loader"`. */
|
|
10
9
|
import { readdir, stat } from "node:fs/promises";
|
|
11
10
|
import os from "node:os";
|
|
12
11
|
import path from "node:path";
|
|
@@ -29,7 +28,6 @@ export function validatePlugin(p, source) {
|
|
|
29
28
|
}
|
|
30
29
|
return o;
|
|
31
30
|
}
|
|
32
|
-
/** SECURITY: this executes the file's code with your privileges. Only load plugins you trust. */
|
|
33
31
|
export async function loadPlugin(file) {
|
|
34
32
|
let mod;
|
|
35
33
|
try {
|
|
@@ -42,7 +40,6 @@ export async function loadPlugin(file) {
|
|
|
42
40
|
}
|
|
43
41
|
const FILE_RE = /\.(?:m?js|cjs|mts|ts)$/;
|
|
44
42
|
const INDEX_RE = /^index\.(?:m?js|mts|ts)$/;
|
|
45
|
-
/** Each path may be a plugin file, or a directory of plugin files and/or `<name>/index.*` folders (not recursive). */
|
|
46
43
|
export async function loadPlugins(paths) {
|
|
47
44
|
const out = [];
|
|
48
45
|
for (const p of paths) {
|
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -16,7 +16,6 @@ import { humanRenderer, jsonRenderer } from "./ui/render.js";
|
|
|
16
16
|
import { renderBanner } from "./ui/banner.js";
|
|
17
17
|
import { pickTheme, THEME_NAMES } from "./ui/theme.js";
|
|
18
18
|
import { Spinner } from "./ui/spinner.js";
|
|
19
|
-
/** Package version, read from package.json. */
|
|
20
19
|
export const VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
21
20
|
const HELP = `tidepull ${VERSION}: a minimal media downloader
|
|
22
21
|
|
|
@@ -59,6 +58,7 @@ Config:
|
|
|
59
58
|
|
|
60
59
|
Network:
|
|
61
60
|
--limit-rate RATE Maximum download speed per file, e.g. 500K or 1.5M
|
|
61
|
+
--write-subs Download subtitle tracks (HLS SUBTITLES renditions) as .vtt next to the media
|
|
62
62
|
--cookies FILE Send cookies from a Netscape cookies.txt file, only to the hosts they belong to
|
|
63
63
|
-N, --concurrent-fragments N HLS segments fetched in parallel (default: 4)
|
|
64
64
|
-R, --retries N Retries per request (default: 3)
|
|
@@ -141,6 +141,7 @@ export async function main(argv) {
|
|
|
141
141
|
"batch-file": { type: "string", short: "a" },
|
|
142
142
|
"download-archive": { type: "string" },
|
|
143
143
|
"limit-rate": { type: "string" },
|
|
144
|
+
"write-subs": { type: "boolean" },
|
|
144
145
|
cookies: { type: "string" },
|
|
145
146
|
"config-location": { type: "string" },
|
|
146
147
|
"no-config": { type: "boolean" },
|
|
@@ -179,8 +180,6 @@ export async function main(argv) {
|
|
|
179
180
|
process.stderr.write(`ERROR: ${errMsg(e)}\n`);
|
|
180
181
|
return 2;
|
|
181
182
|
}
|
|
182
|
-
// Banner: interactive terminals only. Anything machine-facing (quiet, json,
|
|
183
|
-
// get-url, simulate, progress-json) or non-TTY stdout suppresses it.
|
|
184
183
|
const machineMode = values.quiet || values["dump-json"] || values["get-url"] || values.simulate || values["progress-json"];
|
|
185
184
|
if (process.stdout.isTTY && !machineMode) {
|
|
186
185
|
process.stderr.write(renderBanner(VERSION, theme, !values["no-color"] && shouldUseColor()) + "\n");
|
|
@@ -227,7 +226,6 @@ export async function main(argv) {
|
|
|
227
226
|
headers[h.slice(0, i).trim()] = h.slice(i + 1).trim();
|
|
228
227
|
}
|
|
229
228
|
const num = (v, d) => (v !== undefined && Number.isFinite(Number(v)) && Number(v) >= 0 ? Number(v) : d);
|
|
230
|
-
// plugins: explicit --plugin paths, plus the default dir unless disabled
|
|
231
229
|
const pluginPaths = [...(values.plugin ?? [])];
|
|
232
230
|
const defDir = defaultPluginDir();
|
|
233
231
|
if (!values["no-plugins"] && existsSync(defDir))
|
|
@@ -263,11 +261,11 @@ export async function main(argv) {
|
|
|
263
261
|
remuxVideo: values["remux-video"],
|
|
264
262
|
headers,
|
|
265
263
|
limitRate,
|
|
264
|
+
writeSubs: !!values["write-subs"],
|
|
266
265
|
};
|
|
267
266
|
for (const k of Object.keys(opts))
|
|
268
267
|
if (opts[k] === undefined)
|
|
269
268
|
delete opts[k];
|
|
270
|
-
// Ctrl-C cancels in-flight work cleanly (partial .part files are kept for resume)
|
|
271
269
|
const ctl = new AbortController();
|
|
272
270
|
const onSigint = () => ctl.abort();
|
|
273
271
|
process.once("SIGINT", onSigint);
|
|
@@ -280,7 +278,6 @@ export async function main(argv) {
|
|
|
280
278
|
process.stderr.write(e.stack + "\n");
|
|
281
279
|
code = Math.max(code, e instanceof TidepullError ? e.exitCode : 1);
|
|
282
280
|
};
|
|
283
|
-
// Spinner for the resolve/extract phase: TTY stderr only, machine modes never see it.
|
|
284
281
|
const interactive = process.stdout.isTTY && !machineMode && !values["no-progress"];
|
|
285
282
|
const spinner = new Spinner({ text: "extracting…", env: process.env });
|
|
286
283
|
const spin = () => {
|
|
@@ -288,7 +285,6 @@ export async function main(argv) {
|
|
|
288
285
|
spinner.start();
|
|
289
286
|
};
|
|
290
287
|
const stopSpin = (final) => spinner.stop(final);
|
|
291
|
-
/** What to do with one extracted item: print info, list formats, print URLs, or download it. */
|
|
292
288
|
const handle = async (info, fields) => {
|
|
293
289
|
if (values["dump-json"])
|
|
294
290
|
process.stdout.write(JSON.stringify(info) + "\n");
|
|
@@ -323,7 +319,6 @@ export async function main(argv) {
|
|
|
323
319
|
const resolved = await tp.resolve(url, { signal: ctl.signal });
|
|
324
320
|
if (isPlaylist(resolved) && (values["dump-json"] || values["list-formats"])) {
|
|
325
321
|
stopSpin(`${resolved.title}: ${resolved.entries.length} item(s)`);
|
|
326
|
-
// listing a playlist needs no request per entry
|
|
327
322
|
if (values["dump-json"])
|
|
328
323
|
process.stdout.write(JSON.stringify(resolved) + "\n");
|
|
329
324
|
else {
|