// transcodeAudio's m4a: a 16 kHz mono voice (an ASR-rate source: an imported or recorded voice file) must come out. // ffmpeg 9.1's own AAC encoder, given no bitrate and its default coder, never finishes on such a source: it writes a // 44-byte file and loops, and SIGTERM does not stop it (measured 25 Nov 2026 on the cast fixture's voice). A stated // bitrate avoids it, so CODEC_ARGS states one. import assert from "node:assert/strict"; import { existsSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { encodeWav16 } from "../lib/montage/voiceMatch.mjs"; import { VOICE_A, syntheticVoice, withSilence } from "../testing/audio/syntheticVoice.mjs"; import { hasFfmpeg } from "../testing/audio/corruptAudio.mjs"; import type { Paths } from "../lib/paths"; import { transcodeAudio } from "./transcode"; test("a 16 kHz mono voice transcodes to m4a and the encode finishes", { skip: !hasFfmpeg() && "no ffmpeg" }, async () => { const dir = mkdtempSync(path.join(os.tmpdir(), "transcode-aac-")); try { const videoDir = path.join(dir, "data", "v1"); const { mkdirSync } = await import("node:fs"); mkdirSync(videoDir, { recursive: true }); // The exact samples the cast fixture's encode hung on: 2.5 s of VOICE_A, 2.3 s of silence before, 3.8 s after. writeFileSync( path.join(videoDir, "source.wav"), encodeWav16(withSilence(syntheticVoice({ ...VOICE_A, seconds: 2.5 }), { before: 2.3, after: 3.8 })), ); const log: string[] = []; const started = Date.now(); await transcodeAudio({ paths: { ffmpegBin: process.env.FFMPEG_BIN ?? "ffmpeg" } as Paths, videoDir, sourceFilename: "source.wav", targetFormat: "m4a", onLog: (s) => log.push(s), // A hung encode is aborted (execa force-kills 5 s later) and transcodeAudio returns with no file. signal: AbortSignal.timeout(30_000), }); const out = path.join(videoDir, "audio.m4a"); assert.ok(existsSync(out), `audio.m4a written (took ${Date.now() - started} ms)\n${log.join("")}`); assert.ok(statSync(out).size > 10_000, `audio.m4a holds the audio, not a bare header: ${statSync(out).size} B`); } finally { rmSync(dir, { recursive: true, force: true }); } });