#!/usr/bin/env node
// Local finish for one generated clip, with ffmpeg: optional trim, scale to 720 x 1280, the model's room sound
// kept with a short fade in and out (or muted), web-ready H.264 with +faststart, an optional exact caption drawn
// locally (never by the video model), and a poster still. No API, no cost.
//
// Usage:
// node scripts/finish-clip.mjs --input raw.mp4 --output pet-barista.mp4 [--poster poster.jpg] [--poster-at 6.5]
// [--caption "exact caption"] [--caption-y 0.15] [--trim-start 0] [--duration 8] [--size 720x1280] [--muted]
//
// The caption needs sharp (npm i sharp in the folder you run from); nothing else does. It is a white box with
// black bold text, at most 2 lines and 56 characters, at --caption-y (a fraction of the height from the top,
// 0.12 to 0.6) so it stays clear of the app's top bar and bottom caption area.
// Node 18 or newer, ffmpeg 5 or newer.
import { spawnSync } from 'node:child_process'
import { existsSync, mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const usage = 'Usage: node scripts/finish-clip.mjs --input raw.mp4 --output clip.mp4 [--poster poster.jpg] [--poster-at 6.5] [--caption "..."] [--caption-y 0.15] [--trim-start 0] [--duration 8] [--size 720x1280] [--muted]'
const VALUE_FLAGS = new Set(['input', 'output', 'poster', 'poster-at', 'caption', 'caption-y', 'trim-start', 'duration', 'size'])
const SWITCHES = new Set(['muted'])
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 (SWITCHES.has(key)) {
options[key] = true
continue
}
if (!VALUE_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 (!/\.mp4$/i.test(options.output)) fail('--output must end in .mp4.')
if (options.poster && !/\.(jpe?g|png|webp)$/i.test(options.poster)) fail('--poster must end in .jpg, .png or .webp.')
if (!existsSync(options.input)) fail(`Not found: ${options.input}`)
const num = (key, fallback, min, max) => {
const value = Number(options[key] ?? fallback)
if (!Number.isFinite(value) || value < min || value > max) fail(`--${key} must be a number from ${min} to ${max}.`)
return value
}
const size = (options.size ?? '720x1280').match(/^(\d{3,4})x(\d{3,4})$/)
if (!size) fail('--size must look like 720x1280.')
const [width, height] = [Number(size[1]), Number(size[2])]
if (width % 2 || height % 2) fail('--size needs even numbers, like 720x1280.')
const probe = spawnSync('ffprobe', ['-v', 'error', '-print_format', 'json', '-show_format', '-show_streams', options.input], { encoding: 'utf8' })
if (probe.error) fail('ffprobe was not found. Install ffmpeg 5 or newer (it includes ffprobe).')
if (probe.status !== 0) fail(`${options.input} is not a video ffmpeg can read. Pass the MP4 the video model returned.`)
const info = JSON.parse(probe.stdout)
const stream = (info.streams ?? []).find((s) => s.codec_type === 'video')
const hasAudio = (info.streams ?? []).some((s) => s.codec_type === 'audio')
const total = Number(info.format?.duration)
if (!stream || ['mjpeg', 'png', 'webp', 'bmp', 'gif'].includes(stream.codec_name) || !(total >= 1)) {
fail(`${options.input} is a still image or too short to be a clip. finish-clip.mjs needs the generated video (.mp4).`)
}
if (Math.abs(stream.width / stream.height - width / height) > 0.03) {
console.error(`warning: the clip is ${stream.width}x${stream.height}, not the shape of ${width}x${height}. It will be cropped to fill; check the edges.`)
}
const start = num('trim-start', 0, 0, 30)
const length = Math.min(num('duration', total - start, 0.5, 60), total - start)
if (!(length > 0)) fail('Nothing left after --trim-start.')
const muted = Boolean(options.muted) || !hasAudio
if (!options.muted && !hasAudio) console.error('note: the clip has no sound track, so the result is silent. Generate the final with sound for room tone.')
// The caption is drawn as a PNG with sharp and laid over the video, so its spelling is exact.
const work = mkdtempSync(join(tmpdir(), 'pet-clip-'))
let captionPng = ''
const caption = (options.caption ?? '').trim()
if (caption) {
if (caption.length > 56) fail(`--caption is ${caption.length} characters. Keep it to 56 or fewer (two short lines).`)
if (/\p{Extended_Pictographic}/u.test(caption)) fail('--caption cannot hold emoji: they do not render reliably. Use words, or add emoji in the app.')
const y = num('caption-y', 0.15, 0.12, 0.6)
let sharp
try {
sharp = (await import('sharp')).default
} catch {
fail('The caption needs sharp. Run "npm i sharp" in the folder you run this from, or leave out --caption and add the text in the app.')
}
const fontSize = Math.round(width * 0.055)
const maxChars = Math.floor((width * 0.86) / (fontSize * 0.56))
const lines = []
let line = ''
for (const word of caption.split(/\s+/)) {
const next = line ? `${line} ${word}` : word
if (next.length > maxChars && line) {
lines.push(line)
line = word
} else line = next
}
if (line) lines.push(line)
if (lines.length > 2) fail(`--caption needs ${lines.length} lines at this size. Shorten it to two lines (about ${maxChars} characters each).`)
const escapeXml = (v) => v.replace(/[<>&'"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' })[c])
const lineHeight = Math.round(fontSize * 1.3)
const padX = Math.round(fontSize * 0.5)
const padY = Math.round(fontSize * 0.32)
const longest = Math.max(...lines.map((l) => l.length))
const boxW = Math.min(Math.round(width * 0.94), Math.round(longest * fontSize * 0.56 + padX * 2))
const boxH = lines.length * lineHeight + padY * 2
const top = Math.round(height * y)
const left = Math.round((width - boxW) / 2)
const text = lines
.map((l, i) => `${escapeXml(l)}`)
.join('')
const svg = ``
captionPng = join(work, 'caption.png')
await sharp(Buffer.from(svg)).png().toFile(captionPng)
}
const scale = `scale=${width}:${height}:force_original_aspect_ratio=increase,crop=${width}:${height},setsar=1`
const filter = captionPng ? `[0:v]${scale}[v];[v][1:v]overlay=0:0,format=yuv420p[out]` : `[0:v]${scale},format=yuv420p[out]`
const audioArgs = muted
? ['-an']
: ['-map', '0:a:0', '-af', `afade=t=in:st=0:d=0.1,afade=t=out:st=${Math.max(0, length - 0.3).toFixed(3)}:d=0.3`, '-c:a', 'aac', '-b:a', '160k']
const run = spawnSync('ffmpeg', [
'-y', '-v', 'error', '-ss', String(start), '-t', length.toFixed(3), '-i', options.input,
...(captionPng ? ['-i', captionPng] : []),
'-filter_complex', filter, '-map', '[out]',
'-c:v', 'libx264', '-profile:v', 'high', '-preset', 'slow', '-crf', '20', '-maxrate', '6M', '-bufsize', '12M', '-pix_fmt', 'yuv420p',
...audioArgs,
'-movflags', '+faststart', options.output,
], { stdio: 'inherit' })
rmSync(work, { recursive: true, force: true })
if (run.error) fail('ffmpeg was not found. Install ffmpeg 5 or newer.')
if (run.status !== 0) fail('ffmpeg could not finish this clip; see its error above.')
console.log(`${options.output} (${length.toFixed(2)} s, ${width}x${height}${muted ? ', muted' : ', with sound'}${caption ? ', caption drawn locally' : ''})`)
if (options.poster) {
const at = Math.min(num('poster-at', 6.5, 0, 60), Math.max(0, length - 0.1))
const still = spawnSync('ffmpeg', ['-y', '-v', 'error', '-ss', at.toFixed(3), '-i', options.output, '-frames:v', '1', '-q:v', '2', options.poster], { stdio: 'inherit' })
if (still.status !== 0) fail('ffmpeg could not save the poster frame.')
console.log(`${options.poster} (still at ${at.toFixed(1)} s: pick the moment the pet looks at the camera)`)
}