import { execFileSync } from "node:child_process";
import { mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import sharp from "sharp";
const source = "public/blog/fox-courier-first-frame.webp";
const dest = "public/blog";
const frames = await mkdtemp(path.join(tmpdir(), "clout-sketch-study-"));
const staticPanel = await sharp(source).resize(600, 338).png().toBuffer();
const header = Buffer.from(
``,
);
const selected: Buffer[] = [];
for (let i = 0; i < 144; i++) {
const zoom = 1 + (0.08 * i) / 143;
const w = Math.round(600 * zoom),
h = Math.round(338 * zoom);
const moving = await sharp(source)
.resize(w, h)
.extract({
left: Math.floor((w - 600) / 2),
top: Math.floor((h - 338) / 2),
width: 600,
height: 338,
})
.png()
.toBuffer();
const frame = await sharp(header)
.composite([
{ input: staticPanel, left: 40, top: 200 },
{ input: moving, left: 660, top: 200 },
])
.png()
.toBuffer();
await writeFile(path.join(frames, `${String(i).padStart(4, "0")}.png`), frame);
if ([0, 72, 143].includes(i)) selected.push(moving);
if (i === 0)
await sharp(frame).webp({ quality: 92 }).toFile(`${dest}/sketch-camera-study-poster.webp`);
}
execFileSync(
"/opt/homebrew/bin/ffmpeg",
[
"-y",
"-framerate",
"24",
"-i",
path.join(frames, "%04d.png"),
"-c:v",
"libx264",
"-crf",
"20",
"-pix_fmt",
"yuv420p",
"-movflags",
"+faststart",
`${dest}/sketch-camera-study.mp4`,
],
{ stdio: "ignore" },
);
const sheet = Buffer.from(
``,
);
await sharp(sheet)
.composite(
await Promise.all(
selected.map(async (input, i) => ({
input: await sharp(input).resize(510, 287).png().toBuffer(),
left: 30 + i * 530,
top: 155,
})),
),
)
.webp({ quality: 92 })
.toFile(`${dest}/sketch-camera-study-frames.webp`);
await writeFile(
`${dest}/sketch-camera-study.vtt`,
"WEBVTT\n\n00:00.000 --> 00:06.000\nLeft: fixed fox courier illustration. Right: the same drawing slowly crops inward. The fox does not move. No audio.\n",
);
console.log({
frames: 144,
fps: 24,
seconds: 6,
dimensions: "1280x720",
source,
temporaryFrames: frames,
});