#!/usr/bin/env node // Joins the three approved beat clips into one text-free looping reel with ffmpeg: trims, hard cuts, // the clips' own sound joined with short audio crossfades (in sync with the picture), an optional light // grade, an optional centre crop, and a loop tail: the last half second dissolves into the first frame // of shot 1 while the sound fades out, so the reel's last frame is its first frame and it loops cleanly. // Then H.264 MP4 with AAC sound and +faststart. No text is added. // // Usage: // node scripts/assemble-reel.mjs --reel reel.json --out reel.mp4 // [--grade none|clean|warm|cool] [--loop-tail 0-1.5] [--audio clips|none] [--audio-xfade 0-0.3] // [--gain -12-12] [--crop none|4:5|1:1] [--only 1,2,3] [--crf 10-30] [--max-mb 8] [--dry-run] // // --loop-tail 0 turns the loop off (the reel then just ends on shot 3). // --audio none makes a silent file (for a landing-page preview); ASMR reels are posted with sound. // Requires ffmpeg and ffprobe on PATH. Node 18 or newer. No npm packages. import { spawnSync } from 'node:child_process' import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, isAbsolute, join, resolve } from 'node:path' const FLAGS = new Set(['reel', 'out', 'grade', 'loop-tail', 'audio', 'audio-xfade', 'gain', 'crop', 'only', 'crf', 'max-mb', 'dry-run']) const SWITCHES = new Set(['dry-run']) // Light grades only: the macro look comes from the keyframes. See references/output-spec.md. const GRADES = { none: 'null', clean: "curves=master='0/0.01 0.25/0.235 0.75/0.77 1/0.99',eq=saturation=1.04", warm: "colorbalance=rs=0.02:gs=0:bs=-0.02:rm=0.03:gm=0.01:bm=-0.03:rh=0.02:gh=0.01:bh=-0.03,curves=master='0/0.01 0.25/0.235 0.75/0.77 1/0.99'", cool: "colorbalance=rs=-0.02:gs=0:bs=0.03:rm=-0.02:gm=0.01:bm=0.02,curves=master='0/0.01 0.25/0.235 0.75/0.77 1/0.99',eq=saturation=0.97", } function fail(message) { console.error(message) process.exit(1) } function parseFlags(argv) { const out = {} 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)) { out[key] = true; continue } const value = argv[i + 1] if (value === undefined || value.startsWith('--')) fail(`--${key} needs a value.`) out[key] = value i++ } return out } function num(value, name, min, max, fallback) { if (value === undefined || value === null) return fallback const n = Number(value) if (!Number.isFinite(n) || n < min || n > max) fail(`${name} must be a number from ${min} to ${max}, got "${value}".`) return n } function run(cmd, args, label) { const r = spawnSync(cmd, args, { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }) if (r.error?.code === 'ENOENT') fail(`${cmd} is not installed or not on PATH. Install ffmpeg (it includes ffprobe) and try again.`) if (r.status !== 0) fail(`${label} failed:\n${(r.stderr || '').split('\n').slice(-15).join('\n')}`) return r.stdout } function probe(path) { const j = JSON.parse(run('ffprobe', ['-v', 'error', '-print_format', 'json', '-show_streams', '-show_format', path], `ffprobe ${path}`)) const v = j.streams.find((s) => s.codec_type === 'video') if (!v) fail(`${path} has no video stream. Pass the MP4 clips Veo returned, not stills.`) return { width: v.width, height: v.height, duration: Number(j.format.duration), hasAudio: j.streams.some((s) => s.codec_type === 'audio') } } const even = (n) => Math.max(2, Math.floor(n / 2) * 2) const f3 = (n) => Number(n).toFixed(3) const FRAME = 1 / 24 const opts = parseFlags(process.argv.slice(2)) if (!opts.reel || !opts.out) fail('Usage: node scripts/assemble-reel.mjs --reel reel.json --out reel.mp4 [options]. See the header of this file for every option.') if (!existsSync(opts.reel)) fail(`Shot list not found: ${opts.reel}. Create it with scripts/build-shot-list.mjs.`) let reel try { reel = JSON.parse(readFileSync(opts.reel, 'utf8')) } catch { fail(`${opts.reel} is not valid JSON.`) } const base = dirname(resolve(opts.reel)) const rel = (p) => (isAbsolute(p) ? p : join(base, p)) const look = reel.look ?? {} const grade = opts.grade ?? look.grade ?? 'clean' if (!(grade in GRADES)) fail(`--grade must be one of ${Object.keys(GRADES).join(', ')}, not "${grade}".`) const loopTail = num(opts['loop-tail'] ?? look.loop_tail, '--loop-tail', 0, 1.5, 0.5) const xfade = num(opts['audio-xfade'] ?? look.audio_xfade, '--audio-xfade', 0, 0.3, 0.08) const gain = num(opts.gain, '--gain', -12, 12, 0) const crf = num(opts.crf, '--crf', 10, 30, 20) const maxMb = opts['max-mb'] === undefined ? null : num(opts['max-mb'], '--max-mb', 1, 500) const crop = opts.crop ?? 'none' if (!['none', '4:5', '1:1'].includes(crop)) fail(`--crop must be none, 4:5 or 1:1, not "${crop}".`) const audioMode = opts.audio ?? 'clips' if (!['none', 'clips'].includes(audioMode)) fail(`--audio must be none or clips, not "${audioMode}".`) if (!Array.isArray(reel.shots) || !reel.shots.length) fail('The shot list has no shots.') let shots = reel.shots.filter((s) => s.clip && !s.skip) if (opts.only) { const wanted = opts.only.split(',').map((x) => Number(x.trim())) if (wanted.some((n) => !Number.isInteger(n))) fail(`--only takes shot numbers like 1,3, not "${opts.only}".`) shots = wanted.map((n) => { const s = reel.shots.find((x) => x.n === n) if (!s) fail(`--only: there is no shot number ${n}.`) if (!s.clip) fail(`--only: shot ${n} has no clip yet.`) return s }) } if (!shots.length) fail('No shot has a "clip" path yet. Add the approved Veo MP4 paths to the shot list first.') const clips = shots.map((s) => { const path = rel(s.clip) if (!existsSync(path)) fail(`Shot ${s.n}: clip not found at ${path}.`) if (!/\.(mp4|mov|webm)$/i.test(path)) fail(`Shot ${s.n}: "${s.clip}" is not a video. Use the MP4 that Veo returned.`) const info = probe(path) const inT = Number(s.in ?? 0) let outT = Number(s.out ?? info.duration) if (!Number.isFinite(inT) || !Number.isFinite(outT) || inT < 0 || outT <= inT) fail(`Shot ${s.n}: "in" and "out" must be seconds with in < out.`) if (outT > info.duration + 0.05) fail(`Shot ${s.n}: "out" (${outT}s) is past the end of the clip (${info.duration.toFixed(2)}s).`) outT = Math.min(outT, info.duration) return { shot: s, path, info, inT, outT, len: outT - inT } }) const W = Math.min(...clips.map((c) => c.info.width)) const H = Math.min(...clips.map((c) => c.info.height)) if (new Set(clips.map((c) => (c.info.width / c.info.height).toFixed(2))).size > 1) fail('The clips have different aspect ratios. Generate every shot at 9:16.') if (audioMode === 'clips') for (const c of clips) if (!c.info.hasAudio) fail(`Shot ${c.shot.n} has no audio. Regenerate it with generate_audio: true, or use --audio none for a silent preview.`) let outW = W let outH = H if (crop !== 'none') { const [a, b] = crop.split(':').map(Number) if (W / H > a / b) { outH = even(H); outW = even((H * a) / b) } else { outW = even(W); outH = even((W * b) / a) } } const total = clips.reduce((a, c) => a + c.len, 0) if (loopTail > 0 && loopTail > clips[clips.length - 1].len / 2) fail(`--loop-tail ${loopTail} is more than half of the last shot. Use 0.3-0.8.`) const inputs = [] const graph = [] clips.forEach((c, i) => { inputs.push('-i', c.path) const label = i === 0 && loopTail > 0 ? 's0pre' : `s${i}` graph.push(`[${i}:v]trim=start=${f3(c.inT)}:end=${f3(c.outT)},setpts=PTS-STARTPTS,fps=24,scale=${W}:${H}:flags=lanczos,setsar=1,format=yuv420p,settb=1/24[${label}]`) }) if (loopTail > 0) { // A still of shot 1's first frame, long enough to cover the dissolve and the final frames. graph.push(`[s0pre]split=2[s0][lf0]`) graph.push(`[lf0]trim=end_frame=1,tpad=stop_mode=clone:stop_duration=${f3(loopTail + 3 * FRAME)},setpts=PTS-STARTPTS,settb=1/24[loopframe]`) } const starts = [] let acc = 0 for (const c of clips) { starts.push(acc); acc += c.len } graph.push(`${clips.map((_, i) => `[s${i}]`).join('')}concat=n=${clips.length}:v=1:a=0,settb=1/24[cat]`) let chain = 'cat' const loopAt = total - loopTail - FRAME if (loopTail > 0) { // The dissolve ends one frame before the end, so the last frame is exactly shot 1's first frame. graph.push(`[cat][loopframe]xfade=transition=fade:duration=${f3(loopTail)}:offset=${f3(loopAt)},settb=1/24[looped]`) chain = 'looped' } const post = [GRADES[grade]] if (crop !== 'none') post.push(`crop=${outW}:${outH}:(iw-${outW})/2:(ih-${outH})/2`) post.push('format=yuv420p') graph.push(`[${chain}]${post.join(',')}[vout]`) let audioLabel = null const audioJoins = [] if (audioMode === 'clips') { clips.forEach((c, i) => { // Each clip after the first starts a little early, so the crossfade overlaps sound the picture never // shows and the cut sounds stay exactly in sync with the picture. const pre = i === 0 ? 0 : Math.min(xfade, c.inT) audioJoins.push(pre) graph.push(`[${i}:a]atrim=start=${f3(c.inT - pre)}:end=${f3(c.outT)},asetpts=PTS-STARTPTS,aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo[ca${i}]`) }) let a = 'ca0' for (let i = 1; i < clips.length; i++) { const d = audioJoins[i] graph.push(d >= 0.01 ? `[${a}][ca${i}]acrossfade=d=${f3(d)}:c1=tri:c2=tri[aj${i}]` : `[${a}][ca${i}]concat=n=2:v=0:a=1[aj${i}]`) a = `aj${i}` } const tail = loopTail > 0 ? `afade=t=out:st=${f3(loopAt)}:d=${f3(loopTail)}` : `afade=t=out:st=${f3(Math.max(0, total - 0.3))}:d=0.3` graph.push(`[${a}]afade=t=in:d=0.03,apad=whole_dur=${f3(total)},atrim=0:${f3(total)},${tail},volume=${gain}dB[aout]`) audioLabel = 'aout' } const common = ['-y', '-hide_banner', '-v', 'error', ...inputs, '-filter_complex', graph.join(';'), '-map', '[vout]'] const video = ['-c:v', 'libx264', '-preset', 'slow', '-profile:v', 'high', '-pix_fmt', 'yuv420p', '-r', '24'] const audio = audioLabel ? ['-map', `[${audioLabel}]`, '-c:a', 'aac', '-b:a', '192k'] : ['-an'] const tailArgs = ['-t', f3(total), '-movflags', '+faststart'] if (opts['dry-run']) { console.log(`Filter graph:\n${graph.join(';\n')}\n\nDuration: ${total.toFixed(2)} s, ${outW}x${outH}, loop tail ${loopTail} s, audio ${audioMode}`) process.exit(0) } const tmp = mkdtempSync(join(tmpdir(), 'assemble-reel-')) if (maxMb) { const audioKbps = audioLabel ? 192 : 0 const videoKbps = Math.floor((maxMb * 8 * 1024 * 0.94) / total - audioKbps) if (videoKbps < 600) fail(`--max-mb ${maxMb} is too small for ${total.toFixed(1)} s. Raise it.`) const passlog = join(tmp, 'pass') run('ffmpeg', [...common, ...video, '-b:v', `${videoKbps}k`, '-pass', '1', '-passlogfile', passlog, '-an', '-t', f3(total), '-f', 'mp4', process.platform === 'win32' ? 'NUL' : '/dev/null'], 'ffmpeg pass 1') run('ffmpeg', [...common, ...video, '-b:v', `${videoKbps}k`, '-maxrate', `${Math.round(videoKbps * 1.5)}k`, '-bufsize', `${videoKbps * 2}k`, '-pass', '2', '-passlogfile', passlog, ...audio, ...tailArgs, opts.out], 'ffmpeg pass 2') } else { run('ffmpeg', [...common, ...video, '-crf', String(crf), ...audio, ...tailArgs, opts.out], 'ffmpeg') } rmSync(tmp, { recursive: true, force: true }) const edl = { file: opts.out, width: outW, height: outH, duration: Number(total.toFixed(3)), grade, crop, audio: audioMode, loop_tail: loopTail, shots: clips.map((c, i) => ({ n: c.shot.n, beat: c.shot.beat, start: Number(starts[i].toFixed(3)), length: Number(c.len.toFixed(3)), in: c.inT, out: Number(c.outT.toFixed(3)) })), joins: [ ...starts.slice(1).map((at, i) => ({ at: Number(at.toFixed(3)), type: 'cut', d: 0, audio_crossfade: Number((audioJoins[i + 1] ?? 0).toFixed(3)) })), ...(loopTail > 0 ? [{ at: Number(loopAt.toFixed(3)), type: 'loop', d: loopTail }] : []), ], } writeFileSync(`${opts.out}.edl.json`, JSON.stringify(edl, null, 2)) const mb = statSync(opts.out).size / 1024 / 1024 console.log(`${opts.out} ${outW}x${outH} ${total.toFixed(2)} s ${mb.toFixed(2)} MB grade=${grade} audio=${audioMode} loop=${loopTail > 0 ? `${loopTail} s` : 'off'}\nEdit list: ${opts.out}.edl.json\nNext: node scripts/check-reel.mjs --file ${opts.out} --edl ${opts.out}.edl.json${loopTail > 0 ? ' --loop' : ''}`)