85453da49f
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
Docs / Validate docs (push) Waiting to run
CodeQL / Analyze (actions) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Sync skills to ClawHub / Publish changed skills (push) Waiting to run
regression / regression-shards (style-16-prod style-9-prod style-17-prod iframe-render-compat variables-prod mp4-h265-sdr, shard-4) (push) Has been cancelled
regression / regression-shards (style-4-prod style-11-prod style-2-prod animejs-adapter typegpu-adapter parallel-capture-regression, shard-5) (push) Has been cancelled
regression / regression-shards (style-7-prod style-8-prod style-10-prod css-spinner-render-compat webm-transparency mp4-h264-sdr webm-vp9, shard-3) (push) Has been cancelled
regression / regression-shards (sub-composition-video style-18-prod raf-ball-render-compat font-variant-numeric sub-comp-t0 sub-comp-id-selector, shard-7) (push) Has been cancelled
Windows render verification / Detect changes (push) Has been cancelled
Windows render verification / Preflight (lint + format) (push) Has been cancelled
Windows render verification / Render on windows-latest (push) Has been cancelled
Windows render verification / Tests on windows-latest (push) Has been cancelled
CI / Detect changes (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Fallow audit (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Typecheck (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Producer: integration tests (push) Has been cancelled
CI / Producer: unit tests (push) Has been cancelled
CI / File size check (push) Has been cancelled
CI / Test: skills (push) Has been cancelled
CI / Skills: manifest in sync (push) Has been cancelled
CI / CLI: npx shim (macos-latest) (push) Has been cancelled
CI / CLI: npx shim (ubuntu-latest) (push) Has been cancelled
CI / CLI: npx shim (windows-latest) (push) Has been cancelled
CI / SDK: unit + contract + smoke (push) Has been cancelled
CI / Test: runtime contract (push) Has been cancelled
CI / Studio: load smoke (push) Has been cancelled
CI / Smoke: global install (push) Has been cancelled
CI / CLI smoke (required) (push) Has been cancelled
CI / Semantic PR title (push) Has been cancelled
Player perf / Detect changes (push) Has been cancelled
Player perf / Preflight (lint + format) (push) Has been cancelled
Player perf / player-perf (push) Has been cancelled
Player perf / Perf: drift (push) Has been cancelled
Player perf / Perf: fps (push) Has been cancelled
Player perf / Perf: parity (push) Has been cancelled
Player perf / Perf: scrub (push) Has been cancelled
Player perf / Perf: load (push) Has been cancelled
preview-regression / Detect changes (push) Has been cancelled
preview-regression / Preflight (lint + format) (push) Has been cancelled
preview-regression / Preview parity (push) Has been cancelled
preview-regression / preview-regression (push) Has been cancelled
regression / regression (push) Has been cancelled
regression / Detect changes (push) Has been cancelled
regression / Preflight (lint + format) (push) Has been cancelled
regression / regression-shards (hdr-regression style-5-prod style-3-prod mov-prores, shard-1) (push) Has been cancelled
regression / regression-shards (overlay-montage-prod style-12-prod chat missing-host-comp-id png-sequence portrait-edge-bleed, shard-6) (push) Has been cancelled
regression / regression-shards (style-13-prod style-6-prod vignelli-stacking gsap-letters-render-compat audio-mux-parity, shard-8) (push) Has been cancelled
regression / regression-shards (style-15-prod hdr-hlg-regression style-1-prod many-cuts vfr-screen-recording render-symlinked-assets, shard-2) (push) Has been cancelled
239 lines
7.1 KiB
JavaScript
239 lines
7.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { execFileSync } from "node:child_process";
|
|
import { mkdtempSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, extname, join, resolve } from "node:path";
|
|
import { parseArgs } from "node:util";
|
|
import { compileCutList } from "./lib/cutlist.mjs";
|
|
import { track } from "./lib/telemetry.mjs";
|
|
|
|
const { values: args } = parseArgs({
|
|
options: {
|
|
input: { type: "string" },
|
|
transcript: { type: "string" },
|
|
remove: { type: "string" },
|
|
"remove-words": { type: "string" },
|
|
"remove-fillers": { type: "string" },
|
|
"cut-silence": { type: "string" },
|
|
keep: { type: "string" },
|
|
copy: { type: "boolean", default: false },
|
|
plan: { type: "boolean", default: false },
|
|
out: { type: "string" },
|
|
json: { type: "boolean", default: false },
|
|
help: { type: "boolean", short: "h", default: false },
|
|
},
|
|
strict: true,
|
|
});
|
|
|
|
if (args.help) {
|
|
console.log(`media-use transcript-cut — compile transcript edits into video cuts
|
|
|
|
Usage:
|
|
node transcript-cut.mjs --input in.mp4 --transcript transcript.json --remove "12-15" --out out.mp4
|
|
|
|
Options:
|
|
--input Source video/audio file
|
|
--transcript JSON word transcript, array or { words: [...] }
|
|
--remove Time ranges to remove, seconds: a-b,c-d
|
|
--remove-words Word-index ranges to remove: 12-18,40-41
|
|
--remove-fillers Comma list of filler words to remove
|
|
--cut-silence Remove inter-word gaps longer than this many seconds
|
|
--keep Inverse mode: direct kept ranges, mutually exclusive with removal
|
|
--copy Use stream copy for faster, keyframe-snapped cuts
|
|
--plan Print kept segment JSON and exit without ffmpeg
|
|
--out Output file
|
|
--json Output JSON status
|
|
--help, -h Show this help`);
|
|
process.exit(0);
|
|
}
|
|
|
|
try {
|
|
run();
|
|
await track("media_use_transcript_cut", {
|
|
mode: args.plan ? "plan" : "encode",
|
|
remove_fillers: !!args["remove-fillers"],
|
|
cut_silence: !!args["cut-silence"],
|
|
ranges: !!args.remove,
|
|
keep: !!args.keep,
|
|
});
|
|
} catch (err) {
|
|
if (args.json) console.log(JSON.stringify({ ok: false, error: err.message }));
|
|
else console.error(`error: ${err.message}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
function run() {
|
|
if (!args.transcript) throw new Error("--transcript is required");
|
|
const transcript = JSON.parse(readFileSync(resolve(args.transcript), "utf8"));
|
|
const segments = compileCutList(transcript, {
|
|
remove: args.remove,
|
|
removeWords: args["remove-words"],
|
|
removeFillers: args["remove-fillers"],
|
|
cutSilence: args["cut-silence"],
|
|
keep: args.keep,
|
|
});
|
|
|
|
if (args.plan) {
|
|
console.log(JSON.stringify(segments));
|
|
return;
|
|
}
|
|
|
|
if (!args.input || !args.out)
|
|
throw new Error("--input and --out are required unless --plan is set");
|
|
if (segments.length === 0) throw new Error("cut list has no kept segments");
|
|
|
|
const inputPath = resolve(args.input);
|
|
const outPath = resolve(args.out);
|
|
mkdirSync(dirname(outPath), { recursive: true });
|
|
const tmpDir = mkdtempSync(join(tmpdir(), "media-use-cut-"));
|
|
const keptSeconds = sumDurations(segments);
|
|
const totalSeconds = probeDuration(inputPath);
|
|
|
|
try {
|
|
const parts = segments.map((segment, index) => {
|
|
const out = join(
|
|
tmpDir,
|
|
`segment-${String(index).padStart(4, "0")}${extname(outPath) || ".mp4"}`,
|
|
);
|
|
cutSegment(inputPath, segment, out, Boolean(args.copy));
|
|
return out;
|
|
});
|
|
const listPath = join(tmpDir, "list.txt");
|
|
writeFileSync(
|
|
listPath,
|
|
parts.map((part) => `file '${escapeConcatPath(part)}'`).join("\n") + "\n",
|
|
);
|
|
// Encode to a sibling temp (same extension so ffmpeg picks the right muxer),
|
|
// then atomic-rename so a SIGKILL mid-encode can't leave a truncated outPath.
|
|
const tmpOut = `${outPath}.part${extname(outPath) || ".mp4"}`;
|
|
execFileSync(
|
|
"ffmpeg",
|
|
["-y", "-f", "concat", "-safe", "0", "-i", listPath, "-c", "copy", tmpOut],
|
|
{
|
|
stdio: "ignore",
|
|
},
|
|
);
|
|
renameSync(tmpOut, outPath);
|
|
} finally {
|
|
rmSync(tmpDir, { recursive: true, force: true });
|
|
}
|
|
|
|
// Stream copy can only cut on keyframes; on sparse-keyframe footage the snap
|
|
// can silently swallow the whole cut. Compare, then surface the drift in BOTH
|
|
// the stderr warning (human) and the --json result (pipelines).
|
|
let copyDrift = null;
|
|
if (args.copy) {
|
|
const outSeconds = probeDuration(outPath);
|
|
if (Math.abs(outSeconds - keptSeconds) > 1) {
|
|
copyDrift = { produced_s: round3(outSeconds), expected_s: round3(keptSeconds) };
|
|
if (!args.json) {
|
|
console.error(
|
|
`warning: --copy keyframe snapping produced ${round3(outSeconds)}s instead of ${round3(keptSeconds)}s kept; drop --copy for frame-accurate cuts`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (args.json) {
|
|
console.log(
|
|
JSON.stringify({
|
|
ok: true,
|
|
input: inputPath,
|
|
out: outPath,
|
|
segments,
|
|
kept_s: round3(keptSeconds),
|
|
total_s: round3(totalSeconds),
|
|
...(copyDrift && { copy_drift: copyDrift }),
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
`cut ${inputPath} -> ${outPath} (${segments.length} segments, ${fmt(keptSeconds)}s kept of ${fmt(
|
|
totalSeconds,
|
|
)}s)`,
|
|
);
|
|
console.log(`next: resolve --from ${outPath} --type <type>`);
|
|
}
|
|
|
|
function cutSegment(inputPath, segment, outPath, copy) {
|
|
const argv = [
|
|
"-y",
|
|
"-nostdin",
|
|
"-ss",
|
|
fmt(segment.start),
|
|
"-i",
|
|
inputPath,
|
|
"-to",
|
|
fmt(segment.end - segment.start),
|
|
];
|
|
if (copy) {
|
|
argv.push("-c", "copy", "-avoid_negative_ts", "make_zero");
|
|
} else {
|
|
argv.push(...encodeArgsFor(extname(outPath).toLowerCase()));
|
|
}
|
|
argv.push(outPath);
|
|
execFileSync("ffmpeg", argv, { stdio: "ignore" });
|
|
}
|
|
|
|
// Codec set per output container. Audio-only outputs must not get the
|
|
// video-centric aac/x264 set (aac inside .wav breaks timing entirely).
|
|
function encodeArgsFor(ext) {
|
|
if (ext === ".wav") return ["-c:a", "pcm_s16le"];
|
|
if (ext === ".mp3") return ["-c:a", "libmp3lame", "-q:a", "2"];
|
|
if (ext === ".m4a" || ext === ".aac") return ["-c:a", "aac"];
|
|
if (ext === ".flac") return ["-c:a", "flac"];
|
|
return [
|
|
"-c:v",
|
|
"libx264",
|
|
"-preset",
|
|
"veryfast",
|
|
"-crf",
|
|
"18",
|
|
"-c:a",
|
|
"aac",
|
|
"-movflags",
|
|
"+faststart",
|
|
];
|
|
}
|
|
|
|
function probeDuration(filePath) {
|
|
const raw = execFileSync(
|
|
"ffprobe",
|
|
[
|
|
"-v",
|
|
"error",
|
|
"-show_entries",
|
|
"format=duration",
|
|
"-of",
|
|
"default=noprint_wrappers=1:nokey=1",
|
|
filePath,
|
|
],
|
|
{ encoding: "utf8" },
|
|
);
|
|
const duration = Number(raw.trim());
|
|
if (!Number.isFinite(duration) || duration <= 0)
|
|
throw new Error(`could not probe duration: ${filePath}`);
|
|
return duration;
|
|
}
|
|
|
|
function escapeConcatPath(filePath) {
|
|
return filePath.replace(/'/g, "'\\''");
|
|
}
|
|
|
|
function sumDurations(segments) {
|
|
return segments.reduce((sum, segment) => sum + (segment.end - segment.start), 0);
|
|
}
|
|
|
|
function fmt(n) {
|
|
return round3(n)
|
|
.toFixed(3)
|
|
.replace(/\.?0+$/, "");
|
|
}
|
|
|
|
function round3(n) {
|
|
return Math.round(Number(n) * 1000) / 1000;
|
|
}
|