Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(stt): kill the audio stream probe when extraction is aborted
EtienneLescot82ea9ea05380
diff --git a/electron/media/audioPeaks.test.ts b/electron/media/audioPeaks.test.tsindex 644f9bebb..0376651c2 100644--- a/electron/media/audioPeaks.test.ts+++ b/electron/media/audioPeaks.test.ts@@ -8,6 +8,7 @@ import {countAudioStreams,decodePeaks,ffmpegCandidates,+ mediaClockAudioArgs,peakBlockCount,resolveFfmpeg,} from "./audioPeaks";@@ -418,4 +419,10 @@ describe.skipIf(!ffmpegOnPath)("peaks on the media clock, with real FFmpeg", ()const peaks = await decodePeaks("ffmpeg", twoTracks, 4);expect(firstAudibleSec(peaks)).toBe(1);});++ it("kills the stream probe on abort instead of waiting it out", async () => {+ // Killed before it lists anything, the probe counts no stream at all.+ const args = await mediaClockAudioArgs("ffmpeg", twoTracks, AbortSignal.abort());+ expect(args).not.toContain("-filter_complex");+ });});diff --git a/electron/media/audioPeaks.ts b/electron/media/audioPeaks.tsindex 13f66a93f..317747edf 100644--- a/electron/media/audioPeaks.ts+++ b/electron/media/audioPeaks.ts@@ -158,15 +158,20 @@ export function countAudioStreams(listing: string): number {return listing.match(/^\s*Stream #0:\d+\S*: Audio:/gm)?.length ?? 0;}-/** Any failure counts as zero: the decode that follows then runs as it always did,- * and reports the problem itself. */-function probeAudioStreams(ffmpeg: string, filePath: string): Promise<number> {+/** Any failure counts as zero, an abort included: the caller then checks its signal,+ * and otherwise the decode runs as it always did and reports the problem itself. */+function probeAudioStreams(+ ffmpeg: string,+ filePath: string,+ signal?: AbortSignal,+): Promise<number> {return new Promise((resolve) => {let listing = "";const child = spawn(ffmpeg, ["-hide_banner", "-i", filePath], {stdio: ["ignore", "ignore", "pipe"],timeout: PROBE_TIMEOUT_MS,killSignal: "SIGKILL",+ signal,});child.stderr.on("data", (c: Buffer) => {listing += c.toString();@@ -188,8 +193,12 @@ function probeAudioStreams(ffmpeg: string, filePath: string): Promise<number> {* mixes by sample count, not timestamp, so each stream is aligned first, and* `normalize=0` sums like the compositor instead of averaging.*/-export async function mediaClockAudioArgs(ffmpeg: string, filePath: string): Promise<string[]> {- const streams = await probeAudioStreams(ffmpeg, filePath);+export async function mediaClockAudioArgs(+ ffmpeg: string,+ filePath: string,+ signal?: AbortSignal,+): Promise<string[]> {+ const streams = await probeAudioStreams(ffmpeg, filePath, signal);if (streams < 2) return ["-vn", "-af", ON_MEDIA_CLOCK];const aligned = Array.from({ length: streams }, (_, i) => `[0:a:${i}]${ON_MEDIA_CLOCK}[a${i}]`);const labels = aligned.map((_, i) => `[a${i}]`).join("");diff --git a/electron/stt/extractAudio.test.ts b/electron/stt/extractAudio.test.tsindex a55b1a555..d87217668 100644--- a/electron/stt/extractAudio.test.ts+++ b/electron/stt/extractAudio.test.ts@@ -4,11 +4,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest";const spawnMock = vi.fn();const resolveFfmpegMock = vi.fn<() => string | null>();+const audioArgsMock = vi.fn(async (..._args: unknown[]) => ["-vn", "-af", "aresample=first_pts=0"]);vi.mock("node:child_process", () => ({ spawn: (...args: unknown[]) => spawnMock(...args) }));vi.mock("../media/audioPeaks", () => ({resolveFfmpeg: () => resolveFfmpegMock(),- mediaClockAudioArgs: async () => ["-vn", "-af", "aresample=first_pts=0"],+ mediaClockAudioArgs: (...args: unknown[]) => audioArgsMock(...args),}));const { extractMono16kPcm, FfmpegUnavailableError, MediaUnreadableError, NoAudioTrackError } =@@ -202,6 +203,20 @@ describe("extractMono16kPcm", () => {expect(child.kill).toHaveBeenCalled();});+ it("hands the abort to the stream probe, and does not decode after it", async () => {+ // The probe is a process too: a cancel must not wait out a wedged one.+ const controller = new AbortController();+ audioArgsMock.mockImplementationOnce(async (..._args: unknown[]) => {+ controller.abort();+ return ["-vn"];+ });+ await expect(+ extractMono16kPcm("/tmp/a.mp3", { signal: controller.signal }),+ ).rejects.toMatchObject({ name: "AbortError" });+ expect(audioArgsMock).toHaveBeenCalledWith("/usr/bin/ffmpeg", "/tmp/a.mp3", controller.signal);+ expect(spawnMock).not.toHaveBeenCalled();+ });+it("does not spawn at all when the signal is already aborted", async () => {const controller = new AbortController();controller.abort();diff --git a/electron/stt/extractAudio.ts b/electron/stt/extractAudio.tsindex f9697460b..2ffebf559 100644--- a/electron/stt/extractAudio.ts+++ b/electron/stt/extractAudio.ts@@ -82,8 +82,8 @@ export async function extractMono16kPcm(const ffmpeg = resolveFfmpeg();if (!ffmpeg) throw new FfmpegUnavailableError();if (options.signal?.aborted) throw new DOMException("Aborted", "AbortError");- const audioArgs = await mediaClockAudioArgs(ffmpeg, filePath);- // Again: an abort during the probe fired before the listener below existed.+ const audioArgs = await mediaClockAudioArgs(ffmpeg, filePath, options.signal);+ // Again: an abort during the probe (which it kills) fired before the listener below.if (options.signal?.aborted) throw new DOMException("Aborted", "AbortError");const child = spawn(