Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/media-local/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions packages/media-local/test/extract-frame.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});