Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(video): bound both dimensions of sampled frames

Afloat396bb3883b9c
diff --git a/server/turn-video.test.ts b/server/turn-video.test.ts
index de182944a9..ad5d9437ac 100644
--- a/server/turn-video.test.ts
+++ b/server/turn-video.test.ts
@@ -30,10 +30,10 @@ describe.skipIf(!hasFfmpeg())("video frame sampling", () => {
};
// A generated test pattern, not a sample file in the repo: duration and a readable-by-ffmpeg guarantee come free.
- const syntheticClip = (root: string, seconds: number): TurnVideoInput => {
+ const syntheticClip = (root: string, seconds: number, size = "320x240"): TurnVideoInput => {
const path = join(root, "clip.mp4");
const result = spawnSync("ffmpeg", [
- "-f", "lavfi", "-i", `testsrc=duration=${seconds}:size=320x240:rate=5`,
+ "-f", "lavfi", "-i", `testsrc=duration=${seconds}:size=${size}:rate=5`,
"-pix_fmt", "yuv420p", "-y", path,
]);
if (result.status !== 0) throw new Error(`fixture ffmpeg failed: ${result.stderr?.toString()}`);
@@ -76,6 +76,20 @@ describe.skipIf(!hasFfmpeg())("video frame sampling", () => {
expect(frames).toHaveLength(2);
});
+ it.each([
+ ["256x2048", 128, 1024],
+ ["2048x256", 1024, 128],
+ ["1600x1600", 1024, 1024],
+ ["320x240", 320, 240],
+ ])("fits %s frames within both image dimensions without upscaling", async (size, width, height) => {
+ const frames = await videoFrameImages([syntheticClip(store(), 1, size)], 1);
+ expect(frames).toHaveLength(1);
+ const metadata = JSON.parse(execFileSync("ffprobe", [
+ "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "json", frames[0]!.path,
+ ], { encoding: "utf8" })) as { streams: Array<{ width: number; height: number }> };
+ expect(metadata.streams).toEqual([{ width, height }]);
+ });
+
it("stops at the frame limit, and samples nothing when the turn has no room left", async () => {
const root = store();
const clip = syntheticClip(root, 5);
diff --git a/server/turn-video.ts b/server/turn-video.ts
index 53a4cfa01c..6d860e8b91 100644
--- a/server/turn-video.ts
+++ b/server/turn-video.ts
@@ -69,7 +69,7 @@ async function extractFrame(video: TurnVideoInput, atSeconds: number, outPath: s
"-i", video.path,
"-frames:v", "1",
"-q:v", "4",
- "-vf", "scale='min(1024,iw)':-2",
+ "-vf", "scale=w='min(1024,iw)':h='min(1024,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2",
"-y", outPath,
], FFMPEG_TIMEOUT_MS);
}