File size: 1,934 Bytes
87b5db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Generates the SYNTHETIC example clip: a deterministic virtual camera pan+zoom over the CC0 photo
// example-desk-laptop.jpg (no generative editing). The exact crop of every frame is written to
// public/examples/video-pan.json so the tracking evidence can compute ground truth by geometry.
import sharp from 'sharp';
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
const root = new URL('../', import.meta.url).pathname;
const src = root + 'public/examples/example-desk-laptop.jpg';
const OUT_W = 640, OUT_H = 426, FPS = 10, N = 40;
const meta = await sharp(src).metadata();
const tmp = '/tmp/rotoscopia-frames'; rmSync(tmp, { recursive: true, force: true }); mkdirSync(tmp);
const frames = [];
for (let i = 0; i < N; i++) {
  const u = i / (N - 1), e = 0.5 - 0.5 * Math.cos(Math.PI * u); // ease in-out
  const w = Math.round(820 - 140 * e), h = Math.round((w * OUT_H) / OUT_W);
  const left = Math.round(180 + 260 * e), top = Math.round(10 + 130 * e);
  frames.push({ i, t: i / FPS, left, top, width: w, height: h });
  await sharp(src).extract({ left, top, width: w, height: h }).resize(OUT_W, OUT_H, { kernel: 'lanczos3' }).png().toFile(`${tmp}/f${String(i).padStart(3, '0')}.png`);
}
const mp4 = root + 'public/examples/example-desk-pan.mp4';
execFileSync('ffmpeg', ['-y', '-loglevel', 'error', '-framerate', String(FPS), '-i', `${tmp}/f%03d.png`, '-c:v', 'libx264', '-preset', 'veryslow', '-crf', '20', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', '-an', mp4]);
execFileSync('ffmpeg', ['-nostdin', '-y', '-loglevel', 'error', '-i', `${tmp}/f000.png`, '-q:v', '3', root + 'public/examples/example-desk-pan-poster.jpg']);
writeFileSync(root + 'public/examples/video-pan.json', JSON.stringify({ source: 'example-desk-laptop.jpg', sourceSize: [meta.width, meta.height], out: [OUT_W, OUT_H], fps: FPS, frames }, null, 1));
console.log('wrote', mp4, N, 'frames');