@torrent-tv/proxy 2.107.83 → 2.107.85
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/bin/cli.js +45 -1
- package/package.json +1 -1
- package/server.js +3 -2
- package/services/encode/EncodeRun.js +71 -49
- package/services/encode/EncodeRuns.js +5 -0
- package/services/encode/encoder-readings.js +3 -2
- package/services/media/ffprobe-selection.js +79 -0
- package/services/media/probe-packets.js +6 -14
package/bin/cli.js
CHANGED
|
@@ -16,11 +16,12 @@ import "./thread-pool.js";
|
|
|
16
16
|
import { Command } from "commander";
|
|
17
17
|
import { mkdirSync } from "node:fs";
|
|
18
18
|
import os from "node:os";
|
|
19
|
-
import { spawnSync } from "node:child_process";
|
|
19
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
20
20
|
import { createRequire } from "node:module";
|
|
21
21
|
import ffmpegStatic from "ffmpeg-static";
|
|
22
22
|
import nodeDataChannel from "node-datachannel";
|
|
23
23
|
import { startProxyServer } from "../server.js";
|
|
24
|
+
import { ffprobeCandidates, selectFfprobe } from "../services/media/ffprobe-selection.js";
|
|
24
25
|
import { registerClient } from "../services/transport/registry-api.js";
|
|
25
26
|
import { createTunnelClient } from "../services/transport/tunnel-client.js";
|
|
26
27
|
import { createWebRtcManager } from "../services/transport/webrtc-manager.js";
|
|
@@ -190,6 +191,44 @@ const memoryBytes =
|
|
|
190
191
|
const bundledFfmpegBin = typeof ffmpegStatic === "string" ? ffmpegStatic : "";
|
|
191
192
|
const ffmpegBin = options.ffmpegBin ? String(options.ffmpegBin) : bundledFfmpegBin || "ffmpeg";
|
|
192
193
|
|
|
194
|
+
/**
|
|
195
|
+
* The path of the ffprobe binary bundled by `@ffprobe-installer/ffprobe`, or null when
|
|
196
|
+
* the package cannot be loaded on this platform.
|
|
197
|
+
*
|
|
198
|
+
* @returns {string | null}
|
|
199
|
+
*/
|
|
200
|
+
function bundledFfprobeBin() {
|
|
201
|
+
try {
|
|
202
|
+
const { path } = require("@ffprobe-installer/ffprobe");
|
|
203
|
+
return typeof path === "string" && path !== "" ? path : null;
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// How long one run of a candidate ffprobe (`-version`, `-sections`) may take before it is
|
|
210
|
+
// killed and the candidate does not qualify. CHOSEN, not measured: both print at once.
|
|
211
|
+
const FFPROBE_RUN_LIMIT_MS = 10_000;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Run `binary args` and resolve what it printed; reject when it cannot be started.
|
|
215
|
+
*
|
|
216
|
+
* @param {string} binary
|
|
217
|
+
* @param {string[]} args
|
|
218
|
+
* @returns {Promise<{ status: number | null, stdout: string, stderr: string }>}
|
|
219
|
+
*/
|
|
220
|
+
function runProcess(binary, args) {
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
const child = spawn(binary, args, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true, timeout: FFPROBE_RUN_LIMIT_MS });
|
|
223
|
+
let stdout = "";
|
|
224
|
+
let stderr = "";
|
|
225
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => { stdout += chunk; });
|
|
226
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => { stderr += chunk; });
|
|
227
|
+
child.once("error", reject);
|
|
228
|
+
child.once("close", (status) => resolve({ status, stdout, stderr }));
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
193
232
|
/**
|
|
194
233
|
* Verify that the ffmpeg binary is reachable and exits cleanly.
|
|
195
234
|
* Throws with a descriptive message when the check fails.
|
|
@@ -398,11 +437,16 @@ try {
|
|
|
398
437
|
if (transcodeAudio) {
|
|
399
438
|
assertFfmpegAvailability();
|
|
400
439
|
}
|
|
440
|
+
// Chosen once, here, by what each candidate can do; a host with none that qualifies
|
|
441
|
+
// stops with FfprobeUnavailableError (one logged line) instead of refusing every probe.
|
|
442
|
+
const ffprobe = await selectFfprobe({ candidates: ffprobeCandidates({ bundledBinary: bundledFfprobeBin() }), run: runProcess, logger });
|
|
443
|
+
logger.info(`ffprobe: using ${ffprobe.source} ${ffprobe.binary} (version ${ffprobe.version}), which names section stream_side_data`);
|
|
401
444
|
const started = await startProxyServer({
|
|
402
445
|
host: bindHost,
|
|
403
446
|
port: localPort,
|
|
404
447
|
transcodeAudio,
|
|
405
448
|
ffmpegBin,
|
|
449
|
+
ffprobeBin: ffprobe.binary,
|
|
406
450
|
memoryBytes,
|
|
407
451
|
segmentFormat: options.segmentFormat,
|
|
408
452
|
stateDir,
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -110,6 +110,7 @@ function buildPortCandidates(startPort, maxAttempts = 51) {
|
|
|
110
110
|
* @property {number} port - Preferred listen port.
|
|
111
111
|
* @property {boolean} transcodeAudio - Whether HLS audio transcoding is enabled.
|
|
112
112
|
* @property {string} ffmpegBin - Path to the ffmpeg executable.
|
|
113
|
+
* @property {string} ffprobeBin - Path to the ffprobe executable chosen at startup (`services/media/ffprobe-selection.js`).
|
|
113
114
|
* @property {number} [memoryBytes] - Per-torrent budget for pieces held in memory (undefined = store default).
|
|
114
115
|
* @property {string} [segmentFormat] - HLS output container: "fmp4" (default) or "mpegts".
|
|
115
116
|
* @property {string} [stateDir] - Where to keep what this host has measured about itself.
|
|
@@ -122,7 +123,7 @@ function buildPortCandidates(startPort, maxAttempts = 51) {
|
|
|
122
123
|
* @returns {Promise<{ app: import("fastify").FastifyInstance, port: number }>}
|
|
123
124
|
*/
|
|
124
125
|
export async function startProxyServer({
|
|
125
|
-
host, port, transcodeAudio, ffmpegBin, memoryBytes, segmentFormat, stateDir, onSubtitleCues, onChaptersChanged, diagnostics, budgetPolicy,
|
|
126
|
+
host, port, transcodeAudio, ffmpegBin, ffprobeBin, memoryBytes, segmentFormat, stateDir, onSubtitleCues, onChaptersChanged, diagnostics, budgetPolicy,
|
|
126
127
|
deliverySink = false,
|
|
127
128
|
// Whether a memory high-water earns a heap snapshot (`--heap-snapshots`).
|
|
128
129
|
heapSnapshots = false,
|
|
@@ -324,7 +325,7 @@ export async function startProxyServer({
|
|
|
324
325
|
},
|
|
325
326
|
packetMemory: (sourceKey, fileIndex) => indexMemory.forFile(sourceKey, fileIndex),
|
|
326
327
|
readProbe: request => probeReads.read(request),
|
|
327
|
-
probePackets,
|
|
328
|
+
probePackets: request => probePackets({ ...request, ffprobeBin }),
|
|
328
329
|
baseUrl: () => localBaseUrl,
|
|
329
330
|
observeWait: (sourceKey, event, fields) => waitTrace.source(sourceKey, event, fields),
|
|
330
331
|
reads: { revision: (...a) => mediaReads.revision(...a), memoryRevision: () => mediaReads.memoryRevision(),
|
|
@@ -127,6 +127,14 @@ export class EncodeRun {
|
|
|
127
127
|
#closedTimings = new Map();
|
|
128
128
|
/** When the encoder named each closed piece on its channel, until it is published. */
|
|
129
129
|
#namedAt = new Map();
|
|
130
|
+
/**
|
|
131
|
+
* What the run's own clock said when the encoder named each closed piece, until
|
|
132
|
+
* it is published: the moment, the run's own working time and the monotonic
|
|
133
|
+
* time. A piece's speed reading is taken from this, not from when it is
|
|
134
|
+
* published (torrent-tv/meta#414).
|
|
135
|
+
* @type {Map<string, { at: number, workingMs: number, monotonicAt: number }>}
|
|
136
|
+
*/
|
|
137
|
+
#closedAt = new Map();
|
|
130
138
|
#publicationError = null;
|
|
131
139
|
#publicationCode = null;
|
|
132
140
|
#inputTruncated = false;
|
|
@@ -186,14 +194,16 @@ export class EncodeRun {
|
|
|
186
194
|
#stoppedBeforeFirstOutput = 0;
|
|
187
195
|
#stoppedBeforeFirstNamed = 0;
|
|
188
196
|
|
|
189
|
-
/**
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Where the run's last speed reading ended: the film end of its piece on the
|
|
199
|
+
* output's timeline (the run's own start before any), the run's working time
|
|
200
|
+
* when that piece was named closed, and the monotonic time of that moment.
|
|
201
|
+
* The next reading is measured from here.
|
|
202
|
+
*/
|
|
203
|
+
#lastReadingEnd = null;
|
|
194
204
|
|
|
195
|
-
/**
|
|
196
|
-
#
|
|
205
|
+
/** When the run was built, which the idle time of the first reading is counted from. */
|
|
206
|
+
#builtAt = 0;
|
|
197
207
|
|
|
198
208
|
/** @type {{ speed: number, at: number } | null} */
|
|
199
209
|
#speedReading = null;
|
|
@@ -268,6 +278,10 @@ export class EncodeRun {
|
|
|
268
278
|
* that writes it, so it arrives as a plain function rather than this class
|
|
269
279
|
* knowing any naming.
|
|
270
280
|
* @param {number} [params.startSeconds] - Where this run begins on the source timeline.
|
|
281
|
+
* @param {(index: number) => number | null} [params.pieceEndSeconds] - Where
|
|
282
|
+
* piece `index` ends on the same timeline `startSeconds` is taken from; its
|
|
283
|
+
* owner knows the timeline. A speed reading is the film between two such
|
|
284
|
+
* ends. Not finite: the piece gives no reading.
|
|
271
285
|
* @param {number | null} [params.totalSeconds] - Source duration when known.
|
|
272
286
|
*/
|
|
273
287
|
constructor({
|
|
@@ -293,6 +307,7 @@ export class EncodeRun {
|
|
|
293
307
|
usesExplicitCuts = false,
|
|
294
308
|
indexOfName,
|
|
295
309
|
startSeconds = 0,
|
|
310
|
+
pieceEndSeconds,
|
|
296
311
|
totalSeconds = null,
|
|
297
312
|
monotonicNow,
|
|
298
313
|
because = "no reason was given"
|
|
@@ -310,7 +325,9 @@ export class EncodeRun {
|
|
|
310
325
|
this.progress = new RunProgress({ startSeconds, totalSeconds, now: this.now, monotonicNow: this.monotonicNow });
|
|
311
326
|
/** Where this run's time went: its own work, or waiting, or stopped. */
|
|
312
327
|
this.clock = new RunClock({ now: this.now });
|
|
313
|
-
this.#
|
|
328
|
+
this.#builtAt = this.now();
|
|
329
|
+
this.#lastReadingEnd = { filmEndSeconds: startSeconds, workingMs: 0, monotonicAt: this.monotonicNow() };
|
|
330
|
+
this.pieceEndSeconds = typeof pieceEndSeconds === "function" ? pieceEndSeconds : () => null;
|
|
314
331
|
this.onEnded = typeof onEnded === "function" ? onEnded : () => {};
|
|
315
332
|
this.onProgress = typeof onProgress === "function" ? onProgress : () => {};
|
|
316
333
|
this.onProduced = typeof onProduced === "function" ? onProduced : () => {};
|
|
@@ -461,17 +478,6 @@ export class EncodeRun {
|
|
|
461
478
|
/** An input read of this run has its bytes. */
|
|
462
479
|
inputWaitEnds() {
|
|
463
480
|
this.clock.inputWaitEnds();
|
|
464
|
-
if (this.#speedReading === null && this.#workSample !== null) {
|
|
465
|
-
// Progress made while an input read was waiting belongs to the wait, not
|
|
466
|
-
// to the encoder's rate. Start the first measurement from the latest
|
|
467
|
-
// progress known when the input resumes.
|
|
468
|
-
this.#pieceSample = {
|
|
469
|
-
...this.#workSample,
|
|
470
|
-
at: this.now(),
|
|
471
|
-
monotonicAt: this.monotonicNow(),
|
|
472
|
-
workingMs: this.clock.workingMs()
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
481
|
}
|
|
476
482
|
|
|
477
483
|
/** @returns {boolean} Whether an input read of this run is waiting for bytes now. */
|
|
@@ -601,17 +607,9 @@ export class EncodeRun {
|
|
|
601
607
|
}
|
|
602
608
|
|
|
603
609
|
#reportProgress(report) {
|
|
610
|
+
// Progress reports tell how far the encoder is; they do not enter the speed
|
|
611
|
+
// reading, which is taken from the pieces it closed (torrent-tv/meta#414).
|
|
604
612
|
this.progress.note(report);
|
|
605
|
-
if (Number.isFinite(report.processedSeconds) || typeof report.outTime === "string") {
|
|
606
|
-
const at = this.now();
|
|
607
|
-
this.#workSample = {
|
|
608
|
-
at,
|
|
609
|
-
monotonicAt: this.progress.updatedAtMonotonic,
|
|
610
|
-
producedSeconds: Math.max(0, this.progress.processedSeconds - this.progress.startPositionSeconds),
|
|
611
|
-
workingMs: this.clock.workingMs(at)
|
|
612
|
-
};
|
|
613
|
-
if (this.#speedMeasurementPending) this.#measureSpeed();
|
|
614
|
-
}
|
|
615
613
|
this.onProgress(this.progress.snapshot());
|
|
616
614
|
}
|
|
617
615
|
|
|
@@ -651,7 +649,9 @@ export class EncodeRun {
|
|
|
651
649
|
if (name.length === 0) {
|
|
652
650
|
continue;
|
|
653
651
|
}
|
|
654
|
-
|
|
652
|
+
const namedAt = this.now();
|
|
653
|
+
this.#namedAt.set(name, namedAt);
|
|
654
|
+
this.#closedAt.set(name, { at: namedAt, workingMs: this.clock.workingMs(namedAt), monotonicAt: this.monotonicNow() });
|
|
655
655
|
if (this.#publicationError) continue;
|
|
656
656
|
if (this.#stopping) {
|
|
657
657
|
// Flushed by our stop. Never published; it tells where the piece closed
|
|
@@ -704,6 +704,9 @@ export class EncodeRun {
|
|
|
704
704
|
}
|
|
705
705
|
|
|
706
706
|
#publishClosed(name, following = null, { quiet = false, tentative = false } = {}) {
|
|
707
|
+
// Read before the `finally` below deletes it: a reading is dated by the
|
|
708
|
+
// close of its piece, not by its publication.
|
|
709
|
+
const closedAt = this.#closedAt.get(name) ?? null;
|
|
707
710
|
// ITS SERVED NAME, which is what whoever owns the disk gives it in answer.
|
|
708
711
|
// ffmpeg writes a piece under a working name and reports that; the piece
|
|
709
712
|
// becomes servable by being renamed, and everything below works in the
|
|
@@ -735,12 +738,12 @@ export class EncodeRun {
|
|
|
735
738
|
if (!tentative || served) {
|
|
736
739
|
this.#closedTimings.delete(name);
|
|
737
740
|
this.#namedAt.delete(name);
|
|
741
|
+
this.#closedAt.delete(name);
|
|
738
742
|
}
|
|
739
743
|
}
|
|
740
744
|
|
|
741
745
|
if (!served) return false;
|
|
742
|
-
this.#
|
|
743
|
-
this.#measureSpeed();
|
|
746
|
+
this.#measureSpeed(this.indexOfName(served), closedAt);
|
|
744
747
|
if (!this.#stopping) {
|
|
745
748
|
this.#provenName = served;
|
|
746
749
|
}
|
|
@@ -760,27 +763,46 @@ export class EncodeRun {
|
|
|
760
763
|
}
|
|
761
764
|
|
|
762
765
|
/**
|
|
763
|
-
* A published piece
|
|
764
|
-
*
|
|
765
|
-
*
|
|
766
|
-
*
|
|
767
|
-
*
|
|
766
|
+
* A published piece gives a reading: the film between its end on the
|
|
767
|
+
* output's timeline and where the last reading ended (the run's start for
|
|
768
|
+
* the first), over the run's own working time between the moments the two
|
|
769
|
+
* pieces were named closed. Input waits and stops are not working time
|
|
770
|
+
* (`RunClock`); progress reports do not enter it. No reading when the end of
|
|
771
|
+
* the piece is unknown or no film or no working time lies between the two.
|
|
772
|
+
*
|
|
773
|
+
* @param {number | null} index - The published piece's number.
|
|
774
|
+
* @param {{ at: number, workingMs: number, monotonicAt: number } | null} closedAt - When it was named closed.
|
|
768
775
|
*/
|
|
769
|
-
#measureSpeed() {
|
|
770
|
-
|
|
771
|
-
|
|
776
|
+
#measureSpeed(index, closedAt) {
|
|
777
|
+
if (closedAt === null) {
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
const filmEndSeconds = Number(this.pieceEndSeconds(index));
|
|
781
|
+
if (!Number.isFinite(filmEndSeconds)) {
|
|
772
782
|
return;
|
|
773
783
|
}
|
|
774
|
-
const
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
784
|
+
const last = this.#lastReadingEnd;
|
|
785
|
+
const work = workDeltaBetween(
|
|
786
|
+
{ producedSeconds: last.filmEndSeconds, workingMs: last.workingMs },
|
|
787
|
+
{ producedSeconds: filmEndSeconds, workingMs: closedAt.workingMs });
|
|
788
|
+
if (work === null) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
const first = this.#speedReading === null;
|
|
792
|
+
this.#speedReading = { speed: work.workSeconds / work.workingSeconds, at: closedAt.at, ...work,
|
|
793
|
+
captureOwner: "proxy-encoder", captureClock: "performance", captureClockEpoch: performance.timeOrigin,
|
|
794
|
+
captureStartedAtMonotonic: last.monotonicAt,
|
|
795
|
+
captureCompletedAtMonotonic: closedAt.monotonicAt };
|
|
796
|
+
this.#lastReadingEnd = { filmEndSeconds, workingMs: closedAt.workingMs, monotonicAt: closedAt.monotonicAt };
|
|
797
|
+
if (first) {
|
|
798
|
+
const idleSeconds = Math.max(0, (closedAt.at - this.#builtAt) / 1000 - work.workingSeconds);
|
|
799
|
+
this.logger.info(
|
|
800
|
+
`encode-run #${this.from}..#${this.to} on ${this.address}: first speed reading ` +
|
|
801
|
+
`${this.#speedReading.speed.toFixed(2)}x — ${work.workSeconds.toFixed(3)}s of film to the close of #${index} ` +
|
|
802
|
+
`over ${work.workingSeconds.toFixed(3)}s of its own work, ${idleSeconds.toFixed(3)}s waiting for input or ` +
|
|
803
|
+
"stopped left out");
|
|
783
804
|
}
|
|
805
|
+
this.onSpeedMeasured(this.speedReading);
|
|
784
806
|
}
|
|
785
807
|
|
|
786
808
|
/**
|
|
@@ -1154,6 +1154,11 @@ export class EncodeRuns {
|
|
|
1154
1154
|
// judged finished — see getFileStream.
|
|
1155
1155
|
usesExplicitCuts: Boolean(admittedInput || (cutTimes && cutTimes.length > 0)),
|
|
1156
1156
|
startSeconds,
|
|
1157
|
+
// Where piece `index` ends on the timeline `startSeconds` is taken from:
|
|
1158
|
+
// the speed reading is the film between two such ends (torrent-tv/meta#414).
|
|
1159
|
+
pieceEndSeconds: (index) => Number.isInteger(index) && index >= 0 && index + 1 < session.timeline.published.length
|
|
1160
|
+
? session.timeline.publishedStartOf(index + 1)
|
|
1161
|
+
: Number(session.file.durationSeconds) || null,
|
|
1157
1162
|
totalSeconds: Number(session.file.durationSeconds) || null,
|
|
1158
1163
|
spawn: (spawnArgs) =>
|
|
1159
1164
|
spawn(this.#host.ffmpegBin, spawnArgs, {
|
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
* A run's work sample states the film made and the milliseconds of the run's
|
|
12
12
|
* OWN work, with input waits and stops taken out (`RunClock`). Two samples of
|
|
13
13
|
* one run give its processing speed over the stretch between them; the run
|
|
14
|
-
* takes them
|
|
15
|
-
*
|
|
14
|
+
* takes them at the moments it names pieces closed, with the end of each piece
|
|
15
|
+
* on the output's timeline as its film (`EncodeRun.speedReading`), so there is
|
|
16
|
+
* no window to choose and no progress report enters.
|
|
16
17
|
*/
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Which ffprobe binary the proxy probes with is decided once, at startup,
|
|
3
|
+
* by what the binary can do: its `-sections` output names the section
|
|
4
|
+
* `stream_side_data`, which the `streams` statement of `probe-packets.js` asks for.
|
|
5
|
+
* The bundled build of darwin-arm64 (4.4.1) has `stream_side_data_list` and no
|
|
6
|
+
* `stream_side_data`, and refuses every probe; a version number would need a table
|
|
7
|
+
* of releases, the section list is the fact.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The section the `streams` statement asks for. */
|
|
11
|
+
const REQUIRED_SECTION = "stream_side_data";
|
|
12
|
+
|
|
13
|
+
/** Thrown when no candidate qualifies; its message is the one line `selectFfprobe` logged. */
|
|
14
|
+
export class FfprobeUnavailableError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "FfprobeUnavailableError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The candidates in order of preference: the bundled binary when its path is
|
|
23
|
+
* known, then `ffprobe` found on PATH.
|
|
24
|
+
*
|
|
25
|
+
* @param {{ bundledBinary?: string | null }} options
|
|
26
|
+
* @returns {{ source: "bundled" | "PATH", binary: string }[]}
|
|
27
|
+
*/
|
|
28
|
+
export function ffprobeCandidates({ bundledBinary }) {
|
|
29
|
+
const candidates = [];
|
|
30
|
+
if (typeof bundledBinary === "string" && bundledBinary !== "") candidates.push({ source: "bundled", binary: bundledBinary });
|
|
31
|
+
candidates.push({ source: "PATH", binary: "ffprobe" });
|
|
32
|
+
return candidates;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The word after `ffprobe version` on the first line, or "unknown". */
|
|
36
|
+
function versionOf(output) {
|
|
37
|
+
return /^ffprobe version (\S+)/.exec(String(output).split("\n", 1)[0])?.[1] ?? "unknown";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** True when a name in the `-sections` print, taken apart at whitespace and `/`, equals the section. */
|
|
41
|
+
function namesSection(output) {
|
|
42
|
+
return String(output).split(/[\s/]+/).includes(REQUIRED_SECTION);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Ask each candidate, in order, its version and its sections, each at most once, and
|
|
47
|
+
* choose the first that exits 0 from `-sections` and names `stream_side_data`. A later
|
|
48
|
+
* candidate is not run once one is chosen.
|
|
49
|
+
*
|
|
50
|
+
* @param {object} options
|
|
51
|
+
* @param {{ source: string, binary: string }[]} options.candidates
|
|
52
|
+
* @param {(binary: string, args: string[]) => Promise<{ status: number | null, stdout: string, stderr: string }>} options.run
|
|
53
|
+
* Rejects when the process cannot be started.
|
|
54
|
+
* @param {{ error: (line: string) => void }} options.logger
|
|
55
|
+
* @returns {Promise<{ source: string, binary: string, version: string }>}
|
|
56
|
+
* @throws {FfprobeUnavailableError} After one `error` line, when no candidate qualifies.
|
|
57
|
+
*/
|
|
58
|
+
export async function selectFfprobe({ candidates, run, logger }) {
|
|
59
|
+
const entries = [];
|
|
60
|
+
for (const { source, binary } of candidates) {
|
|
61
|
+
let version;
|
|
62
|
+
let sections;
|
|
63
|
+
try {
|
|
64
|
+
const versionRun = await run(binary, ["-version"]);
|
|
65
|
+
version = versionRun.status === 0 ? versionOf(versionRun.stdout) : "unknown";
|
|
66
|
+
sections = await run(binary, ["-hide_banner", "-sections"]);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
69
|
+
entries.push(`${source} ${binary} cannot be run (${reason}), so ${REQUIRED_SECTION} was not checked`);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (sections.status === 0 && namesSection(sections.stdout)) return { source, binary, version };
|
|
73
|
+
const exit = sections.status === 0 ? "" : `, -sections exited ${sections.status}`;
|
|
74
|
+
entries.push(`${source} ${binary} (version ${version}${exit}) lacks section ${REQUIRED_SECTION}`);
|
|
75
|
+
}
|
|
76
|
+
const line = `ffprobe: no usable binary: ${entries.length ? entries.join("; ") : "no candidate to try"}`;
|
|
77
|
+
logger.error(line);
|
|
78
|
+
throw new FfprobeUnavailableError(line);
|
|
79
|
+
}
|
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { accessSync, constants } from "node:fs";
|
|
4
2
|
import { ffprobeRecord } from "./container/ffprobe-record.js";
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
catch { return "ffprobe"; }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** Read only the strict available-byte endpoint; missing reads abort through ProbeRequests. */
|
|
17
|
-
export function probePackets({ url, statement, signal, onRecord = () => {}, ffprobeBin = bundledProbe(), spawnProcess = spawn }) {
|
|
4
|
+
/**
|
|
5
|
+
* Read only the strict available-byte endpoint; missing reads abort through ProbeRequests.
|
|
6
|
+
* `ffprobeBin` is the binary chosen once at startup (`ffprobe-selection.js`); this function never chooses one.
|
|
7
|
+
*/
|
|
8
|
+
export function probePackets({ url, statement, signal, onRecord = () => {}, ffprobeBin, spawnProcess = spawn }) {
|
|
9
|
+
if (typeof ffprobeBin !== "string" || ffprobeBin === "") throw new TypeError("probePackets needs the ffprobe binary chosen at startup.");
|
|
18
10
|
if (!["streams", "packets"].includes(statement)) throw new TypeError("Unknown ffprobe statement.");
|
|
19
11
|
if (signal?.aborted) return Promise.reject(signal.reason ?? new Error("Probe cancelled."));
|
|
20
12
|
onRecord({ kind: "scan-start" });
|