#!/usr/bin/env node // Builds the exact model requests for one stage of the stunt from stunt.json and prints them // (or writes one JSON file per request with --out). It calls no API. // Each request is { id, model, input, approx_cost_usd }: POST `input` to fal model `model` with your // own FAL_KEY (scripts/fal-run.mjs does this), or paste the prompt and images into any // Nano-Banana-class image editor and Kling- or Seedance-class image-to-video tool. // // Stages: // estimate no requests: the expected total for the whole stunt, to quote to the user and get one // yes on before the first paid call // keyframe shot 1 first (the wide frame, from the product photo), then shot 2 (the low-angle hero // frame) with the approved shot 1 still and the product photo as references // draft a cheap silent 480p motion test per shot (Seedance 2.0 mini) // final one 5 s clip with sound per shot: Kling 3 pro (default) or Seedance 2.5 720p // // Usage: // node scripts/build-requests.mjs --plan stunt.json --stage estimate|keyframe|draft|final // [--shot 1|2|all] [--out requests/] [--inline] [--model kling|seedance] [--silent] // // --inline embeds local images as data URIs (needs --out), so no photo goes on a public URL. // Node 18 or newer. No npm packages. import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs' import { dirname, extname, isAbsolute, join, resolve } from 'node:path' const FLAGS = new Set(['plan', 'stage', 'shot', 'out', 'inline', 'model', 'silent']) const SWITCHES = new Set(['inline', 'silent']) const MIME = { '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.webp': 'image/webp' } // fal list prices, checked 2026-11-13 (fal.ai/models//llms.txt, scripts/skill-lab/models.json). // Nano Banana Pro edit $0.15 per 1K or 2K image; Seedance 2.0 mini $0.0721/s at 480p; Kling 3 pro // $0.168/s with sound and $0.112/s silent; Seedance 2.5 $0.473/s at 720p. const KEYFRAME = { model: 'fal-ai/nano-banana-pro/edit', cost: 0.15 } const DRAFT = { model: 'bytedance/seedance-2.0/mini/image-to-video', perSecond: 0.0721 } const FINALS = { kling: { model: 'fal-ai/kling-video/v3/pro/image-to-video', name: 'Kling 3 pro', audio: 0.168, silent: 0.112, durations: [4, 5, 6, 7, 8] }, seedance: { model: 'bytedance/seedance-2.5/image-to-video', name: 'Seedance 2.5 720p', audio: 0.473, silent: 0.473, durations: [4, 5, 6, 7, 8] }, } const NEGATIVE = 'music, singing, speech, voices, shouting, screaming, sirens, alarms, panic, running crowd, text, captions, subtitles, letters, logos, brand names, watermark, close-up faces, explosion, fire, smoke, debris, broken glass, damage, cartoon, morphing, shape change, a second product, cuts, scene change, slow motion' function fail(message) { console.error(message) process.exit(1) } const opts = {} const argv = process.argv.slice(2) for (let i = 0; i < argv.length; i++) { const a = argv[i] if (!a.startsWith('--')) fail(`Unexpected argument "${a}". Flags look like --name value.`) const key = a.slice(2) if (!FLAGS.has(key)) fail(`Unknown flag --${key}. Allowed: ${[...FLAGS].map((f) => `--${f}`).join(', ')}`) if (SWITCHES.has(key)) { opts[key] = true; continue } const value = argv[i + 1] if (value === undefined || value.startsWith('--')) fail(`--${key} needs a value.`) opts[key] = value i++ } if (!opts.plan || !opts.stage) fail('Usage: node scripts/build-requests.mjs --plan stunt.json --stage estimate|keyframe|draft|final [--shot N|all] [--out dir] [--inline] [--model kling|seedance] [--silent]') if (!['estimate', 'keyframe', 'draft', 'final'].includes(opts.stage)) fail(`--stage must be estimate, keyframe, draft or final, not "${opts.stage}".`) if (opts.inline && !opts.out) fail('--inline writes large data URIs; use it with --out .') if (!existsSync(opts.plan)) fail(`Plan not found: ${opts.plan}. Create it with scripts/build-plan.mjs.`) let plan try { plan = JSON.parse(readFileSync(opts.plan, 'utf8')) } catch { fail(`${opts.plan} is not valid JSON.`) } const base = dirname(resolve(opts.plan)) const rel = (p) => (isAbsolute(p) ? p : join(base, p)) const brief = plan.brief ?? {} const locks = plan.locks ?? {} const finalKey = opts.model ?? brief.final_model ?? 'kling' const final = FINALS[finalKey] if (!final) fail(`--model must be kling or seedance, not "${finalKey}".`) const audio = !opts.silent const shots = (plan.shots ?? []).filter((s) => !s.skip) if (!shots.length) fail('The plan has no shots.') for (const s of shots) if (!final.durations.includes(Number(s.duration))) fail(`Shot ${s.n} duration ${s.duration} s is not available on ${final.name}. Use ${final.durations.join(', ')} seconds.`) if (!plan.references?.photo) fail('The plan has no references.photo. This Skill needs one photo of the product.') const round = (v) => Number(v.toFixed(3)) const money = (n) => `$${n.toFixed(2)}` const finalCost = (s) => round((audio ? final.audio : final.silent) * Number(s.duration)) const draftCost = (s) => round(DRAFT.perSecond * Number(s.duration)) if (opts.stage === 'estimate') { const seconds = shots.reduce((a, s) => a + Number(s.duration), 0) const keyframes = shots.length * KEYFRAME.cost const drafts = shots.reduce((a, s) => a + draftCost(s), 0) const finals = shots.reduce((a, s) => a + finalCost(s), 0) const lines = [ [`Keyframes: ${shots.length} x Nano Banana Pro edit 2K`, keyframes], [`Drafts: ${seconds} s on Seedance 2.0 mini 480p, silent`, drafts], [`Finals: ${seconds} s on ${final.name}${audio ? ' with sound' : ' silent'}`, finals], ] const total = keyframes + drafts + finals const reroll = KEYFRAME.cost + finalCost(shots[0]) for (const [label, v] of lines) console.log(` ${label.padEnd(58)} ${money(v)}`) console.log(` ${'Without re-rolls'.padEnd(58)} ${money(total)}`) console.log(` ${'With one re-rolled keyframe and one re-rolled final'.padEnd(58)} ${money(total + reroll)}`) const others = Object.entries(FINALS).filter(([k]) => k !== finalKey).map(([, f]) => `${f.name} ${money(shots.reduce((a, s) => a + (audio ? f.audio : f.silent) * Number(s.duration), 0))} for the finals`) console.log(`\n The same finals on the other model: ${others.join(', ')}.`) console.log(` A draft can be skipped for a shot (saves ${money(draftCost(shots[0]))}); Seedance refuses a start frame with a clearly visible face.`) console.log(`\nQuote to the user: about ${money(total)} for the stunt, up to about ${money(total + reroll)} with a re-roll (the finals are ${money(finals)} of it).`) console.log('Get one explicit yes on this total before the first paid call, and ask again if it will be exceeded.') process.exit(0) } const unfilled = (text) => typeof text === 'string' && text.includes('FILL:') function fillTokens(text, where) { if (typeof text !== 'string' || !text.trim()) fail(`${where} is empty. Write it in ${opts.plan} first.`) if (unfilled(text)) fail(`${where} still contains "FILL:". Write it in ${opts.plan} first.`) const tokens = { PRODUCT: locks.product, SCALE: locks.scale, STREET: locks.street, LIGHT: locks.light, COLOUR: locks.colour, SETPIECE: locks.setpiece } return text.replace(/\{\{(PRODUCT|SCALE|STREET|LIGHT|COLOUR|SETPIECE)\}\}/g, (_, name) => { const value = tokens[name] if (!value || unfilled(value)) fail(`${where} uses {{${name}}} but locks.${name.toLowerCase()} is not written yet.`) return name === 'LIGHT' ? value.trim() : value.trim().replace(/[.]$/, '') }) } function imageRef(path, what) { const abs = rel(path) if (!existsSync(abs)) fail(`${what} not found: ${abs}`) const ext = extname(abs).toLowerCase() if (!MIME[ext]) fail(`${what} must be a .jpg, .png or .webp image, not ${ext || 'a file without extension'}.`) if (!opts.inline) return `file:${abs}` if (statSync(abs).size > 8 * 1024 * 1024) fail(`${what} is over 8 MB. Save it as a JPEG under 8 MB first.`) return `data:${MIME[ext]};base64,${readFileSync(abs).toString('base64')}` } let picked = shots if (opts.shot && opts.shot !== 'all') { const n = Number(opts.shot) if (!Number.isInteger(n)) fail(`--shot takes a shot number or "all", not "${opts.shot}".`) picked = shots.filter((s) => s.n === n) if (!picked.length) fail(`There is no shot ${n} in the plan.`) } const lookLine = `${locks.look ?? ''} Photorealistic, true-to-life scale, with the shadows, reflections and contact of a real object in this street.` const safetyLine = 'No readable text anywhere: shop signs, street signs, vehicles and billboards are blank or plain, with no logos, no brand names, no vehicle badges and no number plates. People are tiny, far away or seen from behind, with no face readable. Nothing is damaged: no debris, smoke, fire or emergency vehicles. The product and the action sit in the middle of the frame, clear of the top 12% and the bottom 20%.' const logoLine = brief.keep_logo ? 'Keep the product\'s own logo from the product photo exactly, letter for letter, in the same place; no other text anywhere.' : 'Leave any label or logo area on the product plain, with no letters.' const requests = [] for (const s of picked) { const where = `shot ${s.n}` if (opts.stage === 'keyframe') { const refs = [] const lines = [] if (s.use_first_frame_ref) { const first = shots[0] if (first === s) fail(`${where} cannot use itself as the first-frame reference. Set use_first_frame_ref to false on shot 1.`) if (!first.keyframe) fail(`${where} uses the shot 1 still as its reference, but shot 1 has no approved keyframe yet. Generate shot 1 first, look at it, and put its path in "keyframe".`) refs.push(imageRef(first.keyframe, 'Shot 1 keyframe')) lines.push(`Reference image ${refs.length} is the approved wide frame: the same street, buildings, set piece, light and product, now seen from a new angle.`) } refs.push(imageRef(plan.references.photo, 'Product photo')) lines.push(`Reference image ${refs.length} is the real product: keep its exact shape, proportions, parts and colours, and take nothing else from that photo (its background, surface and light are not used). ${logoLine}`) const prompt = `Vertical 9:16 photo: one frame from a handheld phone video of a staged CGI street stunt, filmed on a phone's normal wide lens. ${fillTokens(s.keyframe_prompt, `${where} keyframe_prompt`)} ${lines.join(' ')} ${lookLine} ${safetyLine}`.replace(/\s+/g, ' ').trim() const input = { prompt, image_urls: refs, aspect_ratio: '9:16', resolution: '2K', num_images: 1, output_format: 'jpeg' } requests.push({ id: `${s.id}-keyframe`, model: KEYFRAME.model, input, approx_cost_usd: KEYFRAME.cost }) continue } if (!s.keyframe) fail(`${where} has no approved keyframe yet. Generate it, look at it, and put its path in "keyframe".`) const motion = fillTokens(s.motion_prompt, `${where} motion_prompt`).trim() const sound = fillTokens(s.sound, `${where} sound`).trim().replace(/[.]$/, '') const parts = [ 'Handheld phone video, vertical 9:16, one continuous shot, no cuts, real-time speed, the phone sways slightly like a person filming.', /[.!?]$/.test(motion) ? motion : `${motion}.`, 'The product keeps exactly the shape, colours and parts it has in the first frame and does not change size. Pedestrians stay tiny, far away and calm, with no face readable. Nothing is damaged and nothing falls.', ] const withSound = [...parts, `Audio: ${sound}.`, 'No music, no speech, no voices, no sirens. No text on screen.'].join(' ') const silent = [...parts, 'No text on screen.'].join(' ') const image = imageRef(s.keyframe, `${where} keyframe`) const seconds = Number(s.duration) if (opts.stage === 'draft') { const input = { prompt: silent, image_url: image, resolution: '480p', duration: String(seconds), aspect_ratio: 'auto', generate_audio: false } requests.push({ id: `${s.id}-draft`, model: DRAFT.model, input, approx_cost_usd: draftCost(s) }) continue } const prompt = audio ? withSound : silent let input if (finalKey === 'kling') input = { prompt, start_image_url: image, duration: String(seconds), generate_audio: audio, negative_prompt: NEGATIVE } else input = { prompt, image_url: image, resolution: '720p', duration: String(seconds), aspect_ratio: 'auto', generate_audio: audio, bitrate_mode: 'high', codec: 'H264' } requests.push({ id: `${s.id}-final-${finalKey}`, model: final.model, input, approx_cost_usd: finalCost(s) }) } const total = requests.reduce((a, r) => a + r.approx_cost_usd, 0) if (opts.out) { mkdirSync(opts.out, { recursive: true }) for (const r of requests) writeFileSync(join(opts.out, `${r.id}.json`), JSON.stringify(r, null, 2)) console.log(`Wrote ${requests.length} request(s) to ${opts.out}. Approximate cost: ${money(total)}.`) } else { console.log(JSON.stringify(requests, null, 2)) console.error(`${requests.length} request(s), approximate cost ${money(total)}. Local images are shown as file: paths; use --out with --inline to embed them, or fal-run.mjs (which embeds them).`) }