From d6012b02bbaeea44e34a62d279e7839c01d13817 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Mon, 19 Oct 2026 04:39:23 -0400 Subject: [PATCH] fix(media-local): report an ExtractFrame time past the end of the stream Co-Authored-By: Claude Opus 5.5 --- packages/media-local/src/execute.ts | 11 ++++++++++- .../media-local/test/extract-frame.test.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 packages/media-local/test/extract-frame.test.ts diff --git a/packages/media-local/src/execute.ts b/packages/media-local/src/execute.ts index 00e283b41..844bd914f 100644 --- a/packages/media-local/src/execute.ts +++ b/packages/media-local/src/execute.ts @@ -1092,7 +1092,16 @@ export async function executeExtractFrame( maxStdoutBytes: 64 * 1024, ...(env.sharedLibraryPath === undefined ? {} : { sharedLibraryPath: env.sharedLibraryPath }), }); - const bytes = await readFile(output); + let bytes: Uint8Array; + try { + bytes = await readFile(output); + } catch (error) { + // ffmpeg exits successfully when its selection matches no frame, as a `time:` past the end does. + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + throw new Error(need.at.kind === "time" + ? `Frame extraction found no frame at ${need.at.seconds}s; the time is past the end of the selected stream` + : "Frame extraction found no frame for its selection", { cause: error }); + } assert(bytes.byteLength > 0, "Frame extraction produced no image"); const inspected = await outputInspection({ path: output, diff --git a/packages/media-local/test/extract-frame.test.ts b/packages/media-local/test/extract-frame.test.ts new file mode 100644 index 000000000..e9ba7781e --- /dev/null +++ b/packages/media-local/test/extract-frame.test.ts @@ -0,0 +1,19 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { canonicalize } from "@hypit/protocol"; +import { MemoryResourceStore, ffmpegBytes, hasMediaBinaries, testMediaEnvironment } from "../../../test/alpha-video-fixture.js"; +import { executeExtractFrame } from "../src/execute.js"; + +test("a frame time past the end of the stream is reported as such", { + skip: !hasMediaBinaries && "ffmpeg and ffprobe are not installed", +}, async () => { + const resources = new MemoryResourceStore(); + const video = ffmpegBytes(["-f", "lavfi", "-i", "color=c=red:s=32x32:r=10:d=1", + "-c:v", "libx264", "-pix_fmt", "yuv420p", "-f", "mp4", "-movflags", "frag_keyframe+empty_moov", "pipe:1"]); + const source = await resources.put(video, "video/mp4"); + const extract = (seconds: number) => executeExtractFrame(testMediaEnvironment(resources), canonicalize({ + source, streamIndex: 0, sourceFrameCount: 10, at: { kind: "time", seconds }, output: { format: "png" }, + })); + assert.equal((await extract(0.5)).value.kind, "blob"); + await assert.rejects(extract(5), /no frame at 5s; the time is past the end of the selected stream/u); +});