Skip to content

Commit 065db03

Browse files
authored
Route low-sample-rate MP3s to the remux path (2.4.7) (#91)
Chrome's and Edge's AAC encoders take nothing below 44.1 kHz, and old rips are often 32 kHz or less. canEncodeAudio probed without the source rate, answered yes, and the encoder then threw mid-conversion: 30 of this week's 34 native failures, all reading reason "unknown" until 2.4.6 started capturing the detail (#88). Probe at the file's own sample rate and channel count instead. A rate the encoder rejects now takes the existing MP3-remux branch, which re-encodes nothing and works at any rate; a file that used to die at the convert stage produces a working MP4. Confirmed in branded Chrome: the 32 kHz fixture that crashed now remuxes and converts. No resampling; whether low-rate files are common enough to justify a resample-to-AAC upgrade is what the audio_hz telemetry will show. Fixes #88
1 parent a7d9778 commit 065db03

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎e2e/pipeline.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { ALL_FORMATS, BufferSource, Input } from "mediabunny";
55
const sampleZip = fileURLToPath(new URL("../test/files/sample.zip", import.meta.url));
66
const keyedCdg = fileURLToPath(new URL("../test/files/sample-key.cdg", import.meta.url));
77
const sampleMp3 = fileURLToPath(new URL("../test/files/sample.mp3", import.meta.url));
8+
const sampleCdg = fileURLToPath(new URL("../test/files/sample.cdg", import.meta.url));
9+
const lowRateMp3 = fileURLToPath(new URL("../test/files/sample-32k.mp3", import.meta.url));
810

911
/** Run a conversion and return the output MP4's size plus the video's metadata. */
1012
async function convert(page: Page, url: string) {
@@ -186,6 +188,20 @@ test.describe("conversion pipelines", () => {
186188
expect(bg.g).toBeLessThan(60);
187189
});
188190

191+
// Old rips are often 32 kHz or below, and Chrome's and Edge's AAC encoders
192+
// take nothing under 44.1 kHz. The capability probe used to ask without the
193+
// source rate, get a yes, and crash mid-conversion (#88); now it asks at the
194+
// file's own rate and a rejected rate routes to the MP3 remux, which encodes
195+
// nothing and works at any rate. Either audio path is a pass here; failing
196+
// the conversion is the bug.
197+
test("converts a 32 kHz MP3 natively instead of crashing the AAC encoder", async ({ page }) => {
198+
test.skip(!(await nativeAvailable(page)), "no H.264 encoder in this browser");
199+
await page.goto("/?pipeline=webcodecs");
200+
await page.locator('input[type="file"]').setInputFiles([sampleCdg, lowRateMp3]);
201+
await expect(page.locator("video")).toBeVisible({ timeout: 90_000 });
202+
await expect(page.getByRole("button", { name: /try the original converter/i })).toHaveCount(0);
203+
});
204+
189205
// The safety net for the whole rollout: if the new pipeline fails on a device
190206
// or a rip we cannot reproduce, the user is one click from the one that has
191207
// worked for years, without anyone having to understand what a pipeline is.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cdg2mp4",
33
"private": true,
4-
"version": "2.4.6",
4+
"version": "2.4.7",
55
"type": "module",
66
"description": "Karaokio CDG-to-MP4 converter — 100% in-browser, offline, powered by WebCodecs with an ffmpeg.wasm fallback.",
77
"scripts": {

‎src/lib/webcodecs/encode.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,22 @@ async function addAudioTrack(
266266
const track = await input.getPrimaryAudioTrack();
267267
if (!track) throw new Error("The .mp3 file has no audio track.");
268268
const duration = await input.computeDuration();
269-
// The source sample rate rides along to telemetry: an AAC encoder can exist
270-
// and still reject a rate (Chrome's takes nothing below 44.1 kHz, and old
271-
// rips are often 32 kHz or less), and that is invisible in aggregate without
272-
// the rate on the event (#88).
273-
const sampleRate = (await track.getDecoderConfig().catch(() => null))?.sampleRate;
269+
// Also telemetry (#88): the rate rides along on conversion events.
270+
const config = await track.getDecoderConfig().catch(() => null);
271+
const sampleRate = config?.sampleRate;
274272

275273
const canDecode = await track.canDecode();
276-
const canAac = await canEncodeAudio("aac", { quality: AUDIO_QUALITY });
274+
// Probe at the file's own sample rate and channel count, not in the
275+
// abstract: an AAC encoder can exist and still reject a rate (Chrome's and
276+
// Edge's take nothing below 44.1 kHz, and old rips are often 32 kHz or
277+
// less). Asking without the rate said yes, and the encoder then threw mid-
278+
// conversion (#88); asking precisely routes those files to the MP3 remux
279+
// below, which re-encodes nothing and works at any rate.
280+
const canAac = await canEncodeAudio("aac", {
281+
quality: AUDIO_QUALITY,
282+
sampleRate,
283+
numberOfChannels: config?.numberOfChannels,
284+
});
277285

278286
if (canDecode && canAac) {
279287
const source = new AudioSampleSource({ codec: "aac", quality: AUDIO_QUALITY });

‎test/files/sample-32k.mp3‎

63.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)