mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(engine): GPU renders use the NVENC preset names the host's ffmpeg lists (#6930)
* fix(engine): GPU renders use the NVENC preset names the host's ffmpeg lists * fix(engine): one ffmpeg stdout reader for encoder detection and preset listing * ci(engine): raise the engine unpacked size budget for the NVENC preset reader
This commit is contained in:
@@ -61,9 +61,14 @@ export async function selectUsableGpuEncoder(
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function detectGpuEncoder(): Promise<GpuEncoder> {
|
||||
const ffmpeg = spawn(getFfmpegBinary(), ["-encoders"], {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
/** ffmpeg's stdout, or null when it did not exit cleanly within the deadline. */
|
||||
async function ffmpegStdout(
|
||||
args: string[],
|
||||
deadlineMs: number,
|
||||
terminationGraceMs?: number,
|
||||
): Promise<string | null> {
|
||||
const ffmpeg = spawn(getFfmpegBinary(), args, {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
|
||||
windowsHide: true,
|
||||
});
|
||||
@@ -73,9 +78,15 @@ export async function detectGpuEncoder(): Promise<GpuEncoder> {
|
||||
stdout += data.toString();
|
||||
});
|
||||
const outcome = await new ManagedChildProcess(ffmpeg, {
|
||||
deadlineAtMs: Date.now() + 30_000,
|
||||
deadlineAtMs: Date.now() + deadlineMs,
|
||||
terminationGraceMs,
|
||||
}).wait();
|
||||
if (outcome.reason !== "exit" || outcome.exitCode !== 0) return null;
|
||||
return outcome.reason === "exit" && outcome.exitCode === 0 ? stdout : null;
|
||||
}
|
||||
|
||||
export async function detectGpuEncoder(): Promise<GpuEncoder> {
|
||||
const stdout = await ffmpegStdout(["-encoders"], 30_000);
|
||||
if (stdout === null) return null;
|
||||
const candidates = getCompiledGpuEncoders(stdout);
|
||||
return selectUsableGpuEncoder(candidates, canUseGpuEncoder).catch(() => null);
|
||||
}
|
||||
@@ -265,9 +276,15 @@ const QSV_PRESET_MAP: Record<string, string> = {
|
||||
* - `videotoolbox`, `vaapi`, `amf`, `null`: no remap (they either ignore
|
||||
* `-preset` entirely or accept the libx264 vocabulary).
|
||||
*/
|
||||
export function mapPresetForGpuEncoder(encoder: GpuEncoder, preset: string): string {
|
||||
export function mapPresetForGpuEncoder(
|
||||
encoder: GpuEncoder,
|
||||
preset: string,
|
||||
nvencPresets?: ReadonlySet<string>,
|
||||
): string {
|
||||
switch (encoder) {
|
||||
case "nvenc":
|
||||
if (nvencPresets && nvencPresets.size > 0 && !nvencPresets.has("p4"))
|
||||
return legacyNvencPreset(preset, nvencPresets);
|
||||
if (/^p[1-7]$/.test(preset)) return preset;
|
||||
return NVENC_PRESET_MAP[preset] ?? "p4";
|
||||
case "qsv":
|
||||
@@ -276,3 +293,55 @@ export function mapPresetForGpuEncoder(encoder: GpuEncoder, preset: string): str
|
||||
return preset;
|
||||
}
|
||||
}
|
||||
|
||||
// Builds on an older NVENC SDK list `default slow medium fast hp hq ...` and reject p1..p7.
|
||||
const LEGACY_NVENC_PRESET_MAP: Record<string, string> = {
|
||||
ultrafast: "hp",
|
||||
superfast: "hp",
|
||||
veryfast: "hp",
|
||||
faster: "fast",
|
||||
fast: "fast",
|
||||
medium: "medium",
|
||||
slow: "slow",
|
||||
slower: "slow",
|
||||
veryslow: "slow",
|
||||
placebo: "slow",
|
||||
};
|
||||
|
||||
function legacyNvencPreset(preset: string, accepted: ReadonlySet<string>): string {
|
||||
if (accepted.has(preset) && !/^p[1-7]$/.test(preset)) return preset;
|
||||
const mapped = LEGACY_NVENC_PRESET_MAP[preset] ?? "medium";
|
||||
return accepted.has(mapped) ? mapped : "default";
|
||||
}
|
||||
|
||||
/** The names an encoder lists under its `-preset` option in `ffmpeg -h encoder=<name>`. */
|
||||
export function parseEncoderPresetNames(helpText: string): ReadonlySet<string> {
|
||||
const names = new Set<string>();
|
||||
let inPreset = false;
|
||||
for (const line of helpText.split(/\r?\n/)) {
|
||||
if (/^\s{1,3}-\S/.test(line)) {
|
||||
inPreset = /^\s{1,3}-preset\s/.test(line);
|
||||
continue;
|
||||
}
|
||||
const entry = inPreset ? /^\s{4,}([a-z][a-z0-9_]*)\s+-?\d+\s+[E.]\S{3}/.exec(line) : null;
|
||||
if (entry) names.add(entry[1]!);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
let cachedNvencPresets: Promise<ReadonlySet<string>> | undefined = undefined;
|
||||
|
||||
/** The presets this ffmpeg's h264_nvenc takes; empty when it cannot be read, which keeps the pN names. */
|
||||
export function getCachedNvencPresets(): Promise<ReadonlySet<string>> {
|
||||
cachedNvencPresets ??= readNvencPresets();
|
||||
return cachedNvencPresets;
|
||||
}
|
||||
|
||||
async function readNvencPresets(): Promise<ReadonlySet<string>> {
|
||||
const help = await ffmpegStdout(
|
||||
["-hide_banner", "-h", "encoder=h264_nvenc"],
|
||||
GPU_PROBE_TIMEOUT_MS,
|
||||
GPU_PROBE_KILL_GRACE_MS,
|
||||
).catch(() => null);
|
||||
return help === null ? new Set() : parseEncoderPresetNames(help);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user