fix(audio): expose sample rate setting in Convert Audio (#561)

The Convert Audio tool promised configurable bitrate, sample rate, and channel count, but only format and bitrate were exposed. Adds an optional sampleRate setting (8000 to 96000 Hz, omitted = preserve source) wired through the Zod schema, the FFmpeg -ar flag, the standalone settings panel, and the pipeline builder controls.

Impossible combinations fail loudly instead of degrading silently: MP3 + 96000 Hz is rejected (libmp3lame caps at 48 kHz), and MP3 bitrates above the encoder ceiling at low rates (64 kbps at 8 kHz, 160 kbps at 16/22.05 kHz) are rejected rather than clamped. The UI offers only legal combinations and sanitizes stored pipeline settings on load.

Docs updated in English plus all 20 localized pages with refreshed i18n_source_hash stamps; two new UI strings added to all 21 locales.

Fixes #558
This commit is contained in:
SnapOtter
2026-07-18 10:14:50 +08:00
committed by GitHub
parent 6ecc598fc4
commit d4eaa655b2
47 changed files with 541 additions and 133 deletions
+68 -1
View File
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { cleanup, render, screen } from "@testing-library/react";
import { cleanup, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ConvertAudioControls } from "@/components/tools/convert-audio-settings";
@@ -42,6 +42,73 @@ describe("ConvertAudioControls", () => {
expect.objectContaining({ format: "wav", bitrateKbps: 256 }),
);
});
it("omits sampleRate by default (preserve original)", () => {
const onChange = vi.fn();
render(<ConvertAudioControls onChange={onChange} />);
expect(onChange.mock.lastCall?.[0]).not.toHaveProperty("sampleRate");
});
it("emits the chosen sample rate on change", async () => {
const onChange = vi.fn();
render(<ConvertAudioControls onChange={onChange} />);
await userEvent.selectOptions(screen.getByLabelText(/sample rate/i), "44100");
expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ sampleRate: 44100 }));
});
it("initializes sampleRate from incoming settings", () => {
const onChange = vi.fn();
render(
<ConvertAudioControls settings={{ format: "wav", sampleRate: 48000 }} onChange={onChange} />,
);
expect(onChange).toHaveBeenCalledWith(
expect.objectContaining({ format: "wav", sampleRate: 48000 }),
);
});
it("caps bitrate options for low mp3 sample rates and resets the bitrate", async () => {
const onChange = vi.fn();
render(<ConvertAudioControls onChange={onChange} />);
await userEvent.selectOptions(screen.getByLabelText(/sample rate/i), "8000");
// 192 kbps is illegal at 8 kHz (libmp3lame caps at 64); the control must
// reset to a legal value rather than let ffmpeg clamp silently.
expect(onChange).toHaveBeenLastCalledWith(
expect.objectContaining({ sampleRate: 8000, bitrateKbps: 64 }),
);
const bitrateSelect = screen.getByLabelText(/bitrate/i);
const options = [...bitrateSelect.querySelectorAll("option")].map((o) => o.value);
expect(options).toEqual(["32", "48", "64"]);
// Moving back to a full-range rate restores the regular options.
await userEvent.selectOptions(screen.getByLabelText(/sample rate/i), "44100");
expect(onChange).toHaveBeenLastCalledWith(
expect.objectContaining({ sampleRate: 44100, bitrateKbps: 192 }),
);
});
it("sanitizes an out-of-range stored sampleRate to preserve-original", () => {
const onChange = vi.fn();
render(
<ConvertAudioControls settings={{ format: "mp3", sampleRate: 96000 }} onChange={onChange} />,
);
// A stale or API-written value the UI cannot represent must not be emitted
// behind a blank select.
expect(onChange.mock.lastCall?.[0]).toMatchObject({ format: "mp3" });
expect(onChange.mock.lastCall?.[0]).not.toHaveProperty("sampleRate");
});
it("hides 96 kHz for mp3 and drops it when switching to mp3", async () => {
const onChange = vi.fn();
render(<ConvertAudioControls settings={{ format: "wav" }} onChange={onChange} />);
const rateSelect = screen.getByLabelText(/sample rate/i);
await userEvent.selectOptions(rateSelect, "96000");
expect(onChange).toHaveBeenLastCalledWith(expect.objectContaining({ sampleRate: 96000 }));
await userEvent.selectOptions(screen.getByLabelText(/output format/i), "mp3");
expect(onChange.mock.lastCall?.[0]).toMatchObject({ format: "mp3" });
expect(onChange.mock.lastCall?.[0]).not.toHaveProperty("sampleRate");
expect(within(rateSelect).queryByRole("option", { name: /96000/ })).toBeNull();
});
});
describe("TrimVideoControls", async () => {