114 lines
2.7 KiB
TypeScript
114 lines
2.7 KiB
TypeScript
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"
|
|
import os from "node:os"
|
|
|
|
const STDERR_TAIL_BYTES = 4096
|
|
|
|
export type FfmpegProcess = {
|
|
child: ChildProcessWithoutNullStreams
|
|
/** Resolves with the exit code, or null when killed by a signal. */
|
|
exited: Promise<number | null>
|
|
stderrTail: () => string
|
|
}
|
|
|
|
type Limiter = {
|
|
running: number
|
|
waiting: (() => void)[]
|
|
}
|
|
|
|
// Kept on globalThis so dev reloads share one limit with processes they spawned.
|
|
const globalState = globalThis as typeof globalThis & {
|
|
__otunesFfmpegLimiter?: Limiter
|
|
}
|
|
const limiter = (globalState.__otunesFfmpegLimiter ??= {
|
|
running: 0,
|
|
waiting: [],
|
|
})
|
|
|
|
function maxConcurrentFfmpeg() {
|
|
const configured = Number(process.env.OTUNES_MAX_FFMPEG)
|
|
if (Number.isInteger(configured) && configured > 0) {
|
|
return configured
|
|
}
|
|
return Math.max(2, os.availableParallelism())
|
|
}
|
|
|
|
async function acquireSlot(signal?: AbortSignal) {
|
|
signal?.throwIfAborted()
|
|
if (limiter.running < maxConcurrentFfmpeg()) {
|
|
limiter.running += 1
|
|
return
|
|
}
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
const grant = () => {
|
|
signal?.removeEventListener("abort", onAbort)
|
|
resolve()
|
|
}
|
|
const onAbort = () => {
|
|
const index = limiter.waiting.indexOf(grant)
|
|
if (index !== -1) {
|
|
limiter.waiting.splice(index, 1)
|
|
}
|
|
reject(signal?.reason)
|
|
}
|
|
limiter.waiting.push(grant)
|
|
signal?.addEventListener("abort", onAbort, { once: true })
|
|
})
|
|
}
|
|
|
|
function releaseSlot() {
|
|
const next = limiter.waiting.shift()
|
|
if (next) {
|
|
// The slot passes straight to the next waiter.
|
|
next()
|
|
} else {
|
|
limiter.running -= 1
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Spawns ffmpeg once a slot under the concurrency cap is free. The slot is
|
|
* held until the process exits. Rejects if `signal` aborts while queued.
|
|
*/
|
|
export async function spawnFfmpeg(
|
|
args: string[],
|
|
signal?: AbortSignal
|
|
): Promise<FfmpegProcess> {
|
|
await acquireSlot(signal)
|
|
|
|
let child: ChildProcessWithoutNullStreams
|
|
try {
|
|
child = spawn("ffmpeg", [
|
|
"-nostdin",
|
|
"-hide_banner",
|
|
"-loglevel",
|
|
"error",
|
|
...args,
|
|
])
|
|
} catch (error) {
|
|
releaseSlot()
|
|
throw error
|
|
}
|
|
|
|
// stderr must be drained or ffmpeg blocks once the pipe buffer fills.
|
|
let stderr = ""
|
|
child.stderr.on("data", (data: Buffer) => {
|
|
stderr = (stderr + data.toString()).slice(-STDERR_TAIL_BYTES)
|
|
})
|
|
|
|
const exited = new Promise<number | null>((resolve) => {
|
|
let released = false
|
|
const finish = (code: number | null) => {
|
|
if (!released) {
|
|
released = true
|
|
releaseSlot()
|
|
}
|
|
resolve(code)
|
|
}
|
|
child.once("error", () => finish(1))
|
|
child.once("close", (code) => finish(code))
|
|
})
|
|
|
|
return { child, exited, stderrTail: () => stderr.trim() }
|
|
}
|