nixamp 0.6.1 → 0.6.4

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/dist/audio.d.ts CHANGED
@@ -58,6 +58,20 @@ export declare function peaks(pcm: Float32Array): [number, number];
58
58
  /** Interleaved stereo down to mono, for the analyser. */
59
59
  export declare function toMono(pcm: Float32Array): Float32Array;
60
60
  export declare function formatTime(seconds: number): string;
61
+ /**
62
+ * The same tags, read without blocking anything.
63
+ *
64
+ * `probe` is spawnSync, and 0.5.5 tried to fix the tagging pass by yielding
65
+ * between files. That is not enough: each individual call still stops the
66
+ * process for as long as one ffprobe takes, and on a large file over a slow
67
+ * disk that is hundreds of milliseconds. Yield, block, yield, block, and a
68
+ * server delivers a stream in slivers -- measured at 357 KB/s on a machine
69
+ * whose disk reads at 6.5 MB/s and whose link runs at 1.4 Gbps.
70
+ *
71
+ * ffprobe still costs what it costs. It just costs it in a child process now,
72
+ * which is where that work belongs.
73
+ */
74
+ export declare function probeAsync(tools: Tools, path: string): Promise<Track>;
61
75
  /** What is actually inside a container, as opposed to what the name suggests. */
62
76
  export interface Codecs {
63
77
  /** e.g. "h264", "hevc", "vp9". Empty when there is no video stream. */
@@ -82,4 +96,13 @@ export declare function codecsOf(tools: Tools, path: string): Promise<Codecs>;
82
96
  * would cost a core per viewer and look worse. So the streams decide, one part
83
97
  * at a time -- a film can have its video copied and only its DTS re-encoded.
84
98
  */
85
- export declare function videoArgs(codecs: Codecs): string[];
99
+ export declare function videoArgs(codecs: Codecs, capKbps?: number): string[];
100
+ /**
101
+ * The width that suits a bitrate.
102
+ *
103
+ * 1080p squeezed into a megabit is worse than 360p at the same megabit: the
104
+ * encoder spends everything it has on detail it cannot afford and the result
105
+ * smears on every motion. Dropping the resolution with the bitrate is what
106
+ * makes a small stream watchable rather than merely small.
107
+ */
108
+ export declare function widthFor(kbps: number): number;
package/dist/audio.js CHANGED
@@ -204,6 +204,77 @@ export function formatTime(seconds) {
204
204
  const s = total % 60;
205
205
  return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
206
206
  }
207
+ /**
208
+ * The same tags, read without blocking anything.
209
+ *
210
+ * `probe` is spawnSync, and 0.5.5 tried to fix the tagging pass by yielding
211
+ * between files. That is not enough: each individual call still stops the
212
+ * process for as long as one ffprobe takes, and on a large file over a slow
213
+ * disk that is hundreds of milliseconds. Yield, block, yield, block, and a
214
+ * server delivers a stream in slivers -- measured at 357 KB/s on a machine
215
+ * whose disk reads at 6.5 MB/s and whose link runs at 1.4 Gbps.
216
+ *
217
+ * ffprobe still costs what it costs. It just costs it in a child process now,
218
+ * which is where that work belongs.
219
+ */
220
+ export async function probeAsync(tools, path) {
221
+ const [cmd, ...rest] = tools.ffprobe;
222
+ const fallback = {
223
+ path,
224
+ title: path.split("/").pop() ?? path,
225
+ artist: "",
226
+ album: "",
227
+ duration: 0,
228
+ };
229
+ if (!cmd)
230
+ return fallback;
231
+ return new Promise((done) => {
232
+ const child = spawn(cmd, [
233
+ ...rest,
234
+ "-v", "quiet", "-print_format", "json",
235
+ "-show_format", "-show_entries", "format_tags=title,artist,album",
236
+ path,
237
+ ], { stdio: ["ignore", "pipe", "ignore"] });
238
+ let out = "";
239
+ // A file that will not answer must not hold a place in the queue for ever.
240
+ const giveUp = setTimeout(() => child.kill("SIGKILL"), 20_000);
241
+ giveUp.unref?.();
242
+ child.stdout.on("data", (chunk) => {
243
+ if (out.length < 4 * 1024 * 1024)
244
+ out += chunk.toString("utf8");
245
+ });
246
+ child.on("error", () => {
247
+ clearTimeout(giveUp);
248
+ done(fallback);
249
+ });
250
+ child.on("close", (code) => {
251
+ clearTimeout(giveUp);
252
+ if (code !== 0)
253
+ return done(fallback);
254
+ done(readTags(out, fallback));
255
+ });
256
+ });
257
+ }
258
+ /** The tags out of ffprobe's JSON, or the filename when it said nothing useful. */
259
+ function readTags(stdout, fallback) {
260
+ try {
261
+ const parsed = JSON.parse(stdout);
262
+ const tags = parsed.format?.tags ?? {};
263
+ const lower = {};
264
+ for (const [k, v] of Object.entries(tags))
265
+ lower[k.toLowerCase()] = v;
266
+ return {
267
+ path: fallback.path,
268
+ title: lower.title || fallback.title,
269
+ artist: lower.artist ?? "",
270
+ album: lower.album ?? "",
271
+ duration: Number(parsed.format?.duration ?? 0) || 0,
272
+ };
273
+ }
274
+ catch {
275
+ return fallback;
276
+ }
277
+ }
207
278
  /**
208
279
  * Ask ffprobe what the streams are, without holding the event loop.
209
280
  *
@@ -253,7 +324,11 @@ export async function codecsOf(tools, path) {
253
324
  * would cost a core per viewer and look worse. So the streams decide, one part
254
325
  * at a time -- a film can have its video copied and only its DTS re-encoded.
255
326
  */
256
- export function videoArgs(codecs) {
327
+ export function videoArgs(codecs, capKbps = 0) {
328
+ // A ceiling means re-encoding whatever is there, because you cannot cap the
329
+ // bitrate of a stream you are copying: copying is what "unchanged" means.
330
+ if (capKbps > 0)
331
+ return cappedArgs(capKbps);
257
332
  // What a browser can play inside MP4 without help.
258
333
  const keepVideo = codecs.video === "h264";
259
334
  const keepAudio = codecs.audio === "aac" || codecs.audio === "mp3";
@@ -269,3 +344,43 @@ export function videoArgs(codecs) {
269
344
  "-movflags", "frag_keyframe+empty_moov+default_base_moof",
270
345
  ];
271
346
  }
347
+ /**
348
+ * The width that suits a bitrate.
349
+ *
350
+ * 1080p squeezed into a megabit is worse than 360p at the same megabit: the
351
+ * encoder spends everything it has on detail it cannot afford and the result
352
+ * smears on every motion. Dropping the resolution with the bitrate is what
353
+ * makes a small stream watchable rather than merely small.
354
+ */
355
+ export function widthFor(kbps) {
356
+ if (kbps <= 800)
357
+ return 640;
358
+ if (kbps <= 1800)
359
+ return 854;
360
+ if (kbps <= 4000)
361
+ return 1280;
362
+ return 1920;
363
+ }
364
+ /** Arguments for a stream that has to fit through a link of a known size. */
365
+ function cappedArgs(kbps) {
366
+ const audioKbps = kbps <= 800 ? 96 : 128;
367
+ const videoKbps = Math.max(200, kbps - audioKbps);
368
+ return [
369
+ "-c:v", "libx264",
370
+ "-preset", "veryfast",
371
+ "-pix_fmt", "yuv420p",
372
+ // -2 keeps the aspect ratio and an even height, which H.264 requires.
373
+ // The min() never enlarges: a 480p source asked for 720p stays 480p.
374
+ "-vf", `scale='min(${widthFor(kbps)},iw)':-2`,
375
+ "-b:v", `${videoKbps}k`,
376
+ // A ceiling rather than an average, because an average that spikes is a
377
+ // stall on a link this size. The buffer is one second of it.
378
+ "-maxrate", `${videoKbps}k`,
379
+ "-bufsize", `${videoKbps}k`,
380
+ "-c:a", "aac",
381
+ "-b:a", `${audioKbps}k`,
382
+ "-ac", "2",
383
+ "-f", "mp4",
384
+ "-movflags", "frag_keyframe+empty_moov+default_base_moof",
385
+ ];
386
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Asking somebody to watch, when that somebody is not technical.
3
+ *
4
+ * A share link is a URL with a key in it, which is fine for the person who
5
+ * runs the server and useless as a thing to text your mother. An invite is the
6
+ * three ways in, written as a sentence: a link that opens a player, a phone
7
+ * number, and the code to key once it answers.
8
+ *
9
+ * The sender is signed in, because sending is an action with a cost: a text
10
+ * message is money and somebody's phone. The recipient signs in too, but only
11
+ * once and only at the far end of a single click, because a stream can ask to
12
+ * be paid for -- x402 starts charging past five listeners -- and there is
13
+ * nobody to charge without an account. The dial-in path is the exception and
14
+ * stays open to anybody, since a phone call cannot sign in to anything.
15
+ */
16
+ /** Where the phone line answers, and what to key when it does. */
17
+ export interface Invite {
18
+ /** What the stream is called, as the recipient will see it. */
19
+ name: string;
20
+ /** A link that opens a player on this stream, listen only. */
21
+ link: string;
22
+ /** The phone number, when this stream is one the line knows about. */
23
+ phone: string;
24
+ /** The six digits that reach this stream, when it has been published. */
25
+ code: string;
26
+ }
27
+ /** Looks like a phone number rather than an address. */
28
+ export declare function isPhone(value: string): boolean;
29
+ /** Looks like somewhere an email could arrive. */
30
+ export declare function isEmail(value: string): boolean;
31
+ /**
32
+ * The message itself.
33
+ *
34
+ * Short, because it is going into a text message, and ordered by how likely
35
+ * each way in is to work for the person reading it. The link first: most
36
+ * people have a browser in their hand. The phone last, because it is the one
37
+ * that needs no browser at all and is therefore the fallback that never fails.
38
+ */
39
+ export declare function inviteText(invite: Invite): string;
40
+ /** The same thing as a subject line, for the surface that wants one. */
41
+ export declare function inviteSubject(invite: Invite): string;
42
+ /**
43
+ * A link that opens a player on this stream.
44
+ *
45
+ * Sent through nixamp.com when the stream is https, because that page is a
46
+ * player anybody can already open and reaches this stream with `?url=`. An
47
+ * http stream is sent as its own address instead: a browser refuses every
48
+ * request from an https page to an http one, so routing it through nixamp.com
49
+ * would produce a link that cannot work, which is worse than a plainer one
50
+ * that does.
51
+ */
52
+ export declare function watchLink(streamUrl: string, site: string): string;
package/dist/invite.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Asking somebody to watch, when that somebody is not technical.
3
+ *
4
+ * A share link is a URL with a key in it, which is fine for the person who
5
+ * runs the server and useless as a thing to text your mother. An invite is the
6
+ * three ways in, written as a sentence: a link that opens a player, a phone
7
+ * number, and the code to key once it answers.
8
+ *
9
+ * The sender is signed in, because sending is an action with a cost: a text
10
+ * message is money and somebody's phone. The recipient signs in too, but only
11
+ * once and only at the far end of a single click, because a stream can ask to
12
+ * be paid for -- x402 starts charging past five listeners -- and there is
13
+ * nobody to charge without an account. The dial-in path is the exception and
14
+ * stays open to anybody, since a phone call cannot sign in to anything.
15
+ */
16
+ /** Looks like a phone number rather than an address. */
17
+ export function isPhone(value) {
18
+ return /^\+?[\d\s().-]{7,20}$/.test(value.trim()) && /\d{7}/.test(value.replace(/\D/g, ""));
19
+ }
20
+ /** Looks like somewhere an email could arrive. */
21
+ export function isEmail(value) {
22
+ return /^[^@\s]+@[^@\s.]+\.[^@\s]+$/.test(value.trim());
23
+ }
24
+ /**
25
+ * The message itself.
26
+ *
27
+ * Short, because it is going into a text message, and ordered by how likely
28
+ * each way in is to work for the person reading it. The link first: most
29
+ * people have a browser in their hand. The phone last, because it is the one
30
+ * that needs no browser at all and is therefore the fallback that never fails.
31
+ */
32
+ export function inviteText(invite) {
33
+ const lines = [`${invite.name} is streaming.`, "", `Watch: ${invite.link}`];
34
+ if (invite.phone && invite.code) {
35
+ lines.push("", `Or call ${invite.phone} and key ${invite.code} to listen.`);
36
+ }
37
+ return lines.join("\n");
38
+ }
39
+ /** The same thing as a subject line, for the surface that wants one. */
40
+ export function inviteSubject(invite) {
41
+ return `${invite.name} is streaming`;
42
+ }
43
+ /**
44
+ * A link that opens a player on this stream.
45
+ *
46
+ * Sent through nixamp.com when the stream is https, because that page is a
47
+ * player anybody can already open and reaches this stream with `?url=`. An
48
+ * http stream is sent as its own address instead: a browser refuses every
49
+ * request from an https page to an http one, so routing it through nixamp.com
50
+ * would produce a link that cannot work, which is worse than a plainer one
51
+ * that does.
52
+ */
53
+ export function watchLink(streamUrl, site) {
54
+ const bare = streamUrl.replace(/\/+$/, "");
55
+ if (!bare.startsWith("https://"))
56
+ return bare;
57
+ return `${site.replace(/\/+$/, "")}/?url=${encodeURIComponent(bare)}`;
58
+ }
@@ -38,5 +38,7 @@ export declare function loadPlaylist(tools: Tools, root: string, probeTags?: boo
38
38
  * ffprobe rather than for the whole library, and the tagging still finishes in
39
39
  * about the time it did.
40
40
  */
41
- export declare function loadTagged(tools: Tools, source: string): Promise<Track[]>;
41
+ export declare function loadTagged(tools: Tools, source: string,
42
+ /** Injected by the test, which must not depend on ffprobe being installed. */
43
+ probeOne?: (tools: Tools, path: string) => Promise<Track>): Promise<Track[]>;
42
44
  export declare function displayName(track: Track): string;
package/dist/playlist.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /** The playlist: audio found on disk or named by a playlist, in a stable order. */
2
2
  import { readFileSync, readdirSync, statSync } from "node:fs";
3
3
  import { join } from "node:path";
4
- import { probe } from "./audio.js";
4
+ import { probe, probeAsync } from "./audio.js";
5
5
  import { isHls, isPlaylistFile, isRemote, nameOf, parseM3u, parsePls, } from "./sources.js";
6
6
  export const AUDIO_EXTENSIONS = new Set([
7
7
  ".mp3", ".flac", ".ogg", ".oga", ".opus", ".m4a", ".aac",
@@ -135,15 +135,20 @@ export function loadPlaylist(tools, root, probeTags = true) {
135
135
  * ffprobe rather than for the whole library, and the tagging still finishes in
136
136
  * about the time it did.
137
137
  */
138
- export async function loadTagged(tools, source) {
138
+ export async function loadTagged(tools, source,
139
+ /** Injected by the test, which must not depend on ffprobe being installed. */
140
+ probeOne = probeAsync) {
139
141
  // A URL is one thing and is never probed; a playlist carries its own titles.
140
142
  if (isRemote(source) || isPlaylistFile(source))
141
143
  return loadSource(tools, source, true);
142
144
  const paths = findAudio(source);
143
145
  const tracks = [];
144
146
  for (const path of paths) {
145
- tracks.push(probe(tools, path));
146
- await new Promise((done) => setImmediate(done));
147
+ // Awaiting a child process, not blocking on one. Yielding between files
148
+ // was not enough: each spawnSync still stopped everything for as long as
149
+ // one ffprobe took, which on a large file is long enough to strangle a
150
+ // stream being served at the same time.
151
+ tracks.push(await probeOne(tools, path));
147
152
  }
148
153
  return tracks;
149
154
  }
package/dist/server.js CHANGED
@@ -1655,7 +1655,12 @@ export function createHandler(engine, options) {
1655
1655
  // to it raw is bytes it cannot play. Seeking is what this route is for
1656
1656
  // and transcoding gives it up, but an unseekable film beats a silent
1657
1657
  // one -- and the seekable formats are untouched.
1658
- if (playsInBrowser(file)) {
1658
+ // A ceiling the caller asked for, because only the caller knows what its
1659
+ // link can carry. Capped at both ends: nothing below 200k is watchable,
1660
+ // and above 20 megabits the original was always the better answer.
1661
+ const asked = Number(url.searchParams.get("kbps") ?? "");
1662
+ const capKbps = Number.isFinite(asked) && asked > 0 ? Math.min(20_000, Math.max(200, asked)) : 0;
1663
+ if (playsInBrowser(file) && capKbps === 0) {
1659
1664
  sendFile(request, response, file);
1660
1665
  }
1661
1666
  else if (hasPicture(file)) {
@@ -1663,7 +1668,7 @@ export function createHandler(engine, options) {
1663
1668
  // soundtrack over a blank panel; what ffprobe finds inside decides how
1664
1669
  // little work it takes to keep the picture.
1665
1670
  const codecs = await codecsOf({ ffmpeg: [], ffprobe: options.ffprobe ?? ["ffprobe"], play: null }, file);
1666
- pipeFfmpeg(request, response, file, options.ffmpeg ?? ["ffmpeg"], videoArgs(codecs), "video/mp4");
1671
+ pipeFfmpeg(request, response, file, options.ffmpeg ?? ["ffmpeg"], videoArgs(codecs, capKbps), "video/mp4");
1667
1672
  }
1668
1673
  else {
1669
1674
  transcode(request, response, file, options.ffmpeg ?? ["ffmpeg"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nixamp",
3
- "version": "0.6.1",
3
+ "version": "0.6.4",
4
4
  "description": "It really whips the terminal's ass. A Winamp-shaped audio player for your terminal.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/audio.ts CHANGED
@@ -232,6 +232,82 @@ export function formatTime(seconds: number): string {
232
232
  return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
233
233
  }
234
234
 
235
+ /**
236
+ * The same tags, read without blocking anything.
237
+ *
238
+ * `probe` is spawnSync, and 0.5.5 tried to fix the tagging pass by yielding
239
+ * between files. That is not enough: each individual call still stops the
240
+ * process for as long as one ffprobe takes, and on a large file over a slow
241
+ * disk that is hundreds of milliseconds. Yield, block, yield, block, and a
242
+ * server delivers a stream in slivers -- measured at 357 KB/s on a machine
243
+ * whose disk reads at 6.5 MB/s and whose link runs at 1.4 Gbps.
244
+ *
245
+ * ffprobe still costs what it costs. It just costs it in a child process now,
246
+ * which is where that work belongs.
247
+ */
248
+ export async function probeAsync(tools: Tools, path: string): Promise<Track> {
249
+ const [cmd, ...rest] = tools.ffprobe;
250
+ const fallback: Track = {
251
+ path,
252
+ title: path.split("/").pop() ?? path,
253
+ artist: "",
254
+ album: "",
255
+ duration: 0,
256
+ };
257
+ if (!cmd) return fallback;
258
+
259
+ return new Promise<Track>((done) => {
260
+ const child = spawn(
261
+ cmd,
262
+ [
263
+ ...rest,
264
+ "-v", "quiet", "-print_format", "json",
265
+ "-show_format", "-show_entries", "format_tags=title,artist,album",
266
+ path,
267
+ ],
268
+ { stdio: ["ignore", "pipe", "ignore"] },
269
+ );
270
+ let out = "";
271
+ // A file that will not answer must not hold a place in the queue for ever.
272
+ const giveUp = setTimeout(() => child.kill("SIGKILL"), 20_000);
273
+ giveUp.unref?.();
274
+
275
+ child.stdout.on("data", (chunk: Buffer) => {
276
+ if (out.length < 4 * 1024 * 1024) out += chunk.toString("utf8");
277
+ });
278
+ child.on("error", () => {
279
+ clearTimeout(giveUp);
280
+ done(fallback);
281
+ });
282
+ child.on("close", (code) => {
283
+ clearTimeout(giveUp);
284
+ if (code !== 0) return done(fallback);
285
+ done(readTags(out, fallback));
286
+ });
287
+ });
288
+ }
289
+
290
+ /** The tags out of ffprobe's JSON, or the filename when it said nothing useful. */
291
+ function readTags(stdout: string, fallback: Track): Track {
292
+ try {
293
+ const parsed = JSON.parse(stdout) as {
294
+ format?: { duration?: string; tags?: Record<string, string> };
295
+ };
296
+ const tags = parsed.format?.tags ?? {};
297
+ const lower: Record<string, string> = {};
298
+ for (const [k, v] of Object.entries(tags)) lower[k.toLowerCase()] = v;
299
+ return {
300
+ path: fallback.path,
301
+ title: lower.title || fallback.title,
302
+ artist: lower.artist ?? "",
303
+ album: lower.album ?? "",
304
+ duration: Number(parsed.format?.duration ?? 0) || 0,
305
+ };
306
+ } catch {
307
+ return fallback;
308
+ }
309
+ }
310
+
235
311
  /** What is actually inside a container, as opposed to what the name suggests. */
236
312
  export interface Codecs {
237
313
  /** e.g. "h264", "hevc", "vp9". Empty when there is no video stream. */
@@ -293,7 +369,11 @@ export async function codecsOf(tools: Tools, path: string): Promise<Codecs> {
293
369
  * would cost a core per viewer and look worse. So the streams decide, one part
294
370
  * at a time -- a film can have its video copied and only its DTS re-encoded.
295
371
  */
296
- export function videoArgs(codecs: Codecs): string[] {
372
+ export function videoArgs(codecs: Codecs, capKbps = 0): string[] {
373
+ // A ceiling means re-encoding whatever is there, because you cannot cap the
374
+ // bitrate of a stream you are copying: copying is what "unchanged" means.
375
+ if (capKbps > 0) return cappedArgs(capKbps);
376
+
297
377
  // What a browser can play inside MP4 without help.
298
378
  const keepVideo = codecs.video === "h264";
299
379
  const keepAudio = codecs.audio === "aac" || codecs.audio === "mp3";
@@ -309,3 +389,42 @@ export function videoArgs(codecs: Codecs): string[] {
309
389
  "-movflags", "frag_keyframe+empty_moov+default_base_moof",
310
390
  ];
311
391
  }
392
+
393
+ /**
394
+ * The width that suits a bitrate.
395
+ *
396
+ * 1080p squeezed into a megabit is worse than 360p at the same megabit: the
397
+ * encoder spends everything it has on detail it cannot afford and the result
398
+ * smears on every motion. Dropping the resolution with the bitrate is what
399
+ * makes a small stream watchable rather than merely small.
400
+ */
401
+ export function widthFor(kbps: number): number {
402
+ if (kbps <= 800) return 640;
403
+ if (kbps <= 1800) return 854;
404
+ if (kbps <= 4000) return 1280;
405
+ return 1920;
406
+ }
407
+
408
+ /** Arguments for a stream that has to fit through a link of a known size. */
409
+ function cappedArgs(kbps: number): string[] {
410
+ const audioKbps = kbps <= 800 ? 96 : 128;
411
+ const videoKbps = Math.max(200, kbps - audioKbps);
412
+ return [
413
+ "-c:v", "libx264",
414
+ "-preset", "veryfast",
415
+ "-pix_fmt", "yuv420p",
416
+ // -2 keeps the aspect ratio and an even height, which H.264 requires.
417
+ // The min() never enlarges: a 480p source asked for 720p stays 480p.
418
+ "-vf", `scale='min(${widthFor(kbps)},iw)':-2`,
419
+ "-b:v", `${videoKbps}k`,
420
+ // A ceiling rather than an average, because an average that spikes is a
421
+ // stall on a link this size. The buffer is one second of it.
422
+ "-maxrate", `${videoKbps}k`,
423
+ "-bufsize", `${videoKbps}k`,
424
+ "-c:a", "aac",
425
+ "-b:a", `${audioKbps}k`,
426
+ "-ac", "2",
427
+ "-f", "mp4",
428
+ "-movflags", "frag_keyframe+empty_moov+default_base_moof",
429
+ ];
430
+ }
package/src/invite.ts ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Asking somebody to watch, when that somebody is not technical.
3
+ *
4
+ * A share link is a URL with a key in it, which is fine for the person who
5
+ * runs the server and useless as a thing to text your mother. An invite is the
6
+ * three ways in, written as a sentence: a link that opens a player, a phone
7
+ * number, and the code to key once it answers.
8
+ *
9
+ * The sender is signed in, because sending is an action with a cost: a text
10
+ * message is money and somebody's phone. The recipient signs in too, but only
11
+ * once and only at the far end of a single click, because a stream can ask to
12
+ * be paid for -- x402 starts charging past five listeners -- and there is
13
+ * nobody to charge without an account. The dial-in path is the exception and
14
+ * stays open to anybody, since a phone call cannot sign in to anything.
15
+ */
16
+
17
+ /** Where the phone line answers, and what to key when it does. */
18
+ export interface Invite {
19
+ /** What the stream is called, as the recipient will see it. */
20
+ name: string;
21
+ /** A link that opens a player on this stream, listen only. */
22
+ link: string;
23
+ /** The phone number, when this stream is one the line knows about. */
24
+ phone: string;
25
+ /** The six digits that reach this stream, when it has been published. */
26
+ code: string;
27
+ }
28
+
29
+ /** Looks like a phone number rather than an address. */
30
+ export function isPhone(value: string): boolean {
31
+ return /^\+?[\d\s().-]{7,20}$/.test(value.trim()) && /\d{7}/.test(value.replace(/\D/g, ""));
32
+ }
33
+
34
+ /** Looks like somewhere an email could arrive. */
35
+ export function isEmail(value: string): boolean {
36
+ return /^[^@\s]+@[^@\s.]+\.[^@\s]+$/.test(value.trim());
37
+ }
38
+
39
+ /**
40
+ * The message itself.
41
+ *
42
+ * Short, because it is going into a text message, and ordered by how likely
43
+ * each way in is to work for the person reading it. The link first: most
44
+ * people have a browser in their hand. The phone last, because it is the one
45
+ * that needs no browser at all and is therefore the fallback that never fails.
46
+ */
47
+ export function inviteText(invite: Invite): string {
48
+ const lines = [`${invite.name} is streaming.`, "", `Watch: ${invite.link}`];
49
+ if (invite.phone && invite.code) {
50
+ lines.push("", `Or call ${invite.phone} and key ${invite.code} to listen.`);
51
+ }
52
+ return lines.join("\n");
53
+ }
54
+
55
+ /** The same thing as a subject line, for the surface that wants one. */
56
+ export function inviteSubject(invite: Invite): string {
57
+ return `${invite.name} is streaming`;
58
+ }
59
+
60
+ /**
61
+ * A link that opens a player on this stream.
62
+ *
63
+ * Sent through nixamp.com when the stream is https, because that page is a
64
+ * player anybody can already open and reaches this stream with `?url=`. An
65
+ * http stream is sent as its own address instead: a browser refuses every
66
+ * request from an https page to an http one, so routing it through nixamp.com
67
+ * would produce a link that cannot work, which is worse than a plainer one
68
+ * that does.
69
+ */
70
+ export function watchLink(streamUrl: string, site: string): string {
71
+ const bare = streamUrl.replace(/\/+$/, "");
72
+ if (!bare.startsWith("https://")) return bare;
73
+ return `${site.replace(/\/+$/, "")}/?url=${encodeURIComponent(bare)}`;
74
+ }
package/src/playlist.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** The playlist: audio found on disk or named by a playlist, in a stable order. */
2
2
  import { readFileSync, readdirSync, statSync } from "node:fs";
3
3
  import { join } from "node:path";
4
- import { probe, type Tools, type Track } from "./audio.ts";
4
+ import { probe, probeAsync, type Tools, type Track } from "./audio.ts";
5
5
  import {
6
6
  type Entry,
7
7
  isHls,
@@ -146,15 +146,23 @@ export function loadPlaylist(tools: Tools, root: string, probeTags = true): Trac
146
146
  * ffprobe rather than for the whole library, and the tagging still finishes in
147
147
  * about the time it did.
148
148
  */
149
- export async function loadTagged(tools: Tools, source: string): Promise<Track[]> {
149
+ export async function loadTagged(
150
+ tools: Tools,
151
+ source: string,
152
+ /** Injected by the test, which must not depend on ffprobe being installed. */
153
+ probeOne: (tools: Tools, path: string) => Promise<Track> = probeAsync,
154
+ ): Promise<Track[]> {
150
155
  // A URL is one thing and is never probed; a playlist carries its own titles.
151
156
  if (isRemote(source) || isPlaylistFile(source)) return loadSource(tools, source, true);
152
157
 
153
158
  const paths = findAudio(source);
154
159
  const tracks: Track[] = [];
155
160
  for (const path of paths) {
156
- tracks.push(probe(tools, path));
157
- await new Promise<void>((done) => setImmediate(done));
161
+ // Awaiting a child process, not blocking on one. Yielding between files
162
+ // was not enough: each spawnSync still stopped everything for as long as
163
+ // one ffprobe took, which on a large file is long enough to strangle a
164
+ // stream being served at the same time.
165
+ tracks.push(await probeOne(tools, path));
158
166
  }
159
167
  return tracks;
160
168
  }
package/src/server.ts CHANGED
@@ -1954,14 +1954,20 @@ export function createHandler(engine: Engine, options: HandlerOptions) {
1954
1954
  // to it raw is bytes it cannot play. Seeking is what this route is for
1955
1955
  // and transcoding gives it up, but an unseekable film beats a silent
1956
1956
  // one -- and the seekable formats are untouched.
1957
- if (playsInBrowser(file)) {
1957
+ // A ceiling the caller asked for, because only the caller knows what its
1958
+ // link can carry. Capped at both ends: nothing below 200k is watchable,
1959
+ // and above 20 megabits the original was always the better answer.
1960
+ const asked = Number(url.searchParams.get("kbps") ?? "");
1961
+ const capKbps = Number.isFinite(asked) && asked > 0 ? Math.min(20_000, Math.max(200, asked)) : 0;
1962
+
1963
+ if (playsInBrowser(file) && capKbps === 0) {
1958
1964
  sendFile(request, response, file);
1959
1965
  } else if (hasPicture(file)) {
1960
1966
  // A film. It used to arrive as MP3 with `-vn`, which is to say as a
1961
1967
  // soundtrack over a blank panel; what ffprobe finds inside decides how
1962
1968
  // little work it takes to keep the picture.
1963
1969
  const codecs = await codecsOf({ ffmpeg: [], ffprobe: options.ffprobe ?? ["ffprobe"], play: null }, file);
1964
- pipeFfmpeg(request, response, file, options.ffmpeg ?? ["ffmpeg"], videoArgs(codecs), "video/mp4");
1970
+ pipeFfmpeg(request, response, file, options.ffmpeg ?? ["ffmpeg"], videoArgs(codecs, capKbps), "video/mp4");
1965
1971
  } else {
1966
1972
  transcode(request, response, file, options.ffmpeg ?? ["ffmpeg"]);
1967
1973
  }
@@ -1 +1 @@
1
- import{t as e}from"./index-PgqcCBW4.js";var t=3;async function n(n){let{media:r,src:i,isTv:a}=n,{default:o}=await e(async()=>{let{default:e}=await import(`./hls-n74Cnh8A.js`);return{default:e}},[]);if(!o.isSupported())return n.onError(`This browser cannot play HLS streams.`),{destroy:()=>void 0,levels:()=>[]};let s=new o({...a?{maxBufferLength:60,maxMaxBufferLength:120,backBufferLength:30,liveSyncDurationCount:4}:{backBufferLength:90},enableWorker:!0}),c=0,l=!1;s.on(o.Events.ERROR,(e,r)=>{if(!l&&r.fatal){if(c>=t){n.onError(`This stream kept failing and has been stopped.`),s.destroy();return}switch(c+=1,r.type){case o.ErrorTypes.NETWORK_ERROR:n.onNotice(`Reconnecting…`),s.startLoad();break;case o.ErrorTypes.MEDIA_ERROR:n.onNotice(`Recovering…`),s.recoverMediaError();break;default:n.onError(`This stream could not be played.`),s.destroy()}}}),s.on(o.Events.MANIFEST_PARSED,()=>{l||(n.onNotice(null),n.onReady?.({live:s.levels.length>0&&!Number.isFinite(r.duration),levels:u()}))}),s.on(o.Events.LEVEL_LOADED,(e,t)=>{l||n.onReady?.({live:t.details.live,levels:u()})}),s.on(o.Events.FRAG_BUFFERED,()=>{l||n.onNotice(null)});function u(){return s.levels.map((e,t)=>({index:t,height:e.height||null,bitrate:e.bitrate||null,label:e.height?`${String(e.height)}p`:`${String(Math.round((e.bitrate||0)/1e3))}k`}))}return s.loadSource(i),s.attachMedia(r),{destroy(){l=!0,s.destroy()},levels:u,setLevel(e){s.currentLevel=e},currentLevel:()=>s.autoLevelEnabled?-1:s.currentLevel}}export{n as createHlsEngine};
1
+ import{t as e}from"./index-0Asn6zxx.js";var t=3;async function n(n){let{media:r,src:i,isTv:a}=n,{default:o}=await e(async()=>{let{default:e}=await import(`./hls-n74Cnh8A.js`);return{default:e}},[]);if(!o.isSupported())return n.onError(`This browser cannot play HLS streams.`),{destroy:()=>void 0,levels:()=>[]};let s=new o({...a?{maxBufferLength:60,maxMaxBufferLength:120,backBufferLength:30,liveSyncDurationCount:4}:{backBufferLength:90},enableWorker:!0}),c=0,l=!1;s.on(o.Events.ERROR,(e,r)=>{if(!l&&r.fatal){if(c>=t){n.onError(`This stream kept failing and has been stopped.`),s.destroy();return}switch(c+=1,r.type){case o.ErrorTypes.NETWORK_ERROR:n.onNotice(`Reconnecting…`),s.startLoad();break;case o.ErrorTypes.MEDIA_ERROR:n.onNotice(`Recovering…`),s.recoverMediaError();break;default:n.onError(`This stream could not be played.`),s.destroy()}}}),s.on(o.Events.MANIFEST_PARSED,()=>{l||(n.onNotice(null),n.onReady?.({live:s.levels.length>0&&!Number.isFinite(r.duration),levels:u()}))}),s.on(o.Events.LEVEL_LOADED,(e,t)=>{l||n.onReady?.({live:t.details.live,levels:u()})}),s.on(o.Events.FRAG_BUFFERED,()=>{l||n.onNotice(null)});function u(){return s.levels.map((e,t)=>({index:t,height:e.height||null,bitrate:e.bitrate||null,label:e.height?`${String(e.height)}p`:`${String(Math.round((e.bitrate||0)/1e3))}k`}))}return s.loadSource(i),s.attachMedia(r),{destroy(){l=!0,s.destroy()},levels:u,setLevel(e){s.currentLevel=e},currentLevel:()=>s.autoLevelEnabled?-1:s.currentLevel}}export{n as createHlsEngine};