#!/usr/bin/env node // Crops the user's "before" photo to 9:16 (1080 x 1920 by default) with ffmpeg, so the before frame and the after // still share exactly the same framing, and strips the photo's metadata (location, camera, dates). // The image model edits this cropped copy, and assemble-reveal.mjs holds it still at the start of the reveal. // It calls no API and costs nothing. // // Usage: // node scripts/prepare-before.mjs --input photo.jpg --output before-9x16.jpg [--focus 0.5] [--size 1080x1920] // // --focus is where the crop window sits along the photo's long side, 0 to 1 (0.5 is the centre). For a landscape // photo, 0 keeps the left edge and 1 keeps the right edge, so move it until the subject is inside. // Requires ffmpeg and ffprobe on PATH. Node 18 or newer. No npm packages. import { spawnSync } from 'node:child_process' import { existsSync } from 'node:fs' const usage = 'Usage: node scripts/prepare-before.mjs --input photo.jpg --output before-9x16.jpg [--focus 0.5] [--size 1080x1920]' const FLAGS = new Set(['input', 'output', 'focus', 'size']) const fail = (message) => { console.error(`${message}\n${usage}`) process.exit(1) } const options = {} const args = process.argv.slice(2) for (let i = 0; i < args.length; i += 1) { const arg = args[i] if (!arg.startsWith('--')) fail(`Unexpected argument "${arg}".`) const key = arg.slice(2) if (!FLAGS.has(key)) fail(`Unknown flag --${key}.`) const value = args[i + 1] if (value === undefined || value.startsWith('--')) fail(`--${key} needs a value.`) options[key] = value i += 1 } if (!options.input || !options.output) fail('--input and --output are required.') if (!/\.(jpe?g|png)$/i.test(options.output)) fail('--output must end in .jpg or .png.') if (!existsSync(options.input)) fail(`Not found: ${options.input}`) const focus = Number(options.focus ?? 0.5) if (!Number.isFinite(focus) || focus < 0 || focus > 1) fail('--focus must be a number from 0 to 1.') const size = (options.size ?? '1080x1920').match(/^(\d{3,4})x(\d{3,4})$/) if (!size) fail('--size must look like 1080x1920.') const [outW, outH] = [Number(size[1]), Number(size[2])] if (Math.abs(outW / outH - 9 / 16) > 0.01) fail('--size must be 9:16, for example 1080x1920 or 720x1280.') const probe = spawnSync('ffprobe', ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height:stream_side_data=rotation:stream_tags=rotate', '-print_format', 'json', options.input], { encoding: 'utf8' }) if (probe.error) fail('ffprobe was not found. Install ffmpeg (it includes ffprobe).') if (probe.status !== 0) fail(`${options.input} is not an image ffmpeg can read.`) const stream = JSON.parse(probe.stdout).streams?.[0] if (!stream) fail(`${options.input} has no image in it.`) let [w, h] = [stream.width, stream.height] const turn = Math.abs(Number(stream.side_data_list?.[0]?.rotation ?? stream.tags?.rotate ?? 0)) if (turn === 90 || turn === 270) [w, h] = [h, w] // ffmpeg applies the phone's rotation flag when it decodes let crop if (w / h > 9 / 16) { const cw = Math.floor((h * 9) / 16 / 2) * 2 crop = `crop=${cw}:${h}:${Math.round((w - cw) * focus)}:0` const kept = Math.round((cw / w) * 100) if (kept < 60) console.error(`note: this photo is wide, so the crop keeps ${kept}% of its width. Check that the subject is fully inside, or move --focus (now ${focus}).`) } else { const ch = Math.floor((w * 16) / 9 / 2) * 2 crop = `crop=${w}:${ch}:0:${Math.round((h - ch) * focus)}` } const cropH = crop.split(':')[1] if (Number(cropH) < 1280 && w / h > 9 / 16) console.error(`warning: the photo is only ${h} px tall, under 1280. The reveal will look soft; use a larger photo if you have one.`) const r = spawnSync('ffmpeg', ['-y', '-hide_banner', '-v', 'error', '-i', options.input, '-vf', `${crop},scale=${outW}:${outH}:flags=lanczos,setsar=1`, '-map_metadata', '-1', '-frames:v', '1', '-q:v', '2', options.output], { encoding: 'utf8' }) if (r.status !== 0) fail(`ffmpeg failed:\n${(r.stderr || '').split('\n').slice(-8).join('\n')}`) console.log(`${options.output} ${outW}x${outH} from ${w}x${h} (${crop}, metadata removed)`)