import { rename, stat } from 'node:fs/promises'; import { spawn } from 'node:child_process'; import { resolve } from 'node:path'; // Creates 1080p60 website copies of the delivered 4K videos. Originals are never modified. // Copies go to the Expand-drive folder that mirrors the R2 bucket; upload them from there // (see "Website videos" in README.md). Filesystem paths are used only by this local script. const outputDirectory = process.env.PULSECANVAS_ASSET_DIR || '/path/to/file'; const websiteMediaDirectory = process.env.PULSECANVAS_WEBSITE_MEDIA_DIR || '/Volumes/Expand/Video/Library/website-media/pulsecanvas'; const videos = [ { source: 'PulseCanvas-Marketing-4K-Revised.mp4', destination: 'PulseCanvas-Feature.mp4', video: ['-crf', '21', '-maxrate', '9000k', '-bufsize', '18000k'], audio: '192k', }, { source: 'PulseCanvas-Walkthrough-4K-Revised.mp4', destination: 'PulseCanvas-Walkthrough-web.mp4', video: ['-crf', '23', '-maxrate', '4500k', '-bufsize', '9000k'], audio: '160k', }, ]; function run(args) { return new Promise((accept, reject) => { const process = spawn('ffmpeg', args, { stdio: 'inherit' }); process.on('error', reject); process.on('exit', (code) => (code === 0 ? accept() : reject(new Error(`FFmpeg exited ${code}`)))); }); } for (const { source, destination, video, audio } of videos) { const input = resolve(outputDirectory, source); const output = resolve(websiteMediaDirectory, destination); if (!(await stat(input).catch(() => null))) { console.log(`${destination}: no source available.`); continue; } // Published R2 copies are cached for a year, so a new cut needs a new destination name. if (await stat(output).catch(() => null)) { console.log(`${destination}: already exists; give a new cut a new destination name.`); continue; } const temporary = output.replace(/\.mp4$/, '.tmp.mp4'); await run([ '-y', '-hide_banner', '-loglevel', 'error', '-i', input, '-vf', 'scale=1920:1080:flags=lanczos,format=yuv420p', '-c:v', 'libx264', '-preset', 'slow', ...video, '-g', '120', '-profile:v', 'high', '-c:a', 'aac', '-b:a', audio, '-movflags', '+faststart', temporary, ]); await rename(temporary, output); console.log(`${destination}: website copy created; original preserved.`); }