Skip to content

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

fix(recording): trim paused audio from native windows captures

webadderall5c74d8dd6ebf
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 3d7c2922..afee6072 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -350,7 +350,7 @@ interface Window {
setHasUnsavedChanges: (hasChanges: boolean) => void;
onRequestSaveBeforeClose: (callback: () => Promise<boolean>) => () => void;
isNativeWindowsCaptureAvailable: () => Promise<{ available: boolean }>;
- muxNativeWindowsRecording: () => Promise<{
+ muxNativeWindowsRecording: (pauseSegments?: Array<{ startMs: number; endMs: number }>) => Promise<{
success: boolean;
path?: string;
message?: string;
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index c5308430..fcb6e8d2 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -115,6 +115,11 @@ type RecordingSessionData = {
timeOffsetMs?: number
}
+type PauseSegment = {
+ startMs: number
+ endMs: number
+}
+
type RecordingSessionManifest = {
version: 1 | 2
videoFileName: string
@@ -1931,7 +1936,7 @@ function attachWindowsCaptureLifecycle(proc: ChildProcessWithoutNullStreams) {
})
}
-async function muxNativeWindowsVideoWithAudio(videoPath: string, systemAudioPath: string | null, micAudioPath: string | null) {
+async function muxNativeWindowsVideoWithAudio(videoPath: string, systemAudioPath: string | null, micAudioPath: string | null, pauseSegments: PauseSegment[] = []) {
const ffmpegPath = getFfmpegBinaryPath()
const inputs: string[] = ['-i', videoPath]
const audioInputs: string[] = []
@@ -1959,15 +1964,30 @@ async function muxNativeWindowsVideoWithAudio(videoPath: string, systemAudioPath
if (audioInputs.length === 0) return
const mixedOutputPath = `${videoPath}.muxed.mp4`
+ const normalizedPauseSegments = normalizePauseSegments(pauseSegments)
if (audioInputs.length === 2) {
// Both system + mic audio: mix them
+ const filterParts: string[] = []
+ const systemPauseFilter = buildPausedAudioFilter('1:a', 'system_trimmed', normalizedPauseSegments)
+ const micPauseFilter = buildPausedAudioFilter('2:a', 'mic_trimmed', normalizedPauseSegments)
+
+ if (systemPauseFilter) {
+ filterParts.push(systemPauseFilter)
+ }
+ if (micPauseFilter) {
+ filterParts.push(micPauseFilter)
+ }
+
+ filterParts.push(`${micPauseFilter ? '[mic_trimmed]' : '[2:a]'}atrim=start=0.10,asetpts=PTS-STARTPTS[m]`)
+ filterParts.push(`${systemPauseFilter ? '[system_trimmed]' : '[1:a]'}[m]amix=inputs=2:duration=longest:normalize=0[aout]`)
+
await execFileAsync(
ffmpegPath,
[
'-y',
...inputs,
- '-filter_complex', '[2:a]atrim=start=0.10,asetpts=PTS-STARTPTS[m];[1:a][m]amix=inputs=2:duration=longest:normalize=0[aout]',
+ '-filter_complex', filterParts.join(';'),
'-map', '0:v:0',
'-map', '[aout]',
'-c:v', 'copy',
@@ -1980,19 +2000,34 @@ async function muxNativeWindowsVideoWithAudio(videoPath: string, systemAudioPath
)
} else {
// Single audio track
+ const pauseFilter = buildPausedAudioFilter('1:a', 'aout', normalizedPauseSegments)
+
await execFileAsync(
ffmpegPath,
- [
- '-y',
- ...inputs,
- '-map', '0:v:0',
- '-map', '1:a:0',
- '-c:v', 'copy',
- '-c:a', 'aac',
- '-b:a', '192k',
- '-shortest',
- mixedOutputPath,
- ],
+ pauseFilter
+ ? [
+ '-y',
+ ...inputs,
+ '-filter_complex', pauseFilter,
+ '-map', '0:v:0',
+ '-map', '[aout]',
+ '-c:v', 'copy',
+ '-c:a', 'aac',
+ '-b:a', '192k',
+ '-shortest',
+ mixedOutputPath,
+ ]
+ : [
+ '-y',
+ ...inputs,
+ '-map', '0:v:0',
+ '-map', '1:a:0',
+ '-c:v', 'copy',
+ '-c:a', 'aac',
+ '-b:a', '192k',
+ '-shortest',
+ mixedOutputPath,
+ ],
{ timeout: 120000, maxBuffer: 10 * 1024 * 1024 },
)
}
@@ -2007,6 +2042,100 @@ async function muxNativeWindowsVideoWithAudio(videoPath: string, systemAudioPath
}
}
+function normalizePauseSegments(pauseSegments: PauseSegment[] | undefined): PauseSegment[] {
+ if (!Array.isArray(pauseSegments) || pauseSegments.length === 0) {
+ return []
+ }
+
+ const normalized = pauseSegments
+ .map((segment) => {
+ const startMs = Number(segment?.startMs)
+ const endMs = Number(segment?.endMs)
+
+ if (!Number.isFinite(startMs) || !Number.isFinite(endMs)) {
+ return null
+ }
+
+ const clampedStart = Math.max(0, Math.round(startMs))
+ const clampedEnd = Math.max(0, Math.round(endMs))
+ if (clampedEnd <= clampedStart) {
+ return null
+ }
+
+ return { startMs: clampedStart, endMs: clampedEnd }
+ })
+ .filter((segment): segment is PauseSegment => !!segment)
+ .sort((left, right) => left.startMs - right.startMs)
+
+ if (normalized.length <= 1) {
+ return normalized
+ }
+
+ const merged: PauseSegment[] = [{ ...normalized[0] }]
+
+ for (const segment of normalized.slice(1)) {
+ const previous = merged[merged.length - 1]
+ if (segment.startMs <= previous.endMs) {
+ previous.endMs = Math.max(previous.endMs, segment.endMs)
+ } else {
+ merged.push({ ...segment })
+ }
+ }
+
+ return merged
+}
+
+function formatFfmpegSeconds(milliseconds: number) {
+ return (milliseconds / 1000).toFixed(3)
+}
+
+function buildPausedAudioFilter(inputLabel: string, outputLabel: string, pauseSegments: PauseSegment[]) {
+ if (pauseSegments.length === 0) {
+ return null
+ }
+
+ const activeSegments: Array<{ startMs: number, endMs?: number }> = []
+ let cursorMs = 0
+
+ for (const pauseSegment of pauseSegments) {
+ if (pauseSegment.startMs > cursorMs) {
+ activeSegments.push({ startMs: cursorMs, endMs: pauseSegment.startMs })
+ }
+ cursorMs = Math.max(cursorMs, pauseSegment.endMs)
+ }
+
+ activeSegments.push({ startMs: cursorMs })
+
+ const filterParts: string[] = []
+ const segmentLabels: string[] = []
+
+ activeSegments.forEach((segment, index) => {
+ if (typeof segment.endMs === 'number' && segment.endMs <= segment.startMs) {
+ return
+ }
+
+ const segmentLabel = `${outputLabel}_part${index}`
+ const trimArgs = typeof segment.endMs === 'number'
+ ? `start=${formatFfmpegSeconds(segment.startMs)}:end=${formatFfmpegSeconds(segment.endMs)}`
+ : `start=${formatFfmpegSeconds(segment.startMs)}`
+
+ filterParts.push(`[${inputLabel}]atrim=${trimArgs},asetpts=PTS-STARTPTS[${segmentLabel}]`)
+ segmentLabels.push(`[${segmentLabel}]`)
+ })
+
+ if (segmentLabels.length === 0) {
+ return null
+ }
+
+ if (segmentLabels.length === 1) {
+ filterParts.push(`${segmentLabels[0]}anull[${outputLabel}]`)
+ } else {
+ filterParts.push(`${segmentLabels.join('')}concat=n=${segmentLabels.length}:v=0:a=1[${outputLabel}]`)
+ }
+
+ return filterParts.join(';')
+}
+
function waitForNativeCaptureStart(process: ChildProcessWithoutNullStreams) {
return new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
@@ -3852,7 +3981,7 @@ body{background:transparent;overflow:hidden;width:100vw;height:100vh}
return { success: true, diagnostics: lastNativeCaptureDiagnostics }
})
- ipcMain.handle('mux-native-windows-recording', async () => {
+ ipcMain.handle('mux-native-windows-recording', async (_event, pauseSegments?: PauseSegment[]) => {
const videoPath = windowsPendingVideoPath
windowsPendingVideoPath = null
@@ -3862,7 +3991,7 @@ body{background:transparent;overflow:hidden;width:100vw;height:100vh}
try {
if (windowsSystemAudioPath || windowsMicAudioPath) {
- await muxNativeWindowsVideoWithAudio(videoPath, windowsSystemAudioPath, windowsMicAudioPath)
+ await muxNativeWindowsVideoWithAudio(videoPath, windowsSystemAudioPath, windowsMicAudioPath, pauseSegments ?? [])
windowsSystemAudioPath = null
windowsMicAudioPath = null
}
diff --git a/electron/preload.ts b/electron/preload.ts
index be1932d3..d9a8f9a9 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -376,7 +376,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
return () => ipcRenderer.removeListener("request-save-before-close", listener);
},
isNativeWindowsCaptureAvailable: () => ipcRenderer.invoke("is-native-windows-capture-available"),
- muxNativeWindowsRecording: () => ipcRenderer.invoke("mux-native-windows-recording"),
+ muxNativeWindowsRecording: (pauseSegments?: Array<{ startMs: number; endMs: number }>) => ipcRenderer.invoke("mux-native-windows-recording", pauseSegments),
hideOsCursor: () => ipcRenderer.invoke("hide-cursor"),
getAppVersion: () => ipcRenderer.invoke("app:getVersion"),
getRecordingPreferences: () => ipcRenderer.invoke("get-recording-preferences"),
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 7f391316..a7407d67 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -33,6 +33,11 @@ const WEBCAM_HEIGHT = 720;
const WEBCAM_FRAME_RATE = 30;
const WEBCAM_SUFFIX = "-webcam";
+type PauseSegment = {
+ startMs: number;
+ endMs: number;
+};
+
type UseScreenRecorderReturn = {
recording: boolean;
paused: boolean;
@@ -95,6 +100,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
const resolvedWebcamPath = useRef<string | null>(null);
const accumulatedPausedDurationMs = useRef(0);
const pauseStartedAtMs = useRef<number | null>(null);
+ const pauseSegmentsRef = useRef<PauseSegment[]>([]);
const logNativeCaptureDiagnostics = useCallback(async (context: string) => {
if (typeof window.electronAPI?.getLastNativeCaptureDiagnostics !== "function") {
@@ -149,6 +155,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
startTime.current = startedAt;
accumulatedPausedDurationMs.current = 0;
pauseStartedAtMs.current = null;
+ pauseSegmentsRef.current = [];
}, []);
const markRecordingPaused = useCallback((pausedAt: number) => {
@@ -162,7 +169,12 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
return;
}
- accumulatedPausedDurationMs.current += Math.max(0, resumedAt - pauseStartedAtMs.current);
+ const pauseStart = pauseStartedAtMs.current;
+ const pauseDurationMs = Math.max(0, resumedAt - pauseStart);
+ accumulatedPausedDurationMs.current += pauseDurationMs;
+ if (pauseDurationMs > 0) {
+ pauseSegmentsRef.current.push({ startMs: pauseStart, endMs: resumedAt });
+ }
pauseStartedAtMs.current = null;
}, []);
@@ -477,6 +489,8 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
void (async () => {
const webcamPath = await stopWebcamRecorder();
const isNativeWindows = nativeWindowsRecording.current;
+ markRecordingResumed(Date.now());
+ const pauseSegments = pauseSegmentsRef.current.slice();
nativeWindowsRecording.current = false;
const result = await window.electronAPI.stopNativeScreenRecording();
@@ -507,7 +521,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
let finalPath = result.path;
if (isNativeWindows) {
- const muxResult = await window.electronAPI.muxNativeWindowsRecording();
+ const muxResult = await window.electronAPI.muxNativeWindowsRecording(pauseSegments);
if (!muxResult?.success) {
void logNativeCaptureDiagnostics("mux-native-windows-recording");
}