Skip to content

Commit ac1948a

Browse files
fix(engine): a full disk during frame extraction says so, not that the video is broken (#7161)
* fix(engine): a full disk during frame extraction says so, not that the video is broken * fix(engine): a pass that filled the disk is not decoded again in software
1 parent 21f2119 commit ac1948a

4 files changed

Lines changed: 109 additions & 9 deletions

File tree

‎packages/engine/src/services/videoFrameExtractor.mediaEngine.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ describe.skipIf(process.platform === "win32")("extraction decode on the Mac's me
126126
);
127127
});
128128

129+
it("calls an extraction that filled the disk disk_full, not a source that cannot be decoded", async () => {
130+
probe.hasVideoToolbox = false;
131+
// ffmpeg 8's own words when its frames fill the disk (exit 228); the summary after them is all a tail shows.
132+
const stderr = [
133+
"[vost#0:0/mjpeg @ 0x1] Error submitting a packet to the muxer: No space left on device",
134+
"[out#0/image2 @ 0x2] Error muxing a packet",
135+
"[out#0/image2 @ 0x2] Task finished with error code: -28 (No space left on device)",
136+
"[out#0/image2 @ 0x2] Terminating thread with return code -28 (No space left on device)",
137+
"[out#0/image2 @ 0x2] video:40374KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB",
138+
"frame= 307 fps=124 q=2.0 Lsize=N/A time=00:00:10.23 bitrate=N/A speed=4.14x",
139+
"Conversion failed!",
140+
].join("\n");
141+
const real = vi.mocked(runFfmpeg).getMockImplementation()!;
142+
vi.mocked(runFfmpeg).mockImplementation(async (args, options) =>
143+
args.includes("-vf")
144+
? { success: false, exitCode: 228, stderr, durationMs: 1, terminationReason: "exit" }
145+
: real(args, options),
146+
);
147+
let errors: Awaited<ReturnType<typeof extractAllVideoFrames>>["errors"] = [];
148+
try {
149+
const video = { id: "full", src: clips.h264, start: 0, end: 1, mediaStart: 0 };
150+
({ errors } = await extractAllVideoFrames([{ ...video, loop: false, hasAudio: false }], dir, {
151+
fps: 10,
152+
outputDir: join(dir, "full"),
153+
}));
154+
} finally {
155+
vi.mocked(runFfmpeg).mockImplementation(real);
156+
}
157+
expect(errors.map(({ kind, retryable }) => ({ kind, retryable }))).toEqual([
158+
{ kind: "disk_full", retryable: false },
159+
]);
160+
});
161+
129162
it("decodes in software when the media engine refuses a picture", async () => {
130163
const real = vi.mocked(runFfmpeg).getMockImplementation()!;
131164
vi.mocked(runFfmpeg).mockImplementation(async (args, options) =>
@@ -149,6 +182,29 @@ describe.skipIf(process.platform === "win32")("extraction decode on the Mac's me
149182
}
150183
});
151184

185+
it("does not decode again in software when the media engine's pass filled the disk", async () => {
186+
const real = vi.mocked(runFfmpeg).getMockImplementation()!;
187+
vi.mocked(runFfmpeg).mockImplementation(async (args, options) =>
188+
args.includes("-hwaccel")
189+
? {
190+
success: false,
191+
exitCode: 228,
192+
stderr:
193+
"[out#0/image2 @ 0x2] Task finished with error code: -28 (No space left on device)",
194+
durationMs: 0,
195+
terminationReason: "exit",
196+
}
197+
: real(args, options),
198+
);
199+
try {
200+
await extractOn("darwin", clips.h264, "mac-vt-disk-full").catch(() => {});
201+
const runs = vi.mocked(runFfmpeg).mock.calls.map(([callArgs]) => hwaccel(callArgs));
202+
expect(runs).toEqual(["videotoolbox"]);
203+
} finally {
204+
vi.mocked(runFfmpeg).mockImplementation(real);
205+
}
206+
});
207+
152208
it.runIf(realPlatform === "darwin")("writes the same frames as software decode", async () => {
153209
const engine = await extractOn("darwin", clips.h264, "same-engine");
154210
probe.hasVideoToolbox = false;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ export type VideoExtractionFailureKind =
412412
| "ffmpeg_timeout"
413413
| "ffmpeg_transient"
414414
| "ffmpeg_failed"
415+
| "disk_full"
415416
| "zero_output"
416417
| "internal";
417418

@@ -557,6 +558,9 @@ function ffmpegReasonFrom(stderr: string, paths: readonly string[]): string {
557558
.slice(-FFMPEG_REASON_CHARS);
558559
}
559560

561+
// ffmpeg ends a full disk with its last frames' summary, so the strerror text is the only sign of it
562+
const DISK_FULL = /no space left on device|disk quota exceeded/i;
563+
560564
export function isVideoSourceExtractionError(error: unknown): error is VideoSourceExtractionError {
561565
return (
562566
typeof error === "object" &&
@@ -1439,7 +1443,8 @@ async function extractVideoFramesRangeUnredacted(
14391443
!processResult.success &&
14401444
!processResult.failureReason;
14411445
const videoToolboxWasOptional = decodeWithVideoToolbox && !keepsHdrForChrome;
1442-
if (ffmpegExitedFailing && videoToolboxWasOptional) processResult = await extract([]);
1446+
if (ffmpegExitedFailing && videoToolboxWasOptional && !DISK_FULL.test(processResult.stderr))
1447+
processResult = await extract([]);
14431448
if (processResult.failureReason === "external_interruption") {
14441449
throw new VideoSourceExtractionError(
14451450
"external_interruption",
@@ -1481,6 +1486,15 @@ async function extractVideoFramesRangeUnredacted(
14811486
ffmpegReason,
14821487
);
14831488
}
1489+
if (DISK_FULL.test(processResult.stderr)) {
1490+
throw new VideoSourceExtractionError(
1491+
"disk_full",
1492+
false,
1493+
"The disk filled up while video frames were extracted",
1494+
diagnostic,
1495+
ffmpegReason,
1496+
);
1497+
}
14841498
const transientIo =
14851499
/resource temporarily unavailable|device or resource busy|input\/output error/i.test(
14861500
processResult.stderr,

‎packages/producer/src/services/render/stages/extractVideosStage.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ describe("encoder interruption classification", () => {
9292

9393
expect(() => assertVideoExtractionSucceeded(result)).toThrow(EncoderInterruptedError);
9494
});
95+
96+
it("reports a disk that filled up during extraction as ENOSPC, not as unrenderable sources", () => {
97+
const full = (n: number) => ({
98+
videoId: `v${n}`,
99+
kind: "disk_full" as const,
100+
retryable: false,
101+
error: "Error submitting a packet to the muxer: No space left on device",
102+
source: `assets/clip-${n}.mp4`,
103+
});
104+
const result = extractionResult([
105+
full(1),
106+
full(2),
107+
{ videoId: "v3", kind: "zero_output", retryable: false, error: "no frames" },
108+
]);
109+
110+
expect(() => assertVideoExtractionSucceeded(result)).toThrow(
111+
expect.objectContaining({
112+
code: "ENOSPC",
113+
message: "ENOSPC: no space left on device, extracting the frames of 2 video source(s)",
114+
}),
115+
);
116+
});
95117
});
96118

97119
describe("appendAutoDetectedVideoAudio", () => {

‎packages/producer/src/services/render/stages/extractVideosStage.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,26 @@ export class VideoExtractionStageError extends Error {
262262
}
263263

264264
export function assertVideoExtractionSucceeded(result: ExtractionResult): void {
265-
throwIfEncoderInterrupted(result);
265+
throwIfHostFailure(result);
266266
const error = buildVideoExtractionStageError(result);
267267
if (error) throw error;
268268
}
269269

270-
function throwIfEncoderInterrupted(result: ExtractionResult): void {
270+
/** Failures of the machine, not of a source: an interrupted encoder, or a disk that filled up. */
271+
function throwIfHostFailure(result: ExtractionResult): void {
271272
const interrupted = result.errors.find((failure) => failure.kind === "external_interruption");
272-
if (!interrupted) return;
273-
throw encoderFailureError("Video frame extraction failed", {
274-
error: String(interrupted.error),
275-
failureReason: "external_interruption",
276-
});
273+
if (interrupted) {
274+
throw encoderFailureError("Video frame extraction failed", {
275+
error: String(interrupted.error),
276+
failureReason: "external_interruption",
277+
});
278+
}
279+
const full = result.errors.filter((failure) => failure.kind === "disk_full").length;
280+
if (full === 0) return;
281+
throw Object.assign(
282+
new Error(`ENOSPC: no space left on device, extracting the frames of ${full} video source(s)`),
283+
{ code: "ENOSPC" },
284+
);
277285
}
278286

279287
function buildVideoExtractionStageError(
@@ -561,7 +569,7 @@ export async function runExtractVideosStage(
561569
extractionResult.phaseBreakdown.transientRetries =
562570
(extractionResult.phaseBreakdown.transientRetries ?? 0) + hdrProbeTransientRetries;
563571
assertNotAborted();
564-
throwIfEncoderInterrupted(extractionResult);
572+
throwIfHostFailure(extractionResult);
565573
failureToEnforce = applyVideoExtractionFailurePolicy(extractionResult, extractionPolicy, log);
566574

567575
materializeExtractedFramesForCompiledDir(extractionResult.extracted, compiledDir, {

0 commit comments

Comments
 (0)