Skip to content

Commit 42a3e7a

Browse files
miga-heygenclaude
andauthored
fix(skills): loop-extended BGM writes .mp3, not MP3-in-a-.wav (#3884)
ensureBgmCovers() (duplicated in faceless-explainer, pr-to-video, and product-launch-video's assemble-index.mjs) always re-encodes a short BGM track with libmp3lame, but preserved the source asset's own extension when naming the loop-extended output — so a "bgm.wav" source produced "bgm.loop.wav" containing genuine MP3 audio. ffprobe/ffmpeg/Chromium all decode this correctly via the WAVE_FORMAT_MPEGLAYER3 tag, but a naive/strict WAV parser (e.g. Python's stdlib `wave` module) hard-fails on it. The function's own doc comment already documented the intended output as "*.loop.mp3" — the code just didn't match it. Fix: always emit a .loop.mp3 path regardless of the source extension. music-to-video's assemble-index.mjs does not have this function (it uses the raw BGM asset directly, no loop-extension), so only 3 of the 4 skills needed the change. Co-authored-by: Claude Sonnet 5 <[email protected]>
1 parent ea7e1db commit 42a3e7a

7 files changed

Lines changed: 186 additions & 12 deletions

File tree

‎skills-manifest.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"files": 142
77
},
88
"faceless-explainer": {
9-
"hash": "76162336b4c88963",
9+
"hash": "3f84e9cf9c2468ca",
1010
"files": 24
1111
},
1212
"figma": {
@@ -62,11 +62,11 @@
6262
"files": 132
6363
},
6464
"pr-to-video": {
65-
"hash": "265c1f85d732d79c",
65+
"hash": "b93575a96d213ff5",
6666
"files": 30
6767
},
6868
"product-launch-video": {
69-
"hash": "3e1efc70cad06b58",
69+
"hash": "b1597ed153cafcc7",
7070
"files": 30
7171
},
7272
"remotion-to-hyperframes": {

‎skills/faceless-explainer/scripts/assemble-index.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ function ensureBgmCovers(relPath, hyperframesDir, total) {
9292
if (!Number.isFinite(dur) || dur <= 0)
9393
return { looped: false, short: false, reason: "unreadable duration" };
9494
if (dur >= total - 0.1) return { looped: false, short: false, dur }; // already covers
95-
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.$1");
95+
// Always emit .mp3: the encode below is libmp3lame regardless of the source
96+
// extension, so preserving relPath's own extension (e.g. "bgm.wav") would
97+
// smuggle an MP3 stream into a .wav-named file (PRINFRA-309).
98+
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.mp3");
9699
const absOut = join(hyperframesDir, relOut);
97100
const fadeOut = Math.max(0, total - 1.5);
98101
const ff = spawnSync(

‎skills/faceless-explainer/scripts/assemble-index.test.mjs‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import assert from "node:assert/strict";
2-
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { spawnSync } from "node:child_process";
66
import test from "node:test";
77

88
const assembleScript = new URL("./assemble-index.mjs", import.meta.url).pathname;
9+
const HAS_FFMPEG =
10+
spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0 &&
11+
spawnSync("ffprobe", ["-version"], { stdio: "ignore" }).status === 0;
912

1013
// ── bgm_pending at the assembly boundary ─────────────────────────────────────
1114
// Regression: the flag survived into audio_meta.json but assemble rebuilt its audio object
1215
// from three named keys and dropped it, so the step that actually builds the film could not
1316
// tell "not ready yet" from "silent by design" and would ship the silent one.
1417

15-
function assembleWith({ audioMeta, extraArgs = [] }) {
18+
function assembleWith({ audioMeta, extraArgs = [], beforeAssemble }) {
1619
const dir = mkdtempSync(join(tmpdir(), "product-launch-assemble-"));
1720
writeFileSync(
1821
join(dir, "STORYBOARD.md"),
@@ -25,6 +28,7 @@ function assembleWith({ audioMeta, extraArgs = [] }) {
2528
'<section class="clip" data-start="0" data-duration="3"></section></div>',
2629
);
2730
if (audioMeta) writeFileSync(join(dir, "audio_meta.json"), JSON.stringify(audioMeta));
31+
if (beforeAssemble) beforeAssemble(dir);
2832
const r = spawnSync(
2933
process.execPath,
3034
[
@@ -70,3 +74,54 @@ test("a film that is silent BY DESIGN still assembles untouched", () => {
7074
assert.equal(existsSync(join(dir, "index.html")), true);
7175
assert.doesNotMatch(r.stderr, /bgm_pending/);
7276
});
77+
78+
// ── PRINFRA-309: loop-extended BGM must be a real .mp3, not MP3-in-a-.wav ────
79+
// ensureBgmCovers() always encodes the loop-extended track with libmp3lame — so a source
80+
// asset named "*.wav" that's short of TOTAL used to come out as MP3 audio inside a
81+
// .loop.wav-named file: decodes fine via ffprobe/ffmpeg/Chromium (which sniff the real
82+
// WAVE_FORMAT_MPEGLAYER3 tag), but hard-fails in any naive/strict WAV parser (e.g. Python's
83+
// stdlib `wave` module) that doesn't do full format-tag dispatch.
84+
test(
85+
"loop-extended bgm is written as .mp3, not .loop.wav, even when the source is a .wav",
86+
{ skip: !HAS_FFMPEG },
87+
() => {
88+
const { dir, r } = assembleWith({
89+
audioMeta: { bgm: { path: "assets/bgm/bed.wav" }, voices: [], sfx: [] },
90+
beforeAssemble: (projectDir) => {
91+
mkdirSync(join(projectDir, "assets", "bgm"), { recursive: true });
92+
// 1s tone, well short of the 3s TOTAL from the single storyboard frame above —
93+
// guarantees ensureBgmCovers() takes the loop-extend branch.
94+
const gen = spawnSync(
95+
"ffmpeg",
96+
[
97+
"-y",
98+
"-f",
99+
"lavfi",
100+
"-i",
101+
"sine=frequency=440:duration=1",
102+
join(projectDir, "assets", "bgm", "bed.wav"),
103+
],
104+
{ encoding: "utf8" },
105+
);
106+
assert.equal(gen.status, 0, gen.stderr);
107+
},
108+
});
109+
110+
assert.equal(r.status, 0, r.stderr);
111+
const html = readFileSync(join(dir, "index.html"), "utf8");
112+
const bgmEl = html.match(/id="el-bgm"[\s\S]*?src="([^"]+)"/);
113+
assert.ok(bgmEl, "expected a bgm <audio> element in index.html");
114+
const bgmSrc = bgmEl[1];
115+
assert.match(bgmSrc, /\.mp3$/, `bgm src should end in .mp3, got "${bgmSrc}"`);
116+
assert.doesNotMatch(bgmSrc, /\.wav$/);
117+
118+
const outPath = join(dir, bgmSrc);
119+
assert.equal(existsSync(outPath), true);
120+
const probe = spawnSync(
121+
"ffprobe",
122+
["-v", "error", "-show_entries", "stream=codec_name", "-of", "csv=p=0", "--", outPath],
123+
{ encoding: "utf8" },
124+
);
125+
assert.equal(probe.stdout.trim(), "mp3");
126+
},
127+
);

‎skills/pr-to-video/scripts/assemble-index.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ function ensureBgmCovers(relPath, hyperframesDir, total) {
9393
if (!Number.isFinite(dur) || dur <= 0)
9494
return { looped: false, short: false, reason: "unreadable duration" };
9595
if (dur >= total - 0.1) return { looped: false, short: false, dur }; // already covers
96-
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.$1");
96+
// Always emit .mp3: the encode below is libmp3lame regardless of the source
97+
// extension, so preserving relPath's own extension (e.g. "bgm.wav") would
98+
// smuggle an MP3 stream into a .wav-named file (PRINFRA-309).
99+
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.mp3");
97100
const absOut = join(hyperframesDir, relOut);
98101
const fadeOut = Math.max(0, total - 1.5);
99102
const ff = spawnSync(

‎skills/pr-to-video/scripts/assemble-index.test.mjs‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import assert from "node:assert/strict";
2-
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { spawnSync } from "node:child_process";
66
import test from "node:test";
77

88
const assembleScript = new URL("./assemble-index.mjs", import.meta.url).pathname;
9+
const HAS_FFMPEG =
10+
spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0 &&
11+
spawnSync("ffprobe", ["-version"], { stdio: "ignore" }).status === 0;
912

1013
// ── bgm_pending at the assembly boundary ─────────────────────────────────────
1114
// Regression: the flag survived into audio_meta.json but assemble rebuilt its audio object
1215
// from three named keys and dropped it, so the step that actually builds the film could not
1316
// tell "not ready yet" from "silent by design" and would ship the silent one.
1417

15-
function assembleWith({ audioMeta, extraArgs = [] }) {
18+
function assembleWith({ audioMeta, extraArgs = [], beforeAssemble }) {
1619
const dir = mkdtempSync(join(tmpdir(), "product-launch-assemble-"));
1720
writeFileSync(
1821
join(dir, "STORYBOARD.md"),
@@ -28,6 +31,7 @@ function assembleWith({ audioMeta, extraArgs = [] }) {
2831
"</div></template>",
2932
);
3033
if (audioMeta) writeFileSync(join(dir, "audio_meta.json"), JSON.stringify(audioMeta));
34+
if (beforeAssemble) beforeAssemble(dir);
3135
const r = spawnSync(
3236
process.execPath,
3337
[
@@ -73,3 +77,54 @@ test("a film that is silent BY DESIGN still assembles untouched", () => {
7377
assert.equal(existsSync(join(dir, "index.html")), true);
7478
assert.doesNotMatch(r.stderr, /bgm_pending/);
7579
});
80+
81+
// ── PRINFRA-309: loop-extended BGM must be a real .mp3, not MP3-in-a-.wav ────
82+
// ensureBgmCovers() always encodes the loop-extended track with libmp3lame — so a source
83+
// asset named "*.wav" that's short of TOTAL used to come out as MP3 audio inside a
84+
// .loop.wav-named file: decodes fine via ffprobe/ffmpeg/Chromium (which sniff the real
85+
// WAVE_FORMAT_MPEGLAYER3 tag), but hard-fails in any naive/strict WAV parser (e.g. Python's
86+
// stdlib `wave` module) that doesn't do full format-tag dispatch.
87+
test(
88+
"loop-extended bgm is written as .mp3, not .loop.wav, even when the source is a .wav",
89+
{ skip: !HAS_FFMPEG },
90+
() => {
91+
const { dir, r } = assembleWith({
92+
audioMeta: { bgm: { path: "assets/bgm/bed.wav" }, voices: [], sfx: [] },
93+
beforeAssemble: (projectDir) => {
94+
mkdirSync(join(projectDir, "assets", "bgm"), { recursive: true });
95+
// 1s tone, well short of the 3s TOTAL from the single storyboard frame above —
96+
// guarantees ensureBgmCovers() takes the loop-extend branch.
97+
const gen = spawnSync(
98+
"ffmpeg",
99+
[
100+
"-y",
101+
"-f",
102+
"lavfi",
103+
"-i",
104+
"sine=frequency=440:duration=1",
105+
join(projectDir, "assets", "bgm", "bed.wav"),
106+
],
107+
{ encoding: "utf8" },
108+
);
109+
assert.equal(gen.status, 0, gen.stderr);
110+
},
111+
});
112+
113+
assert.equal(r.status, 0, r.stderr);
114+
const html = readFileSync(join(dir, "index.html"), "utf8");
115+
const bgmEl = html.match(/id="el-bgm"[\s\S]*?src="([^"]+)"/);
116+
assert.ok(bgmEl, "expected a bgm <audio> element in index.html");
117+
const bgmSrc = bgmEl[1];
118+
assert.match(bgmSrc, /\.mp3$/, `bgm src should end in .mp3, got "${bgmSrc}"`);
119+
assert.doesNotMatch(bgmSrc, /\.wav$/);
120+
121+
const outPath = join(dir, bgmSrc);
122+
assert.equal(existsSync(outPath), true);
123+
const probe = spawnSync(
124+
"ffprobe",
125+
["-v", "error", "-show_entries", "stream=codec_name", "-of", "csv=p=0", "--", outPath],
126+
{ encoding: "utf8" },
127+
);
128+
assert.equal(probe.stdout.trim(), "mp3");
129+
},
130+
);

‎skills/product-launch-video/scripts/assemble-index.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ function ensureBgmCovers(relPath, hyperframesDir, total) {
9292
if (!Number.isFinite(dur) || dur <= 0)
9393
return { looped: false, short: false, reason: "unreadable duration" };
9494
if (dur >= total - 0.1) return { looped: false, short: false, dur }; // already covers
95-
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.$1");
95+
// Always emit .mp3: the encode below is libmp3lame regardless of the source
96+
// extension, so preserving relPath's own extension (e.g. "bgm.wav") would
97+
// smuggle an MP3 stream into a .wav-named file (PRINFRA-309).
98+
const relOut = relPath.replace(/\.([^./]+)$/, ".loop.mp3");
9699
const absOut = join(hyperframesDir, relOut);
97100
const fadeOut = Math.max(0, total - 1.5);
98101
const ff = spawnSync(

‎skills/product-launch-video/scripts/assemble-index.test.mjs‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import assert from "node:assert/strict";
2-
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { spawnSync } from "node:child_process";
66
import test from "node:test";
77

88
const assembleScript = new URL("./assemble-index.mjs", import.meta.url).pathname;
9+
const HAS_FFMPEG =
10+
spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0 &&
11+
spawnSync("ffprobe", ["-version"], { stdio: "ignore" }).status === 0;
912

1013
// ── bgm_pending at the assembly boundary ─────────────────────────────────────
1114
// Regression: the flag survived into audio_meta.json but assemble rebuilt its audio object
1215
// from three named keys and dropped it, so the step that actually builds the film could not
1316
// tell "not ready yet" from "silent by design" and would ship the silent one.
1417

15-
function assembleWith({ audioMeta, extraArgs = [] }) {
18+
function assembleWith({ audioMeta, extraArgs = [], beforeAssemble }) {
1619
const dir = mkdtempSync(join(tmpdir(), "product-launch-assemble-"));
1720
writeFileSync(
1821
join(dir, "STORYBOARD.md"),
@@ -25,6 +28,7 @@ function assembleWith({ audioMeta, extraArgs = [] }) {
2528
'<section class="clip" data-start="0" data-duration="3"></section></div>',
2629
);
2730
if (audioMeta) writeFileSync(join(dir, "audio_meta.json"), JSON.stringify(audioMeta));
31+
if (beforeAssemble) beforeAssemble(dir);
2832
const r = spawnSync(
2933
process.execPath,
3034
[
@@ -70,3 +74,54 @@ test("a film that is silent BY DESIGN still assembles untouched", () => {
7074
assert.equal(existsSync(join(dir, "index.html")), true);
7175
assert.doesNotMatch(r.stderr, /bgm_pending/);
7276
});
77+
78+
// ── PRINFRA-309: loop-extended BGM must be a real .mp3, not MP3-in-a-.wav ────
79+
// ensureBgmCovers() always encodes the loop-extended track with libmp3lame — so a source
80+
// asset named "*.wav" that's short of TOTAL used to come out as MP3 audio inside a
81+
// .loop.wav-named file: decodes fine via ffprobe/ffmpeg/Chromium (which sniff the real
82+
// WAVE_FORMAT_MPEGLAYER3 tag), but hard-fails in any naive/strict WAV parser (e.g. Python's
83+
// stdlib `wave` module) that doesn't do full format-tag dispatch.
84+
test(
85+
"loop-extended bgm is written as .mp3, not .loop.wav, even when the source is a .wav",
86+
{ skip: !HAS_FFMPEG },
87+
() => {
88+
const { dir, r } = assembleWith({
89+
audioMeta: { bgm: { path: "assets/bgm/bed.wav" }, voices: [], sfx: [] },
90+
beforeAssemble: (projectDir) => {
91+
mkdirSync(join(projectDir, "assets", "bgm"), { recursive: true });
92+
// 1s tone, well short of the 3s TOTAL from the single storyboard frame above —
93+
// guarantees ensureBgmCovers() takes the loop-extend branch.
94+
const gen = spawnSync(
95+
"ffmpeg",
96+
[
97+
"-y",
98+
"-f",
99+
"lavfi",
100+
"-i",
101+
"sine=frequency=440:duration=1",
102+
join(projectDir, "assets", "bgm", "bed.wav"),
103+
],
104+
{ encoding: "utf8" },
105+
);
106+
assert.equal(gen.status, 0, gen.stderr);
107+
},
108+
});
109+
110+
assert.equal(r.status, 0, r.stderr);
111+
const html = readFileSync(join(dir, "index.html"), "utf8");
112+
const bgmEl = html.match(/id="el-bgm"[\s\S]*?src="([^"]+)"/);
113+
assert.ok(bgmEl, "expected a bgm <audio> element in index.html");
114+
const bgmSrc = bgmEl[1];
115+
assert.match(bgmSrc, /\.mp3$/, `bgm src should end in .mp3, got "${bgmSrc}"`);
116+
assert.doesNotMatch(bgmSrc, /\.wav$/);
117+
118+
const outPath = join(dir, bgmSrc);
119+
assert.equal(existsSync(outPath), true);
120+
const probe = spawnSync(
121+
"ffprobe",
122+
["-v", "error", "-show_entries", "stream=codec_name", "-of", "csv=p=0", "--", outPath],
123+
{ encoding: "utf8" },
124+
);
125+
assert.equal(probe.stdout.trim(), "mp3");
126+
},
127+
);

0 commit comments

Comments
 (0)