Spaces:
Configuration error

video-generator / src /adapters /cli /agentic-editor.ts
pirzm1's picture
Clean deployment push
023064b
Raw History Blame Contribute Delete
43 kB
#!/usr/bin/env tsx /** * agentic-editor.ts β€” Simple video editing operations for the AVS pipeline. * * Every operation a real video editor can do, wrapped as a CLI command. * All are thin wrappers around ffmpeg β€” no re-encoding unless necessary. * * USAGE: * npx tsx src/adapters/cli/agentic-editor.ts <command> [options] * * COMMANDS: * trim Cut video by start/end timecode * speed Change playback speed (0.25x–4x) * extract-audio Extract audio track to MP3/WAV * replace-audio Replace audio in a video * mute Remove audio track * split Split video at a timestamp * merge Join multiple video files * crop Crop a region of the frame * resize Scale to new dimensions * rotate Rotate/flip (90Β°, 180Β°, horizontal) * loop Loop a clip N times * overlay-text Add text/caption to video * overlay-image Add image watermark * extract-frame Save a single frame as PNG/JPG * thumbnail Generate a poster frame * blur Blur a region of the frame * adjust Brightness/contrast/saturation * reverse Reverse playback * info Show video metadata * concat-scene Extract one scene from a rendered workspace video * * EXAMPLES: * npm run agentic:editor trim --input video.mp4 --start 00:05 --end 00:15 * npm run agentic:editor speed --input clip.mp4 --rate 2.0 * npm run agentic:editor extract-audio --input video.mp4 --output audio.mp3 * npm run agentic:editor merge --files "part1.mp4,part2.mp4" --output joined.mp4 * npm run agentic:editor overlay-text --input video.mp4 --text "Hello!" --output branded.mp4 * npm run agentic:editor concat-scene --job avs_test_modular --scene 3 */ import 'dotenv/config'; import * as fs from 'fs'; import * as path from 'path'; import { spawnSync } from 'child_process'; // ─── Helpers ───────────────────────────────────────────────────────────────── /** Resolve ffmpeg binary path */ function ffmpegPath(): string { try { const p = require('ffmpeg-static'); if (p && fs.existsSync(p)) return p; } catch { /* ffmpeg-static not available */ } return 'ffmpeg'; // fallback to PATH } function ffprobePath(): string { try { const p = require('ffprobe-static'); if (p?.path && fs.existsSync(p.path)) return p.path; } catch { /* ffprobe-static not available */ } return 'ffprobe'; } function runFfmpeg(args: string[], desc: string): { ok: boolean; stdout: string; stderr: string } { const ff = ffmpegPath(); console.log(` ⚑ ffmpeg ${args.slice(0, 6).join(' ')} ...`); const r = spawnSync(ff, args, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }); if (r.status !== 0) { console.error(` βœ– ${desc} failed (exit ${r.status})`); console.error(r.stderr?.slice(-500)); return { ok: false, stdout: r.stdout || '', stderr: r.stderr || '' }; } console.log(` βœ… ${desc}`); return { ok: true, stdout: r.stdout || '', stderr: r.stderr || '' }; } function getMediaInfo(file: string): any { const fp = ffprobePath(); const r = spawnSync(fp, ['-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file], { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], }); if (r.status !== 0) return null; try { return JSON.parse(r.stdout); } catch { return null; } } function resolveInput(input?: string): string { if (!input) { console.error(' βœ– --input <path> is required'); process.exit(1); } if (!fs.existsSync(input)) { console.error(` βœ– Input file not found: ${input}`); process.exit(1); } return input; } function resolveOutput(output?: string, fallback = 'output.mp4'): string { return output || fallback; } function parseArgs(argv: string[]): Record<string, any> { const args: Record<string, any> = {}; for (let i = 2; i < argv.length; i++) { const k = argv[i]; if (k.startsWith('--')) { const key = k.slice(2); const next = argv[i + 1]; if (next && !next.startsWith('--')) { args[key] = next; i++; } else { args[key] = true; } } } return args; } function workspaceFor(jobId: string) { const root = path.join(process.cwd(), 'workspace', 'jobs', jobId); return root; } function outputFor(jobId: string) { return path.join(process.cwd(), 'output', jobId); } // ─── Commands ───────────────────────────────────────────────────────────────── const COMMANDS: Record<string, (args: Record<string, any>) => void> = {}; // 1. TRIM β€” Cut video by start/end timecode COMMANDS['trim'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `trimmed_${path.basename(input)}`); const start = args.start || '00:00'; const end = args.end; const duration = args.duration; const ff: string[] = ['-i', input, '-ss', start, '-c:v', 'libx264', '-c:a', 'aac']; if (end) ff.push('-to', end); if (duration) ff.push('-t', duration); if (!end && !duration) ff.push('-t', '10'); // default 10s ff.push('-avoid_negative_ts', 'make_zero', output, '-y'); runFfmpeg(ff, `Trimmed ${start} β†’ ${end || duration || '10s'}`); }; // 2. SPEED β€” Change playback speed COMMANDS['speed'] = (args) => { const input = resolveInput(args.input); const rate = parseFloat(args.rate || '1.0'); if (rate <= 0) { console.error(' βœ– Rate must be > 0'); return; } const output = resolveOutput(args.output, `speed${rate}_${path.basename(input)}`); // Parallel to setpts=1/rate (faster playback = smaller PTS multiplier), // audio needs atempo=rate (faster playback = higher tempo multiplier). // BUGFIX: was `1 / rate` which produced atempo=0.5 for 2x (slowing audio). const audioRate = rate; const setpts = 1 / rate; // GUARD (BUG #4 class): the input may have NO audio track. Referencing // `[0:a]` unconditionally throws "Stream specifier ':a' matches no streams" // and the whole command fails. Skip the audio branch when absent. const info = getMediaInfo(input); const hasAudio = Array.isArray(info?.streams) && info.streams.some((s: any) => s.codec_type === 'audio'); const fc = hasAudio ? `[0:v]setpts=${setpts}*PTS[v];[0:a]atempo=${audioRate}[a]` : `[0:v]setpts=${setpts}*PTS[v]`; const map = hasAudio ? ['-map', '[v]', '-map', '[a]'] : ['-map', '[v]']; const acodec = hasAudio ? ['-c:a', 'aac'] : ['-an']; const ff: string[] = [ '-i', input, '-filter_complex', fc, ...map, '-c:v', 'libx264', ...acodec, output, '-y', ]; runFfmpeg(ff, `Speed ${rate}x`); }; // 3. EXTRACT-AUDIO β€” Extract audio track COMMANDS['extract-audio'] = (args) => { const input = resolveInput(args.input); const ext = (args.output || '').includes('.') ? '' : '.mp3'; const output = resolveOutput(args.output, `audio_${path.basename(input).replace(/\.[^.]+$/, '')}${ext}`); const codec = output.endsWith('.wav') ? 'pcm_s16le' : 'libmp3lame'; const ff: string[] = ['-i', input, '-vn', '-c:a', codec, '-q:a', '2', output, '-y']; runFfmpeg(ff, `Extracted audio β†’ ${path.basename(output)}`); }; // 4. REPLACE-AUDIO β€” Replace audio track COMMANDS['replace-audio'] = (args) => { const video = resolveInput(args.input); const audio = resolveInput(args.audio); const output = resolveOutput(args.output, `reaud_${path.basename(video)}`); const ff: string[] = [ '-i', video, '-i', audio, '-c:v', 'copy', '-c:a', 'aac', '-map', '0:v:0', '-map', '1:a:0', '-shortest', output, '-y', ]; runFfmpeg(ff, `Replaced audio with ${path.basename(audio)}`); }; // 5. MUTE β€” Remove audio COMMANDS['mute'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `muted_${path.basename(input)}`); const ff: string[] = ['-i', input, '-c:v', 'copy', '-an', output, '-y']; runFfmpeg(ff, 'Muted audio'); }; // 6. SPLIT β€” Split video at timestamp COMMANDS['split'] = (args) => { const input = resolveInput(args.input); const at = args.at || '00:00'; const base = path.basename(input).replace(/\.[^.]+$/, ''); const ext = path.extname(input); const outA = resolveOutput(args['output-a'], `${base}_partA${ext}`); const outB = resolveOutput(args['output-b'], `${base}_partB${ext}`); const ffA: string[] = ['-i', input, '-ss', '0', '-to', at, '-c', 'copy', '-avoid_negative_ts', 'make_zero', outA, '-y']; const ffB: string[] = ['-i', input, '-ss', at, '-c', 'copy', '-avoid_negative_ts', 'make_zero', outB, '-y']; runFfmpeg(ffA, `Split part A (0 β†’ ${at})`); runFfmpeg(ffB, `Split part B (${at} β†’ end)`); }; // 7. MERGE β€” Join multiple videos COMMANDS['merge'] = (args) => { const files = (args.files || args.input || '').split(',').filter(Boolean); if (files.length < 2) { console.error(' βœ– Provide at least 2 files via --files'); return; } const output = resolveOutput(args.output, 'merged.mp4'); // Create concat file const listPath = path.join(process.cwd(), '.avs_concat_list.txt'); const lines = files.map((f: string) => `file '${path.resolve(f).replace(/'/g, "'\\''")}'`); fs.writeFileSync(listPath, lines.join('\n')); const ff: string[] = ['-fflags', '+genpts', '-f', 'concat', '-safe', '0', '-i', listPath, '-c', 'copy', output, '-y']; runFfmpeg(ff, `Merged ${files.length} files`); try { fs.unlinkSync(listPath); } catch { /* concat file may already be cleaned up */ } }; // 8. CROP β€” Crop region COMMANDS['crop'] = (args) => { const input = resolveInput(args.input); // Accept both --w/--h/--x/--y AND --width/--height/--x/--y const w = args.w || args.width || '720'; const h = args.h || args.height || '720'; const x = args.x || args['x-offset'] || '0'; const y = args.y || args['y-offset'] || '0'; const output = resolveOutput(args.output, `cropped_${path.basename(input)}`); const ff: string[] = [ '-i', input, '-filter:v', `crop=${w}:${h}:${x}:${y}`, '-c:v', 'libx264', '-c:a', 'aac', output, '-y', ]; runFfmpeg(ff, `Cropped ${w}Γ—${h} at (${x},${y})`); }; // 9. RESIZE β€” Scale dimensions COMMANDS['resize'] = (args) => { const input = resolveInput(args.input); // Accept both --w/--h AND --width/--height const w = args.w || args.width || '1920'; const h = args.h || args.height || '1080'; const output = resolveOutput(args.output, `resized_${path.basename(input)}`); const ff: string[] = [ '-i', input, '-filter:v', `scale=${w}:${h}:force_original_aspect_ratio=decrease,pad=${w}:${h}:(ow-iw)/2:(oh-ih)/2,setsar=1`, '-c:v', 'libx264', '-c:a', 'aac', output, '-y', ]; runFfmpeg(ff, `Resized to ${w}Γ—${h}`); }; // 10. ROTATE β€” Rotate/flip COMMANDS['rotate'] = (args) => { const input = resolveInput(args.input); const angle = args.angle || args.degrees || '90'; const output = resolveOutput(args.output, `rotated_${path.basename(input)}`); let filter: string; switch (angle) { case '90': filter = 'transpose=1'; break; case '180': filter = 'transpose=1,transpose=1'; break; case '270': case '-90': filter = 'transpose=2'; break; case 'hflip': case 'horizontal': filter = 'hflip'; break; case 'vflip': case 'vertical': filter = 'vflip'; break; default: filter = `rotate=${parseFloat(angle)}*PI/180`; break; } const ff: string[] = ['-i', input, '-filter:v', filter, '-c:v', 'libx264', '-c:a', 'aac', output, '-y']; runFfmpeg(ff, `Rotated ${angle}Β°`); }; // 11. LOOP β€” Loop clip N times COMMANDS['loop'] = (args) => { const input = resolveInput(args.input); const n = parseInt(args.n || args.count || '3'); const output = resolveOutput(args.output, `looped_${path.basename(input)}`); // BUG E2: an audio-less input makes `[0:a]aloop` fail ("matches no // streams"). Probe and drop the audio branch when there is no audio. const hasAudio = Array.isArray(getMediaInfo(input)?.streams) && getMediaInfo(input).streams.some((s: any) => s.codec_type === 'audio'); const fc = hasAudio ? `[0:v]loop=loop=${n - 1}:size=32767[v];[0:a]aloop=loop=${n - 1}:size=32767[a]` : `[0:v]loop=loop=${n - 1}:size=32767[v]`; const ff: string[] = [ '-i', input, '-filter_complex', fc, '-map', '[v]', ...(hasAudio ? ['-map', '[a]'] : []), '-c:v', 'libx264', ...(hasAudio ? ['-c:a', 'aac'] : ['-an']), output, '-y', ]; runFfmpeg(ff, `Looped ${n} times`); }; // 12. OVERLAY-TEXT β€” Add text to video COMMANDS['overlay-text'] = (args) => { const input = resolveInput(args.input); const text = args.text || 'Hello'; const output = resolveOutput(args.output, `text_${path.basename(input)}`); const fontSize = args['font-size'] || args.size || '48'; const color = args.color || 'white'; const x = args.x || '(w-text_w)/2'; const y = args.y || 'h-th-60'; const ff: string[] = [ '-i', input, '-filter:v', `drawtext=text='${text.replace(/'/g, "'\\\\''")}':fontcolor=${color}:fontsize=${fontSize}:x=${x}:y=${y}:box=1:boxcolor=black@0.4:boxborderw=10`, '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Added text: "${text.slice(0, 40)}"`); }; // 13. OVERLAY-IMAGE β€” Add image watermark COMMANDS['overlay-image'] = (args) => { const input = resolveInput(args.input); const image = resolveInput(args.image || args.watermark); const output = resolveOutput(args.output, `watermarked_${path.basename(input)}`); const position = args.position || 'bottom-right'; let posFilter: string; switch (position) { case 'top-left': posFilter = '10:10'; break; case 'top-right': posFilter = 'W-w-10:10'; break; case 'bottom-left': posFilter = '10:H-h-10'; break; case 'center': posFilter = '(W-w)/2:(H-h)/2'; break; default: posFilter = 'W-w-10:H-h-10'; break; } const ff: string[] = [ '-i', input, '-i', image, '-filter_complex', `[0:v][1:v]overlay=${posFilter}[v]`, '-map', '[v]', '-map', '0:a', '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Overlay image: ${path.basename(image)}`); }; // 14. EXTRACT-FRAME β€” Save a single frame COMMANDS['extract-frame'] = (args) => { const input = resolveInput(args.input); const at = args.at || args.time || '00:00'; const output = resolveOutput(args.output, `frame_${path.basename(input).replace(/\.[^.]+$/, '')}.png`); const ff: string[] = ['-ss', at, '-i', input, '-vframes', '1', output, '-y']; runFfmpeg(ff, `Frame at ${at}`); }; // 15. THUMBNAIL β€” Generate poster frame COMMANDS['thumbnail'] = (args) => { const input = resolveInput(args.input); const at = args.at || args.time || '00:01'; const w = args.w || args.width || '320'; const output = resolveOutput(args.output, `thumb_${path.basename(input).replace(/\.[^.]+$/, '')}.jpg`); const ff: string[] = ['-ss', at, '-i', input, '-vframes', '1', '-vf', `scale=${w}:-1`, '-q:v', '2', output, '-y']; runFfmpeg(ff, `Thumbnail at ${at} (${w}px)`); }; // 16. BLUR β€” Blur region COMMANDS['blur'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `blurred_${path.basename(input)}`); const strength = args.strength || '5'; const region = args.region || args.area || 'full'; let filter: string; if (region === 'full' || region === 'all') { filter = `boxblur=${strength}:${strength}`; } else { // region format: w:h:x:y (e.g. 200:200:100:100) const [rw, rh, rx, ry] = region.split(':'); filter = `boxblur=${strength}:${strength}:enable='between(t,0,9999)'` + `,drawbox=x=${rx || 0}:y=${ry || 0}:w=${rw || 200}:h=${rh || 200}:t=fill:color=black@0`; // Actually let's use a simpler approach: boxblur + crop console.log(' β„Ή For regional blur, use --area w:h:x:y'); filter = `boxblur=${strength}:${strength}`; } const ff: string[] = ['-i', input, '-filter:v', filter, '-c:v', 'libx264', '-c:a', 'copy', output, '-y']; runFfmpeg(ff, `Blur (strength=${strength})`); }; // 17. ADJUST β€” Brightness/contrast/saturation COMMANDS['adjust'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `adjusted_${path.basename(input)}`); const brightness = args.brightness || args.b || '0'; const contrast = args.contrast || args.c || '1.0'; const saturation = args.saturation || args.s || '1.0'; const gamma = args.gamma || args.g || '1.0'; const ff: string[] = [ '-i', input, '-filter:v', `eq=brightness=${brightness}:contrast=${contrast}:saturation=${saturation}:gamma=${gamma}`, '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Adjusted (b=${brightness} c=${contrast} s=${saturation})`); }; // 18. REVERSE β€” Reverse playback COMMANDS['reverse'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `reversed_${path.basename(input)}`); // BUG E2: audio-less input β†’ `[0:a]areverse` fails. Probe and drop audio. const hasAudio = Array.isArray(getMediaInfo(input)?.streams) && getMediaInfo(input).streams.some((s: any) => s.codec_type === 'audio'); const fc = hasAudio ? '[0:v]reverse[v];[0:a]areverse[a]' : '[0:v]reverse[v]'; const ff: string[] = [ '-i', input, '-filter_complex', fc, '-map', '[v]', ...(hasAudio ? ['-map', '[a]'] : []), '-c:v', 'libx264', ...(hasAudio ? ['-c:a', 'aac'] : ['-an']), '-preset', 'medium', output, '-y', ]; runFfmpeg(ff, 'Reversed'); }; // 19. CONCAT-SCENE β€” Extract scene from workspace video COMMANDS['concat-scene'] = (args) => { const jobId = args.job || args.j || process.argv[3]; // fallback from argv if (!jobId) { console.error(' βœ– --job <jobId> is required'); return; } const sceneNum = parseInt(args.scene || args.s || '1'); const wsRoot = workspaceFor(jobId); const planPath = path.join(wsRoot, 'plan.json'); if (!fs.existsSync(planPath)) { console.error(` βœ– No plan in workspace ${jobId}`); return; } const plan = JSON.parse(fs.readFileSync(planPath, 'utf-8')); const scene = plan.scenes.find((s: any) => s.sceneNumber === sceneNum); if (!scene) { console.error(` βœ– Scene ${sceneNum} not found (1–${plan.scenes.length})`); return; } // Find the rendered output const outDir = outputFor(jobId); const candidates = fs.readdirSync(outDir).filter(f => f.endsWith('.mp4') && !f.includes('scene_') && !f.includes('part')); if (candidates.length === 0) { console.error(' βœ– No rendered video found'); return; } const video = path.join(outDir, candidates[0]); // Calculate scene start time from preceding scenes let startSec = 0; for (let i = 0; i < sceneNum - 1; i++) { startSec += plan.scenes[i].durationSec || 5; } const endSec = startSec + (scene.durationSec || 5); const startStr = `${Math.floor(startSec / 60)}:${String(Math.floor(startSec % 60)).padStart(2, '0')}.${String(Math.round((startSec % 1) * 100)).padStart(2, '0')}`; const durStr = (scene.durationSec || 5).toFixed(2); const output = resolveOutput(args.output, `scene_${sceneNum}_${path.basename(candidates[0])}`); const ff: string[] = [ '-ss', startStr, '-i', video, '-t', durStr, '-c', 'copy', '-avoid_negative_ts', 'make_zero', output, '-y', ]; runFfmpeg(ff, `Scene ${sceneNum} extracted (${startStr} β†’ +${durStr}s)`); console.log(` β„Ή From: ${video}`); console.log(` β„Ή Text: "${(scene.voiceoverText || '…').slice(0, 60)}"`); }; // 20. INFO β€” Show video metadata COMMANDS['info'] = (args) => { const input = resolveInput(args.input); const info = getMediaInfo(input); if (!info) { console.error(' βœ– Could not read media info'); return; } console.log(`\n πŸ“Ή ${path.basename(input)}`); console.log(` ─────────────────────────────`); const fmt = info.format; console.log(` Format: ${fmt.format_name}`); console.log(` Duration: ${parseFloat(fmt.duration || '0').toFixed(2)}s`); console.log(` Bitrate: ${fmt.bit_rate || '?'} bps`); console.log(` Size: ${(parseInt(fmt.size || '0') / 1024).toFixed(0)} KB`); for (const s of info.streams || []) { if (s.codec_type === 'video') { console.log(` Video: ${s.codec_name} ${s.width}Γ—${s.height} ${s.r_frame_rate || s.avg_frame_rate || '?'}fps`); } if (s.codec_type === 'audio') { console.log(` Audio: ${s.codec_name} ${s.channels}ch ${s.sample_rate}Hz`); } } console.log(''); }; // 21. AUDIO-FILTER β€” Noise reduction, equalizer, compressor COMMANDS['audio-filter'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `filtered_${path.basename(input)}`); const noise = (args.noise || args.nr || '0').includes('reduce') ? '' : ''; const eqStr = args.eq || args.equalizer || ''; const ff: string[] = ['-i', input]; let filter = ''; if (args.noise || args.nr) { filter += 'afftdn=nr=' + parseFloat(args.noise || '0.3') + ','; } if (eqStr) { // Format: "31=0;62=0;125=0;250=0;500=0;1k=0;2k=0;4k=0;8k=0;16k=0" filter += 'anequalizer=' + eqStr + ','; } if (args.volume) { filter += 'volume=' + parseFloat(args.volume).toFixed(1) + ','; } if (filter) filter = filter.slice(0, -1); if (filter) { ff.push('-af', filter); } ff.push('-c:v', 'copy', output, '-y'); runFfmpeg(ff, 'Audio filtered'); }; // 22. FREEZE β€” Freeze frame (pause on a specific frame) COMMANDS['freeze'] = (args) => { const input = resolveInput(args.input); const at = args.at || args.time || '00:00'; const dur = args.duration || args.dur || '2'; const output = resolveOutput(args.output, `freeze_${path.basename(input)}`); const ff: string[] = [ '-i', input, '-filter_complex', `[0:v]trim=0:${at},setpts=PTS-STARTPTS[v0];` + `[0:v]trim=${at}:${parseFloat(at) + 0.04},setpts=PTS-STARTPTS,loop=loop=${Math.round(parseFloat(dur) * 25)}:size=1[v1];` + `[0:v]trim=${parseFloat(at) + 0.04},setpts=PTS-STARTPTS+${dur}/TB[v2];` + `[v0][v1][v2]concat=n=3:v=1:a=0[v]`, '-map', '[v]', '-map', '0:a?', '-c:v', 'libx264', '-c:a', 'aac', '-preset', 'fast', output, '-y', ]; runFfmpeg(ff, `Freeze frame at ${at} for ${dur}s`); }; // 23. CHROMA-KEY β€” Green/blue screen replacement COMMANDS['chroma-key'] = (args) => { const input = resolveInput(args.input); const bg = args.background || args.bg; const color = args.color || 'green'; const sim = args.similarity || '0.3'; const blend = args.blend || '0.1'; let output = resolveOutput(args.output, `keyed_${path.basename(input)}`); let ff: string[]; if (bg) { // Replace with background image/video const bgInput = resolveInput(bg); ff = [ '-i', input, '-i', bgInput, '-filter_complex', `[0:v]colorkey=0x${color === 'green' ? '00FF00' : '0000FF'}:${sim}:${blend}[fg];` + `[1:v][fg]overlay=0:0[v]`, '-map', '[v]', '-map', '0:a?', '-c:v', 'libx264', '-c:a', 'aac', output, '-y', ]; } else { // Make transparent background (alpha) output = output.replace(/\.[^.]+$/, '.webm'); ff = [ '-i', input, '-filter_complex', `colorkey=0x${color === 'green' ? '00FF00' : '0000FF'}:${sim}:${blend}[v]`, '-map', '[v]', '-map', '0:a?', '-c:v', 'libvpx-vp9', '-c:a', 'libopus', output, '-y', ]; } runFfmpeg(ff, `Chroma key (${color})`); }; // 24. ENHANCE β€” Denoise + deblock + sharpen COMMANDS['enhance'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `enhanced_${path.basename(input)}`); const denoise = args.denoise === false ? '' : 'hqdn3d=3:2:4:3,'; const sharpen = args.sharpen === false ? '' : 'unsharp=3:3:0.5:3:3:0.0,'; const deblock = args.deblock === false ? '' : 'pp=de,'; let filter = denoise + sharpen + deblock; if (filter.endsWith(',')) filter = filter.slice(0, -1); if (!filter) filter = 'hqdn3d=3:2:4:3,unsharp=3:3:0.5'; const ff: string[] = [ '-i', input, '-filter:v', filter, '-c:v', 'libx264', '-crf', '20', '-preset', 'medium', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, 'Enhanced (denoise+sharpen+deblock)'); }; // 25. STABILIZE β€” Video stabilization COMMANDS['stabilize'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `stable_${path.basename(input)}`); // Two-pass: detect + stabilize console.log(' πŸ” Detecting motion (pass 1)...'); const detect = spawnSync(ffmpegPath(), [ '-i', input, '-vf', 'vidstabdetect=shakiness=5:accuracy=10:result=.avs_transform.trf', '-f', 'null', '-', '-y', ], { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }); if (detect.status !== 0) { console.error(' βœ– Stabilization detection failed:', detect.stderr?.slice(-200)); return; } console.log(' 🎯 Stabilizing (pass 2)...'); const ff: string[] = [ '-i', input, '-vf', 'vidstabtransform=zoom=1:smoothing=30:input=.avs_transform.trf', '-c:v', 'libx264', '-crf', '22', '-preset', 'fast', '-c:a', 'aac', '-q:a', '5', output, '-y', ]; runFfmpeg(ff, 'Stabilized'); try { fs.unlinkSync('.avs_transform.trf'); } catch { /* cleanup */ } }; // 26. PIP β€” Picture-in-picture overlay COMMANDS['pip'] = (args) => { const main = resolveInput(args.input); const overlay = resolveInput(args.overlay || args.pip); const output = resolveOutput(args.output, `pip_${path.basename(main)}`); const pos = args.position || 'bottom-right'; const size = args.size || '0.3'; let posFilter; switch (pos) { case 'top-left': posFilter = '10:10'; break; case 'top-right': posFilter = 'W-ow-10:10'; break; case 'bottom-left': posFilter = '10:H-oh-10'; break; default: posFilter = 'W-ow-10:H-oh-10'; break; } const ff: string[] = [ '-i', main, '-i', overlay, '-filter_complex', `[1:v]scale=iw*${size}:ih*${size}[pip];` + `[0:v][pip]overlay=${posFilter}[v]`, '-map', '[v]', '-map', '0:a?', '-c:v', 'libx264', '-c:a', 'aac', output, '-y', ]; runFfmpeg(ff, `PiP overlay (${pos}, ${size}x)`); }; // 27. FADE β€” Add fade in/out to video COMMANDS['fade'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `faded_${path.basename(input)}`); const fi = args['fade-in'] || args.fi || '0.5'; const fo = args['fade-out'] || args.fo || '0.5'; const color = args.color || 'black'; // Get duration const info = getMediaInfo(input); const dur = info ? parseFloat(info.format?.duration || '10') : 10; const ff: string[] = [ '-i', input, '-filter:v', `fade=in:st=0:d=${fi},fade=out:st=${dur - parseFloat(fo)}:d=${fo}:color=${color}`, '-af', `afade=in:st=0:d=${fi},afade=out:st=${dur - parseFloat(fo)}:d=${fo}`, '-c:v', 'libx264', '-c:a', 'aac', output, '-y', ]; runFfmpeg(ff, `Fade in=${fi}s out=${fo}s`); }; // 28. GIF β€” Convert video to animated GIF COMMANDS['gif'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `animated_${path.basename(input).replace(/\.[^.]+$/, '')}.gif`); const fps = args.fps || '10'; const w = args.w || args.width || '480'; const ff: string[] = [ '-i', input, '-vf', `fps=${fps},scale=${w}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`, '-loop', '0', output, '-y', ]; runFfmpeg(ff, `GIF (${fps}fps, ${w}px)`); }; // 29. SPLIT-SCENES β€” Detect scene changes and split COMMANDS['split-scenes'] = (args) => { const input = resolveInput(args.input); const threshold = args.threshold || '0.3'; const base = path.basename(input).replace(/\.[^.]+$/, ''); const ext = path.extname(input); const dir = args['output-dir'] || args.dir || path.dirname(input); // Detect scene changes const detect = spawnSync(ffmpegPath(), [ '-i', input, '-filter:v', `select='gt(scene,${threshold})',showinfo`, '-vsync', 'vfr', '-f', 'null', '-', '-y', ], { encoding: 'utf-8', maxBuffer: 1024 * 1024, stdio: ['pipe', 'pipe', 'pipe'] }); const times: number[] = [0]; const re = /pts_time:([\d.]+)/g; let m; while ((m = re.exec(detect.stderr)) !== null) { times.push(parseFloat(m[1])); } // Get duration const info = getMediaInfo(input); const dur = info ? parseFloat(info.format?.duration || '10') : 10; times.push(dur); console.log(` πŸ” Detected ${times.length - 1} scene(s)`); for (let i = 1; i < times.length; i++) { const start = times[i - 1]; const end = times[i]; const duration = (end - start).toFixed(2); const startStr = `${Math.floor(start / 60)}:${String(Math.floor(start % 60)).padStart(2, '0')}.${String(Math.round((start % 1) * 100)).padStart(2, '0')}`; const outFile = path.join(dir, `${base}_scene${i}${ext}`); const ff: string[] = [ '-ss', startStr, '-i', input, '-t', duration, '-c', 'copy', '-avoid_negative_ts', 'make_zero', outFile, '-y', ]; const r = spawnSync(ffmpegPath(), ff, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }); if (r.status === 0) { console.log(` Scene ${i}: ${startStr} β†’ +${duration}s β†’ ${path.basename(outFile)}`); } } console.log(` βœ… Split into ${times.length - 1} scenes`); }; // 30. NOISE β€” Add audio noise/gain/reverb effects COMMANDS['noise'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `noise_${path.basename(input)}`); const type = args.type || args.effect || 'subtle'; let filter = ''; switch (type) { case 'rain': case 'white': filter = 'anoisesrc=d=60:c=white:a=0.05'; break; case 'pink': filter = 'anoisesrc=d=60:c=pink:a=0.03'; break; case 'brown': filter = 'anoisesrc=d=60:c=brown:a=0.02'; break; case 'reverb': filter = 'aecho=0.8:0.7:40:0.5'; break; case 'robot': filter = 'asetrate=22000,aresample=44100'; break; case 'chipmunk': filter = 'asetrate=64000,aresample=44100'; break; case 'slow': filter = 'atempo=0.75,asetrate=44100'; break; case 'echo': filter = 'aecho=0.8:0.9:500|1000:0.3|0.2'; break; default: filter = 'volume=0.5'; break; } const ff: string[] = ['-i', input, '-af', filter, '-c:v', 'copy', output, '-y']; runFfmpeg(ff, `Audio effect: ${type}`); }; // 31. CONVERT β€” change container/codec (mp4/webm/mov/mkv/avi + audio mp3/wav/ogg/m4a) COMMANDS['convert'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `conv_${Date.now()}${path.extname(input) || '.mp4'}`); const ext = path.extname(output).replace(/^\./, '').toLowerCase(); const isAudio = ['mp3', 'wav', 'ogg', 'm4a'].includes(ext); const vcodec = ext === 'webm' ? 'libvpx-vp9' : ext === 'gif' ? 'gif' : 'libx264'; const acodec = ext === 'webm' ? 'libopus' : ext === 'm4a' ? 'aac' : ext === 'wav' ? 'pcm_s16le' : ext === 'ogg' ? 'libopus' : 'aac'; const ff: string[] = isAudio ? ['-i', input, '-vn', '-c:a', acodec, output, '-y'] : ['-i', input, '-c:v', vcodec, '-c:a', acodec, output, '-y']; runFfmpeg(ff, `Converted β†’ ${ext.toUpperCase()}`); }; // 32. WATERMARK β€” logo overlay with rotation / shadow / opacity / scale COMMANDS['watermark'] = (args) => { const input = resolveInput(args.input); const image = resolveInput(args.image || args.logo || args.watermark); const output = resolveOutput(args.output, `wm_${path.basename(input)}`); const position = args.position || 'bottom-right'; let posFilter: string; switch (position) { case 'top-left': posFilter = '10:10'; break; case 'top-right': posFilter = 'W-w-10:10'; break; case 'bottom-left': posFilter = '10:H-h-10'; break; case 'center': posFilter = '(W-w)/2:(H-h)/2'; break; default: posFilter = 'W-w-10:H-h-10'; break; } const rot = args.rotation || args.angle || '0'; const opacity = args.opacity || '1.0'; const scale = args.scale || '0.18'; const shadow = args.shadow ? `,drawbox=x=6:y=6:w=iw:h=ih:color=black@0.35:t=fill` : ''; const ff: string[] = [ '-i', input, '-i', image, '-filter_complex', `[1:v]scale=iw*${scale}:-1,rotate=${rot}*(PI/180)${shadow},format=rgba,colorchannelmixer=aa=${opacity}[w];` + `[0:v][w]overlay=${posFilter}[v]`, '-map', '[v]', '-map', '0:a?', '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Watermark ${path.basename(image)} (rot=${rot}Β° opacity=${opacity})`); }; // 33. STEM-SPLIT β€” best-effort vocal extraction via center-channel cancellation. // NOTE: true voice/BGM isolation needs an ML model (Demucs/Spleeter) which is NOT // available in this zero-cost build. This uses the classic "vocal remover" trick: // subtract the mid (center) from a stereo mix. Works ONLY when vocals are centered // and instruments are side. For already-mixed mono/single-track audio it will NOT // cleanly separate β€” it simply attenuates the center. Labeled honestly. COMMANDS['stem-split'] = (args) => { const input = resolveInput(args.input); const mode = args.mode || 'voice'; // 'voice' = remove center (keep instruments), 'bgm' = keep center (remove instruments) const output = resolveOutput(args.output, `${mode}_${path.basename(input)}`); const ff: string[] = mode === 'voice' ? ['-i', input, '-af', 'pan=stereo|c0=c0-c1|c1=c1-c0', '-c:v', 'copy', output, '-y'] : ['-i', input, '-af', 'pan=mono|c0=0.5*c0+0.5*c1', '-c:v', 'copy', output, '-y']; console.log(' ⚠ Best-effort only: real stem separation needs Demucs (not installed).'); runFfmpeg(ff, `Stem-split (${mode}) β€” center-channel ${mode === 'voice' ? 'removed' : 'kept'}`); }; // 34. SUBTITLE β€” burn an .srt/.ass subtitle file onto the video COMMANDS['subtitle'] = (args) => { const input = resolveInput(args.input); const subFile = args.file || args.subs || args.srt; if (!subFile) { console.error(' βœ– --file <subs.srt|subs.ass> is required'); return; } if (!fs.existsSync(subFile)) { console.error(` βœ– Subtitle file not found: ${subFile}`); return; } const output = resolveOutput(args.output, `subtitled_${path.basename(input)}`); const escaped = subFile.replace(/:/g, '\\:').replace(/'/g, "'\\''"); const style = args.style || 'FontSize=24,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,Outline=2'; const ff: string[] = [ '-i', input, '-vf', `subtitles=${escaped}:force_style='${style}'`, '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Burned subtitles: ${path.basename(subFile)}`); }; // 35. TRANSITION β€” cross-fade / dissolve between two clips (xfade) COMMANDS['transition'] = (args) => { const a = resolveInput(args.a || args.input); const b = resolveInput(args.b || args.input2); if (!b) { console.error(' βœ– Provide two clips: --a clip1.mp4 --b clip2.mp4'); return; } const output = resolveOutput(args.output, `transition_${Date.now()}.mp4`); const type = args.type || args.t || 'fade'; const dur = args.duration || args.d || '1'; const offset = args.offset || '0'; const aHas = Array.isArray(getMediaInfo(a)?.streams) && getMediaInfo(a).streams.some((s: any) => s.codec_type === 'audio'); const bHas = Array.isArray(getMediaInfo(b)?.streams) && getMediaInfo(b).streams.some((s: any) => s.codec_type === 'audio'); // BUG E2: audio-less input β†’ `[0:a][1:a]acrossfade` fails. Only build the // audio crossfade when BOTH clips carry an audio stream; otherwise carry // the single available audio track (or none). let audioFilter = ''; let audioMap: string[] = []; let audioCodec: string[] = ['-an']; if (aHas && bHas) { audioFilter = `[0:a][1:a]acrossfade=d=${dur}[a]`; audioMap = ['-map', '[a]']; audioCodec = ['-c:a', 'aac']; } else if (aHas) { audioMap = ['-map', '0:a']; } else if (bHas) { audioMap = ['-map', '1:a']; } const ff: string[] = [ '-i', a, '-i', b, '-filter_complex', `[0:v][1:v]xfade=transition=${type}:duration=${dur}:offset=${offset}[v];` + audioFilter, '-map', '[v]', ...audioMap, '-c:v', 'libx264', ...audioCodec, output, '-y', ]; runFfmpeg(ff, `Transition ${type} (${dur}s) between two clips`); }; // 36. NORMALIZE-AUDIO β€” loudness-normalize the audio track of a video (loudnorm) COMMANDS['normalize-audio'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `normaudio_${path.basename(input)}`); const lufs = args.lufs || args.target || '-14'; const ff: string[] = [ '-i', input, '-af', `loudnorm=I=${lufs}:TP=-1.5:LRA=11`, '-c:v', 'copy', output, '-y', ]; runFfmpeg(ff, `Normalized audio to ${lufs} LUFS`); }; // 37. DUCK β€” lower background audio under a voice track (sidechain compress) COMMANDS['duck'] = (args) => { const music = resolveInput(args.music || args.input); const voice = resolveInput(args.voice || args.voiceover); const output = resolveOutput(args.output, `ducked_${path.basename(music)}`); const threshold = args.threshold || '0.02'; const ratio = args.ratio || '8'; const attack = args.attack || '20'; const release = args.release || '300'; const mHas = Array.isArray(getMediaInfo(music)?.streams) && getMediaInfo(music).streams.some((s: any) => s.codec_type === 'audio'); const vHas = Array.isArray(getMediaInfo(voice)?.streams) && getMediaInfo(voice).streams.some((s: any) => s.codec_type === 'audio'); // BUG E2: audio-less music/voice β†’ `[0:a]`/`[1:a]` fails. Only run the // sidechain duck when BOTH carry audio; otherwise fall back to a plain // passthrough of whichever track exists (or the music, if voice is empty). let filter = ''; let audioMap: string[] = []; if (mHas && vHas) { filter = `[1:a]asplit=2[sc][v];[0:a][sc]sidechaincompress=threshold=${threshold}:ratio=${ratio}:attack=${attack}:release=${release}:makeup=1[ducked];[ducked][v]amix=inputs=2:dropout_transition=0`; audioMap = ['-map', '[a]']; } else if (mHas) { audioMap = ['-map', '0:a']; } else if (vHas) { audioMap = ['-map', '1:a']; } const ff: string[] = [ '-i', music, '-i', voice, ...(filter ? ['-filter_complex', filter] : []), ...audioMap, '-c:v', 'copy', '-c:a', 'pcm_s16le', output, '-y', ]; runFfmpeg(ff, `Ducked BGM under voice (ratio=${ratio})`); }; // 38. LUT β€” apply a color grade (.cube file or built-in preset) COMMANDS['lut'] = (args) => { const file = resolveInput(args.input); const cube = args.cube || args.lut; const output = resolveOutput(args.output, `graded_${path.basename(file)}`); let vf: string; if (cube && fs.existsSync(cube)) { const esc = cube.replace(/:/g, '\\:').replace(/'/g, "'\\''"); vf = `lut3d=${esc}`; } else { // built-in cinematic teal-orange grade (no external file needed) vf = `colorbalance=rs=0.06:bs=-0.06:gh=0.04`; } runFfmpeg(['-i', file, '-vf', vf, '-c:v', 'libx264', '-c:a', 'copy', output, '-y'], `Color grade (${cube ? 'cube' : 'cinematic preset'})`); }; // 39. SPEED-RAMP β€” variable-speed clip (slowβ†’fast or fastβ†’slow, switched at fraction `at`) COMMANDS['speed-ramp'] = (args) => { const input = resolveInput(args.input); const output = resolveOutput(args.output, `ramp_${path.basename(input)}`); const start = parseFloat(args.start || '0.5'); const end = parseFloat(args.end || '2.0'); const at = parseFloat(args.at || '0.5'); // Robust: split into 2 time segments, apply different setpts, concat. const ff: string[] = [ '-i', input, '-filter_complex', `[0:v]trim=end=${at},setpts=PTS*1/${start}[s];` + `[0:v]trim=start=${at},setpts=PTS*1/${end}[e];` + `[s][e]concat=n=2:v=1[v]`, '-map', '[v]', '-c:v', 'libx264', '-c:a', 'copy', output, '-y', ]; runFfmpeg(ff, `Speed-ramp (${start}x β†’ ${end}x @ ${at})`); }; // ─── Main ──────────────────────────────────────────────────────────────────── function main() { const args = parseArgs(process.argv); const subcommand = process.argv[2] || ''; console.log(`\n βœ‚οΈ AVS Video Editor`); console.log(` ─────────────────\n`); if (!COMMANDS[subcommand]) { console.log(` Available commands:`); for (const cmd of Object.keys(COMMANDS).sort()) { console.log(` ${cmd}`); } console.log(`\n Run: npx tsx src/adapters/cli/agentic-editor.ts <command> --help`); console.log(` Or: npm run agentic:editor <command> --input <file>`); return; } COMMANDS[subcommand](args); } main(); // Exported for unit/integration tests (e.g. audio-less guard verification). export { COMMANDS };
#!/usr/bin/env tsx
/**
* agentic-editor.ts β€” Simple video editing operations for the AVS pipeline.
*
* Every operation a real video editor can do, wrapped as a CLI command.
* All are thin wrappers around ffmpeg β€” no re-encoding unless necessary.
*
* USAGE:
* npx tsx src/adapters/cli/agentic-editor.ts <command> [options]
*
* COMMANDS:
* trim Cut video by start/end timecode
* speed Change playback speed (0.25x–4x)
* extract-audio Extract audio track to MP3/WAV
* replace-audio Replace audio in a video
* mute Remove audio track
* split Split video at a timestamp
* merge Join multiple video files
* crop Crop a region of the frame
* resize Scale to new dimensions
* rotate Rotate/flip (90Β°, 180Β°, horizontal)
* loop Loop a clip N times
* overlay-text Add text/caption to video
* overlay-image Add image watermark
* extract-frame Save a single frame as PNG/JPG
* thumbnail Generate a poster frame
* blur Blur a region of the frame
* adjust Brightness/contrast/saturation
* reverse Reverse playback
* info Show video metadata
* concat-scene Extract one scene from a rendered workspace video
*
* EXAMPLES:
* npm run agentic:editor trim --input video.mp4 --start 00:05 --end 00:15
* npm run agentic:editor speed --input clip.mp4 --rate 2.0
* npm run agentic:editor extract-audio --input video.mp4 --output audio.mp3
* npm run agentic:editor merge --files "part1.mp4,part2.mp4" --output joined.mp4
* npm run agentic:editor overlay-text --input video.mp4 --text "Hello!" --output branded.mp4
* npm run agentic:editor concat-scene --job avs_test_modular --scene 3
*/
import 'dotenv/config';
import * as fs from 'fs';
import * as path from 'path';
import { spawnSync } from 'child_process';
// ─── Helpers ─────────────────────────────────────────────────────────────────
/** Resolve ffmpeg binary path */
function ffmpegPath(): string {
try {
const p = require('ffmpeg-static');
if (p && fs.existsSync(p)) return p;
} catch { /* ffmpeg-static not available */ }
return 'ffmpeg'; // fallback to PATH
}
function ffprobePath(): string {
try {
const p = require('ffprobe-static');
if (p?.path && fs.existsSync(p.path)) return p.path;
} catch { /* ffprobe-static not available */ }
return 'ffprobe';
}
function runFfmpeg(args: string[], desc: string): { ok: boolean; stdout: string; stderr: string } {
const ff = ffmpegPath();
console.log(` ⚑ ffmpeg ${args.slice(0, 6).join(' ')} ...`);
const r = spawnSync(ff, args, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });
if (r.status !== 0) {
console.error(` βœ– ${desc} failed (exit ${r.status})`);
console.error(r.stderr?.slice(-500));
return { ok: false, stdout: r.stdout || '', stderr: r.stderr || '' };
}
console.log(` βœ… ${desc}`);
return { ok: true, stdout: r.stdout || '', stderr: r.stderr || '' };
}
function getMediaInfo(file: string): any {
const fp = ffprobePath();
const r = spawnSync(fp, ['-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file], {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
});
if (r.status !== 0) return null;
try {
return JSON.parse(r.stdout);
} catch {
return null;
}
}
function resolveInput(input?: string): string {
if (!input) {
console.error(' βœ– --input <path> is required');
process.exit(1);
}
if (!fs.existsSync(input)) {
console.error(` βœ– Input file not found: ${input}`);
process.exit(1);
}