Skip to content

Commit 3429fc3

Browse files
fix(media-use): transcript-cut reads its parts back only as the container it wrote them in (#8876)
* fix(media-use): transcript-cut reads its parts back only as the container it wrote them in The join opened each part with every reader ffmpeg has. A --copy part keeps the input's packets as they came, so in a raw container like .aac its bytes could be taken for a playlist or list naming other local files, which the join would then copy into --out. The join now opens only local files, as the parts list and the one container its parts were written in, using the same reader list as the input read. * fix(media-use): a --copy cut keeps --out's container for every listed reader Containers missing from the reader table fell back to .mov parts, which cannot hold Opus and re-timed a .ts cut. Every common extension of a reader in the self-contained list now keeps its own container, as on main, and the reader test covers .opus. * fix(media-use): add partsListInput to the shared literal-media-input owner It was added to a generated copy only, so the copy check failed. The owner in the hyperframes skill now holds it and every copy is regenerated from it.
1 parent 31dc4b8 commit 3429fc3

7 files changed

Lines changed: 191 additions & 7 deletions

File tree

‎packages/cli/src/media-use/lib/literal-media-input.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@ export function selfContainedInput(file) {
2727
assertLiteralMediaPath(file);
2828
return ["-protocol_whitelist", "file", "-format_whitelist", SELF_CONTAINED_FORMATS];
2929
}
30+
31+
/** ffmpeg options that read `list` as a concat list of local parts, each opened only as `reader`, which wrote them. */
32+
export function partsListInput(list, reader) {
33+
if (!SELF_CONTAINED_FORMATS.split(",").includes(reader))
34+
throw new Error(`not a self-contained reader: ${reader}`);
35+
const readers = `concat,${reader}`;
36+
return [
37+
"-protocol_whitelist",
38+
"file",
39+
"-format_whitelist",
40+
readers,
41+
"-f",
42+
"concat",
43+
"-safe",
44+
"0",
45+
"-i",
46+
list,
47+
];
48+
}

‎skills-manifest.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"source": "heygen-com/hyperframes",
33
"skills": {
44
"figma": {
5-
"hash": "62f15b1c3702d0b5",
5+
"hash": "09d532370df2e133",
66
"files": 4
77
},
88
"hyperframes": {
9-
"hash": "fd6270da13122d2b",
9+
"hash": "5840a96b8ffa9773",
1010
"files": 87
1111
},
1212
"hyperframes-cli": {
@@ -18,7 +18,7 @@
1818
"files": 13
1919
},
2020
"media-use": {
21-
"hash": "9a3cbbe64d10a4d9",
21+
"hash": "691512b346d6c774",
2222
"files": 139
2323
},
2424
"remotion-to-hyperframes": {

‎skills/figma/scripts/lib/literal-media-input.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@ export function selfContainedInput(file) {
2727
assertLiteralMediaPath(file);
2828
return ["-protocol_whitelist", "file", "-format_whitelist", SELF_CONTAINED_FORMATS];
2929
}
30+
31+
/** ffmpeg options that read `list` as a concat list of local parts, each opened only as `reader`, which wrote them. */
32+
export function partsListInput(list, reader) {
33+
if (!SELF_CONTAINED_FORMATS.split(",").includes(reader))
34+
throw new Error(`not a self-contained reader: ${reader}`);
35+
const readers = `concat,${reader}`;
36+
return [
37+
"-protocol_whitelist",
38+
"file",
39+
"-format_whitelist",
40+
readers,
41+
"-f",
42+
"concat",
43+
"-safe",
44+
"0",
45+
"-i",
46+
list,
47+
];
48+
}

‎skills/hyperframes/scripts/lib/literal-media-input.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@ export function selfContainedInput(file) {
2727
assertLiteralMediaPath(file);
2828
return ["-protocol_whitelist", "file", "-format_whitelist", SELF_CONTAINED_FORMATS];
2929
}
30+
31+
/** ffmpeg options that read `list` as a concat list of local parts, each opened only as `reader`, which wrote them. */
32+
export function partsListInput(list, reader) {
33+
if (!SELF_CONTAINED_FORMATS.split(",").includes(reader))
34+
throw new Error(`not a self-contained reader: ${reader}`);
35+
const readers = `concat,${reader}`;
36+
return [
37+
"-protocol_whitelist",
38+
"file",
39+
"-format_whitelist",
40+
readers,
41+
"-f",
42+
"concat",
43+
"-safe",
44+
"0",
45+
"-i",
46+
list,
47+
];
48+
}

‎skills/media-use/scripts/lib/literal-media-input.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@ export function selfContainedInput(file) {
2727
assertLiteralMediaPath(file);
2828
return ["-protocol_whitelist", "file", "-format_whitelist", SELF_CONTAINED_FORMATS];
2929
}
30+
31+
/** ffmpeg options that read `list` as a concat list of local parts, each opened only as `reader`, which wrote them. */
32+
export function partsListInput(list, reader) {
33+
if (!SELF_CONTAINED_FORMATS.split(",").includes(reader))
34+
throw new Error(`not a self-contained reader: ${reader}`);
35+
const readers = `concat,${reader}`;
36+
return [
37+
"-protocol_whitelist",
38+
"file",
39+
"-format_whitelist",
40+
readers,
41+
"-f",
42+
"concat",
43+
"-safe",
44+
"0",
45+
"-i",
46+
list,
47+
];
48+
}

‎skills/media-use/scripts/transcript-cut.mjs‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { tmpdir } from "node:os";
77
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
88
import { parseArgs } from "node:util";
99
import { compileCutList } from "./lib/cutlist.mjs";
10-
import { assertLiteralMediaPath, selfContainedInput } from "./lib/literal-media-input.mjs";
10+
import {
11+
assertLiteralMediaPath,
12+
partsListInput,
13+
selfContainedInput,
14+
} from "./lib/literal-media-input.mjs";
1115
import { ownFolder } from "./lib/manifest.mjs";
1216
import { fadeFilterFor } from "./lib/transcriptCutFade.mjs";
1317
import { track } from "./lib/telemetry.mjs";
@@ -104,6 +108,13 @@ function run() {
104108
const keptSeconds = sumDurations(segments);
105109

106110
try {
111+
// Each part is read back only as the container it was written in: --copy keeps the input's packets as they
112+
// came, so any other reader could take a part for a list of other files.
113+
const part = args.copy
114+
? copyPart(outPath)
115+
: isAudioOnly(outPath)
116+
? { ext: ".wav", reader: "wav" }
117+
: { ext: ".mov", reader: "mov" };
107118
const parts = segments.map((segment, index) => {
108119
// Intermediates carry PCM audio, not the final codec. Encoding each
109120
// segment to a lossy codec separately makes the encoder pad every segment
@@ -113,8 +124,7 @@ function run() {
113124
// The video container is .mov, not .mkv: ffmpeg writes Matroska
114125
// timestamps in whole milliseconds, which moves frames off the source's
115126
// frame grid, and the concat copy below carries that into the output.
116-
const ext = args.copy ? extname(outPath) || ".mp4" : isAudioOnly(outPath) ? ".wav" : ".mov";
117-
const out = join(tmpDir, `segment-${String(index).padStart(4, "0")}${ext}`);
127+
const out = join(tmpDir, `segment-${String(index).padStart(4, "0")}${part.ext}`);
118128
// --copy stays fade-free (stream copy cannot filter). A segment's true
119129
// start/end (index 0's start, the last segment's end) borders nothing
120130
// kept, so only an interior splice edge gets a ramp.
@@ -145,7 +155,7 @@ function run() {
145155
: ["-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart"];
146156
execFileSync(
147157
"ffmpeg",
148-
["-y", "-f", "concat", "-safe", "0", "-i", listPath, ...concatCodecs, tmpOut],
158+
["-y", ...partsListInput(listPath, part.reader), ...concatCodecs, tmpOut],
149159
{
150160
stdio: "ignore",
151161
},
@@ -232,6 +242,46 @@ function cutSegment(inputPath, segment, outPath, copy, fade) {
232242
execFileSync("ffmpeg", argv, { stdio: "ignore" });
233243
}
234244

245+
// The reader of a --copy part in --out's own container; a container with no reader listed here is copied as .mov.
246+
function copyPart(outPath) {
247+
const readers = {
248+
".mp4": "mov",
249+
".mov": "mov",
250+
".m4a": "mov",
251+
".3gp": "mov",
252+
".mkv": "matroska",
253+
".webm": "matroska",
254+
".mka": "matroska",
255+
".wav": "wav",
256+
".flac": "flac",
257+
".mp3": "mp3",
258+
".aac": "aac",
259+
".ogg": "ogg",
260+
".oga": "ogg",
261+
".opus": "ogg",
262+
".ts": "mpegts",
263+
".m2ts": "mpegts",
264+
".mts": "mpegts",
265+
".mpg": "mpeg",
266+
".mpeg": "mpeg",
267+
".avi": "avi",
268+
".flv": "flv",
269+
".mxf": "mxf",
270+
".asf": "asf",
271+
".wmv": "asf",
272+
".wma": "asf",
273+
".aif": "aiff",
274+
".aiff": "aiff",
275+
".caf": "caf",
276+
".ac3": "ac3",
277+
".wv": "wv",
278+
".au": "au",
279+
};
280+
const ext = extname(outPath).toLowerCase();
281+
const reader = readers[ext];
282+
return reader ? { ext, reader } : { ext: ".mov", reader: "mov" };
283+
}
284+
235285
// Codec set per output container. Audio-only outputs must not get the
236286
// video-centric aac/x264 set (aac inside .wav breaks timing entirely).
237287
function isAudioOnly(filePath) {

‎skills/media-use/scripts/transcript-cut.test.mjs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert/strict";
22
import { execFileSync, spawnSync } from "node:child_process";
33
import {
4+
chmodSync,
45
lstatSync,
56
mkdirSync,
67
mkdtempSync,
@@ -14,6 +15,7 @@ import { tmpdir } from "node:os";
1415
import { join } from "node:path";
1516
import test from "node:test";
1617
import { fileURLToPath } from "node:url";
18+
import { SELF_CONTAINED_FORMATS } from "./lib/literal-media-input.mjs";
1719

1820
const SCRIPT = fileURLToPath(new URL("./transcript-cut.mjs", import.meta.url));
1921
const HAS_FFMPEG =
@@ -412,3 +414,59 @@ test("a cut into a new folder inside the project is made there", { skip: !HAS_FF
412414
assert.equal(result.status, 0, result.stderr || result.stdout);
413415
assert.ok(lstatSync(join(dir, "cuts", "deep", "out.wav")).isFile());
414416
});
417+
418+
// fails without the fix: --copy writes each part in --out's container (a raw .aac keeps packet bytes as they came),
419+
// and the join opened the parts with every reader, so a part could be read as a manifest naming other files.
420+
for (const { out, codec, reader } of [
421+
{ out: "cut.aac", codec: "aac", reader: "aac" },
422+
{ out: "cut.opus", codec: "libopus", reader: "ogg" },
423+
]) {
424+
test(
425+
`a --copy cut to ${out} reads its parts back only as ${reader}, the container it wrote them in`,
426+
{ skip: (!HAS_FFMPEG || process.platform === "win32") && "needs ffmpeg and a POSIX shell" },
427+
(t) => {
428+
const { dir, cleanup } = fixture();
429+
t.after(cleanup);
430+
const source = join(dir, `source.${out.split(".").pop()}`);
431+
execFileSync("ffmpeg", [
432+
...["-y", "-v", "error", "-f", "lavfi", "-i", "sine=duration=1", "-c:a", codec, source],
433+
]);
434+
const transcriptPath = join(dir, "transcript.json");
435+
writeFileSync(
436+
transcriptPath,
437+
JSON.stringify({ words: [{ text: "tone", start: 0, end: 1 }] }),
438+
);
439+
const bin = join(dir, "bin");
440+
mkdirSync(bin);
441+
const log = join(dir, "ffmpeg-calls.txt");
442+
const real = execFileSync("sh", ["-c", "command -v ffmpeg"], { encoding: "utf8" }).trim();
443+
writeFileSync(
444+
join(bin, "ffmpeg"),
445+
`#!/bin/sh\nfor a in "$@"; do printf '%s\\t' "$a"; done >> '${log}'\necho >> '${log}'\nexec '${real}' "$@"\n`,
446+
);
447+
chmodSync(join(bin, "ffmpeg"), 0o755);
448+
const outPath = join(dir, out);
449+
const keep = ["--keep", "0.1-0.6", "--copy", "--out", outPath];
450+
const result = spawnSync(
451+
process.execPath,
452+
[SCRIPT, "--input", source, "--transcript", transcriptPath, ...keep],
453+
{ encoding: "utf8", env: { ...process.env, PATH: `${bin}:${process.env.PATH}` } },
454+
);
455+
assert.equal(result.status, 0, result.stderr || result.stdout);
456+
const decoded = spawnSync("ffmpeg", ["-v", "error", "-i", outPath, "-f", "null", "-"], {
457+
encoding: "utf8",
458+
});
459+
assert.equal(decoded.status, 0, decoded.stderr);
460+
assert.equal(decoded.stderr, "");
461+
const calls = readFileSync(log, "utf8")
462+
.trim()
463+
.split("\n")
464+
.map((line) => line.split("\t").slice(0, -1));
465+
const joined = calls.find((args) => args.includes("concat"));
466+
const value = (flag) => joined[joined.indexOf(flag) + 1];
467+
assert.equal(value("-protocol_whitelist"), "file");
468+
assert.equal(value("-format_whitelist"), `concat,${reader}`);
469+
assert.ok(SELF_CONTAINED_FORMATS.split(",").includes(reader), reader);
470+
},
471+
);
472+
}

0 commit comments

Comments
 (0)