Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Fix silent MP4 exports with FFmpeg audio fallback

Wing90063f8ff27ae45
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 440e3a0a..1ccdbbfc 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -175,12 +175,26 @@ interface Window {
encoderName?: string;
error?: string;
}>;
- nativeVideoExportCancel: (
- sessionId: string,
- ) => Promise<{ success: boolean; error?: string }>;
- getVideoAudioFallbackPaths: (
- videoPath: string,
- ) => Promise<{ success: boolean; paths: string[]; error?: string }>;
+ nativeVideoExportCancel: (
+ sessionId: string,
+ ) => Promise<{ success: boolean; error?: string }>;
+ muxExportedVideoAudio: (
+ videoData: ArrayBuffer,
+ options?: {
+ audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
+ audioSourcePath?: string | null;
+ trimSegments?: Array<{ startMs: number; endMs: number }>;
+ editedAudioData?: ArrayBuffer;
+ editedAudioMimeType?: string | null;
+ },
+ ) => Promise<{
+ success: boolean;
+ data?: Uint8Array;
+ error?: string;
+ }>;
+ getVideoAudioFallbackPaths: (
+ videoPath: string,
+ ) => Promise<{ success: boolean; paths: string[]; error?: string }>;
setRecordingState: (recording: boolean) => Promise<void>;
getCursorTelemetry: (videoPath?: string) => Promise<{
success: boolean;
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 37186c0c..919e64f0 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -1152,6 +1152,25 @@ function loadFfmpegStatic() {
return null
}
+function resolveSystemFfmpegBinaryPath() {
+ const locator = process.platform === 'win32' ? 'where' : 'which'
+ const result = spawnSync(locator, ['ffmpeg'], {
+ encoding: 'utf-8',
+ windowsHide: true,
+ })
+
+ if (result.status !== 0) {
+ return null
+ }
+
+ const candidate = result.stdout
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .find((line) => line.length > 0)
+
+ return candidate || null
+}
+
function loadUiohookModule() {
const moduleExports = nodeRequire('uiohook-napi')
return (
@@ -1167,15 +1186,22 @@ function loadUiohookModule() {
function getFfmpegBinaryPath() {
const ffmpegStatic = loadFfmpegStatic()
- if (!ffmpegStatic || typeof ffmpegStatic !== 'string') {
- throw new Error('FFmpeg binary is unavailable. Install ffmpeg-static for this platform.')
+ if (ffmpegStatic && typeof ffmpegStatic === 'string') {
+ const bundledPath = app.isPackaged
+ ? ffmpegStatic.replace(/\.asar([\/\\])/, '.asar.unpacked$1')
+ : ffmpegStatic
+
+ if (existsSync(bundledPath)) {
+ return bundledPath
+ }
}
- if (app.isPackaged) {
- return ffmpegStatic.replace(/\.asar([\/\\])/, '.asar.unpacked$1')
+ const systemFfmpeg = resolveSystemFfmpegBinaryPath()
+ if (systemFfmpeg) {
+ return systemFfmpeg
}
- return ffmpegStatic
+ throw new Error('FFmpeg binary is unavailable. Install ffmpeg-static for this platform or make ffmpeg available on PATH.')
}
type NativeVideoExportSession = {
@@ -1586,6 +1612,34 @@ async function muxNativeVideoExportAudio(
}
}
+async function muxExportedVideoAudioBuffer(
+ videoData: ArrayBuffer,
+ options: NativeVideoExportFinishOptions,
+) {
+ const tempVideoPath = path.join(
+ app.getPath('temp'),
+ `recordly-export-video-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.mp4`,
+ )
+
+ try {
+ await fs.writeFile(tempVideoPath, Buffer.from(videoData))
+ const finalizedPath = await muxNativeVideoExportAudio(tempVideoPath, options)
+ const muxedData = await fs.readFile(finalizedPath)
+ return new Uint8Array(muxedData)
+ } finally {
+ await Promise.allSettled([
+ removeTemporaryExportFile(tempVideoPath),
+ removeTemporaryExportFile(`${tempVideoPath}.muxed.mp4`),
+ removeTemporaryExportFile(
+ path.join(
+ path.dirname(tempVideoPath),
+ `${path.basename(tempVideoPath, path.extname(tempVideoPath))}-final.mp4`,
+ ),
+ ),
+ ])
+ }
+}
+
/** Probe the duration of a media file (in seconds) using the container header. */
async function probeMediaDurationSeconds(filePath: string): Promise<number> {
const ffmpegPath = getFfmpegBinaryPath()
@@ -5531,6 +5585,24 @@ body{background:transparent;overflow:hidden;width:100vw;height:100vh}
},
)
+ ipcMain.handle(
+ 'mux-exported-video-audio',
+ async (_, videoData: ArrayBuffer, options?: NativeVideoExportFinishOptions) => {
+ try {
+ const data = await muxExportedVideoAudioBuffer(videoData, options ?? {})
+ return {
+ success: true,
+ data,
+ }
+ } catch (error) {
+ return {
+ success: false,
+ error: String(error),
+ }
+ }
+ },
+ )
+
ipcMain.handle('native-video-export-cancel', async (_, sessionId: string) => {
const session = nativeVideoExportSessions.get(sessionId)
if (!session) {
diff --git a/electron/preload.ts b/electron/preload.ts
index 4478a02b..aa17c976 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -161,6 +161,18 @@ contextBridge.exposeInMainWorld("electronAPI", {
});
});
},
+ muxExportedVideoAudio: (
+ videoData: ArrayBuffer,
+ options?: {
+ audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
+ audioSourcePath?: string | null;
+ trimSegments?: Array<{ startMs: number; endMs: number }>;
+ editedAudioData?: ArrayBuffer;
+ editedAudioMimeType?: string | null;
+ },
+ ) => {
+ return ipcRenderer.invoke("mux-exported-video-audio", videoData, options);
+ },
getVideoAudioFallbackPaths: (videoPath: string) => {
return ipcRenderer.invoke("get-video-audio-fallback-paths", videoPath);
},
diff --git a/package-lock.json b/package-lock.json
index 3db4e158..be372cdc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "recordly",
- "version": "1.1.18",
+ "version": "1.1.22",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "recordly",
- "version": "1.1.18",
+ "version": "1.1.22",
"hasInstallScript": true,
"dependencies": {
"capturekit": "^1.0.13",
diff --git a/package.json b/package.json
index 111525a4..0d37a181 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
"url": "https://github.com/webadderall/Recordly/issues"
},
"private": true,
- "version": "1.1.21",
+ "version": "1.1.22",
"type": "module",
"scripts": {
"dev": "vite",
diff --git a/src/lib/exporter/audioEncoder.ts b/src/lib/exporter/audioEncoder.ts
index 9cc5684c..2367b3e5 100644
--- a/src/lib/exporter/audioEncoder.ts
+++ b/src/lib/exporter/audioEncoder.ts
@@ -14,6 +14,23 @@ const ENCODE_BACKPRESSURE_LIMIT = 20
const MIN_SPEED_REGION_DELTA_MS = 0.0001
const MP4_AUDIO_CODEC = 'mp4a.40.2'
+export async function isAacAudioEncodingSupported(
+ sampleRate = 48_000,
+ numberOfChannels = 2,
+): Promise<boolean> {
+ try {
+ const support = await AudioEncoder.isConfigSupported({
+ codec: MP4_AUDIO_CODEC,
+ sampleRate,
+ numberOfChannels,
+ bitrate: AUDIO_BITRATE,
+ })
+ return support.supported === true
+ } catch {
+ return false
+ }
+}
+
type TrimLikeRegion = TrimRegion | ClipRegion
export class AudioProcessor {
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index 1cfc3dc6..c1c1d366 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -292,6 +292,8 @@ export class ModernVideoExporter {
const videoInfo = await this.streamingDecoder.loadMetadata(this.config.videoUrl);
this.metadataLoadTimeMs = this.getNowMs() - stageStartedAt;
const nativeAudioPlan = this.buildNativeAudioPlan(videoInfo);
+ const shouldUseFfmpegAudioFallback =
+ !useNativeEncoder && nativeAudioPlan.audioMode !== "none";
const effectiveDuration = this.streamingDecoder.getEffectiveDuration(
this.config.trimRegions,
this.config.speedRegions,
@@ -352,7 +354,7 @@ export class ModernVideoExporter {
if (!useNativeEncoder) {
const hasAudio = nativeAudioPlan.audioMode !== "none";
- this.muxer = new VideoMuxer(this.config, hasAudio);
+ this.muxer = new VideoMuxer(this.config, hasAudio && !shouldUseFfmpegAudioFallback);
await this.muxer.initialize();
}
@@ -451,7 +453,7 @@ export class ModernVideoExporter {
this.reportFinalizingProgress(totalFrames, 98);
await this.awaitWithFinalizationTimeout(this.pendingMuxing, "muxing queued video chunks");
- if (nativeAudioPlan.audioMode !== "none" && !this.cancelled) {
+ if (nativeAudioPlan.audioMode !== "none" && !shouldUseFfmpegAudioFallback && !this.cancelled) {
const demuxer = this.streamingDecoder.getDemuxer();
if (
demuxer ||
@@ -483,6 +485,26 @@ export class ModernVideoExporter {
const blob = await this.awaitWithFinalizationTimeout(this.muxer!.finalize(), "muxer finalization");
this.finalizationTimeMs = this.getNowMs() - stageStartedAt;
+ if (shouldUseFfmpegAudioFallback) {
+ console.warn(
+ `[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.`,
+ );
+ const muxedResult = await this.finalizeExportWithFfmpegAudio(blob, nativeAudioPlan);
+ if (!muxedResult.success || !muxedResult.blob) {
+ return {
+ success: false,
+ error: muxedResult.error || "Failed to mux audio with FFmpeg",
+ metrics: muxedResult.metrics ?? this.buildExportMetrics(),
+ };
+ }
+
+ return {
+ success: true,
+ blob: muxedResult.blob,
+ metrics: muxedResult.metrics ?? this.buildExportMetrics(),
+ };
+ }
+
return { success: true, blob, metrics: this.buildExportMetrics() };
} catch (error) {
if (this.cancelled && !this.encoderError) {
@@ -928,6 +950,68 @@ export class ModernVideoExporter {
};
}
+ private async finalizeExportWithFfmpegAudio(
+ videoBlob: Blob,
+ audioPlan: NativeAudioPlan,
+ ): Promise<ExportResult> {
+ if (typeof window === "undefined" || !window.electronAPI?.muxExportedVideoAudio) {
+ return {
+ success: false,
+ error: "FFmpeg audio fallback is unavailable in this environment.",
+ };
+ }
+
+ let editedAudioBuffer: ArrayBuffer | undefined;
+ let editedAudioMimeType: string | null = null;
+
+ if (audioPlan.audioMode === "edited-track") {
+ this.audioProcessor = new AudioProcessor();
+ this.audioProcessor.setOnProgress((progress) => {
+ this.reportFinalizingProgress(this.processedFrameCount, 99, progress);
+ });
+ const audioBlob = await this.awaitWithFinalizationTimeout(
+ this.audioProcessor.renderEditedAudioTrack(
+ this.config.videoUrl,
+ this.config.trimRegions,
+ this.config.speedRegions,
+ this.config.audioRegions,
+ this.config.sourceAudioFallbackPaths,
+ ),
+ "FFmpeg edited audio rendering",
+ );
+ editedAudioBuffer = await audioBlob.arrayBuffer();
+ editedAudioMimeType = audioBlob.type || null;
+ }
+
+ const videoBuffer = await videoBlob.arrayBuffer();
+ const result = await this.awaitWithFinalizationTimeout(
+ window.electronAPI.muxExportedVideoAudio(videoBuffer, {
+ audioMode: audioPlan.audioMode,
+ audioSourcePath:
+ audioPlan.audioMode === "copy-source" || audioPlan.audioMode === "trim-source"
+ ? audioPlan.audioSourcePath
+ : null,
+ trimSegments: audioPlan.audioMode === "trim-source" ? audioPlan.trimSegments : undefined,
+ editedAudioData: editedAudioBuffer,
+ editedAudioMimeType,
+ }),
+ "FFmpeg audio muxing",
+ );
+
+ if (!result.success || !result.data) {
+ return {
+ success: false,
+ error: result.error || "Failed to mux exported audio with FFmpeg",
+ };
+ }
+
+ const videoBytes = result.data.slice();
+ return {
+ success: true,
+ blob: new Blob([videoBytes.buffer], { type: "video/mp4" }),
+ };
+ }
+
private async encodeRenderedFrame(timestamp: number, frameDuration: number, frameIndex: number) {
const canvas = this.renderer!.getCanvas();
diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts
index 62825a79..4c249df5 100644
--- a/src/lib/exporter/videoExporter.ts
+++ b/src/lib/exporter/videoExporter.ts
@@ -124,12 +124,12 @@ export class VideoExporter {
});
const videoInfo = await this.streamingDecoder.loadMetadata(this.config.videoUrl);
const shouldUseExperimentalNativeExport = this.shouldUseExperimentalNativeExport();
- const nativeAudioPlan = shouldUseExperimentalNativeExport
- ? this.buildNativeAudioPlan(videoInfo)
- : null;
+ const audioPlan = this.buildNativeAudioPlan(videoInfo);
+ const nativeAudioPlan = shouldUseExperimentalNativeExport ? audioPlan : null;
let useNativeEncoder = shouldUseExperimentalNativeExport
? await this.tryStartNativeVideoExport()
: false;
+ const shouldUseFfmpegAudioFallback = !useNativeEncoder && audioPlan.audioMode !== "none";
if (!useNativeEncoder) {
await this.initializeEncoder();
@@ -187,7 +187,7 @@ export class VideoExporter {
const hasAudio = videoInfo.hasAudio || hasAudioRegions || hasSourceAudioFallback;
if (!useNativeEncoder) {
- this.muxer = new VideoMuxer(this.config, hasAudio);
+ this.muxer = new VideoMuxer(this.config, hasAudio && !shouldUseFfmpegAudioFallback);
await this.muxer.initialize();
}
@@ -262,7 +262,7 @@ export class VideoExporter {
this.reportFinalizingProgress(totalFrames, 98);
await this.awaitWithFinalizationTimeout(this.pendingMuxing, "muxing queued video chunks");
- if (hasAudio && !this.cancelled) {
+ if (hasAudio && !shouldUseFfmpegAudioFallback && !this.cancelled) {
const demuxer = this.streamingDecoder.getDemuxer();
if (demuxer || hasAudioRegions || hasSourceAudioFallback) {
this.audioProcessor = new AudioProcessor();
@@ -290,6 +290,13 @@ export class VideoExporter {
this.reportFinalizingProgress(totalFrames, 99);
const blob = await this.awaitWithFinalizationTimeout(this.muxer!.finalize(), "muxer finalization");
+ if (shouldUseFfmpegAudioFallback) {
+ console.warn(
+ "[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.",
+ );
+ return await this.finalizeExportWithFfmpegAudio(blob, audioPlan, totalFrames);
+ }
+
return { success: true, blob };
} catch (error) {
if (this.cancelled && !this.encoderError) {
@@ -567,6 +574,70 @@ export class VideoExporter {
};
}
+ private async finalizeExportWithFfmpegAudio(
+ videoBlob: Blob,
+ audioPlan: NativeAudioPlan,
+ totalFrames: number,
+ ): Promise<ExportResult> {
+ if (typeof window === "undefined" || !window.electronAPI?.muxExportedVideoAudio) {
+ return {
+ success: false,
+ error: "FFmpeg audio fallback is unavailable in this environment.",
+ };
+ }
+
+ let editedAudioBuffer: ArrayBuffer | undefined;
+ let editedAudioMimeType: string | null = null;
+
+ if (audioPlan.audioMode === "edited-track") {
+ this.audioProcessor = new AudioProcessor();
+ this.audioProcessor.setOnProgress((progress) => {
+ this.reportFinalizingProgress(totalFrames, 99, progress);
+ });
+ const audioBlob = await this.awaitWithFinalizationTimeout(
+ this.audioProcessor.renderEditedAudioTrack(
+ this.config.videoUrl,
+ this.config.trimRegions,
+ this.config.speedRegions,
+ this.config.audioRegions,
+ this.config.sourceAudioFallbackPaths,
+ ),
+ "ffmpeg edited audio rendering",
+ );
+ editedAudioBuffer = await audioBlob.arrayBuffer();
+ editedAudioMimeType = audioBlob.type || null;
+ }
+
+ const videoBuffer = await videoBlob.arrayBuffer();
+ const result = await this.awaitWithFinalizationTimeout(
+ window.electronAPI.muxExportedVideoAudio(videoBuffer, {
+ audioMode: audioPlan.audioMode,
+ audioSourcePath:
+ audioPlan.audioMode === "copy-source" || audioPlan.audioMode === "trim-source"
+ ? audioPlan.audioSourcePath
+ : null,
+ trimSegments: audioPlan.audioMode === "trim-source" ? audioPlan.trimSegments : undefined,
+ editedAudioData: editedAudioBuffer,
+ editedAudioMimeType,
+ }),
+ "ffmpeg audio muxing",
+ );
+
+ if (!result.success || !result.data) {
+ return {
+ success: false,
+ error: result.error || "Failed to mux exported audio with FFmpeg",
+ };
+ }
+
+ const blobData = new Uint8Array(result.data.byteLength);
+ blobData.set(result.data);
+ return {
+ success: true,
+ blob: new Blob([blobData.buffer], { type: "video/mp4" }),
+ };
+ }
+
private async encodeRenderedFrame(timestamp: number, frameDuration: number, frameIndex: number) {
const canvas = this.renderer!.getCanvas();