@boy-offi9-inc/tidepull 0.10.1 → 0.11.1

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 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 |
@@ -22,9 +22,6 @@ export interface DownloadOpts {
22
22
  export declare function downloadHttp(url: string, dest: string, o: DownloadOpts & {
23
23
  resume: boolean;
24
24
  }): Promise<void>;
25
- export interface HlsOpts extends DownloadOpts {
26
- concurrency: number;
27
- }
28
25
  export interface SegmentRef {
29
26
  url: string;
30
27
  seq: number;
@@ -45,7 +42,12 @@ export interface HlsOpts extends DownloadOpts {
45
42
  maxDuration?: number;
46
43
  maxSegments?: number;
47
44
  refreshMs?: number;
45
+ from?: number;
46
+ to?: number;
48
47
  }
48
+ export declare function sliceByDuration(segments: Array<SegmentRef & {
49
+ duration?: number;
50
+ }>, from?: number, to?: number): SegmentRef[];
49
51
  export declare function makeSegmentFetcher(o: HlsOpts): {
50
52
  fetchBuf: (u: string, range?: {
51
53
  start: number;
@@ -83,6 +83,26 @@ 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
+ }
86
106
  function partWriter(dest) {
87
107
  const out = createWriteStream(`${dest}.part`);
88
108
  const write = (b) => new Promise((res, rej) => out.write(b, (e) => (e ? rej(e) : res())));
@@ -187,13 +207,20 @@ export async function downloadHls(url, dest, o) {
187
207
  throw new TidepullError("Expected a media playlist, got a master playlist");
188
208
  const media = parseMedia(text, finalUrl);
189
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");
190
212
  if (!o.live)
191
213
  throw new TidepullError("Live HLS streams are not supported yet; pass live: true to record them");
192
214
  return downloadHlsLive(url, dest, o);
193
215
  }
194
- return downloadSegments(media.segments, dest, o);
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);
195
220
  }
196
221
  export async function downloadHlsLive(url, dest, o) {
222
+ if (o.from !== undefined || o.to !== undefined)
223
+ throw new TidepullError("Time-range clipping (--from/--to) only works on VOD playlists");
197
224
  const first = await o.http.text(url, { headers: o.headers, signal: o.signal });
198
225
  if (isMasterPlaylist(first.text))
199
226
  throw new TidepullError("Expected a media playlist, got a master playlist");
@@ -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>;
@@ -93,8 +93,12 @@ export async function downloadInfo(info, opts, ctx) {
93
93
  const limiter = opts.limitRate ? new RateLimiter(opts.limitRate) : undefined;
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
+ if (opts.from !== undefined || opts.to !== undefined) {
97
+ if (f.protocol !== "hls")
98
+ throw new TidepullError(`Time-range clipping (--from/--to) is only supported for HLS VOD downloads, not ${f.protocol}`);
99
+ }
96
100
  if (f.protocol === "hls")
97
- ({ ext } = await downloadHls(f.url, dest, { ...common, concurrency: opts.concurrency, live: opts.live, maxDuration: opts.maxDuration, maxSegments: opts.maxSegments }));
101
+ ({ 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
102
  else if (f.protocol === "dash")
99
103
  ({ ext } = await downloadDash(f, dest, { ...common, concurrency: opts.concurrency }));
100
104
  else
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.10.1",
3
+ "version": "0.11.1",
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=53",
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": {