Skip to content

Commit 315a7b7

Browse files
fix(engine): hide ffmpeg console windows on Windows (#3381)
ffmpeg and ffprobe are console-subsystem binaries and Node defaults windowsHide to false, so every spawn opened a visible console window on Windows. A render shells out dozens of times across parallel workers, which flashed a burst of windows across the user's desktop. Applied at every production spawn site rather than only the two named in the report, since they all share the cause: runFfmpeg, both gpuEncoder probes, ffprobe, streamingEncoder, audioExtractor and the distributed version check. windowsHide is a no-op on macOS and Linux. The dev-only parity and regression harnesses are left alone; they never run on a user's desktop. Closes #3379
1 parent a9ea07e commit 315a7b7

7 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎packages/engine/src/services/streamingEncoder.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ export async function spawnStreamingEncoder(
457457

458458
const ffmpeg: ChildProcess = spawn(getFfmpegBinary(), args, {
459459
stdio: ["pipe", "pipe", "pipe"],
460+
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
461+
windowsHide: true,
460462
});
461463
trackChildProcess(ffmpeg);
462464

‎packages/engine/src/utils/ffprobe.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ async function runFfprobe(
6969
// Nothing is ever written to the child's stdin; leaving it as a pipe is
7070
// what lets a stdin-reading invocation block indefinitely.
7171
stdio: ["ignore", "pipe", "pipe"],
72+
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
73+
windowsHide: true,
7274
});
7375
trackChildProcess(proc);
7476
// Decoded through StringDecoder rather than per-chunk toString(): a

‎packages/engine/src/utils/gpuEncoder.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export async function selectUsableGpuEncoder(
6464
export async function detectGpuEncoder(): Promise<GpuEncoder> {
6565
const ffmpeg = spawn(getFfmpegBinary(), ["-encoders"], {
6666
stdio: ["pipe", "pipe", "pipe"],
67+
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
68+
windowsHide: true,
6769
});
6870
trackChildProcess(ffmpeg);
6971
let stdout = "";
@@ -146,6 +148,7 @@ export function getProbeArgs(encoder: ConcreteGpuEncoder): string[] {
146148
async function canUseGpuEncoder(encoder: ConcreteGpuEncoder): Promise<boolean> {
147149
const ffmpeg = spawn(getFfmpegBinary(), getProbeArgs(encoder), {
148150
stdio: ["ignore", "ignore", "pipe"],
151+
windowsHide: true,
149152
});
150153
trackChildProcess(ffmpeg);
151154
const outcome = await new ManagedChildProcess(ffmpeg, {

‎packages/engine/src/utils/runFfmpeg.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ export function formatFfmpegError(
9292

9393
export async function runFfmpeg(args: string[], opts?: RunFfmpegOptions): Promise<RunFfmpegResult> {
9494
const timeout = opts?.timeout ?? DEFAULT_TIMEOUT;
95-
const ffmpeg = spawn(getFfmpegBinary(), args);
95+
// windowsHide: ffmpeg/ffprobe are console-subsystem binaries, so without
96+
// this Node opens a visible console window per spawn on Windows. A render
97+
// shells out dozens of times across parallel workers, which flashes a burst
98+
// of windows across the user's desktop. No-op on macOS and Linux.
99+
const ffmpeg = spawn(getFfmpegBinary(), args, { windowsHide: true });
96100
trackChildProcess(ffmpeg);
97101
const managed = new ManagedChildProcess(ffmpeg, {
98102
signal: opts?.signal,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { EventEmitter } from "node:events";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
// Hoisted so the mock factory below can reach it without a top-level variable.
5+
const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }));
6+
7+
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
8+
vi.mock("child_process", () => ({ spawn: spawnMock }));
9+
10+
/** Minimal stand-in for the ChildProcess runFfmpeg awaits. */
11+
function fakeFfmpeg() {
12+
const proc = new EventEmitter() as EventEmitter & Record<string, unknown>;
13+
proc.stdout = new EventEmitter();
14+
proc.stderr = new EventEmitter();
15+
proc.stdin = { write: vi.fn(), end: vi.fn() };
16+
proc.kill = vi.fn();
17+
proc.pid = 4242;
18+
queueMicrotask(() => proc.emit("close", 0, null));
19+
return proc;
20+
}
21+
22+
describe("runFfmpeg spawn options", () => {
23+
it("hides the console window so Windows renders do not flash terminals", async () => {
24+
// Regression for the Windows popup report: ffmpeg is a console-subsystem
25+
// binary, and Node defaults `windowsHide` to false, so every spawn opened a
26+
// visible window. A render shells out dozens of times across parallel
27+
// workers, which produced a burst of windows on the user's desktop.
28+
// Asserted on the options actually handed to spawn rather than on the
29+
// source text, so a future call site that drops the flag is caught by
30+
// behaviour.
31+
spawnMock.mockImplementation(() => fakeFfmpeg());
32+
33+
const { runFfmpeg } = await import("./runFfmpeg.js");
34+
await runFfmpeg(["-version"]);
35+
36+
expect(spawnMock).toHaveBeenCalledTimes(1);
37+
const options = spawnMock.mock.calls[0]?.[2] as { windowsHide?: boolean } | undefined;
38+
expect(options?.windowsHide).toBe(true);
39+
});
40+
});

‎packages/producer/src/services/audioExtractor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export function parseAudioElements(html: string): AudioElement[] {
8888
*/
8989
function runFFmpeg(args: string[]): Promise<void> {
9090
return new Promise((resolve, reject) => {
91-
const ffmpeg = spawn(getFfmpegBinary(), args);
91+
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
92+
const ffmpeg = spawn(getFfmpegBinary(), args, { windowsHide: true });
9293
trackChildProcess(ffmpeg);
9394
let stderr = "";
9495

‎packages/producer/src/services/distributed/shared.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@ let cachedFfmpegVersion: string | null = null;
333333
*/
334334
export async function readFfmpegVersion(): Promise<string> {
335335
if (cachedFfmpegVersion !== null) return cachedFfmpegVersion;
336-
const { stdout } = await execFile("ffmpeg", ["-version"], { maxBuffer: 1024 * 1024 });
336+
const { stdout } = await execFile("ffmpeg", ["-version"], {
337+
maxBuffer: 1024 * 1024,
338+
// See runFfmpeg.ts: keeps a console window off the user's desktop on Windows.
339+
windowsHide: true,
340+
});
337341
const firstLine = stdout.split(/\r?\n/)[0]?.trim() ?? "";
338342
if (!firstLine) {
339343
throw new Error("ffmpeg -version returned empty output");

0 commit comments

Comments
 (0)