|
| 1 | +import { run } from "../run.js"; |
| 2 | +import { outputName } from "../utils.js"; |
| 3 | + |
| 4 | +export function register(program) { |
| 5 | + program |
| 6 | + .command("preview") |
| 7 | + .description("Generate a short highlight preview of a video") |
| 8 | + .argument("<input>", "Input video file") |
| 9 | + .option("--clips <n>", "Number of sample clips", "5") |
| 10 | + .option("--clip-duration <sec>", "Duration of each clip in seconds", "2") |
| 11 | + .option("-o, --output <path>", "Output file path") |
| 12 | + .option("--dry-run", "Print the FFmpeg command without running it") |
| 13 | + .option("-y", "Overwrite output without asking") |
| 14 | + .action((input, opts) => { |
| 15 | + const clips = parseInt(opts.clips, 10); |
| 16 | + const clipDur = parseFloat(opts.clipDuration); |
| 17 | + |
| 18 | + // Build select filter to pick evenly spaced clips |
| 19 | + // Uses the select filter with periodic sampling |
| 20 | + const selectParts = []; |
| 21 | + for (let i = 0; i < clips; i++) { |
| 22 | + // Each clip: between(t, start, start+clipDur) |
| 23 | + // We use a fraction-based approach assuming unknown total duration |
| 24 | + selectParts.push(`between(t,t_total*${i}/${clips},t_total*${i}/${clips}+${clipDur})`); |
| 25 | + } |
| 26 | + |
| 27 | + // Simpler approach: use segment + select with trim |
| 28 | + // Most reliable: use the select with periodic segments |
| 29 | + const interval = clipDur; |
| 30 | + const filter = `select='if(lt(mod(t\\,floor(t/${clips})),${clipDur}),1,0)',setpts=N/FRAME_RATE/TB`; |
| 31 | + |
| 32 | + const out = opts.output || outputName(input, "preview"); |
| 33 | + const args = [ |
| 34 | + "-i", input, |
| 35 | + "-vf", filter, |
| 36 | + "-af", `aselect='if(lt(mod(t\\,floor(t/${clips})),${clipDur}),1,0)',asetpts=N/SR/TB`, |
| 37 | + "-t", String(clips * clipDur), |
| 38 | + ]; |
| 39 | + |
| 40 | + if (opts.y) args.push("-y"); |
| 41 | + args.push(out); |
| 42 | + run(args, { dryRun: opts.dryRun }); |
| 43 | + }); |
| 44 | +} |
0 commit comments