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(export): mix all source audio tracks so the mic isn't dropped
Glen Barnescdea61543cf0
diff --git a/.gitignore b/.gitignoreindex 61dc9d2fc..33cf7a9d2 100644--- a/.gitignore+++ b/.gitignore@@ -49,6 +49,7 @@ playwright-report/# Vitest browser mode screenshots__screenshots__/+.vitest-attachments/# shell files/shell.shdiff --git a/src/lib/exporter/audioEncoder.test.ts b/src/lib/exporter/audioEncoder.test.tsindex 0fb3708c5..13f29af10 100644--- a/src/lib/exporter/audioEncoder.test.ts+++ b/src/lib/exporter/audioEncoder.test.ts@@ -1,5 +1,5 @@import { afterEach, describe, expect, it, vi } from "vitest";-import { AudioProcessor, downmixPlanarChannelsForExport } from "./audioEncoder";+import { AudioProcessor, downmixPlanarChannelsForExport, mixPlanarSources } from "./audioEncoder";describe("AudioProcessor.selectSupportedExportCodec", () => {afterEach(() => {@@ -70,3 +70,95 @@ describe("downmixPlanarChannelsForExport", () => {expect(Array.from(stereo)).toEqual([0.25, -0.5, 0.25, -0.5]);});});++describe("mixPlanarSources", () => {+ it("sums overlapping stereo sources sample-aligned", () => {+ const system = {+ planes: [new Float32Array([0.1, 0.2, 0.3]), new Float32Array([0.1, 0.2, 0.3])],+ startFrame: 0,+ };+ const mic = {+ planes: [new Float32Array([0.2, 0.2, 0.2]), new Float32Array([0.4, 0.4, 0.4])],+ startFrame: 0,+ };++ const mixed = mixPlanarSources([system, mic], 2, 3);++ expect(mixed).toHaveLength(2);+ expect(Array.from(mixed[0])).toEqual([+ expect.closeTo(0.3, 5),+ expect.closeTo(0.4, 5),+ expect.closeTo(0.5, 5),+ ]);+ expect(Array.from(mixed[1])).toEqual([+ expect.closeTo(0.5, 5),+ expect.closeTo(0.6, 5),+ expect.closeTo(0.7, 5),+ ]);+ });++ it("recovers a track when the other is silent (the #108 case)", () => {+ // System audio captured silence; the microphone track holds the real signal.+ const silentSystem = {+ planes: [new Float32Array([0, 0, 0]), new Float32Array([0, 0, 0])],+ startFrame: 0,+ };+ const mic = {+ planes: [new Float32Array([0.5, -0.5, 0.25]), new Float32Array([0.5, -0.5, 0.25])],+ startFrame: 0,+ };++ const mixed = mixPlanarSources([silentSystem, mic], 2, 3);++ expect(Array.from(mixed[0])).toEqual([+ expect.closeTo(0.5, 5),+ expect.closeTo(-0.5, 5),+ expect.closeTo(0.25, 5),+ ]);+ });++ it("aligns each source at its startFrame offset", () => {+ const early = { planes: [new Float32Array([1, 1])], startFrame: 0 };+ // Mic packets often start slightly after the video (source start_time > 0).+ const late = { planes: [new Float32Array([0.5, 0.5])], startFrame: 2 };++ const mixed = mixPlanarSources([early, late], 1, 4);++ expect(Array.from(mixed[0])).toEqual([+ expect.closeTo(1, 5),+ expect.closeTo(1, 5),+ expect.closeTo(0.5, 5),+ expect.closeTo(0.5, 5),+ ]);+ });++ it("upmixes a mono source to stereo before mixing", () => {+ const mono = { planes: [new Float32Array([0.3, 0.3])], startFrame: 0 };+ const stereo = {+ planes: [new Float32Array([0.1, 0.1]), new Float32Array([0.2, 0.2])],+ startFrame: 0,+ };++ const mixed = mixPlanarSources([mono, stereo], 2, 2);++ expect(Array.from(mixed[0])).toEqual([expect.closeTo(0.4, 5), expect.closeTo(0.4, 5)]);+ expect(Array.from(mixed[1])).toEqual([expect.closeTo(0.5, 5), expect.closeTo(0.5, 5)]);+ });++ it("clamps the summed signal to [-1, 1] to avoid overflow", () => {+ const a = { planes: [new Float32Array([0.8, -0.8])], startFrame: 0 };+ const b = { planes: [new Float32Array([0.8, -0.8])], startFrame: 0 };++ const mixed = mixPlanarSources([a, b], 1, 2);++ expect(Array.from(mixed[0])).toEqual([1, -1]);+ });++ it("ignores samples that fall beyond the mixed length", () => {+ const source = { planes: [new Float32Array([0.5, 0.5, 0.5])], startFrame: 1 };++ const mixed = mixPlanarSources([source], 1, 2);++ expect(Array.from(mixed[0])).toEqual([expect.closeTo(0, 5), expect.closeTo(0.5, 5)]);+ });+});diff --git a/src/lib/exporter/audioEncoder.ts b/src/lib/exporter/audioEncoder.tsindex b97aea1fd..9e7e85cae 100644--- a/src/lib/exporter/audioEncoder.ts+++ b/src/lib/exporter/audioEncoder.ts@@ -1,4 +1,4 @@-import { WebDemuxer } from "web-demuxer";+import { AVMediaType, type WebAVStream, WebDemuxer } from "web-demuxer";import {MAX_NATIVE_PLAYBACK_RATE,type SpeedRegion,@@ -133,6 +133,68 @@ function getStereoDownmixWeights(sourceChannels: number) {};}+/** One decoded audio source to be mixed: its per-channel PCM and where it starts on the timeline. */+export interface PlanarAudioSource {+ /** Per-channel planar samples (`channels[c][frame]`). */+ planes: Float32Array[];+ /** Timeline sample offset of this source's first sample (from the stream's start_time). */+ startFrame: number;+}++/**+ * Mixes several decoded audio sources into a single planar buffer.+ *+ * Native macOS recordings write system audio and the microphone as two separate+ * tracks in the same file. The exporter historically decoded only the one track+ * FFmpeg's `av_find_best_stream` selects — which, when both tracks are marked+ * default, is the first (system) track. If nothing was playing that track is+ * silent, so the mic was dropped from the export (issue #108). Mixing every audio+ * source together mirrors the browser recorder, which already blends system + mic+ * into one track before encoding.+ *+ * Each source is first downmixed to `targetChannels`, then summed sample-aligned at+ * its `startFrame`. The result is clamped to [-1, 1] so a loud overlap can't wrap or+ * overflow the encoder. Sources of differing lengths and offsets are supported;+ * samples past `totalFrames` are dropped.+ */+export function mixPlanarSources(+ sources: PlanarAudioSource[],+ targetChannels: number,+ totalFrames: number,+): Float32Array[] {+ const mixed = Array.from({ length: targetChannels }, () => new Float32Array(totalFrames));++ for (const source of sources) {+ const frameCount = source.planes[0]?.length ?? 0;+ if (frameCount === 0) continue;++ // Reuse the export downmix so mono→stereo, surround→stereo, etc. match the+ // single-track path exactly. Output is planar-concatenated: [ch0…, ch1…].+ const downmixed = downmixPlanarChannelsForExport(source.planes, targetChannels);++ for (let channel = 0; channel < targetChannels; channel++) {+ const target = mixed[channel];+ const base = channel * frameCount;+ for (let frame = 0; frame < frameCount; frame++) {+ const outFrame = source.startFrame + frame;+ if (outFrame < 0) continue;+ if (outFrame >= totalFrames) break;+ target[outFrame] += downmixed[base + frame];+ }+ }+ }++ for (const channel of mixed) {+ for (let frame = 0; frame < channel.length; frame++) {+ const sample = channel[frame];+ if (sample > 1) channel[frame] = 1;+ else if (sample < -1) channel[frame] = -1;+ }+ }++ return mixed;+}+export function downmixPlanarChannelsForExport(sourcePlanes: Float32Array[],targetChannels: number,@@ -226,6 +288,269 @@ export class AudioProcessor {);}+ /** All audio streams in the source, in container order. Empty on failure. */+ private static async listAudioStreams(demuxer: WebDemuxer): Promise<WebAVStream[]> {+ try {+ const info = await demuxer.getMediaInfo();+ return info.streams.filter((s) => s.codec_type_string === "audio");+ } catch {+ return [];+ }+ }++ /**+ * Fully decodes one specific audio stream to contiguous planar PCM. Unlike+ * `demuxer.read("audio")`/`getDecoderConfig("audio")` — which resolve to the+ * single stream `av_find_best_stream` picks — this targets a stream by its+ * container index, so each track of a multi-track recording can be decoded+ * independently and then mixed. Returns null when the stream can't be decoded.+ */+ private async decodeAudioStreamToPlanes(+ demuxer: WebDemuxer,+ stream: WebAVStream,+ readEndSec: number | undefined,+ ): Promise<(PlanarAudioSource & { sampleRate: number; numberOfChannels: number }) | null> {+ const config: AudioDecoderConfig = {+ codec: stream.codec_string || "",+ sampleRate: stream.sample_rate,+ numberOfChannels: stream.channels,+ description: stream.extradata && stream.extradata.length > 0 ? stream.extradata : undefined,+ };+ if (!config.codec || !config.sampleRate || !config.numberOfChannels) return null;++ const support = await AudioDecoder.isConfigSupported(config);+ if (!support.supported) {+ console.warn(+ `[AudioProcessor] Audio codec not supported for stream ${stream.index}:`,+ config.codec,+ );+ return null;+ }++ const decoded: AudioData[] = [];+ const decoder = new AudioDecoder({+ output: (data: AudioData) => decoded.push(data),+ error: (e: DOMException) => console.error("[AudioProcessor] Decode error:", e),+ });+ decoder.configure(config);++ const safeReadEnd =+ typeof readEndSec === "number" && Number.isFinite(readEndSec) ? Math.max(0, readEndSec) : 0;+ const packetStream = demuxer.readAVPacket(+ 0,+ safeReadEnd,+ AVMediaType.AVMEDIA_TYPE_AUDIO,+ stream.index,+ );+ const reader = packetStream.getReader();+ try {+ while (!this.cancelled) {+ const { done, value: packet } = await reader.read();+ if (done || !packet) break;+ decoder.decode(+ new EncodedAudioChunk({+ type: packet.keyframe === 1 ? "key" : "delta",+ timestamp: Math.round(packet.timestamp * 1_000_000),+ duration: Math.round(packet.duration * 1_000_000),+ data: packet.data,+ }),+ );+ while (decoder.decodeQueueSize > DECODE_BACKPRESSURE_LIMIT && !this.cancelled) {+ await new Promise((resolve) => setTimeout(resolve, 1));+ }+ }+ } finally {+ try {+ await reader.cancel();+ } catch {+ /* reader already closed */+ }+ }++ if (decoder.state === "configured") {+ if (!this.cancelled) await decoder.flush();+ decoder.close();+ }++ if (this.cancelled || decoded.length === 0) {+ for (const d of decoded) d.close();+ return null;+ }++ // Decode order is normally monotonic; sort defensively so timeline placement+ // stays correct even if the decoder emits out of order.+ decoded.sort((a, b) => a.timestamp - b.timestamp);+ const sampleRate = decoded[0].sampleRate;+ const numberOfChannels = decoded[0].numberOfChannels;+ // Absolute timeline position of this stream's first sample (mic tracks often+ // start slightly after the video, e.g. start_time ~0.17s), so mixing keeps+ // every track locked to the same source-time origin the video uses.+ const startFrame = Math.max(0, Math.round((decoded[0].timestamp / 1_000_000) * sampleRate));++ let totalFrames = 0;+ for (const d of decoded) {+ const frameOffset =+ Math.round((d.timestamp / 1_000_000) * sampleRate) - startFrame + d.numberOfFrames;+ if (frameOffset > totalFrames) totalFrames = frameOffset;+ }++ const planes = Array.from({ length: numberOfChannels }, () => new Float32Array(totalFrames));+ for (const d of decoded) {+ const at = Math.max(0, Math.round((d.timestamp / 1_000_000) * sampleRate) - startFrame);+ const room = totalFrames - at;+ if (room > 0) {+ const take = Math.min(d.numberOfFrames, room);+ for (let c = 0; c < numberOfChannels; c++) {+ const temp = new Float32Array(d.numberOfFrames);+ d.copyTo(temp, { format: "f32-planar", planeIndex: c });+ planes[c].set(temp.subarray(0, take), at);+ }+ }+ d.close();+ }++ return { planes, startFrame, sampleRate, numberOfChannels };+ }++ /**+ * Decodes every audio stream and mixes them into one planar timeline (see+ * {@link mixPlanarSources}). Returns null when there is at most one audio stream+ * (callers keep the existing single-stream fast path) or when the streams can't+ * be mixed (e.g. mismatched sample rates — never the case for native captures,+ * which are all 48 kHz).+ */+ private async decodeMixedAudioPlanes(+ demuxer: WebDemuxer,+ streams: WebAVStream[],+ readEndSec: number | undefined,+ outputChannels: number,+ ): Promise<{ planes: Float32Array[]; sampleRate: number; numberOfChannels: number } | null> {+ if (streams.length < 2) return null;++ const sources: Array<PlanarAudioSource & { sampleRate: number }> = [];+ for (const stream of streams) {+ if (this.cancelled) return null;+ const decoded = await this.decodeAudioStreamToPlanes(demuxer, stream, readEndSec);+ if (decoded) sources.push(decoded);+ }++ if (sources.length === 0) return null;+ // A single decodable stream can't be "mixed" — fall back to the normal path.+ if (sources.length === 1) return null;++ const sampleRate = sources[0].sampleRate;+ if (sources.some((s) => s.sampleRate !== sampleRate)) {+ console.warn("[AudioProcessor] Audio streams differ in sample rate; skipping mix");+ return null;+ }++ const channels = Math.min(2, Math.max(1, outputChannels || 2));+ let totalFrames = 0;+ for (const s of sources) {+ const end = s.startFrame + (s.planes[0]?.length ?? 0);+ if (end > totalFrames) totalFrames = end;+ }+ if (totalFrames === 0) return null;++ const planes = mixPlanarSources(+ sources.map((s) => ({ planes: s.planes, startFrame: s.startFrame })),+ channels,+ totalFrames,+ );+ console.log(+ `[AudioProcessor] Mixed ${sources.length} audio tracks (${totalFrames} frames @ ${sampleRate}Hz)`,+ );+ return { planes, sampleRate, numberOfChannels: channels };+ }++ /**+ * Encodes a pre-mixed planar timeline into the muxer, applying the same trim+ * skipping + timestamp remap the single-stream trim-only path uses. Blocks are a+ * single AAC frame wide so trim boundaries land at the same granularity.+ */+ private async encodeMixedPlanarTimeline(+ mixed: { planes: Float32Array[]; sampleRate: number; numberOfChannels: number },+ muxer: VideoMuxer,+ sortedTrims: TrimRegion[],+ exportCodec: ExportAudioCodec,+ ): Promise<void> {+ const { planes, sampleRate } = mixed;+ const totalFrames = planes[0]?.length ?? 0;+ if (totalFrames === 0) return;++ const outputSampleRate = exportCodec.sampleRate || sampleRate;+ const outputChannels = exportCodec.numberOfChannels || mixed.numberOfChannels;++ const encodeConfig: AudioEncoderConfig = {+ codec: exportCodec.encoderCodec,+ sampleRate: outputSampleRate,+ numberOfChannels: outputChannels,+ bitrate: AUDIO_BITRATE,+ };+ const encodeSupport = await AudioEncoder.isConfigSupported(encodeConfig);+ if (!encodeSupport.supported) {+ console.warn(`[AudioProcessor] ${exportCodec.label} encoding not supported, skipping audio`);+ return;+ }++ const encodedChunks: { chunk: EncodedAudioChunk; meta?: EncodedAudioChunkMetadata }[] = [];+ const encoder = new AudioEncoder({+ output: (chunk: EncodedAudioChunk, meta?: EncodedAudioChunkMetadata) => {+ encodedChunks.push({ chunk, meta });+ },+ error: (e: DOMException) => console.error("[AudioProcessor] Encode error:", e),+ });+ encoder.configure(encodeConfig);++ // One AAC frame per block so trim skipping matches the packet granularity of+ // the single-stream path.+ const BLOCK_FRAMES = 1024;+ for (let start = 0; start < totalFrames && !this.cancelled; start += BLOCK_FRAMES) {+ const count = Math.min(BLOCK_FRAMES, totalFrames - start);+ const timestampMs = (start / sampleRate) * 1000;+ if (this.isInTrimRegion(timestampMs, sortedTrims)) continue;++ const trimOffsetMs = this.computeTrimOffset(timestampMs, sortedTrims);+ const adjustedTimestampUs = Math.max(+ 0,+ Math.round((start / sampleRate - trimOffsetMs / 1000) * 1_000_000),+ );++ const buffer = new Float32Array(count * outputChannels);+ for (let c = 0; c < outputChannels; c++) {+ const srcPlane = planes[Math.min(c, planes.length - 1)];+ buffer.set(srcPlane.subarray(start, start + count), c * count);+ }++ const audioData = new AudioData({+ format: "f32-planar",+ sampleRate: outputSampleRate,+ numberOfFrames: count,+ numberOfChannels: outputChannels,+ timestamp: adjustedTimestampUs,+ data: buffer.buffer as ArrayBuffer,+ });+ encoder.encode(audioData);+ audioData.close();++ while (encoder.encodeQueueSize > ENCODE_BACKPRESSURE_LIMIT && !this.cancelled) {+ await new Promise((resolve) => setTimeout(resolve, 1));+ }+ }++ if (encoder.state === "configured") {+ await encoder.flush();+ encoder.close();+ }++ for (const { chunk, meta } of encodedChunks) {+ if (this.cancelled) break;+ await muxer.addAudioChunk(chunk, meta);+ }++ console.log(`[AudioProcessor] Encoded mixed audio timeline: ${encodedChunks.length} chunks`);+ }+/*** Two modes: no speed regions uses the fast WebCodecs trim-only pipeline; speed* regions use the pitch-preserving rendered timeline pipeline.@@ -254,7 +579,12 @@ export class AudioProcessor {if (sortedSpeedRegions.length > 0) {const segments = buildSpeedSegments(validatedDurationSec, sortedTrims, sortedSpeedRegions);- if (maxTimelineSpeed(segments) > MAX_NATIVE_PLAYBACK_RATE) {+ // The real-time capture path plays the file through an <audio> element,+ // which decodes only one audio track — so multi-track recordings must take+ // the offline WebCodecs path, the only one that can decode every track and+ // mix them. (Losing the mic entirely is worse than WSOLA vs real-time.)+ const audioStreams = await AudioProcessor.listAudioStreams(demuxer);+ if (maxTimelineSpeed(segments) > MAX_NATIVE_PLAYBACK_RATE || audioStreams.length > 1) {await this.renderOfflineTimelineAudio(demuxer,muxer,@@ -293,6 +623,27 @@ export class AudioProcessor {readEndSec?: number,exportCodec?: ExportAudioCodec,): Promise<void> {+ // Multi-track recordings (native macOS writes system audio + mic as separate+ // tracks) must be mixed — reading only the best stream drops the other track,+ // which is how the mic went missing in issue #108. Single-track sources keep+ // the original fast path below.+ const audioStreams = await AudioProcessor.listAudioStreams(demuxer);+ if (audioStreams.length > 1 && exportCodec) {+ const mixed = await this.decodeMixedAudioPlanes(+ demuxer,+ audioStreams,+ readEndSec,+ exportCodec.numberOfChannels,+ );+ if (this.cancelled) return;+ if (mixed) {+ await this.encodeMixedPlanarTimeline(mixed, muxer, sortedTrims, exportCodec);+ return;+ }+ // Mixing unavailable (e.g. only one stream decoded); fall through to the+ // single-stream path rather than exporting silence.+ }+let audioConfig: AudioDecoderConfig;try {audioConfig = await demuxer.getDecoderConfig("audio");@@ -648,22 +999,42 @@ export class AudioProcessor {exportCodec: ExportAudioCodec,targetFrameRate: number,): Promise<void> {- let audioConfig: AudioDecoderConfig;- try {- audioConfig = await demuxer.getDecoderConfig("audio");- } catch {- console.warn("[AudioProcessor] No audio track found, skipping");- return;- }+ // Multi-track sources are decoded and mixed up front, then fed through the same+ // segment/WSOLA machinery as a single pre-mixed stream. Single-track sources+ // stream through the decoder as before.+ const audioStreams = await AudioProcessor.listAudioStreams(demuxer);+ const lastSegmentEndSec = segments.length > 0 ? segments[segments.length - 1].endSec : 0;+ const mixReadEndSec = lastSegmentEndSec + 0.5;+ const mixed =+ audioStreams.length > 1+ ? await this.decodeMixedAudioPlanes(+ demuxer,+ audioStreams,+ mixReadEndSec,+ exportCodec.numberOfChannels,+ )+ : null;+ if (this.cancelled) return;- const codecCheck = await AudioDecoder.isConfigSupported(audioConfig);- if (!codecCheck.supported) {- console.warn("[AudioProcessor] Audio codec not supported:", audioConfig.codec);- return;+ let audioConfig: AudioDecoderConfig | null = null;+ if (!mixed) {+ try {+ audioConfig = await demuxer.getDecoderConfig("audio");+ } catch {+ console.warn("[AudioProcessor] No audio track found, skipping");+ return;+ }++ const codecCheck = await AudioDecoder.isConfigSupported(audioConfig);+ if (!codecCheck.supported) {+ console.warn("[AudioProcessor] Audio codec not supported:", audioConfig.codec);+ return;+ }}- const sampleRate = audioConfig.sampleRate || 48000;- const outputChannels = exportCodec.numberOfChannels || audioConfig.numberOfChannels || 2;+ const sampleRate = mixed?.sampleRate || audioConfig?.sampleRate || 48000;+ const outputChannels =+ exportCodec.numberOfChannels || mixed?.numberOfChannels || audioConfig?.numberOfChannels || 2;const outputSampleRate = exportCodec.sampleRate || sampleRate;const encodeConfig: AudioEncoderConfig = {@@ -812,89 +1183,104 @@ export class AudioProcessor {}};- // Streaming decode: frames arrive in source order via the decoder callback.- const pending: AudioData[] = [];- const decoder = new AudioDecoder({- output: (data: AudioData) => pending.push(data),- error: (e: DOMException) => console.error("[AudioProcessor] Decode error:", e),- });- decoder.configure(audioConfig);-- const drainPending = async () => {- while (pending.length > 0) {- if (this.cancelled) {- for (const d of pending) d.close();- pending.length = 0;- return;- }- const data = pending.shift();- if (!data) continue;- const frameCount = data.numberOfFrames;- const sourceChannels = data.numberOfChannels;- const srcPlanes: Float32Array[] = [];- for (let c = 0; c < sourceChannels; c++) {- const plane = new Float32Array(frameCount);- data.copyTo(plane, { format: "f32-planar", planeIndex: c });- srcPlanes.push(plane);- }- // Raw source timestamps — the same origin the video decoder uses- // (streamingDecoder), so audio and video stay in one coordinate system.- // Frames before the first segment start (e.g. negative-timestamp codec- // preroll, or a leading trim) are correctly discarded by routeFrame.- const frameStartSample = Math.round((data.timestamp / 1_000_000) * sampleRate);- data.close();-- // Match source channels through untouched; otherwise downmix (to 1 or 2).- let planes: Float32Array[];- if (outputChannels === sourceChannels) {- planes = srcPlanes;- } else {- const downmixed = downmixPlanarChannelsForExport(srcPlanes, outputChannels);- planes = [];- for (let c = 0; c < outputChannels; c++) {- planes.push(downmixed.subarray(c * frameCount, (c + 1) * frameCount));- }- }-- routeFrame(planes, frameStartSample, frameCount);+ if (mixed) {+ // Pre-mixed timeline (multi-track): route it through the same segment/WSOLA+ // machinery in source order, in fixed-size chunks. The mixed buffer starts+ // at source-time 0 so a chunk's start index is its absolute source sample.+ const total = mixed.planes[0]?.length ?? 0;+ const CHUNK_FRAMES = 4096;+ for (let start = 0; start < total && !this.cancelled; start += CHUNK_FRAMES) {+ const count = Math.min(CHUNK_FRAMES, total - start);+ const chunkPlanes = mixed.planes.map((p) => p.subarray(start, start + count));+ routeFrame(chunkPlanes, start, count);flushOutput(false);-while (encoder.encodeQueueSize > ENCODE_BACKPRESSURE_LIMIT && !this.cancelled) {await new Promise((resolve) => setTimeout(resolve, 1));}}- };+ } else if (audioConfig) {+ // Streaming decode: frames arrive in source order via the decoder callback.+ const pending: AudioData[] = [];+ const decoder = new AudioDecoder({+ output: (data: AudioData) => pending.push(data),+ error: (e: DOMException) => console.error("[AudioProcessor] Decode error:", e),+ });+ decoder.configure(audioConfig);- // Read past the last segment's source end (mirrors streamingDecoder's +0.5s- // window so both paths read the same distance past the boundary).- const lastSegmentEndSec = segments.length > 0 ? segments[segments.length - 1].endSec : 0;- const readEndSec = lastSegmentEndSec + 0.5;- const reader = demuxer.read("audio", 0, readEndSec).getReader();- try {- while (!this.cancelled) {- const { done, value: chunk } = await reader.read();- if (done || !chunk) break;- decoder.decode(chunk);- while (decoder.decodeQueueSize > DECODE_BACKPRESSURE_LIMIT && !this.cancelled) {- await new Promise((resolve) => setTimeout(resolve, 1));+ const drainPending = async () => {+ while (pending.length > 0) {+ if (this.cancelled) {+ for (const d of pending) d.close();+ pending.length = 0;+ return;+ }+ const data = pending.shift();+ if (!data) continue;+ const frameCount = data.numberOfFrames;+ const sourceChannels = data.numberOfChannels;+ const srcPlanes: Float32Array[] = [];+ for (let c = 0; c < sourceChannels; c++) {+ const plane = new Float32Array(frameCount);+ data.copyTo(plane, { format: "f32-planar", planeIndex: c });+ srcPlanes.push(plane);+ }+ // Raw source timestamps — the same origin the video decoder uses+ // (streamingDecoder), so audio and video stay in one coordinate system.+ // Frames before the first segment start (e.g. negative-timestamp codec+ // preroll, or a leading trim) are correctly discarded by routeFrame.+ const frameStartSample = Math.round((data.timestamp / 1_000_000) * sampleRate);+ data.close();++ // Match source channels through untouched; otherwise downmix (to 1 or 2).+ let planes: Float32Array[];+ if (outputChannels === sourceChannels) {+ planes = srcPlanes;+ } else {+ const downmixed = downmixPlanarChannelsForExport(srcPlanes, outputChannels);+ planes = [];+ for (let c = 0; c < outputChannels; c++) {+ planes.push(downmixed.subarray(c * frameCount, (c + 1) * frameCount));+ }+ }++ routeFrame(planes, frameStartSample, frameCount);+ flushOutput(false);++ while (encoder.encodeQueueSize > ENCODE_BACKPRESSURE_LIMIT && !this.cancelled) {+ await new Promise((resolve) => setTimeout(resolve, 1));+ }}- await drainPending();- }- } finally {+ };++ // Read past the last segment's source end (mirrors streamingDecoder's +0.5s+ // window so both paths read the same distance past the boundary).+ const reader = demuxer.read("audio", 0, mixReadEndSec).getReader();try {- await reader.cancel();- } catch {- /* reader already closed */+ while (!this.cancelled) {+ const { done, value: chunk } = await reader.read();+ if (done || !chunk) break;+ decoder.decode(chunk);+ while (decoder.decodeQueueSize > DECODE_BACKPRESSURE_LIMIT && !this.cancelled) {+ await new Promise((resolve) => setTimeout(resolve, 1));+ }+ await drainPending();+ }+ } finally {+ try {+ await reader.cancel();+ } catch {+ /* reader already closed */+ }}- }- if (decoder.state === "configured") {- if (!this.cancelled) {- await decoder.flush();+ if (decoder.state === "configured") {+ if (!this.cancelled) {+ await decoder.flush();+ }+ decoder.close();}- decoder.close();+ await drainPending();}- await drainPending();if (this.cancelled) {if (encoder.state === "configured") encoder.close();diff --git a/src/lib/exporter/audioMixExport.browser.test.ts b/src/lib/exporter/audioMixExport.browser.test.tsnew file mode 100644index 000000000..0897ba979--- /dev/null+++ b/src/lib/exporter/audioMixExport.browser.test.ts@@ -0,0 +1,90 @@+import { describe, expect, it } from "vitest";+import dualAudioUrl from "../../../tests/fixtures/sample-dual-audio.mp4?url";+import { VideoExporter } from "./videoExporter";++/**+ * Regression test for issue #108: native macOS recordings write system audio and+ * the microphone as two separate tracks. FFmpeg's `av_find_best_stream` (used by+ * web-demuxer) resolves the bare "audio" selector to the first track, so when the+ * system-audio track is silent the exporter dropped the mic and produced silence.+ *+ * The fixture mirrors that layout: track 0 is silent, track 1 is a 440 Hz tone.+ * A correct export mixes both, so the output must carry the tone.+ */+async function measureAudioRms(blob: Blob): Promise<{ rms: number; hasAudio: boolean }> {+ const bytes = await blob.arrayBuffer();+ const audioContext = new AudioContext();+ try {+ const audioBuffer = await audioContext.decodeAudioData(bytes.slice(0));+ if (audioBuffer.numberOfChannels === 0 || audioBuffer.length === 0) {+ return { rms: 0, hasAudio: false };+ }+ let sumSquares = 0;+ let sampleCount = 0;+ for (let channel = 0; channel < audioBuffer.numberOfChannels; channel++) {+ const data = audioBuffer.getChannelData(channel);+ for (let i = 0; i < data.length; i++) {+ sumSquares += data[i] * data[i];+ sampleCount++;+ }+ }+ return { rms: Math.sqrt(sumSquares / sampleCount), hasAudio: true };+ } finally {+ await audioContext.close();+ }+}++describe("Multi-track audio export (real browser)", () => {+ it("mixes both audio tracks so a silent first track can't drop the mic (#108)", async () => {+ const exporter = new VideoExporter({+ videoUrl: dualAudioUrl,+ width: 320,+ height: 180,+ frameRate: 15,+ bitrate: 1_000_000,+ wallpaper: "#1a1a2e",+ zoomRegions: [],+ showShadow: false,+ shadowIntensity: 0,+ showBlur: false,+ cropRegion: { x: 0, y: 0, width: 1, height: 1 },+ });++ const result = await exporter.export();+ expect(result.success, result.error).toBe(true);+ expect(result.blob).toBeInstanceOf(Blob);++ const { rms, hasAudio } = await measureAudioRms(result.blob!);+ expect(hasAudio).toBe(true);+ // Silence (the first track alone) sits near 0; the mixed-in 440 Hz tone lifts+ // RMS well above the noise floor. A comfortable threshold below the tone's+ // real level (~0.08) but far above silence.+ expect(rms).toBeGreaterThan(0.01);+ });++ it("mixes both audio tracks through the speed-region (offline) path too", async () => {+ const exporter = new VideoExporter({+ videoUrl: dualAudioUrl,+ width: 320,+ height: 180,+ frameRate: 15,+ bitrate: 1_000_000,+ wallpaper: "#1a1a2e",+ zoomRegions: [],+ // A speed region routes audio through renderOfflineTimelineAudio, a distinct+ // mixing branch from the trim-only path exercised above.+ speedRegions: [{ id: "speed", startMs: 500, endMs: 1500, speed: 2 }],+ showShadow: false,+ shadowIntensity: 0,+ showBlur: false,+ cropRegion: { x: 0, y: 0, width: 1, height: 1 },+ });++ const result = await exporter.export();+ expect(result.success, result.error).toBe(true);++ const { rms, hasAudio } = await measureAudioRms(result.blob!);+ expect(hasAudio).toBe(true);+ expect(rms).toBeGreaterThan(0.01);+ });+});diff --git a/src/lib/exporter/streamingDecoder.ts b/src/lib/exporter/streamingDecoder.tsindex 54f4b9f8d..8dd763470 100644--- a/src/lib/exporter/streamingDecoder.ts+++ b/src/lib/exporter/streamingDecoder.ts@@ -67,6 +67,8 @@ export interface DecodedVideoInfo {codec: string;hasAudio: boolean;audioCodec?: string;+ /** Number of audio streams in the container (native macOS writes system + mic separately). */+ audioStreamCount: number;}type EarlyDecodeEndCheck = {@@ -283,7 +285,8 @@ export class StreamingVideoDecoder {}}- const audioStream = mediaInfo.streams.find((s) => s.codec_type_string === "audio");+ const audioStreams = mediaInfo.streams.filter((s) => s.codec_type_string === "audio");+ const audioStream = audioStreams[0];// Scan video packets for the true content boundary; MediaRecorder (especially on// Linux) writes unreliable container durations and packet timestamps are ground truth.@@ -329,6 +332,7 @@ export class StreamingVideoDecoder {codec: videoStream?.codec_string || "unknown",hasAudio: !!audioStream,audioCodec: audioStream?.codec_string,+ audioStreamCount: audioStreams.length,};return this.metadata;diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.tsindex 2703a9437..03af3fa88 100644--- a/src/lib/exporter/videoExporter.ts+++ b/src/lib/exporter/videoExporter.ts@@ -121,14 +121,14 @@ function isDefaultCrop(cropRegion: CropRegion) {export function isSourceCopyFastPathEligible(config: VideoExporterConfig,- videoInfo: { width: number; height: number },+ videoInfo: { width: number; height: number; audioStreamCount?: number },) {return getSourceCopyFastPathBlockers(config, videoInfo).length === 0;}export function getSourceCopyFastPathBlockers(config: VideoExporterConfig,- videoInfo: { width: number; height: number },+ videoInfo: { width: number; height: number; audioStreamCount?: number },) {const blockers: string[] = [];@@ -137,6 +137,13 @@ export function getSourceCopyFastPathBlockers(`output-size ${config.width}x${config.height} differs from source ${videoInfo.width}x${videoInfo.height}`,);}+ // Copying the source verbatim would carry over its multiple audio tracks (native+ // macOS writes system audio + mic separately). Most players play only the first,+ // which is often the silent system track — so multi-track sources must go through+ // the full pipeline, which mixes every track into one (issue #108).+ if ((videoInfo.audioStreamCount ?? 0) > 1) {+ blockers.push("source has multiple audio tracks (must be mixed)");+ }if (config.webcamVideoUrl) blockers.push("webcam overlay is enabled");if (hasActiveTimeRegions(config.trimRegions)) blockers.push("trim regions are present");if (hasActiveSpeedRegions(config.speedRegions)) blockers.push("speed regions are present");diff --git a/tests/fixtures/sample-dual-audio.mp4 b/tests/fixtures/sample-dual-audio.mp4new file mode 100644index 000000000..e0a504dd4Binary files /dev/null and b/tests/fixtures/sample-dual-audio.mp4 differ