Skip to content

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

feat(export): prepare native GPU beta candidate

wiiiii1233ab66c905723
diff --git a/electron-builder.json5 b/electron-builder.json5
index bef41d48..1aaa56af 100644
--- a/electron-builder.json5
+++ b/electron-builder.json5
@@ -5,8 +5,9 @@
"electronUpdaterCompatibility": ">=2.16",
"asar": true,
"asarUnpack": [
- "node_modules/ffmpeg-static/**",
- "node_modules/uiohook-napi/**",
+ "node_modules/ffmpeg-static/**",
+ "node_modules/ffprobe-static/**",
+ "node_modules/uiohook-napi/**",
"electron/native/**"
],
"productName": "Recordly",
@@ -16,13 +17,16 @@
"directories": {
"output": "release"
},
- "files": [
+ "files": [
"dist",
"dist-electron",
"electron/native",
+ "!node_modules/ffprobe-static/bin/darwin/**",
+ "!node_modules/ffprobe-static/bin/linux/**",
+ "!node_modules/ffprobe-static/bin/win32/ia32/**",
"!electron/native/**/build/**",
"!*.png",
- "!preview*.png",
+ "!preview*.png",
"!*.md",
"!README.md",
"!CONTRIBUTING.md",
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 19ba097a..afee0f4f 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -151,6 +151,7 @@ interface RendererNativeStaticLayoutMetrics extends RendererFfmpegAudioMuxMetric
interface RendererNativeStaticLayoutProgress {
sessionId?: string;
backend?: RendererNativeStaticLayoutChunkMetric["backend"];
+ stage?: "preparing" | "finalizing";
elapsedMs?: number;
averageFps?: number;
instantFps?: number;
@@ -347,6 +348,13 @@ interface Window {
aspectRatio: number;
}>;
zoomTelemetry?: Array<{ timeMs: number; scale: number; x: number; y: number }>;
+ timelineSegments?: Array<{
+ sourceStartMs: number;
+ sourceEndMs: number;
+ outputStartMs: number;
+ outputEndMs: number;
+ speed: number;
+ }>;
chunkDurationSec?: number;
experimentalWindowsGpuCompositor?: boolean;
audioOptions?: {
@@ -755,9 +763,7 @@ interface Window {
setHasUnsavedChanges: (hasChanges: boolean) => void;
onRequestSaveBeforeClose: (callback: () => Promise<boolean>) => () => void;
isNativeWindowsCaptureAvailable: () => Promise<{ available: boolean }>;
- muxNativeWindowsRecording: (
- pauseSegments?: Array<{ startMs: number; endMs: number }>,
- ) => Promise<{
+ muxNativeWindowsRecording: (expectedDurationMs?: number) => Promise<{
success: boolean;
path?: string;
message?: string;
diff --git a/electron/ipc/export/native-video.test.ts b/electron/ipc/export/native-video.test.ts
index 7bc25eaa..b0f8f1db 100644
--- a/electron/ipc/export/native-video.test.ts
+++ b/electron/ipc/export/native-video.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it, vi } from "vitest";
vi.mock("electron", () => ({
app: {
getAppPath: vi.fn(() => process.cwd()),
+ getGPUInfo: vi.fn(async () => ({ gpuDevice: [] })),
getPath: vi.fn(() => process.env.TEMP ?? process.cwd()),
isPackaged: false,
},
@@ -10,6 +11,7 @@ vi.mock("electron", () => ({
vi.mock("../ffmpeg/binary", () => ({
getFfmpegBinaryPath: vi.fn(() => "ffmpeg"),
+ getFfprobeBinaryPath: vi.fn(() => "ffprobe"),
}));
vi.mock("../state", () => ({
@@ -44,20 +46,35 @@ vi.mock("node:child_process", () => ({
spawn: vi.fn(),
}));
+import { app } from "electron";
import {
+ buildExperimentalNvidiaCudaStaticLayoutArgs,
+ buildNativeStaticLayoutTimelineSegments,
buildNativeVideoAudioMuxArgs,
+ getExperimentalNvidiaCudaExportSkipReason,
getNvidiaCudaAudioExportSkipReason,
+ hasNvidiaGpuDeviceInGpuInfo,
+ mapNvidiaCudaWrapperProgressPercentage,
muxExportedVideoAudioBuffer,
+ type NativeStaticLayoutExportOptions,
normalizeNativeStaticLayoutBackground,
parseFfmpegDurationSeconds,
parseFfmpegFrameRate,
+ parseFfmpegProgressLineSeconds,
parseNativeVideoMetadataProbeOutput,
parseNvidiaCudaExportSummary,
parseWindowsGpuExportProgressLine,
parseWindowsGpuExportSummary,
+ resolveExperimentalNvidiaCudaExportScriptPath,
validateNvidiaCudaExportSummary,
} from "./native-video";
+const electronAppMock = app as unknown as {
+ getAppPath: ReturnType<typeof vi.fn>;
+ getGPUInfo: ReturnType<typeof vi.fn>;
+ isPackaged: boolean;
+};
+
function withNvidiaCudaAudioOverride<T>(value: string | undefined, callback: () => T) {
const envName = "RECORDLY_NVIDIA_CUDA_ALLOW_AUDIO_EXPORT";
const originalValue = process.env[envName];
@@ -78,6 +95,57 @@ function withNvidiaCudaAudioOverride<T>(value: string | undefined, callback: ()
}
}
+function resetFsAccessMock() {
+ fsMocks.access.mockImplementation(async () => {
+ throw new Error("missing");
+ });
+}
+
+function createNvidiaCudaSkipOptions(
+ overrides: Partial<NativeStaticLayoutExportOptions> = {},
+): NativeStaticLayoutExportOptions {
+ return {
+ inputPath: "input.mp4",
+ width: 1920,
+ height: 1080,
+ frameRate: 30,
+ bitrate: 8_000_000,
+ encodingMode: "quality",
+ durationSec: 10,
+ contentWidth: 1600,
+ contentHeight: 900,
+ offsetX: 160,
+ offsetY: 90,
+ backgroundColor: "#101010",
+ experimentalWindowsGpuCompositor: true,
+ ...overrides,
+ };
+}
+
+async function withPackagedCudaCandidate<T>(gpuInfo: unknown, callback: () => Promise<T>) {
+ const envName = "RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT";
+ const originalEnv = process.env[envName];
+ const originalIsPackaged = electronAppMock.isPackaged;
+ electronAppMock.isPackaged = true;
+ electronAppMock.getGPUInfo.mockResolvedValue(gpuInfo);
+ fsMocks.access.mockResolvedValue(undefined);
+ delete process.env[envName];
+
+ try {
+ return await callback();
+ } finally {
+ if (originalEnv === undefined) {
+ delete process.env[envName];
+ } else {
+ process.env[envName] = originalEnv;
+ }
+ electronAppMock.isPackaged = originalIsPackaged;
+ electronAppMock.getGPUInfo.mockReset();
+ electronAppMock.getGPUInfo.mockResolvedValue({ gpuDevice: [] });
+ resetFsAccessMock();
+ }
+}
+
describe("normalizeNativeStaticLayoutBackground", () => {
it("falls back to a solid background when the configured image file is missing", async () => {
const normalized = await normalizeNativeStaticLayoutBackground({
@@ -128,6 +196,284 @@ describe("getNvidiaCudaAudioExportSkipReason", () => {
expect(getNvidiaCudaAudioExportSkipReason("copy-source")).toBeNull();
});
});
+
+ it("allows validated fallback candidates to try CUDA audio exports", () => {
+ withNvidiaCudaAudioOverride(undefined, () => {
+ expect(
+ getNvidiaCudaAudioExportSkipReason("copy-source", {
+ allowValidatedFallbackCandidate: true,
+ }),
+ ).toBeNull();
+ });
+ });
+});
+
+describe("hasNvidiaGpuDeviceInGpuInfo", () => {
+ it("detects NVIDIA GPUs by vendor id or device strings", () => {
+ expect(
+ hasNvidiaGpuDeviceInGpuInfo({
+ gpuDevice: [{ active: false, vendorId: 0x10de, deviceId: 0x1f91 }],
+ }),
+ ).toBe(true);
+ expect(
+ hasNvidiaGpuDeviceInGpuInfo({
+ gpuDevice: [{ vendorString: "NVIDIA Corporation" }],
+ }),
+ ).toBe(true);
+ expect(
+ hasNvidiaGpuDeviceInGpuInfo({
+ gpuDevice: [{ deviceString: "NVIDIA GeForce GTX 1650" }],
+ }),
+ ).toBe(true);
+ });
+
+ it("rejects non-NVIDIA and malformed GPU info", () => {
+ expect(
+ hasNvidiaGpuDeviceInGpuInfo({
+ gpuDevice: [
+ { vendorId: 0x1002, deviceString: "AMD Radeon" },
+ { vendorId: 0x8086, deviceString: "Intel UHD Graphics" },
+ ],
+ }),
+ ).toBe(false);
+ expect(hasNvidiaGpuDeviceInGpuInfo({})).toBe(false);
+ expect(hasNvidiaGpuDeviceInGpuInfo(null)).toBe(false);
+ });
+});
+
+describe("getExperimentalNvidiaCudaExportSkipReason", () => {
+ it("auto-enables packaged CUDA candidates when the helper and an NVIDIA GPU are present", async () => {
+ const reason = await withPackagedCudaCandidate(
+ { gpuDevice: [{ vendorId: 0x10de, deviceString: "NVIDIA GeForce GTX 1650" }] },
+ () =>
+ getExperimentalNvidiaCudaExportSkipReason(
+ createNvidiaCudaSkipOptions({
+ audioOptions: { audioMode: "copy-source", audioSourcePath: "input.mp4" },
+ }),
+ ),
+ );
+
+ expect(reason).toBe(process.platform === "win32" ? null : "not-windows");
+ });
+
+ it("allows explicit lab CUDA audio exports when forced onto the shared mux path", async () => {
+ const exportEnvName = "RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT";
+ const forceEnvName = "RECORDLY_NVIDIA_CUDA_FORCE_VIDEO_ONLY";
+ const allowAudioEnvName = "RECORDLY_NVIDIA_CUDA_ALLOW_AUDIO_EXPORT";
+ const originalExportEnv = process.env[exportEnvName];
+ const originalForceEnv = process.env[forceEnvName];
+ const originalAllowAudioEnv = process.env[allowAudioEnvName];
+ process.env[exportEnvName] = "1";
+ process.env[forceEnvName] = "1";
+ delete process.env[allowAudioEnvName];
+
+ try {
+ const reason = await getExperimentalNvidiaCudaExportSkipReason(
+ createNvidiaCudaSkipOptions({
+ audioOptions: { audioMode: "copy-source", audioSourcePath: "input.mp4" },
+ }),
+ );
+
+ expect(reason).toBe(process.platform === "win32" ? null : "not-windows");
+ } finally {
+ if (originalExportEnv === undefined) {
+ delete process.env[exportEnvName];
+ } else {
+ process.env[exportEnvName] = originalExportEnv;
+ }
+ if (originalForceEnv === undefined) {
+ delete process.env[forceEnvName];
+ } else {
+ process.env[forceEnvName] = originalForceEnv;
+ }
+ if (originalAllowAudioEnv === undefined) {
+ delete process.env[allowAudioEnvName];
+ } else {
+ process.env[allowAudioEnvName] = originalAllowAudioEnv;
+ }
+ }
+ });
+
+ it("skips packaged CUDA auto-candidates when Electron reports no NVIDIA GPU", async () => {
+ const reason = await withPackagedCudaCandidate(
+ { gpuDevice: [{ vendorId: 0x8086, deviceString: "Intel UHD Graphics" }] },
+ () => getExperimentalNvidiaCudaExportSkipReason(createNvidiaCudaSkipOptions()),
+ );
+
+ expect(reason).toBe(
+ process.platform === "win32" ? "nvidia-gpu-unavailable" : "not-windows",
+ );
+ });
+
+ it("lets the packaged auto-candidate be explicitly disabled", async () => {
+ const reason = await withPackagedCudaCandidate(
+ { gpuDevice: [{ vendorId: 0x10de, deviceString: "NVIDIA GeForce GTX 1650" }] },
+ async () => {
+ process.env.RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT = "0";
+ return getExperimentalNvidiaCudaExportSkipReason(createNvidiaCudaSkipOptions());
+ },
+ );
+
+ expect(reason).toBe(process.platform === "win32" ? "env-disabled" : "not-windows");
+ });
+});
+
+describe("resolveExperimentalNvidiaCudaExportScriptPath", () => {
+ it("prefers the packaged app.asar.unpacked CUDA wrapper over the virtual app.asar copy", async () => {
+ const envName = "RECORDLY_NVIDIA_CUDA_EXPORT_SCRIPT";
+ const originalEnv = process.env[envName];
+ const originalResourcesPath = Object.getOwnPropertyDescriptor(process, "resourcesPath");
+ delete process.env[envName];
+
+ try {
+ if (process.platform !== "win32") {
+ expect(await resolveExperimentalNvidiaCudaExportScriptPath()).toBeNull();
+ return;
+ }
+
+ const resourcesPath = "C:\\Recordly\\resources";
+ const unpackedScriptPath =
+ "C:\\Recordly\\resources\\app.asar.unpacked\\electron\\native\\nvidia-cuda-compositor\\run-mp4-pipeline.mjs";
+ const asarScriptPath =
+ "C:\\Recordly\\resources\\app.asar\\electron\\native\\nvidia-cuda-compositor\\run-mp4-pipeline.mjs";
+ Object.defineProperty(process, "resourcesPath", {
+ configurable: true,
+ value: resourcesPath,
+ });
+ electronAppMock.getAppPath.mockReturnValue("C:\\Recordly\\resources\\app.asar");
+ fsMocks.access.mockImplementation(async (candidate: string) => {
+ if (candidate === unpackedScriptPath || candidate === asarScriptPath) {
+ return;
+ }
+ throw new Error(`missing ${candidate}`);
+ });
+
+ expect(await resolveExperimentalNvidiaCudaExportScriptPath()).toBe(
+ unpackedScriptPath,
+ );
+ } finally {
+ if (originalEnv === undefined) {
+ delete process.env[envName];
+ } else {
+ process.env[envName] = originalEnv;
+ }
+ if (originalResourcesPath) {
+ Object.defineProperty(process, "resourcesPath", originalResourcesPath);
+ } else {
+ delete (process as NodeJS.Process & { resourcesPath?: string }).resourcesPath;
+ }
+ electronAppMock.getAppPath.mockReset();
+ electronAppMock.getAppPath.mockReturnValue(process.cwd());
+ resetFsAccessMock();
+ }
+ });
+});
+
+describe("buildExperimentalNvidiaCudaStaticLayoutArgs", () => {
+ it("keeps explicit copy-source CUDA audio inline by default", () => {
+ const args = buildExperimentalNvidiaCudaStaticLayoutArgs(
+ createNvidiaCudaSkipOptions({
+ audioOptions: { audioMode: "copy-source", audioSourcePath: "input.mp4" },
+ }),
+ "output.mp4",
+ "work",
+ );
+
+ expect(args).not.toContain("--video-only");
+ });
+
+ it("forces packaged auto CUDA candidates onto the shared audio mux path", () => {
+ const args = buildExperimentalNvidiaCudaStaticLayoutArgs(
+ createNvidiaCudaSkipOptions({
+ audioOptions: { audioMode: "copy-source", audioSourcePath: "input.mp4" },
+ nvidiaCudaForceVideoOnly: true,
+ }),
+ "output.mp4",
+ "work",
+ );
+
+ expect(args).toContain("--video-only");
+ });
+
+ it("passes native timeline maps to the CUDA wrapper", () => {
+ const args = buildExperimentalNvidiaCudaStaticLayoutArgs(
+ createNvidiaCudaSkipOptions({
+ timelineMapPath: "timeline-map.csv",
+ timelineSegments: [
+ {
+ sourceStartMs: 0,
+ sourceEndMs: 6_000,
+ outputStartMs: 0,
+ outputEndMs: 4_000,
+ speed: 1.5,
+ },
+ ],
+ }),
+ "output.mp4",
+ "work",
+ );
+
+ expect(args).toEqual(expect.arrayContaining(["--timeline-map", "timeline-map.csv"]));
+ });
+
+ it("passes webcam source-time controls to the CUDA wrapper", () => {
+ const args = buildExperimentalNvidiaCudaStaticLayoutArgs(
+ createNvidiaCudaSkipOptions({
+ webcamInputPath: "webcam.mp4",
+ webcamLeft: 32,
+ webcamTop: 48,
+ webcamSize: 240,
+ webcamRadius: 18,
+ webcamTimeOffsetMs: -125.5,
+ }),
+ "output.mp4",
+ "work",
+ );
+
+ expect(args).toEqual(
+ expect.arrayContaining([
+ "--webcam-input",
+ "webcam.mp4",
+ "--webcam-time-offset-ms",
+ "-125.5",
+ "--webcam-stream",
+ ]),
+ );
+ });
+});
+
+describe("buildNativeStaticLayoutTimelineSegments", () => {
+ it("derives contiguous output timeline ranges from edited-track source segments", () => {
+ expect(
+ buildNativeStaticLayoutTimelineSegments([
+ { startMs: 0, endMs: 2_000, speed: 1 },
+ { startMs: 2_000, endMs: 8_000, speed: 1.5 },
+ { startMs: 8_000, endMs: 10_000, speed: 0.5 },
+ ]),
+ ).toEqual([
+ {
+ sourceStartMs: 0,
+ sourceEndMs: 2_000,
+ outputStartMs: 0,
+ outputEndMs: 2_000,
+ speed: 1,
+ },
+ {
+ sourceStartMs: 2_000,
+ sourceEndMs: 8_000,
+ outputStartMs: 2_000,
+ outputEndMs: 6_000,
+ speed: 1.5,
+ },
+ {
+ sourceStartMs: 8_000,
+ sourceEndMs: 10_000,
+ outputStartMs: 6_000,
+ outputEndMs: 10_000,
+ speed: 0.5,
+ },
+ ]);
+ });
});
describe("muxExportedVideoAudioBuffer", () => {
@@ -193,6 +539,44 @@ describe("buildNativeVideoAudioMuxArgs", () => {
expect(args).toEqual(expect.arrayContaining(["-filter_complex"]));
expect(args).toEqual(expect.arrayContaining(["-c:a", "aac", "-b:a", "192k"]));
});
+
+ it("pads and trims edited-track filtergraph audio to the expected duration", () => {
+ const args = buildNativeVideoAudioMuxArgs("video.mp4", "source.mp4", "out.mp4", {
+ audioMode: "edited-track",
+ editedTrackStrategy: "filtergraph-fast-path",
+ audioSourceSampleRate: 48_000,
+ editedTrackSegments: [{ startMs: 0, endMs: 4_000, speed: 0.5 }],
+ outputDurationSec: 8,
+ });
+
+ expect(args).toEqual(expect.arrayContaining(["-map", "[aout_sync]"]));
+ expect(args.join(";")).toContain(
+ "[aout]apad,atrim=duration=8.000,asetpts=PTS-STARTPTS[aout_sync]",
+ );
+ });
+
+ it("can enable machine-readable FFmpeg mux progress", () => {
+ const args = buildNativeVideoAudioMuxArgs(
+ "video.mp4",
+ "source.mp4",
+ "out.mp4",
+ { audioMode: "copy-source", outputDurationSec: 60 },
+ { progressPipe: 2 },
+ );
+
+ expect(args).toEqual(
+ expect.arrayContaining(["-stats_period", "0.5", "-progress", "pipe:2", "-nostats"]),
+ );
+ });
+});
+
+describe("parseFfmpegProgressLineSeconds", () => {
+ it("parses FFmpeg progress timestamps into seconds", () => {
+ expect(parseFfmpegProgressLineSeconds("out_time_us=1500000")).toBe(1.5);
+ expect(parseFfmpegProgressLineSeconds("out_time_ms=2500000")).toBe(2.5);
+ expect(parseFfmpegProgressLineSeconds("out_time=00:01:02.500000")).toBe(62.5);
+ expect(parseFfmpegProgressLineSeconds("progress=continue")).toBeNull();
+ });
});
describe("parseWindowsGpuExportSummary", () => {
@@ -309,9 +693,26 @@ describe("validateNvidiaCudaExportSummary", () => {
{ durationSec: 10, targetFrames: 300, requiresTimelineSync: true },
);
- expect(issues).toEqual([
- "CUDA timeline mode is not timestamp-aligned for audio export",
- ]);
+ expect(issues).toEqual(["CUDA timeline mode is not timestamp-aligned for audio export"]);
+ });
+
+ it("accepts video-only CUDA output when audio is muxed by the shared export path", () => {
+ const issues = validateNvidiaCudaExportSummary(
+ {
+ success: true,
+ targetFrames: 300,
+ durationSec: 10,
+ nativeSummary: {
+ success: true,
+ frames: 300,
+ selectionStage: "decoder-policy-mapped-callback",
+ },
+ outputVideo: { duration: "9.999900", nb_frames: "300" },
+ },
+ { durationSec: 10, targetFrames: 300, requiresTimelineSync: false },
+ );
+
+ expect(issues).toEqual([]);
});
it("accepts audio CUDA output when the helper reports PTS-aligned selection", () => {
@@ -340,12 +741,13 @@ describe("parseWindowsGpuExportProgressLine", () => {
it("parses bounded helper progress lines", () => {
expect(
parseWindowsGpuExportProgressLine(
- 'PROGRESS {"currentFrame":30,"totalFrames":60,"percentage":50,"averageFps":240.5,"instantFps":180.25,"intervalMs":166.4,"intervalFrames":30,"intervalEncodeMs":120.2,"intervalPipelineWaitMs":46.2,"intervalMonolithicCompositeFrames":0}',
+ 'PROGRESS {"currentFrame":30,"totalFrames":60,"percentage":50,"averageFps":240.5,"instantFps":180.25,"intervalMs":166.4,"intervalFrames":30,"intervalEncodeMs":120.2,"intervalPipelineWaitMs":46.2,"intervalMonolithicCompositeFrames":0,"stage":"finalizing"}',
),
).toEqual({
currentFrame: 30,
totalFrames: 60,
percentage: 50,
+ stage: "finalizing",
averageFps: 240.5,
instantFps: 180.25,
intervalMs: 166.4,
@@ -356,6 +758,19 @@ describe("parseWindowsGpuExportProgressLine", () => {
});
});
+ it("preserves CUDA preparation progress as a non-rendering stage", () => {
+ expect(
+ parseWindowsGpuExportProgressLine(
+ 'PROGRESS {"currentFrame":0,"totalFrames":100,"percentage":2.5,"stage":"preparing"}',
+ ),
+ ).toEqual({
+ currentFrame: 0,
+ totalFrames: 100,
+ percentage: 2.5,
+ stage: "preparing",
+ });
+ });
+
it("ignores non-progress or malformed helper stderr", () => {
expect(parseWindowsGpuExportProgressLine("warning: encoder selected")).toBeNull();
expect(parseWindowsGpuExportProgressLine("PROGRESS not-json")).toBeNull();
@@ -367,6 +782,47 @@ describe("parseWindowsGpuExportProgressLine", () => {
});
});
+describe("mapNvidiaCudaWrapperProgressPercentage", () => {
+ it("keeps preflight progress and maps native encode into the main export span", () => {
+ expect(
+ mapNvidiaCudaWrapperProgressPercentage({
+ currentFrame: 0,
+ totalFrames: 100,
+ percentage: 2.5,
+ }),
+ ).toBe(2.5);
+ expect(
+ mapNvidiaCudaWrapperProgressPercentage({
+ currentFrame: 0,
+ totalFrames: 100,
+ percentage: 0,
+ }),
+ ).toBe(3);
+ expect(
+ mapNvidiaCudaWrapperProgressPercentage({
+ currentFrame: 50,
+ totalFrames: 100,
+ percentage: 50,
+ }),
+ ).toBe(50.5);
+ expect(
+ mapNvidiaCudaWrapperProgressPercentage({
+ currentFrame: 100,
+ totalFrames: 100,
+ percentage: 100,
+ }),
+ ).toBe(98);
+ expect(
+ mapNvidiaCudaWrapperProgressPercentage({
+ currentFrame: 100,
+ totalFrames: 100,
+ percentage: 97.25,
+ stage: "finalizing",
+ }),
+ ).toBe(97.25);
+ });
+});
+
describe("parseNativeVideoMetadataProbeOutput", () => {
it("parses FFmpeg input metadata with video and audio streams", () => {
const metadata = parseNativeVideoMetadataProbeOutput(`
diff --git a/electron/ipc/export/native-video.ts b/electron/ipc/export/native-video.ts
index eb79fafc..ede5be0c 100644
--- a/electron/ipc/export/native-video.ts
+++ b/electron/ipc/export/native-video.ts
@@ -8,13 +8,14 @@ import type { Readable, Writable } from "node:stream";
import { promisify } from "node:util";
import type { WebContents } from "electron";
import { app, powerSaveBlocker } from "electron";
-import { getFfmpegBinaryPath } from "../ffmpeg/binary";
+import { getFfmpegBinaryPath, getFfprobeBinaryPath } from "../ffmpeg/binary";
import type {
NativeExportEncodingMode,
NativeStaticLayoutBackend,
NativeStaticLayoutExportArgsConfig,
NativeVideoAudioMuxMetrics,
NativeVideoExportAudioMode,
+ NativeVideoExportEditedTrackSegment,
NativeVideoExportFinishOptions,
} from "../nativeVideoExport";
import {
@@ -41,7 +42,20 @@ const getNowMs = () => performance.now();
const formatFfmpegSeconds = (milliseconds: number) => (milliseconds / 1000).toFixed(3);
const MISSING_NATIVE_STATIC_BACKGROUND_COLOR = "#ffffff";
const NATIVE_EXPORT_HIGH_PRIORITY = os.constants.priority.PRIORITY_HIGH;
+const NVIDIA_PCI_VENDOR_ID = 0x10de;
+const NVIDIA_CUDA_EXPORT_ENV = "RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT";
const NVIDIA_CUDA_ALLOW_AUDIO_EXPORT_ENV = "RECORDLY_NVIDIA_CUDA_ALLOW_AUDIO_EXPORT";
+const NVIDIA_CUDA_FORCE_VIDEO_ONLY_ENV = "RECORDLY_NVIDIA_CUDA_FORCE_VIDEO_ONLY";
+
+type ElectronGpuDeviceLike = {
+ vendorId?: number | string;
+ vendorString?: string;
+ deviceString?: string;
+};
+
+type ElectronGpuInfoLike = {
+ gpuDevice?: ElectronGpuDeviceLike[];
+};
export type NativeVideoExportSession = {
ffmpegProcess: ChildProcessByStdio<Writable, null, Readable>;
@@ -62,6 +76,14 @@ export type NativeVideoExportSession = {
export const nativeVideoExportSessions = new Map<string, NativeVideoExportSession>();
+export interface NativeStaticLayoutTimelineSegment {
+ sourceStartMs: number;
+ sourceEndMs: number;
+ outputStartMs: number;
+ outputEndMs: number;
+ speed: number;
+}
+
export interface NativeStaticLayoutExportOptions {
sessionId?: string;
inputPath: string;
@@ -111,14 +133,18 @@ export interface NativeStaticLayoutExportOptions {
cursorAtlasMetadataPath?: string | null;
zoomTelemetry?: Array<{ timeMs: number; scale: number; x: number; y: number }>;
zoomTelemetryPath?: string | null;
+ timelineSegments?: NativeStaticLayoutTimelineSegment[];
+ timelineMapPath?: string | null;
chunkDurationSec?: number;
experimentalWindowsGpuCompositor?: boolean;
audioOptions?: NativeVideoExportFinishOptions;
+ nvidiaCudaForceVideoOnly?: boolean;
}
export interface NativeStaticLayoutExportProgress {
sessionId?: string;
backend?: NativeStaticLayoutBackend;
+ stage?: "preparing" | "finalizing";
elapsedMs?: number;
averageFps?: number;
instantFps?: number;
@@ -140,6 +166,16 @@ export interface NativeStaticLayoutExportProgress {
percentage: number;
}
+type NativeVideoAudioMuxProgress = {
+ ratio: number;
+ processedSec?: number;
+ totalSec?: number;
+};
+
+type NativeVideoAudioMuxArgsOptions = {
+ progressPipe?: 1 | 2;
+};
+
export interface WindowsGpuExportSummary {
success?: boolean;
width?: number;
@@ -342,9 +378,7 @@ export function parseNvidiaCudaExportSummary(stdout: string): NvidiaCudaExportSu
function getFiniteNumber(value: unknown) {
const numberValue = typeof value === "string" ? Number(value) : value;
- return typeof numberValue === "number" && Number.isFinite(numberValue)
- ? numberValue
- : null;
+ return typeof numberValue === "number" && Number.isFinite(numberValue) ? numberValue : null;
}
function getNvidiaCudaOutputStreamNumber(stream: unknown, property: string) {
@@ -367,41 +401,22 @@ export function validateNvidiaCudaExportSummary(
const expectedFrames = Math.max(1, Math.round(expected.targetFrames));
const minimumFrames = Math.max(1, Math.floor(expectedFrames * 0.95));
const expectedDurationSec = Math.max(0, expected.durationSec);
- const durationToleranceSec = Math.min(
- 2,
- Math.max(0.5, expectedDurationSec * 0.02),
- );
+ const durationToleranceSec = Math.min(2, Math.max(0.5, expectedDurationSec * 0.02));
const nativeFrames = getFiniteNumber(summary.nativeSummary?.frames);
- const outputVideoFrames = getNvidiaCudaOutputStreamNumber(
- summary.outputVideo,
- "nb_frames",
- );
- const outputVideoDurationSec = getNvidiaCudaOutputStreamNumber(
- summary.outputVideo,
- "duration",
- );
- const outputAudioDurationSec = getNvidiaCudaOutputStreamNumber(
- summary.outputAudio,
- "duration",
- );
+ const outputVideoFrames = getNvidiaCudaOutputStreamNumber(summary.outputVideo, "nb_frames");
+ const outputVideoDurationSec = getNvidiaCudaOutputStreamNumber(summary.outputVideo, "duration");
+ const outputAudioDurationSec = getNvidiaCudaOutputStreamNumber(summary.outputAudio, "duration");
if (!summary.outputVideo) {
issues.push("missing output video probe");
}
- if (
- expected.requiresTimelineSync &&
- !isNvidiaCudaTimestampAlignedSummary(summary)
- ) {
- issues.push(
- "CUDA timeline mode is not timestamp-aligned for audio export",
- );
+ if (expected.requiresTimelineSync && !isNvidiaCudaTimestampAlignedSummary(summary)) {
+ issues.push("CUDA timeline mode is not timestamp-aligned for audio export");
}
if (nativeFrames === null) {
issues.push("missing native frame count");
} else if (nativeFrames < minimumFrames) {
- issues.push(
- `native frames ${nativeFrames} below expected minimum ${minimumFrames}`,
- );
+ issues.push(`native frames ${nativeFrames} below expected minimum ${minimumFrames}`);
}
if (outputVideoFrames !== null && outputVideoFrames < minimumFrames) {
issues.push(
@@ -539,7 +554,10 @@ async function persistNvidiaCudaExportDiagnostics(params: {
),
]);
} catch (error) {
- console.warn("[native-static-layout-export] Failed to persist NVIDIA CUDA diagnostics", error);
+ console.warn(
+ "[native-static-layout-export] Failed to persist NVIDIA CUDA diagnostics",
+ error,
+ );
}
}
@@ -572,6 +590,7 @@ export function parseWindowsGpuExportProgressLine(
intervalRoiCompositeFrames?: unknown;
intervalMonolithicCompositeFrames?: unknown;
intervalCopyCompositeFrames?: unknown;
+ stage?: unknown;
};
const currentFrame = Number(parsed.currentFrame);
const totalFrames = Number(parsed.totalFrames);
@@ -590,6 +609,9 @@ export function parseWindowsGpuExportProgressLine(
totalFrames: Math.max(1, Math.floor(totalFrames)),
percentage: Math.min(100, Math.max(0, percentage)),
};
+ if (parsed.stage === "preparing" || parsed.stage === "finalizing") {
+ progress.stage = parsed.stage;
+ }
const optionalNumberFields = [
"averageFps",
"instantFps",
@@ -619,6 +641,18 @@ export function parseWindowsGpuExportProgressLine(
}
}
+export function mapNvidiaCudaWrapperProgressPercentage(progress: NativeStaticLayoutExportProgress) {
+ if (progress.stage === "finalizing") {
+ return progress.percentage;
+ }
+
+ if (progress.currentFrame > 0 || progress.percentage === 0) {
+ return Math.min(98, 3 + progress.percentage * 0.95);
+ }
+
+ return progress.percentage;
+}
+
function startNativeStaticLayoutExportPowerGuard() {
try {
const blockerId = powerSaveBlocker.start("prevent-app-suspension");
@@ -639,10 +673,7 @@ function startNativeStaticLayoutExportPowerGuard() {
}
}
-function setNativeStaticLayoutExportProcessPriority(
- pid: number | undefined,
- label: string,
-) {
+function setNativeStaticLayoutExportProcessPriority(pid: number | undefined, label: string) {
if (!pid) {
return false;
}
@@ -651,10 +682,7 @@ function setNativeStaticLayoutExportProcessPriority(
os.setPriority(pid, NATIVE_EXPORT_HIGH_PRIORITY);
return true;
} catch (error) {
- console.warn(
- `[native-static-layout-export] Failed to raise ${label} priority`,
- error,
- );
+ console.warn(`[native-static-layout-export] Failed to raise ${label} priority`, error);
return false;
}
}
@@ -846,6 +874,163 @@ async function runFfmpegWithMetrics(
});
}
+function parseFfmpegProgressClockSeconds(value: string) {
+ const match = value.match(/^(\d+):(\d+):(\d+(?:\.\d+)?)$/);
+ if (!match) {
+ return null;
+ }
+
+ const hours = Number(match[1]);
+ const minutes = Number(match[2]);
+ const seconds = Number(match[3]);
+ if (![hours, minutes, seconds].every(Number.isFinite)) {
+ return null;
+ }
+
+ return hours * 3600 + minutes * 60 + seconds;
+}
+
+export function parseFfmpegProgressLineSeconds(line: string) {
+ const separatorIndex = line.indexOf("=");
+ if (separatorIndex <= 0) {
+ return null;
+ }
+
+ const key = line.slice(0, separatorIndex).trim();
+ const value = line.slice(separatorIndex + 1).trim();
+ if (key === "out_time_us" || key === "out_time_ms") {
+ const microseconds = Number(value);
+ return Number.isFinite(microseconds) && microseconds >= 0 ? microseconds / 1_000_000 : null;
+ }
+ if (key === "out_time") {
+ return parseFfmpegProgressClockSeconds(value);
+ }
+
+ return null;
+}
+
+function createFfmpegMuxProgressHandler(
+ totalSec: number | undefined,
+ onProgress?: (progress: NativeVideoAudioMuxProgress) => void,
+) {
+ if (!onProgress || !Number.isFinite(totalSec) || !totalSec || totalSec <= 0) {
+ return () => undefined;
+ }
+
+ let lineBuffer = "";
+ let lastRatio = 0;
+ return (chunk: Buffer | string) => {
+ lineBuffer += chunk.toString();
+ const lines = lineBuffer.split(/\r?\n/);
+ lineBuffer = lines.pop() ?? "";
+ for (const line of lines) {
+ const processedSec = parseFfmpegProgressLineSeconds(line);
+ if (processedSec === null) {
+ continue;
+ }
+ const ratio = Math.max(0, Math.min(1, processedSec / totalSec));
+ if (ratio < lastRatio + 0.0025 && ratio < 1) {
+ continue;
+ }
+ lastRatio = Math.max(lastRatio, ratio);
+ onProgress({
+ ratio: lastRatio,
+ processedSec,
+ totalSec,
+ });
+ }
+ };
+}
+
+async function runFfmpegAudioMux(
+ ffmpegPath: string,
+ args: string[],
+ timeoutMs: number,
+ options: NativeVideoExportFinishOptions,
+ onProgress?: (progress: NativeVideoAudioMuxProgress) => void,
+ session?: NativeStaticLayoutExportSession,
+) {
+ if (!onProgress && !session) {
+ await execFileAsync(ffmpegPath, args, {
+ timeout: timeoutMs,
+ maxBuffer: 20 * 1024 * 1024,
+ });
+ return;
+ }
+
+ const handleProgressChunk = createFfmpegMuxProgressHandler(
+ options.outputDurationSec,
+ onProgress,
+ );
+ await new Promise<void>((resolve, reject) => {
+ const child = spawn(ffmpegPath, args, {
+ stdio: ["ignore", "ignore", "pipe"],
+ });
+ if (session) {
+ session.currentProcess = child;
+ if (session.terminating) {
+ child.kill("SIGKILL");
+ }
+ }
+
+ let stderr = "";
+ let settled = false;
+ const timeout = setTimeout(() => {
+ if (settled) return;
+ child.kill("SIGKILL");
+ }, timeoutMs);
+
+ child.stderr.on("data", (chunk: Buffer) => {
+ const text = chunk.toString();
+ stderr += text;
+ handleProgressChunk(text);
+ });
+ child.once("error", (error) => {
+ if (settled) return;
+ settled = true;
+ if (session?.currentProcess === child) {
+ session.currentProcess = null;
+ }
+ clearTimeout(timeout);
+ reject(error);
+ });
+ child.once("close", (code, signal) => {
+ if (settled) return;
+ settled = true;
+ if (session?.currentProcess === child) {
+ session.currentProcess = null;
+ }
+ clearTimeout(timeout);
+ if (session?.terminating) {
+ reject(new Error("Native static layout export was cancelled"));
+ return;
+ }
+ if (code !== 0) {
+ const suffix = signal ? ` (signal ${signal})` : "";
+ reject(
+ new Error(
+ `FFmpeg audio mux exited with code ${code ?? "unknown"}${suffix}` +
+ (stderr.trim() ? `\nSTDERR:\n${stderr.trim()}` : ""),
+ ),
+ );
+ return;
+ }
+ if (onProgress) {
+ const completeProgress: NativeVideoAudioMuxProgress = { ratio: 1 };
+ if (
+ typeof options.outputDurationSec === "number" &&
+ Number.isFinite(options.outputDurationSec)
+ ) {
+ completeProgress.processedSec = options.outputDurationSec;
+ completeProgress.totalSec = options.outputDurationSec;
+ }
+ onProgress(completeProgress);
+ }
+ resolve();
+ });
+ });
+}
+
export function isHardwareAcceleratedVideoEncoder(encoderName: string) {
return /(videotoolbox|nvenc|qsv|amf|mf)/i.test(encoderName);
}
@@ -1095,6 +1280,132 @@ function formatCliNumber(value: number) {
: value.toFixed(6).replace(/0+$/, "").replace(/\.$/, "");
}
+function hasNativeStaticLayoutTimeline(options: NativeStaticLayoutExportOptions) {
+ return (options.timelineSegments?.length ?? 0) > 0;
+}
+
+export function buildNativeStaticLayoutTimelineSegments(
+ segments: NativeVideoExportEditedTrackSegment[],
+): NativeStaticLayoutTimelineSegment[] {
+ const timelineSegments: NativeStaticLayoutTimelineSegment[] = [];
+ let outputCursorMs = 0;
+
+ for (const segment of segments) {
+ const sourceStartMs = Math.max(0, segment.startMs);
+ const sourceEndMs = Math.max(sourceStartMs, segment.endMs);
+ const speed = segment.speed;
+ if (
+ !Number.isFinite(sourceStartMs) ||
+ !Number.isFinite(sourceEndMs) ||
+ !Number.isFinite(speed) ||
+ sourceEndMs - sourceStartMs <= 0.5 ||
+ speed <= 0
+ ) {
+ return [];
+ }
+
+ const outputDurationMs = (sourceEndMs - sourceStartMs) / speed;
+ if (!Number.isFinite(outputDurationMs) || outputDurationMs <= 0.5) {
+ return [];
+ }
+
+ const outputStartMs = outputCursorMs;
+ const outputEndMs = outputStartMs + outputDurationMs;
+ timelineSegments.push({
+ sourceStartMs,
+ sourceEndMs,
+ outputStartMs,
+ outputEndMs,
+ speed,
+ });
+ outputCursorMs = outputEndMs;
+ }
+
+ return timelineSegments;
+}
+
+function validateNativeStaticLayoutTimelineSegments(
+ segments: NativeStaticLayoutTimelineSegment[] | undefined,
+ durationSec: number,
+) {
+ if (!segments?.length) {
+ return [];
+ }
+
+ const normalized: NativeStaticLayoutTimelineSegment[] = [];
+ let expectedOutputStartMs = 0;
+ for (const segment of segments) {
+ const sourceStartMs = Math.max(0, segment.sourceStartMs);
+ const sourceEndMs = Math.max(sourceStartMs, segment.sourceEndMs);
+ const outputStartMs = Math.max(0, segment.outputStartMs);
+ const outputEndMs = Math.max(outputStartMs, segment.outputEndMs);
+ const speed = segment.speed;
+ if (
+ !Number.isFinite(sourceStartMs) ||
+ !Number.isFinite(sourceEndMs) ||
+ !Number.isFinite(outputStartMs) ||
+ !Number.isFinite(outputEndMs) ||
+ !Number.isFinite(speed) ||
+ sourceEndMs - sourceStartMs <= 0.5 ||
+ outputEndMs - outputStartMs <= 0.5 ||
+ speed <= 0
+ ) {
+ throw new Error("Native timeline map contains an invalid segment");
+ }
+
+ if (Math.abs(outputStartMs - expectedOutputStartMs) > 2) {
+ throw new Error("Native timeline map output ranges must be contiguous");
+ }
+
+ const expectedOutputDurationMs = (sourceEndMs - sourceStartMs) / speed;
+ if (Math.abs(expectedOutputDurationMs - (outputEndMs - outputStartMs)) > 2) {
+ throw new Error("Native timeline map segment speed does not match its output duration");
+ }
+
+ normalized.push({
+ sourceStartMs,
+ sourceEndMs,
+ outputStartMs,
+ outputEndMs,
+ speed,
+ });
+ expectedOutputStartMs = outputEndMs;
+ }
+
+ if (Math.abs(expectedOutputStartMs / 1000 - durationSec) > 0.05) {
+ throw new Error("Native timeline map duration does not match the export duration");
+ }
+
+ return normalized;
+}
+
+async function prepareNativeStaticLayoutTimelineMap(
+ options: NativeStaticLayoutExportOptions,
+ outputPath: string,
+) {
+ const segments = validateNativeStaticLayoutTimelineSegments(
+ options.timelineSegments,
+ options.durationSec,
+ );
+ if (segments.length === 0) {
+ return null;
+ }
+
+ const lines = segments.map((segment) =>
+ [
+ segment.sourceStartMs,
+ segment.sourceEndMs,
+ segment.outputStartMs,
+ segment.outputEndMs,
+ segment.speed,
+ ]
+ .map(formatCliNumber)
+ .join(","),
+ );
+ await fs.writeFile(outputPath, `${lines.join("\n")}\n`, "utf8");
+ return outputPath;
+}
+
async function pathExists(filePath: string) {
try {
await fs.access(filePath);
@@ -1104,6 +1415,72 @@ async function pathExists(filePath: string) {
}
}
+function isNvidiaVendorId(value: unknown) {
+ if (typeof value === "number") {
+ return value === NVIDIA_PCI_VENDOR_ID;
+ }
+ if (typeof value !== "string") {
+ return false;
+ }
+
+ const normalized = value.trim().toLowerCase();
+ if (normalized.length === 0) {
+ return false;
+ }
+ const parsed = normalized.startsWith("0x")
+ ? Number.parseInt(normalized.slice(2), 16)
+ : Number.parseInt(normalized, 10);
+
+ return Number.isFinite(parsed) && parsed === NVIDIA_PCI_VENDOR_ID;
+}
+
+function includesNvidiaGpuName(value: unknown) {
+ return typeof value === "string" && /\bnvidia\b/i.test(value);
+}
+
+function isNvidiaGpuDevice(device: unknown) {
+ if (!device || typeof device !== "object") {
+ return false;
+ }
+
+ const gpuDevice = device as ElectronGpuDeviceLike;
+ return (
+ isNvidiaVendorId(gpuDevice.vendorId) ||
+ includesNvidiaGpuName(gpuDevice.vendorString) ||
+ includesNvidiaGpuName(gpuDevice.deviceString)
+ );
+}
+
+export function hasNvidiaGpuDeviceInGpuInfo(gpuInfo: unknown) {
+ if (!gpuInfo || typeof gpuInfo !== "object") {
+ return false;
+ }
+
+ const devices = (gpuInfo as ElectronGpuInfoLike).gpuDevice;
+ return Array.isArray(devices) && devices.some(isNvidiaGpuDevice);
+}
+
+async function hasNvidiaGpuForCudaExportCandidate() {
+ const getGPUInfo = (
+ app as typeof app & {
+ getGPUInfo?: (infoType: "basic") => Promise<unknown>;
+ }
+ ).getGPUInfo;
+ if (typeof getGPUInfo !== "function") {
+ return true;
+ }
+
+ try {
+ return hasNvidiaGpuDeviceInGpuInfo(await getGPUInfo.call(app, "basic"));
+ } catch (error) {
+ console.warn(
+ "[native-static-layout-export] Unable to inspect GPU info before NVIDIA CUDA export; letting the helper decide",
+ error,
+ );
+ return true;
+ }
+}
+
function getNativeBinPlatformArch() {
return process.arch === "arm64" ? "win32-arm64" : "win32-x64";
}
@@ -1163,6 +1540,7 @@ async function resolveExperimentalWindowsGpuExporterPath() {
export function getNvidiaCudaAudioExportSkipReason(
audioMode: NativeVideoExportAudioMode | undefined,
+ options: { allowValidatedFallbackCandidate?: boolean } = {},
) {
const resolvedAudioMode = audioMode ?? "none";
if (resolvedAudioMode === "none") {
@@ -1171,27 +1549,64 @@ export function getNvidiaCudaAudioExportSkipReason(
if (process.env[NVIDIA_CUDA_ALLOW_AUDIO_EXPORT_ENV] === "1") {
return null;
}
+ if (options.allowValidatedFallbackCandidate) {
+ return null;
+ }
return `audio-mode:${resolvedAudioMode}`;
}
-function getExperimentalNvidiaCudaExportSkipReason(
+function isExplicitNvidiaCudaExportEnabled() {
+ return process.env[NVIDIA_CUDA_EXPORT_ENV] === "1";
+}
+
+function isExplicitNvidiaCudaExportDisabled() {
+ return process.env[NVIDIA_CUDA_EXPORT_ENV] === "0";
+}
+
+function isPackagedNvidiaCudaExportAutoCandidateEnabled() {
+ return app.isPackaged && !isExplicitNvidiaCudaExportDisabled();
+}
+
+function isPackagedNvidiaCudaExportAutoCandidateActive() {
+ return isPackagedNvidiaCudaExportAutoCandidateEnabled() && !isExplicitNvidiaCudaExportEnabled();
+}
+
+function isNvidiaCudaForceVideoOnlyEnabled() {
+ return process.env[NVIDIA_CUDA_FORCE_VIDEO_ONLY_ENV] === "1";
+}
+
+export async function getExperimentalNvidiaCudaExportSkipReason(
options: NativeStaticLayoutExportOptions,
) {
if (process.platform !== "win32") {
return "not-windows";
}
- if (process.env.RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT !== "1") {
+ const explicitCuda = isExplicitNvidiaCudaExportEnabled();
+ const packagedAutoCandidate = isPackagedNvidiaCudaExportAutoCandidateEnabled();
+ if (!explicitCuda && !packagedAutoCandidate) {
return "env-disabled";
}
if (!options.experimentalWindowsGpuCompositor) {
return "windows-gpu-compositor-disabled";
}
- return getNvidiaCudaAudioExportSkipReason(options.audioOptions?.audioMode);
+ if (packagedAutoCandidate && !explicitCuda) {
+ if (!(await resolveExperimentalNvidiaCudaExportScriptPath())) {
+ return "cuda-wrapper-unavailable";
+ }
+ if (!(await hasNvidiaGpuForCudaExportCandidate())) {
+ return "nvidia-gpu-unavailable";
+ }
+ }
+
+ return getNvidiaCudaAudioExportSkipReason(options.audioOptions?.audioMode, {
+ allowValidatedFallbackCandidate:
+ packagedAutoCandidate || isNvidiaCudaForceVideoOnlyEnabled(),
+ });
}
-async function resolveExperimentalNvidiaCudaExportScriptPath() {
+export async function resolveExperimentalNvidiaCudaExportScriptPath() {
if (process.platform !== "win32") {
return null;
}
@@ -1201,6 +1616,19 @@ async function resolveExperimentalNvidiaCudaExportScriptPath() {
if (configuredPath) {
candidates.push(configuredPath);
}
+ const resourcesPath = (process as NodeJS.Process & { resourcesPath?: string }).resourcesPath;
+ if (resourcesPath) {
+ candidates.push(
+ path.join(
+ resourcesPath,
+ "app.asar.unpacked",
+ "electron",
+ "native",
+ "nvidia-cuda-compositor",
+ "run-mp4-pipeline.mjs",
+ ),
+ );
+ }
candidates.push(
path.join(
process.cwd(),
@@ -1210,15 +1638,14 @@ async function resolveExperimentalNvidiaCudaExportScriptPath() {
"run-mp4-pipeline.mjs",
),
path.join(
- app.getAppPath(),
+ app.getAppPath().replace(/app\.asar$/, "app.asar.unpacked"),
"electron",
"native",
"nvidia-cuda-compositor",
"run-mp4-pipeline.mjs",
),
path.join(
- process.resourcesPath,
- "app.asar.unpacked",
+ app.getAppPath(),
"electron",
"native",
"nvidia-cuda-compositor",
@@ -1357,6 +1784,9 @@ function buildExperimentalWindowsGpuStaticLayoutArgs(
if (options.zoomTelemetryPath) {
args.push("--zoom-telemetry", options.zoomTelemetryPath);
}
+ if (options.timelineMapPath) {
+ args.push("--timeline-map", options.timelineMapPath);
+ }
return args;
}
@@ -1649,7 +2079,7 @@ async function prepareWindowsGpuWebcamInput(
return { inputPath: outputPath, elapsedMs: result.elapsedMs };
}
-function buildExperimentalNvidiaCudaStaticLayoutArgs(
+export function buildExperimentalNvidiaCudaStaticLayoutArgs(
options: NativeStaticLayoutExportOptions,
outputPath: string,
workDir: string,
@@ -1718,6 +2148,8 @@ function buildExperimentalNvidiaCudaStaticLayoutArgs(
String(Math.round(options.webcamSize ?? 0)),
"--webcam-radius",
String(Math.round(Math.max(0, options.webcamRadius ?? 0))),
+ "--webcam-time-offset-ms",
+ formatCliNumber(options.webcamTimeOffsetMs ?? 0),
"--webcam-stream",
);
if (options.webcamMirror !== false) {
@@ -1745,6 +2177,9 @@ function buildExperimentalNvidiaCudaStaticLayoutArgs(
if (options.zoomTelemetryPath) {
args.push("--zoom-telemetry", options.zoomTelemetryPath);
}
+ if (options.timelineMapPath) {
+ args.push("--timeline-map", options.timelineMapPath);
+ }
if (process.env.RECORDLY_NVIDIA_CUDA_SAMPLE_GPU === "1") {
args.push("--sample-gpu");
}
@@ -1753,6 +2188,10 @@ function buildExperimentalNvidiaCudaStaticLayoutArgs(
}
function canMuxNvidiaCudaSourceAudioInline(options: NativeStaticLayoutExportOptions) {
+ if (options.nvidiaCudaForceVideoOnly) {
+ return false;
+ }
+
const audioOptions = options.audioOptions;
if (audioOptions?.audioMode !== "copy-source" || !audioOptions.audioSourcePath) {
return false;
@@ -1790,6 +2229,8 @@ async function runExperimentalNvidiaCudaStaticLayoutExport(
...nodeCommand.env,
RECORDLY_NVIDIA_CUDA_EXPORT_HIGH_PRIORITY:
process.env.RECORDLY_NVIDIA_CUDA_EXPORT_HIGH_PRIORITY ?? "1",
+ RECORDLY_FFMPEG_EXE: ffmpegPath,
+ RECORDLY_FFPROBE_EXE: process.env.RECORDLY_FFPROBE_EXE ?? getFfprobeBinaryPath(),
[pathKey]: `${ffmpegDirectory}${path.delimiter}${process.env[pathKey] ?? ""}`,
};
const powerGuard = startNativeStaticLayoutExportPowerGuard();
@@ -1851,10 +2292,8 @@ async function runExperimentalNvidiaCudaStaticLayoutExport(
: elapsedMs > 0 && progress.currentFrame > 0
? (progress.currentFrame * 1000) / elapsedMs
: undefined;
- lastProgressPercentage = Math.max(
- lastProgressPercentage,
- progress.percentage,
- );
+ const mappedPercentage = mapNvidiaCudaWrapperProgressPercentage(progress);
+ lastProgressPercentage = Math.max(lastProgressPercentage, mappedPercentage);
onProgress?.({
...progress,
percentage: lastProgressPercentage,
@@ -2106,6 +2545,19 @@ export async function exportNativeStaticLayoutVideo(
try {
nativeStaticLayoutExportSessions.set(sessionId, session);
await fs.mkdir(chunkDirectory, { recursive: true });
+ const timelineMapPath = await prepareNativeStaticLayoutTimelineMap(
+ options,
+ path.join(chunkDirectory, "timeline-map.csv"),
+ );
+ if (timelineMapPath) {
+ if (!(options.experimentalWindowsGpuCompositor && process.platform === "win32")) {
+ throw new Error("Native timeline-map export requires the Windows GPU compositor");
+ }
+ options = {
+ ...options,
+ timelineMapPath,
+ };
+ }
const fullConfig: NativeStaticLayoutExportArgsConfig = {
inputPath: options.inputPath,
outputPath: videoOnlyPath,
@@ -2194,11 +2646,35 @@ export async function exportNativeStaticLayoutVideo(
}
let experimentalNvidiaCudaOptions = experimentalGpuOptions;
const nvidiaCudaSkipReason =
- getExperimentalNvidiaCudaExportSkipReason(options);
+ await getExperimentalNvidiaCudaExportSkipReason(options);
let shouldTryNvidiaCuda = nvidiaCudaSkipReason === null;
+ if (
+ shouldTryNvidiaCuda &&
+ (isPackagedNvidiaCudaExportAutoCandidateActive() ||
+ isNvidiaCudaForceVideoOnlyEnabled()) &&
+ (experimentalNvidiaCudaOptions.audioOptions?.audioMode ?? "none") !== "none"
+ ) {
+ experimentalNvidiaCudaOptions = {
+ ...experimentalNvidiaCudaOptions,
+ nvidiaCudaForceVideoOnly: true,
+ };
+ console.info(
+ "[native-static-layout-export] NVIDIA CUDA candidate will use shared audio mux validation",
+ {
+ audioMode:
+ experimentalNvidiaCudaOptions.audioOptions?.audioMode ?? "none",
+ forcedByEnv: isNvidiaCudaForceVideoOnlyEnabled(),
+ packagedAutoCandidate: isPackagedNvidiaCudaExportAutoCandidateActive(),
+ },
+ );
+ }
+ const shouldLogNvidiaCudaSkip =
+ isExplicitNvidiaCudaExportEnabled() ||
+ (isPackagedNvidiaCudaExportAutoCandidateEnabled() &&
+ nvidiaCudaSkipReason !== "env-disabled");
if (
!shouldTryNvidiaCuda &&
- process.env.RECORDLY_EXPERIMENTAL_NVIDIA_CUDA_EXPORT === "1" &&
+ shouldLogNvidiaCudaSkip &&
nvidiaCudaSkipReason !== "env-disabled"
) {
console.warn(
@@ -2207,6 +2683,7 @@ export async function exportNativeStaticLayoutVideo(
reason: nvidiaCudaSkipReason,
audioMode: options.audioOptions?.audioMode ?? "none",
overrideEnv: NVIDIA_CUDA_ALLOW_AUDIO_EXPORT_ENV,
+ packagedAutoCandidate: isPackagedNvidiaCudaExportAutoCandidateEnabled(),
},
);
}
@@ -2223,7 +2700,7 @@ export async function exportNativeStaticLayoutVideo(
shouldTryNvidiaCuda = Boolean(cursorTelemetryPath && cursorAtlas);
if (cursorTelemetryPath && cursorAtlas) {
experimentalNvidiaCudaOptions = {
- ...experimentalGpuOptions,
+ ...experimentalNvidiaCudaOptions,
cursorTelemetryPath,
cursorAtlasPath: cursorAtlas.atlasPath,
cursorAtlasMetadataPath: cursorAtlas.metadataPath,
@@ -2233,8 +2710,9 @@ export async function exportNativeStaticLayoutVideo(
if (shouldTryNvidiaCuda) {
try {
- const shouldMuxAudioInline =
- canMuxNvidiaCudaSourceAudioInline(experimentalNvidiaCudaOptions);
+ const shouldMuxAudioInline = canMuxNvidiaCudaSourceAudioInline(
+ experimentalNvidiaCudaOptions,
+ );
const cudaResult = await runExperimentalNvidiaCudaStaticLayoutExport(
ffmpegPath,
experimentalNvidiaCudaOptions,
@@ -2248,9 +2726,7 @@ export async function exportNativeStaticLayoutVideo(
{
durationSec: options.durationSec,
targetFrames: Math.ceil(options.durationSec * options.frameRate),
- requiresTimelineSync:
- (experimentalNvidiaCudaOptions.audioOptions?.audioMode ??
- "none") !== "none",
+ requiresTimelineSync: shouldMuxAudioInline,
},
);
if (cudaValidationIssues.length > 0) {
@@ -2258,6 +2734,12 @@ export async function exportNativeStaticLayoutVideo(
`Experimental NVIDIA CUDA compositor produced an invalid output: ${cudaValidationIssues.join("; ")}`,
);
}
+ const outputStat = await fs.stat(videoOnlyPath);
+ if (outputStat.size <= 0) {
+ throw new Error(
+ "Experimental NVIDIA CUDA compositor produced an empty output file",
+ );
+ }
console.info(
"[native-static-layout-export] NVIDIA CUDA compositor completed",
{
@@ -2289,7 +2771,6 @@ export async function exportNativeStaticLayoutVideo(
zoomSamples: cudaResult.summary.nativeSummary?.zoomSamples,
},
);
- const outputStat = await fs.stat(videoOnlyPath);
metrics.chunkCount = 1;
metrics.chunkDurationSec = options.durationSec;
metrics.chunkExecMs += cudaResult.elapsedMs;
@@ -2310,7 +2791,7 @@ export async function exportNativeStaticLayoutVideo(
}
metrics.fallbackChunkCount++;
console.warn(
- "[native-static-layout-export] Experimental NVIDIA CUDA compositor unavailable; falling back to Windows GPU compositor:",
+ "[native-static-layout-export] Experimental NVIDIA CUDA compositor failed or produced invalid output; falling back to Windows GPU compositor:",
error,
);
await removeTemporaryExportFile(videoOnlyPath);
@@ -2364,6 +2845,9 @@ export async function exportNativeStaticLayoutVideo(
if (session.terminating) {
throw error;
}
+ if (hasNativeStaticLayoutTimeline(options)) {
+ throw error;
+ }
metrics.fallbackChunkCount++;
console.warn(
"[native-static-layout-export] Experimental Windows GPU compositor unavailable; falling back to FFmpeg static layout:",
@@ -2376,6 +2860,10 @@ export async function exportNativeStaticLayoutVideo(
}
}
+ if (!didRenderVideo && hasNativeStaticLayoutTimeline(options)) {
+ throw new Error("Native timeline-map export requires a GPU compositor backend");
+ }
+
if (!didRenderVideo && usePrecompositedLayout) {
const maskPath = path.join(chunkDirectory, "layout-mask.pgm");
const staticBackgroundPath = path.join(chunkDirectory, "layout-background.png");
@@ -2566,9 +3054,30 @@ export async function exportNativeStaticLayoutVideo(
metrics,
};
}
+ const audioMuxProgressStart = 97.25;
+ const audioMuxProgressEnd = 99;
+ const progressTotalFrames = Math.max(1, Math.ceil(options.durationSec * options.frameRate));
const finalized = await muxNativeVideoExportAudio(
videoOnlyPath,
options.audioOptions ?? {},
+ (progress) => {
+ const ratio = Math.max(0, Math.min(1, progress.ratio));
+ const percentage =
+ audioMuxProgressStart + (audioMuxProgressEnd - audioMuxProgressStart) * ratio;
+ const progressPayload: NativeStaticLayoutExportProgress = {
+ sessionId,
+ stage: "finalizing",
+ currentFrame: progressTotalFrames,
+ totalFrames: progressTotalFrames,
+ percentage,
+ };
+ const backend = metrics.chunks[0]?.backend;
+ if (backend) {
+ progressPayload.backend = backend;
+ }
+ onProgress?.(progressPayload);
+ },
+ session,
);
Object.assign(metrics, finalized.metrics);
outputPathToKeep = finalized.outputPath;
@@ -2704,20 +3213,22 @@ export function buildNativeVideoAudioMuxArgs(
audioInputPath: string,
outputPath: string,
options: NativeVideoExportFinishOptions,
+ argsOptions: NativeVideoAudioMuxArgsOptions = {},
) {
const audioMode = options.audioMode ?? "none";
const useEditedTrackFiltergraph =
audioMode === "edited-track" && options.editedTrackStrategy === "filtergraph-fast-path";
- const args = [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-i",
- videoPath,
- "-i",
- audioInputPath,
- ];
+ const args = ["-y", "-hide_banner", "-loglevel", "error"];
+ if (argsOptions.progressPipe) {
+ args.push(
+ "-stats_period",
+ "0.5",
+ "-progress",
+ `pipe:${argsOptions.progressPipe}`,
+ "-nostats",
+ );
+ }
+ args.push("-i", videoPath, "-i", audioInputPath);
if (audioMode === "trim-source") {
const filter = buildTrimmedSourceAudioFilter(options.trimSegments ?? []);
@@ -2734,7 +3245,23 @@ export function buildNativeVideoAudioMuxArgs(
if (!filter) {
throw new Error("Edited-track filtergraph inputs are incomplete for native export");
}
- args.push("-filter_complex", filter, "-map", "0:v:0", "-map", "[aout]");
+ if (
+ typeof options.outputDurationSec === "number" &&
+ Number.isFinite(options.outputDurationSec) &&
+ options.outputDurationSec > 0
+ ) {
+ const duration = formatFfmpegSeconds(options.outputDurationSec * 1000);
+ args.push(
+ "-filter_complex",
+ `${filter};[aout]apad,atrim=duration=${duration},asetpts=PTS-STARTPTS[aout_sync]`,
+ "-map",
+ "0:v:0",
+ "-map",
+ "[aout_sync]",
+ );
+ } else {
+ args.push("-filter_complex", filter, "-map", "0:v:0", "-map", "[aout]");
+ }
} else {
args.push("-map", "0:v:0", "-map", "1:a:0");
}
@@ -2762,6 +3289,8 @@ export function buildNativeVideoAudioMuxArgs(
export async function muxNativeVideoExportAudio(
videoPath: string,
options: NativeVideoExportFinishOptions,
+ onProgress?: (progress: NativeVideoAudioMuxProgress) => void,
+ session?: NativeStaticLayoutExportSession,
) {
const audioMode = options.audioMode ?? "none";
if (audioMode === "none") {
@@ -2807,14 +3336,17 @@ export async function muxNativeVideoExportAudio(
`${path.basename(videoPath, path.extname(videoPath))}-final.mp4`,
);
- const args = buildNativeVideoAudioMuxArgs(videoPath, audioInputPath, outputPath, options);
+ const args = buildNativeVideoAudioMuxArgs(
+ videoPath,
+ audioInputPath,
+ outputPath,
+ options,
+ onProgress ? { progressPipe: 2 } : {},
+ );
try {
const ffmpegExecStartedAt = getNowMs();
- await execFileAsync(ffmpegPath, args, {
- timeout: 15 * 60 * 1000,
- maxBuffer: 20 * 1024 * 1024,
- });
+ await runFfmpegAudioMux(ffmpegPath, args, 15 * 60 * 1000, options, onProgress, session);
metrics.ffmpegExecMs = getNowMs() - ffmpegExecStartedAt;
console.info("[native-video-export] Audio mux completed", {
ffmpegExecMs: metrics.ffmpegExecMs,
diff --git a/electron/ipc/ffmpeg/binary.ts b/electron/ipc/ffmpeg/binary.ts
index c76acd40..a0cf8468 100644
--- a/electron/ipc/ffmpeg/binary.ts
+++ b/electron/ipc/ffmpeg/binary.ts
@@ -22,6 +22,31 @@ export function loadFfmpegStatic(): string | null {
return null;
}
+export function loadFfprobeStatic(): string | null {
+ try {
+ const moduleExports = nodeRequire("ffprobe-static");
+ if (typeof moduleExports === "string") {
+ return moduleExports;
+ }
+
+ if (typeof moduleExports?.path === "string") {
+ return moduleExports.path as string;
+ }
+
+ if (typeof moduleExports?.default === "string") {
+ return moduleExports.default as string;
+ }
+
+ if (typeof moduleExports?.default?.path === "string") {
+ return moduleExports.default.path as string;
+ }
+ } catch {
+ // ffprobe-static not available; fall through to system FFprobe
+ }
+
+ return null;
+}
+
export function resolveSystemFfmpegBinaryPath(): string | null {
const locator = process.platform === "win32" ? "where" : "which";
const result = spawnSync(locator, ["ffmpeg"], {
@@ -32,8 +57,8 @@ export function resolveSystemFfmpegBinaryPath(): string | null {
if (result.status === 0) {
const candidate = result.stdout
.split(/\r?\n/)
-.map((line: string) => line.trim())
- .find((line: string) => line.length > 0);
+ .map((line: string) => line.trim())
+ .find((line: string) => line.length > 0);
if (candidate) {
return candidate;
@@ -57,6 +82,40 @@ export function resolveSystemFfmpegBinaryPath(): string | null {
return null;
}
+export function resolveSystemFfprobeBinaryPath(): string | null {
+ const locator = process.platform === "win32" ? "where" : "which";
+ const result = spawnSync(locator, ["ffprobe"], {
+ encoding: "utf-8",
+ windowsHide: true,
+ });
+
+ if (result.status === 0) {
+ const candidate = result.stdout
+ .split(/\r?\n/)
+ .map((line: string) => line.trim())
+ .find((line: string) => line.length > 0);
+
+ if (candidate) {
+ return candidate;
+ }
+ }
+
+ if (process.platform !== "win32") {
+ const commonPaths = [
+ "/opt/homebrew/bin/ffprobe",
+ "/usr/local/bin/ffprobe",
+ "/usr/bin/ffprobe",
+ ];
+ for (const p of commonPaths) {
+ if (existsSync(p)) {
+ return p;
+ }
+ }
+ }
+
+ return null;
+}
+
export function getFfmpegBinaryPath(): string {
const ffmpegStatic = loadFfmpegStatic();
if (ffmpegStatic && typeof ffmpegStatic === "string") {
@@ -78,3 +137,25 @@ export function getFfmpegBinaryPath(): string {
"FFmpeg binary is unavailable. Install ffmpeg-static for this platform or make ffmpeg available on PATH.",
);
}
+
+export function getFfprobeBinaryPath(): string {
+ const ffprobeStatic = loadFfprobeStatic();
+ if (ffprobeStatic && typeof ffprobeStatic === "string") {
+ const bundledPath = app.isPackaged
+ ? ffprobeStatic.replace(/\.asar([/\\])/, ".asar.unpacked$1")
+ : ffprobeStatic;
+
+ if (existsSync(bundledPath)) {
+ return bundledPath;
+ }
+ }
+
+ const systemFfprobe = resolveSystemFfprobeBinaryPath();
+ if (systemFfprobe) {
+ return systemFfprobe;
+ }
+
+ throw new Error(
+ "FFprobe binary is unavailable. Install ffprobe-static for this platform or make ffprobe available on PATH.",
+ );
+}
diff --git a/electron/ipc/recording/windows.ts b/electron/ipc/recording/windows.ts
index 4d498192..9c944028 100644
--- a/electron/ipc/recording/windows.ts
+++ b/electron/ipc/recording/windows.ts
@@ -8,9 +8,7 @@ import { getFfmpegBinaryPath } from "../ffmpeg/binary";
import {
appendSyncedAudioFilter,
applyRecordedAudioStartDelay,
- buildPausedAudioFilter,
getAudioSyncAdjustment,
- normalizePauseSegments,
} from "../ffmpeg/filters";
import { getWindowsCaptureExePath } from "../paths/binaries";
import {
@@ -23,7 +21,7 @@ import {
windowsCaptureTargetPath,
windowsNativeCaptureActive,
} from "../state";
-import type { AudioSyncAdjustment, PauseSegment } from "../types";
+import type { AudioSyncAdjustment } from "../types";
import { moveFileWithOverwrite } from "../utils";
import {
getCompanionAudioStartDelayMs,
@@ -37,6 +35,7 @@ const execFileAsync = promisify(execFile);
// Match the browser path's "usable speech level" intent with a standard
// loudness pass on native Windows mic audio instead of a fixed tiny boost.
const WINDOWS_NATIVE_MIC_PRE_FILTERS = ["loudnorm=I=-16:TP=-1.5:LRA=11"];
+const MIN_NATIVE_WINDOWS_VIDEO_PAD_MS = 500;
export async function isNativeWindowsCaptureAvailable(): Promise<boolean> {
if (process.platform !== "win32") return false;
@@ -161,11 +160,68 @@ export function attachWindowsCaptureLifecycle(proc: ChildProcessWithoutNullStrea
});
}
+export async function extendNativeWindowsVideoToDuration(
+ videoPath: string,
+ targetDurationMs: number | null | undefined,
+) {
+ if (!Number.isFinite(targetDurationMs) || (targetDurationMs ?? 0) <= 0) {
+ return { padded: false, durationSeconds: 0 };
+ }
+
+ const currentDurationSeconds = await probeMediaDurationSeconds(videoPath);
+ if (currentDurationSeconds <= 0) {
+ return { padded: false, durationSeconds: currentDurationSeconds };
+ }
+
+ const targetDurationSeconds = (targetDurationMs ?? 0) / 1000;
+ const padDurationSeconds = targetDurationSeconds - currentDurationSeconds;
+ if (padDurationSeconds * 1000 < MIN_NATIVE_WINDOWS_VIDEO_PAD_MS) {
+ return { padded: false, durationSeconds: currentDurationSeconds };
+ }
+
+ const ffmpegPath = getFfmpegBinaryPath();
+ const paddedOutputPath = `${videoPath}.duration-padded.mp4`;
+
+ try {
+ await execFileAsync(
+ ffmpegPath,
+ [
+ "-y",
+ "-hide_banner",
+ "-nostdin",
+ "-nostats",
+ "-i",
+ videoPath,
+ "-vf",
+ `tpad=stop_mode=clone:stop_duration=${padDurationSeconds.toFixed(3)}`,
+ "-an",
+ "-c:v",
+ "libx264",
+ "-preset",
+ "veryfast",
+ "-crf",
+ "18",
+ "-pix_fmt",
+ "yuv420p",
+ "-movflags",
+ "+faststart",
+ paddedOutputPath,
+ ],
+ { timeout: 300000, maxBuffer: 10 * 1024 * 1024 },
+ );
+ await validateRecordedVideo(paddedOutputPath);
+ await moveFileWithOverwrite(paddedOutputPath, videoPath);
+ return { padded: true, durationSeconds: targetDurationSeconds };
+ } catch (error) {
+ await fs.rm(paddedOutputPath, { force: true }).catch(() => undefined);
+ throw error;
+ }
+}
+
export async function muxNativeWindowsVideoWithAudio(
videoPath: string,
systemAudioPath: string | null,
micAudioPath: string | null,
- pauseSegments: PauseSegment[] = [],
) {
const ffmpegPath = getFfmpegBinaryPath();
const inputs: string[] = ["-i", videoPath];
@@ -232,7 +288,6 @@ export async function muxNativeWindowsVideoWithAudio(
}
const mixedOutputPath = `${videoPath}.muxed.mp4`;
- const normalizedPauseSegments = normalizePauseSegments(pauseSegments);
const systemAdjustment = audioAdjustments.get("system") ?? {
mode: "none",
delayMs: 0,
@@ -249,29 +304,8 @@ export async function muxNativeWindowsVideoWithAudio(
try {
if (audioInputs.length === 2) {
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);
- }
-
- const systemLabel = systemPauseFilter ? "[system_trimmed]" : "[1:a]";
- const micLabel = micPauseFilter ? "[mic_trimmed]" : "[2:a]";
-
- appendSyncedAudioFilter(filterParts, systemLabel, "s", systemAdjustment);
- appendSyncedAudioFilter(filterParts, micLabel, "m", micAdjustment, {
+ appendSyncedAudioFilter(filterParts, "[1:a]", "s", systemAdjustment);
+ appendSyncedAudioFilter(filterParts, "[2:a]", "m", micAdjustment, {
preFilters: WINDOWS_NATIVE_MIC_PRE_FILTERS,
});
filterParts.push("[s][m]amix=inputs=2:duration=longest:normalize=0[aout]");
@@ -302,11 +336,6 @@ export async function muxNativeWindowsVideoWithAudio(
{ timeout: muxTimeoutMs, maxBuffer: 20 * 1024 * 1024 },
);
} else {
- const pauseFilter = buildPausedAudioFilter(
- "1:a",
- "trimmed_audio",
- normalizedPauseSegments,
- );
const singleAdjustment = audioAdjustments.get(audioInputs[0]) ?? {
mode: "none",
delayMs: 0,
@@ -314,17 +343,13 @@ export async function muxNativeWindowsVideoWithAudio(
durationDeltaMs: 0,
};
+ const filterParts: string[] = [];
// Always route through the filter graph so that aresample=async=1 is
// applied. This corrects progressive clock drift between video and
// audio tracks that a simple duration comparison cannot detect.
- const filterParts: string[] = [];
- if (pauseFilter) {
- filterParts.push(pauseFilter);
- }
- const srcLabel = pauseFilter ? "[trimmed_audio]" : "[1:a]";
appendSyncedAudioFilter(
filterParts,
- srcLabel,
+ "[1:a]",
"aout",
singleAdjustment,
audioInputs[0] === "mic" ? { preFilters: WINDOWS_NATIVE_MIC_PRE_FILTERS } : 1,
diff --git a/electron/ipc/register/recording.ts b/electron/ipc/register/recording.ts
index 8d395e78..f8ecd1d0 100644
--- a/electron/ipc/register/recording.ts
+++ b/electron/ipc/register/recording.ts
@@ -59,6 +59,7 @@ import {
} from "../recording/mac";
import {
attachWindowsCaptureLifecycle,
+ extendNativeWindowsVideoToDuration,
isNativeWindowsCaptureAvailable,
muxNativeWindowsVideoWithAudio,
waitForWindowsCaptureStart,
@@ -125,7 +126,6 @@ import {
import type {
CursorTelemetryPoint,
NativeMacRecordingOptions,
- PauseSegment,
SelectedSource,
} from "../types";
import {
@@ -1036,7 +1036,7 @@ export function registerRecordingHandlers(
ipcMain.handle(
"mux-native-windows-recording",
- async (_event, pauseSegments?: PauseSegment[]) => {
+ async (_event, expectedDurationMs?: number) => {
const videoPath = windowsPendingVideoPath;
const orphanedMicAudioPath = windowsOrphanedMicAudioPath;
setWindowsPendingVideoPath(null);
@@ -1047,12 +1047,28 @@ export function registerRecordingHandlers(
}
try {
+ try {
+ const padding = await extendNativeWindowsVideoToDuration(
+ videoPath,
+ expectedDurationMs,
+ );
+ if (padding.padded) {
+ console.log(
+ `[mux-win] Extended native Windows video to ${padding.durationSeconds.toFixed(3)}s using the final frame`,
+ );
+ }
+ } catch (paddingError) {
+ console.warn(
+ "[mux-win] Failed to extend native Windows video duration:",
+ paddingError,
+ );
+ }
+
if (windowsSystemAudioPath || windowsMicAudioPath) {
await muxNativeWindowsVideoWithAudio(
videoPath,
windowsSystemAudioPath,
windowsMicAudioPath,
- pauseSegments ?? [],
);
setWindowsSystemAudioPath(null);
setWindowsMicAudioPath(null);
diff --git a/electron/native/bin/win32-x64/helpers-manifest.json b/electron/native/bin/win32-x64/helpers-manifest.json
index e2c5313f..3e34d6ed 100644
--- a/electron/native/bin/win32-x64/helpers-manifest.json
+++ b/electron/native/bin/win32-x64/helpers-manifest.json
@@ -5,31 +5,31 @@
"helpers": {
"wgc-capture": {
"binaryName": "wgc-capture.exe",
- "binarySha256": "424c64fc9569e8f6add29130ee935dfcb4451c07c1036d3738c22d9b79ad39cd",
+ "binarySha256": "31beb9528e79262da6aa31fef09b6125153a838f957060ccc1ec5d4da844516c",
"sourceDir": "electron/native/wgc-capture",
- "sourceFingerprint": "4fd7e2f5d0e804a8aaaaddcba3a3e02b42739c0148a0d38be24325ddbb659ec1",
- "updatedAt": "2026-05-03T15:39:44.054Z"
+ "sourceFingerprint": "4bb09b770a21eb32daf6325d46d73d0522f20a4cbce9dda8b3df2277f8cfd0be",
+ "updatedAt": "2026-05-05T18:41:50.078Z"
},
"cursor-monitor": {
"binaryName": "cursor-monitor.exe",
"binarySha256": "6ae6d91103b6e891a851e8ea5791e1c1f9aaab700134c18bc4c46cfffd7fdd12",
"sourceDir": "electron/native/cursor-monitor",
"sourceFingerprint": "6ad1b8b50bb336f2a48937b06f5ec56d90b6ab4a3e56a4bca278cf67a5d3e52e",
- "updatedAt": "2026-05-03T15:39:52.446Z"
+ "updatedAt": "2026-05-05T18:42:26.007Z"
},
"recordly-gpu-export": {
"binaryName": "recordly-gpu-export.exe",
- "binarySha256": "9b3d4dff520356e5db563cc3992d777f8bab8eaf6d0bb718f9c1d7d7da37fac5",
+ "binarySha256": "e79cb823dd2cfade857db67696ae6456ae0ba3ae9381b6792c948ac648521bd4",
"sourceDir": "electron/native/gpu-export-probe",
- "sourceFingerprint": "75bf080c4a5cbbcb1d42fb088a1545130c613e42d9e7681cc23f9151d1c8072b",
- "updatedAt": "2026-05-03T15:39:47.938Z"
+ "sourceFingerprint": "45aaa4c0cbeb45ecaa9ff0a87b870c33bd0d5b09fb5b3acef45b246970a12e7f",
+ "updatedAt": "2026-05-05T18:41:54.966Z"
},
"recordly-nvidia-cuda-compositor": {
"binaryName": "recordly-nvidia-cuda-compositor.exe",
- "binarySha256": "f274039c46445a3b8e57c9faa6307238d6facc04d5c88f7bb84c43563feda649",
+ "binarySha256": "2913d78e2c1114d32c92fb4238749f7ff46131ec92cf3ad0b55af910c7cdbb07",
"sourceDir": "electron/native/nvidia-cuda-compositor",
- "sourceFingerprint": "bc6f657ff3226f8c3bcdc89354de4118d0ef0f3ac1b5fde6d496bda9b77b65c6",
- "updatedAt": "2026-05-04T02:54:12.432Z"
+ "sourceFingerprint": "a60538981da7dd3d9645f59926d42f4f2f0f1f0663d3905e0416fdff8becb314",
+ "updatedAt": "2026-05-05T18:42:20.123Z"
}
}
}
diff --git a/electron/native/bin/win32-x64/recordly-gpu-export.exe b/electron/native/bin/win32-x64/recordly-gpu-export.exe
index 2a64a27d..5b1662d1 100644
Binary files a/electron/native/bin/win32-x64/recordly-gpu-export.exe and b/electron/native/bin/win32-x64/recordly-gpu-export.exe differ
diff --git a/electron/native/bin/win32-x64/recordly-nvidia-cuda-compositor.exe b/electron/native/bin/win32-x64/recordly-nvidia-cuda-compositor.exe
index 2dfd3e71..9443ee8e 100644
Binary files a/electron/native/bin/win32-x64/recordly-nvidia-cuda-compositor.exe and b/electron/native/bin/win32-x64/recordly-nvidia-cuda-compositor.exe differ
diff --git a/electron/native/bin/win32-x64/wgc-capture.exe b/electron/native/bin/win32-x64/wgc-capture.exe
index 9cc29a56..bdbbdcc4 100644
Binary files a/electron/native/bin/win32-x64/wgc-capture.exe and b/electron/native/bin/win32-x64/wgc-capture.exe differ
diff --git a/electron/native/gpu-export-probe/src/main.cpp b/electron/native/gpu-export-probe/src/main.cpp
index 3026a362..9f34bdda 100644
--- a/electron/native/gpu-export-probe/src/main.cpp
+++ b/electron/native/gpu-export-probe/src/main.cpp
@@ -17,6 +17,7 @@
#include <cstdio>
#include <cstdint>
#include <cstring>
+#include <fstream>
#include <memory>
#include <iostream>
#include <sstream>
@@ -73,6 +74,7 @@ struct Options {
std::wstring cursorAtlasMetadataPath;
float cursorSize = 84.0f;
std::wstring zoomTelemetryPath;
+ std::wstring timelineMapPath;
bool preferHighPerformanceAdapter = false;
int adapterIndex = -1;
bool fastEncoderTuning = false;
@@ -157,6 +159,14 @@ struct ZoomSample {
float y = 0.0f;
};
+struct TimelineSegment {
+ double sourceStartMs = 0.0;
+ double sourceEndMs = 0.0;
+ double outputStartMs = 0.0;
+ double outputEndMs = 0.0;
+ double speed = 1.0;
+};
+
struct Timer {
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
@@ -256,6 +266,95 @@ bool parseHexColor(const std::wstring& value, float& red, float& green, float& b
}
}
+std::vector<TimelineSegment> loadTimelineMap(const std::wstring& path) {
+ std::vector<TimelineSegment> segments;
+ if (path.empty()) {
+ return segments;
+ }
+
+ FILE* file = nullptr;
+ if (_wfopen_s(&file, path.c_str(), L"rb") != 0 || !file) {
+ std::cerr << "[gpu-export] Unable to open timeline map" << std::endl;
+ return segments;
+ }
+
+ char line[512] = {};
+ double expectedOutputStartMs = 0.0;
+ while (fgets(line, sizeof(line), file)) {
+ TimelineSegment segment;
+ if (sscanf_s(
+ line,
+ "%lf,%lf,%lf,%lf,%lf",
+ &segment.sourceStartMs,
+ &segment.sourceEndMs,
+ &segment.outputStartMs,
+ &segment.outputEndMs,
+ &segment.speed) != 5) {
+ segments.clear();
+ break;
+ }
+ if (
+ !std::isfinite(segment.sourceStartMs) ||
+ !std::isfinite(segment.sourceEndMs) ||
+ !std::isfinite(segment.outputStartMs) ||
+ !std::isfinite(segment.outputEndMs) ||
+ !std::isfinite(segment.speed) ||
+ segment.sourceEndMs <= segment.sourceStartMs ||
+ segment.outputEndMs <= segment.outputStartMs ||
+ segment.speed <= 0.0 ||
+ std::abs(segment.outputStartMs - expectedOutputStartMs) > 2.0) {
+ segments.clear();
+ break;
+ }
+ expectedOutputStartMs = segment.outputEndMs;
+ segments.push_back(segment);
+ }
+ fclose(file);
+ return segments;
+}
+
+bool sourceToOutputMs(
+ const std::vector<TimelineSegment>& segments,
+ double sourceMs,
+ double& outputMs) {
+ if (segments.empty()) {
+ outputMs = sourceMs;
+ return true;
+ }
+
+ constexpr double kToleranceMs = 1.0;
+ for (const auto& segment : segments) {
+ if (sourceMs + kToleranceMs < segment.sourceStartMs) {
+ return false;
+ }
+ if (sourceMs <= segment.sourceEndMs + kToleranceMs) {
+ const double clampedSourceMs =
+ std::min(segment.sourceEndMs, std::max(segment.sourceStartMs, sourceMs));
+ outputMs = segment.outputStartMs + (clampedSourceMs - segment.sourceStartMs) / segment.speed;
+ outputMs = std::min(segment.outputEndMs, std::max(segment.outputStartMs, outputMs));
+ return true;
+ }
+ }
+
+ return false;
+}
+
+double outputToSourceMs(const std::vector<TimelineSegment>& segments, double outputMs) {
+ if (segments.empty()) {
+ return outputMs;
+ }
+
+ for (const auto& segment : segments) {
+ if (outputMs <= segment.outputEndMs + 1.0) {
+ const double clampedOutputMs =
+ std::min(segment.outputEndMs, std::max(segment.outputStartMs, outputMs));
+ return segment.sourceStartMs + (clampedOutputMs - segment.outputStartMs) * segment.speed;
+ }
+ }
+
+ return segments.back().sourceEndMs;
+}
+
Options parseOptions(int argc, wchar_t** argv) {
std::vector<std::wstring> args;
args.reserve(static_cast<size_t>(argc));
@@ -328,6 +427,10 @@ Options parseOptions(int argc, wchar_t** argv) {
if (!zoomTelemetry.empty()) {
options.zoomTelemetryPath = zoomTelemetry;
}
+ const auto timelineMap = getArgValue(args, L"--timeline-map");
+ if (!timelineMap.empty()) {
+ options.timelineMapPath = timelineMap;
+ }
options.preferHighPerformanceAdapter = hasArg(args, L"--prefer-high-performance-adapter");
options.adapterIndex = static_cast<int>(parseLongArg(args, L"--adapter-index", options.adapterIndex));
options.fastEncoderTuning = hasArg(args, L"--fast-encoder-tuning");
@@ -376,6 +479,11 @@ public:
options_ = options;
loadCursorTelemetry();
loadZoomTelemetry();
+ timelineSegments_ = loadTimelineMap(options_.timelineMapPath);
+ if (!options_.timelineMapPath.empty() && timelineSegments_.empty()) {
+ std::cerr << "[gpu-export] Timeline map is invalid or empty" << std::endl;
+ return false;
+ }
{
const Timer timer;
@@ -2867,11 +2975,34 @@ float4 main(PSIn input) : SV_Target {
if (firstSourceTimestamp < 0) {
firstSourceTimestamp = timestamp;
}
- const LONGLONG outputTimestamp = std::max<LONGLONG>(0, timestamp - firstSourceTimestamp);
- if (outputTimestamp >= maxOutputTimestamp) {
+ const LONGLONG sourceTimestamp = std::max<LONGLONG>(0, timestamp - firstSourceTimestamp);
+ const double sourceTimestampMs = static_cast<double>(sourceTimestamp) / 10'000.0;
+ double mappedOutputMs = sourceTimestampMs;
+ if (!sourceToOutputMs(timelineSegments_, mappedOutputMs, mappedOutputMs)) {
+ continue;
+ }
+ const LONGLONG outputTimestamp =
+ static_cast<LONGLONG>(std::llround(mappedOutputMs * 10'000.0));
+ if (
+ (timelineSegments_.empty() && outputTimestamp >= maxOutputTimestamp) ||
+ (!timelineSegments_.empty() && outputTimestamp > maxOutputTimestamp + outputFrameDuration)
+ ) {
break;
}
const LONGLONG sampleWindowEnd = outputTimestamp + (outputFrameDuration / 2);
+ const bool timelineSampleNearEnd =
+ !timelineSegments_.empty() &&
+ sourceTimestampMs >=
+ (timelineSegments_.back().sourceEndMs - std::max(1.0, 2000.0 / options_.fps));
+ const UINT expectedFramesForSample = timelineSegments_.empty()
+ ? expectedOutputFrames
+ : timelineSampleNearEnd
+ ? expectedOutputFrames
+ : std::min<UINT>(
+ expectedOutputFrames,
+ static_cast<UINT>(std::floor(
+ (static_cast<double>(outputTimestamp) / 10'000'000.0) *
+ static_cast<double>(options_.fps))) + 1);
if (sampleWindowEnd < nextOutputTimestamp) {
continue;
}
@@ -2911,18 +3042,29 @@ float4 main(PSIn input) : SV_Target {
while (
frameIndex < maxFrames &&
frameIndex < expectedOutputFrames &&
- nextOutputTimestamp <= sampleWindowEnd &&
- nextOutputTimestamp < maxOutputTimestamp
+ (
+ timelineSegments_.empty()
+ ? (nextOutputTimestamp <= sampleWindowEnd &&
+ nextOutputTimestamp < maxOutputTimestamp)
+ : (frameIndex < expectedFramesForSample)
+ )
) {
+ const LONGLONG overlayTimestamp = timelineSegments_.empty()
+ ? nextOutputTimestamp
+ : static_cast<LONGLONG>(std::llround(
+ outputToSourceMs(
+ timelineSegments_,
+ static_cast<double>(nextOutputTimestamp) / 10'000.0) *
+ 10'000.0));
const Timer processTimer;
if (options_.shaderComposite) {
if (!convertSourceTextureToBgra(uploadedSourceTexture, 0, frameIndex)) {
return false;
}
- if (!readWebcamFrameForTimestamp(nextOutputTimestamp, frameIndex)) {
+ if (!readWebcamFrameForTimestamp(overlayTimestamp, frameIndex)) {
return false;
}
- if (!renderShaderComposite(nextOutputTimestamp)) {
+ if (!renderShaderComposite(overlayTimestamp)) {
return false;
}
if (!convertBgraToNv12(frameIndex)) {
@@ -2959,18 +3101,29 @@ float4 main(PSIn input) : SV_Target {
while (
frameIndex < maxFrames &&
frameIndex < expectedOutputFrames &&
- nextOutputTimestamp <= sampleWindowEnd &&
- nextOutputTimestamp < maxOutputTimestamp
+ (
+ timelineSegments_.empty()
+ ? (nextOutputTimestamp <= sampleWindowEnd &&
+ nextOutputTimestamp < maxOutputTimestamp)
+ : (frameIndex < expectedFramesForSample)
+ )
) {
+ const LONGLONG overlayTimestamp = timelineSegments_.empty()
+ ? nextOutputTimestamp
+ : static_cast<LONGLONG>(std::llround(
+ outputToSourceMs(
+ timelineSegments_,
+ static_cast<double>(nextOutputTimestamp) / 10'000.0) *
+ 10'000.0));
const Timer processTimer;
if (options_.shaderComposite) {
if (!convertSourceTextureToBgra(sourceTexture.Get(), subresourceIndex, frameIndex)) {
return false;
}
- if (!readWebcamFrameForTimestamp(nextOutputTimestamp, frameIndex)) {
+ if (!readWebcamFrameForTimestamp(overlayTimestamp, frameIndex)) {
return false;
}
- if (!renderShaderComposite(nextOutputTimestamp)) {
+ if (!renderShaderComposite(overlayTimestamp)) {
return false;
}
if (!convertBgraToNv12(frameIndex)) {
@@ -3020,6 +3173,8 @@ float4 main(PSIn input) : SV_Target {
<< "\"cursorOverlay\":" << (hasCursorOverlay() ? "true" : "false") << ","
<< "\"cursorAtlas\":" << (hasCursorAtlas_ ? "true" : "false") << ","
<< "\"zoomOverlay\":" << (hasZoomOverlay() ? "true" : "false") << ","
+ << "\"timelineMap\":" << (!timelineSegments_.empty() ? "true" : "false") << ","
+ << "\"timelineSegments\":" << timelineSegments_.size() << ","
<< "\"gpuDecodeSurface\":" << (sawDxgiSurface ? "true" : "false") << ","
<< "\"sourceWidth\":" << sourceWidth_ << ","
<< "\"sourceHeight\":" << sourceHeight_ << ","
@@ -3126,6 +3281,7 @@ float4 main(PSIn input) : SV_Target {
ComPtr<ID3D11Texture2D> webcamUploadBgraTexture_;
std::vector<BYTE> uploadScratch_;
std::vector<BYTE> uploadPaddedScratch_;
+ std::vector<TimelineSegment> timelineSegments_;
std::vector<ComPtr<ID3D11Texture2D>> nv12Textures_;
std::vector<ComPtr<ID3D11VideoProcessorOutputView>> nv12OutputViews_;
std::vector<ComPtr<ID3D11VideoProcessorOutputView>> bgraNv12OutputViews_;
diff --git a/electron/native/nvidia-cuda-compositor/run-mp4-pipeline.mjs b/electron/native/nvidia-cuda-compositor/run-mp4-pipeline.mjs
index 88f7bd4f..af9ced72 100644
--- a/electron/native/nvidia-cuda-compositor/run-mp4-pipeline.mjs
+++ b/electron/native/nvidia-cuda-compositor/run-mp4-pipeline.mjs
@@ -8,592 +8,924 @@ import { fileURLToPath } from "node:url";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const repoRoot = resolve(scriptDir, "..", "..", "..");
+const unpackAsarPath = (candidate) => candidate.replace(/\.asar([/\\])/, ".asar.unpacked$1");
+
+function resolvePackageBinary(moduleName) {
+ try {
+ const moduleExports = require(moduleName);
+ const candidate =
+ typeof moduleExports === "string"
+ ? moduleExports
+ : typeof moduleExports?.path === "string"
+ ? moduleExports.path
+ : typeof moduleExports?.default === "string"
+ ? moduleExports.default
+ : typeof moduleExports?.default?.path === "string"
+ ? moduleExports.default.path
+ : "";
+ if (!candidate) {
+ return "";
+ }
+
+ const unpacked = unpackAsarPath(candidate);
+ return existsSync(unpacked) ? unpacked : candidate;
+ } catch {
+ return "";
+ }
+}
+
+function resolveToolCommand(envNames, moduleName, fallbackName) {
+ for (const envName of envNames) {
+ const configured = process.env[envName];
+ if (configured) {
+ return configured;
+ }
+ }
+
+ return resolvePackageBinary(moduleName) || fallbackName;
+}
+
+const ffmpegCommand = resolveToolCommand(["RECORDLY_FFMPEG_EXE"], "ffmpeg-static", "ffmpeg");
+const ffprobeCommand = resolveToolCommand(["RECORDLY_FFPROBE_EXE"], "ffprobe-static", "ffprobe");
const cursorTypes = [
- "arrow",
- "text",
- "pointer",
- "crosshair",
- "open-hand",
- "closed-hand",
- "resize-ew",
- "resize-ns",
- "not-allowed",
+ "arrow",
+ "text",
+ "pointer",
+ "crosshair",
+ "open-hand",
+ "closed-hand",
+ "resize-ew",
+ "resize-ns",
+ "not-allowed",
];
const cursorTypeIndexes = new Map(cursorTypes.map((type, index) => [type, index]));
function fail(message) {
- throw new Error(message);
+ throw new Error(message);
}
function getArg(name, fallback = "") {
- const index = process.argv.indexOf(name);
- if (index === -1) {
- return fallback;
- }
- if (index + 1 >= process.argv.length) {
- fail(`Missing value for ${name}`);
- }
- return process.argv[index + 1];
+ const index = process.argv.indexOf(name);
+ if (index === -1) {
+ return fallback;
+ }
+ if (index + 1 >= process.argv.length) {
+ fail(`Missing value for ${name}`);
+ }
+ return process.argv[index + 1];
}
function getNumberArg(name, fallback) {
- const value = getArg(name, "");
- if (!value) {
- return fallback;
- }
- const parsed = Number(value);
- if (!Number.isFinite(parsed) || parsed <= 0) {
- fail(`Invalid ${name}: ${value}`);
- }
- return parsed;
+ const value = getArg(name, "");
+ if (!value) {
+ return fallback;
+ }
+ const parsed = Number(value);
+ if (!Number.isFinite(parsed) || parsed <= 0) {
+ fail(`Invalid ${name}: ${value}`);
+ }
+ return parsed;
}
function getNonNegativeNumberArg(name, fallback) {
- const value = getArg(name, "");
- if (!value) {
- return fallback;
- }
- const parsed = Number(value);
- if (!Number.isFinite(parsed) || parsed < 0) {
- fail(`Invalid ${name}: ${value}`);
- }
- return parsed;
+ const value = getArg(name, "");
+ if (!value) {
+ return fallback;
+ }
+ const parsed = Number(value);
+ if (!Number.isFinite(parsed) || parsed < 0) {
+ fail(`Invalid ${name}: ${value}`);
+ }
+ return parsed;
+}
+
+function getFiniteNumberArg(name, fallback) {
+ const value = getArg(name, "");
+ if (!value) {
+ return fallback;
+ }
+ const parsed = Number(value);
+ if (!Number.isFinite(parsed)) {
+ fail(`Invalid ${name}: ${value}`);
+ }
+ return parsed;
}
function hasArg(name) {
- return process.argv.includes(name);
+ return process.argv.includes(name);
}
function shouldRaiseChildPriority() {
- return process.env.RECORDLY_NVIDIA_CUDA_EXPORT_HIGH_PRIORITY !== "0";
+ return process.env.RECORDLY_NVIDIA_CUDA_EXPORT_HIGH_PRIORITY !== "0";
}
function raiseChildPriority(child, label) {
- if (!shouldRaiseChildPriority() || !child.pid) {
- return false;
- }
-
- try {
- os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
- return true;
- } catch (error) {
- console.warn(`[nvidia-cuda-export] Failed to raise ${label} priority: ${error}`);
- return false;
- }
+ if (!shouldRaiseChildPriority() || !child.pid) {
+ return false;
+ }
+
+ try {
+ os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
+ return true;
+ } catch (error) {
+ console.warn(`[nvidia-cuda-export] Failed to raise ${label} priority: ${error}`);
+ return false;
+ }
}
function run(command, args, options = {}) {
- const startedAt = performance.now();
- const result = spawnSync(command, args, {
- encoding: "utf8",
- maxBuffer: 64 * 1024 * 1024,
- windowsHide: true,
- ...options,
- });
- const elapsedMs = performance.now() - startedAt;
- if (result.error) {
- fail(`${command} failed to start: ${result.error.message}`);
- }
- if (result.status !== 0) {
- const stderr = result.stderr?.trim();
- const stdout = result.stdout?.trim();
- fail(
- `${command} exited with ${result.status}` +
- (stderr ? `\nSTDERR:\n${stderr}` : "") +
- (stdout ? `\nSTDOUT:\n${stdout}` : ""),
- );
- }
- return {
- elapsedMs,
- stdout: result.stdout ?? "",
- stderr: result.stderr ?? "",
- };
+ const startedAt = performance.now();
+ const result = spawnSync(command, args, {
+ encoding: "utf8",
+ maxBuffer: 64 * 1024 * 1024,
+ windowsHide: true,
+ ...options,
+ });
+ const elapsedMs = performance.now() - startedAt;
+ if (result.error) {
+ fail(`${command} failed to start: ${result.error.message}`);
+ }
+ if (result.status !== 0) {
+ const stderr = result.stderr?.trim();
+ const stdout = result.stdout?.trim();
+ fail(
+ `${command} exited with ${result.status}` +
+ (stderr ? `\nSTDERR:\n${stderr}` : "") +
+ (stdout ? `\nSTDOUT:\n${stdout}` : ""),
+ );
+ }
+ return {
+ elapsedMs,
+ stdout: result.stdout ?? "",
+ stderr: result.stderr ?? "",
+ };
+}
+
+function runAsync(command, args, options = {}) {
+ const startedAt = performance.now();
+
+ return new Promise((resolvePromise, rejectPromise) => {
+ const child = spawn(command, args, {
+ windowsHide: true,
+ stdio: ["ignore", "pipe", "pipe"],
+ ...options,
+ });
+ let stdout = "";
+ let stderr = "";
+
+ child.stdout.on("data", (chunk) => {
+ stdout += chunk.toString();
+ });
+ child.stderr.on("data", (chunk) => {
+ stderr += chunk.toString();
+ });
+ child.once("error", (error) => {
+ rejectPromise(new Error(`${command} failed to start: ${error.message}`));
+ });
+ child.once("close", (status, signal) => {
+ const elapsedMs = performance.now() - startedAt;
+ if (status !== 0) {
+ const suffix = signal ? ` (signal ${signal})` : "";
+ rejectPromise(
+ new Error(
+ `${command} exited with ${status}${suffix}` +
+ (stderr.trim() ? `\nSTDERR:\n${stderr.trim()}` : "") +
+ (stdout.trim() ? `\nSTDOUT:\n${stdout.trim()}` : ""),
+ ),
+ );
+ return;
+ }
+
+ resolvePromise({ elapsedMs, stdout, stderr });
+ });
+ });
}
-function emitPreparationProgress(totalFrames, percentage) {
- const payload = {
- currentFrame: 0,
- totalFrames: Math.max(1, Math.floor(totalFrames)),
- percentage: Number(Math.min(99, Math.max(0, percentage)).toFixed(2)),
- };
- process.stderr.write(`PROGRESS ${JSON.stringify(payload)}\n`);
+function parseProgressTimeSeconds(text) {
+ const matches = [...text.matchAll(/\btime=(\d+):(\d+):(\d+(?:\.\d+)?)/g)];
+ const latest = matches.at(-1);
+ if (!latest) {
+ return null;
+ }
+
+ const hours = Number(latest[1]);
+ const minutes = Number(latest[2]);
+ const seconds = Number(latest[3]);
+ if (![hours, minutes, seconds].every(Number.isFinite)) {
+ return null;
+ }
+
+ return hours * 3600 + minutes * 60 + seconds;
+}
+
+function runWithProgress(command, args, progressOptions, options = {}) {
+ const startedAt = performance.now();
+ const { totalDurationSec, totalFrames, startPercentage, endPercentage, stage } =
+ progressOptions;
+
+ return new Promise((resolvePromise, rejectPromise) => {
+ const child = spawn(command, args, {
+ windowsHide: true,
+ ...options,
+ });
+ let stdout = "";
+ let stderr = "";
+ let lastEmittedPercentage = startPercentage;
+
+ const maybeEmitProgress = (text) => {
+ if (!Number.isFinite(totalDurationSec) || totalDurationSec <= 0) {
+ return;
+ }
+ const elapsedSec = parseProgressTimeSeconds(text);
+ if (elapsedSec === null) {
+ return;
+ }
+ const ratio = Math.max(0, Math.min(1, elapsedSec / totalDurationSec));
+ const percentage = startPercentage + (endPercentage - startPercentage) * ratio;
+ if (percentage >= lastEmittedPercentage + 0.25 || percentage >= endPercentage) {
+ lastEmittedPercentage = Math.min(endPercentage, percentage);
+ emitPreparationProgress(totalFrames, lastEmittedPercentage, stage);
+ }
+ };
+
+ child.stdout.on("data", (chunk) => {
+ stdout += chunk.toString();
+ });
+ child.stderr.on("data", (chunk) => {
+ const text = chunk.toString();
+ stderr += text;
+ maybeEmitProgress(text);
+ });
+ child.once("error", (error) => {
+ rejectPromise(new Error(`${command} failed to start: ${error.message}`));
+ });
+ child.once("close", (status, signal) => {
+ const elapsedMs = performance.now() - startedAt;
+ if (status !== 0) {
+ const suffix = signal ? ` (signal ${signal})` : "";
+ rejectPromise(
+ new Error(
+ `${command} exited with ${status}${suffix}` +
+ (stderr.trim() ? `\nSTDERR:\n${stderr.trim()}` : "") +
+ (stdout.trim() ? `\nSTDOUT:\n${stdout.trim()}` : ""),
+ ),
+ );
+ return;
+ }
+
+ emitPreparationProgress(totalFrames, endPercentage, stage);
+ resolvePromise({ elapsedMs, stdout, stderr });
+ });
+ });
+}
+
+function emitPreparationProgress(totalFrames, percentage, stage) {
+ const progressStage = stage ?? "preparing";
+ const finalizing = progressStage === "finalizing";
+ const payload = {
+ currentFrame: finalizing ? Math.max(1, Math.floor(totalFrames)) : 0,
+ totalFrames: Math.max(1, Math.floor(totalFrames)),
+ percentage: Number(Math.min(99, Math.max(0, percentage)).toFixed(2)),
+ stage: progressStage,
+ };
+ process.stderr.write(`PROGRESS ${JSON.stringify(payload)}\n`);
}
function parseFfmpegStatsFrameCount(stderr) {
- const matches = [...String(stderr ?? "").matchAll(/frame=\s*(\d+)/g)];
- if (!matches.length) {
- return 0;
- }
- const frameCount = Number(matches[matches.length - 1][1]);
- return Number.isFinite(frameCount) && frameCount > 0 ? Math.floor(frameCount) : 0;
+ const matches = [...String(stderr ?? "").matchAll(/frame=\s*(\d+)/g)];
+ if (!matches.length) {
+ return 0;
+ }
+ const frameCount = Number(matches[matches.length - 1][1]);
+ return Number.isFinite(frameCount) && frameCount > 0 ? Math.floor(frameCount) : 0;
+}
+
+const gpuSampleFields = [
+ "timestamp",
+ "temperature.gpu",
+ "power.draw",
+ "pstate",
+ "utilization.gpu",
+ "utilization.decoder",
+ "utilization.encoder",
+ "clocks.sm",
+ "clocks.mem",
+];
+
+const gpuThrottleReasonFields = [
+ { key: "gpuIdle", query: "clocks_throttle_reasons.gpu_idle" },
+ {
+ key: "applicationsClocksSetting",
+ query: "clocks_throttle_reasons.applications_clocks_setting",
+ },
+ { key: "swPowerCap", query: "clocks_throttle_reasons.sw_power_cap" },
+ { key: "hwSlowdown", query: "clocks_throttle_reasons.hw_slowdown" },
+ { key: "hwThermalSlowdown", query: "clocks_throttle_reasons.hw_thermal_slowdown" },
+ {
+ key: "hwPowerBrakeSlowdown",
+ query: "clocks_throttle_reasons.hw_power_brake_slowdown",
+ },
+ { key: "swThermalSlowdown", query: "clocks_throttle_reasons.sw_thermal_slowdown" },
+ { key: "syncBoost", query: "clocks_throttle_reasons.sync_boost" },
+];
+
+function runNvidiaSmiGpuQuery(fields) {
+ const result = spawnSync(
+ "nvidia-smi",
+ [`--query-gpu=${fields.join(",")}`, "--format=csv,noheader,nounits"],
+ { encoding: "utf8", windowsHide: true },
+ );
+ if (result.status !== 0 || !result.stdout.trim()) {
+ return null;
+ }
+ return result.stdout
+ .trim()
+ .split(",")
+ .map((part) => part.trim());
+}
+
+function parseNvidiaSmiNumber(value) {
+ const parsed = Number(value);
+ return Number.isFinite(parsed) ? parsed : null;
+}
+
+function parseNvidiaSmiActiveFlag(value) {
+ if (/^active$/i.test(value)) {
+ return true;
+ }
+ if (/^not active$/i.test(value)) {
+ return false;
+ }
+ return null;
}
function sampleGpu() {
- const result = spawnSync(
- "nvidia-smi",
- [
- "--query-gpu=timestamp,temperature.gpu,power.draw,pstate,utilization.gpu,utilization.decoder,utilization.encoder,clocks.sm,clocks.mem",
- "--format=csv,noheader,nounits",
- ],
- { encoding: "utf8", windowsHide: true },
- );
- if (result.status !== 0 || !result.stdout.trim()) {
- return null;
- }
- const parts = result.stdout.trim().split(",").map((part) => part.trim());
- return {
- timestamp: parts[0],
- temperatureC: Number(parts[1]),
- powerW: Number(parts[2]),
- pstate: parts[3],
- gpuUtilizationPct: Number(parts[4]),
- decoderUtilizationPct: Number(parts[5]),
- encoderUtilizationPct: Number(parts[6]),
- smClockMhz: Number(parts[7]),
- memoryClockMhz: Number(parts[8]),
- };
+ const throttleFields = [
+ "clocks_throttle_reasons.active",
+ ...gpuThrottleReasonFields.map((field) => field.query),
+ ];
+ const parts =
+ runNvidiaSmiGpuQuery([...gpuSampleFields, ...throttleFields]) ??
+ runNvidiaSmiGpuQuery(gpuSampleFields);
+ if (!parts) {
+ return null;
+ }
+
+ const sample = {
+ timestamp: parts[0],
+ temperatureC: parseNvidiaSmiNumber(parts[1]),
+ powerW: parseNvidiaSmiNumber(parts[2]),
+ pstate: parts[3],
+ gpuUtilizationPct: parseNvidiaSmiNumber(parts[4]),
+ decoderUtilizationPct: parseNvidiaSmiNumber(parts[5]),
+ encoderUtilizationPct: parseNvidiaSmiNumber(parts[6]),
+ smClockMhz: parseNvidiaSmiNumber(parts[7]),
+ memoryClockMhz: parseNvidiaSmiNumber(parts[8]),
+ };
+ if (parts.length >= gpuSampleFields.length + throttleFields.length) {
+ const throttleStart = gpuSampleFields.length;
+ const clockThrottleReasons = {};
+ for (const [index, field] of gpuThrottleReasonFields.entries()) {
+ const active = parseNvidiaSmiActiveFlag(parts[throttleStart + 1 + index]);
+ if (active !== null) {
+ clockThrottleReasons[field.key] = active;
+ }
+ }
+ sample.clockThrottleReasonsActiveMask = parts[throttleStart];
+ sample.clockThrottleReasons = clockThrottleReasons;
+ sample.activeClockThrottleReasons = Object.entries(clockThrottleReasons)
+ .filter(([, active]) => active)
+ .map(([key]) => key);
+ }
+ return sample;
}
function summarizeGpuSamples(samples) {
- if (!samples.length) {
- return null;
- }
- const numeric = [
- "temperatureC",
- "powerW",
- "gpuUtilizationPct",
- "decoderUtilizationPct",
- "encoderUtilizationPct",
- "smClockMhz",
- "memoryClockMhz",
- ];
- const summary = {
- samples: samples.length,
- pstateValues: [...new Set(samples.map((sample) => sample.pstate))],
- };
- for (const key of numeric) {
- const values = samples.map((sample) => sample[key]).filter(Number.isFinite);
- if (!values.length) {
- continue;
- }
- summary[key] = {
- min: Math.min(...values),
- max: Math.max(...values),
- avg: Number((values.reduce((sum, value) => sum + value, 0) / values.length).toFixed(2)),
- };
- }
- return summary;
+ if (!samples.length) {
+ return null;
+ }
+ const numeric = [
+ "temperatureC",
+ "powerW",
+ "gpuUtilizationPct",
+ "decoderUtilizationPct",
+ "encoderUtilizationPct",
+ "smClockMhz",
+ "memoryClockMhz",
+ ];
+ const summary = {
+ samples: samples.length,
+ pstateValues: [...new Set(samples.map((sample) => sample.pstate))],
+ };
+ for (const key of numeric) {
+ const values = samples.map((sample) => sample[key]).filter(Number.isFinite);
+ if (!values.length) {
+ continue;
+ }
+ summary[key] = {
+ min: Math.min(...values),
+ max: Math.max(...values),
+ avg: Number((values.reduce((sum, value) => sum + value, 0) / values.length).toFixed(2)),
+ };
+ }
+ const activeMasks = [
+ ...new Set(
+ samples
+ .map((sample) => sample.clockThrottleReasonsActiveMask)
+ .filter((mask) => typeof mask === "string" && mask.length > 0),
+ ),
+ ];
+ if (activeMasks.length) {
+ summary.clockThrottleReasonsActiveMasks = activeMasks;
+ }
+ const throttleReasonCounts = {};
+ for (const sample of samples) {
+ for (const reason of sample.activeClockThrottleReasons ?? []) {
+ throttleReasonCounts[reason] = (throttleReasonCounts[reason] ?? 0) + 1;
+ }
+ }
+ if (Object.keys(throttleReasonCounts).length) {
+ summary.clockThrottleReasonCounts = throttleReasonCounts;
+ }
+ return summary;
}
function cursorBounceScale(interactionType, ageMs, durationMs = 180) {
- if (!["click", "double-click", "right-click", "middle-click"].includes(interactionType)) {
- return 1;
- }
- if (ageMs < 0 || ageMs > durationMs) {
- return 1;
- }
- const progress = 1 - ageMs / durationMs;
- return Math.max(0.72, 1 - Math.sin(progress * Math.PI) * 0.08);
+ if (!["click", "double-click", "right-click", "middle-click"].includes(interactionType)) {
+ return 1;
+ }
+ if (ageMs < 0 || ageMs > durationMs) {
+ return 1;
+ }
+ const progress = 1 - ageMs / durationMs;
+ return Math.max(0.72, 1 - Math.sin(progress * Math.PI) * 0.08);
}
function latestClickSample(samples, sampleIndex) {
- for (let index = sampleIndex; index >= 0; index -= 1) {
- const sample = samples[index];
- if (["click", "double-click", "right-click", "middle-click"].includes(sample?.interactionType)) {
- return sample;
- }
- }
- return null;
+ for (let index = sampleIndex; index >= 0; index -= 1) {
+ const sample = samples[index];
+ if (
+ ["click", "double-click", "right-click", "middle-click"].includes(
+ sample?.interactionType,
+ )
+ ) {
+ return sample;
+ }
+ }
+ return null;
}
function writeCursorSamples(cursorPayload, outputPath) {
- const samples = Array.isArray(cursorPayload.samples) ? cursorPayload.samples : [];
- const cursorLines = samples
- .map((sample, index) => {
- if (
- !Number.isFinite(sample?.timeMs) ||
- !Number.isFinite(sample?.cx) ||
- !Number.isFinite(sample?.cy)
- ) {
- return null;
- }
- const clickSample = latestClickSample(samples, index);
- const bounceScale = Number.isFinite(sample.bounceScale)
- ? sample.bounceScale
- : clickSample
- ? cursorBounceScale(clickSample.interactionType, sample.timeMs - clickSample.timeMs)
- : 1;
- return [
- sample.timeMs,
- sample.cx,
- sample.cy,
- cursorTypeIndexes.get(sample.cursorType) ??
- (Number.isFinite(sample.cursorTypeIndex) ? Math.max(0, Math.min(8, Math.round(sample.cursorTypeIndex))) : 0),
- Number(bounceScale.toFixed(4)),
- ].join("\t");
- })
- .filter(Boolean)
- .join("\n");
- writeFileSync(outputPath, cursorLines ? `${cursorLines}\n` : "");
- return samples.length;
+ const samples = Array.isArray(cursorPayload.samples) ? cursorPayload.samples : [];
+ const cursorLines = samples
+ .map((sample, index) => {
+ if (
+ !Number.isFinite(sample?.timeMs) ||
+ !Number.isFinite(sample?.cx) ||
+ !Number.isFinite(sample?.cy)
+ ) {
+ return null;
+ }
+ const clickSample = latestClickSample(samples, index);
+ const bounceScale = Number.isFinite(sample.bounceScale)
+ ? sample.bounceScale
+ : clickSample
+ ? cursorBounceScale(
+ clickSample.interactionType,
+ sample.timeMs - clickSample.timeMs,
+ )
+ : 1;
+ return [
+ sample.timeMs,
+ sample.cx,
+ sample.cy,
+ cursorTypeIndexes.get(sample.cursorType) ??
+ (Number.isFinite(sample.cursorTypeIndex)
+ ? Math.max(0, Math.min(8, Math.round(sample.cursorTypeIndex)))
+ : 0),
+ Number(bounceScale.toFixed(4)),
+ ].join("\t");
+ })
+ .filter(Boolean)
+ .join("\n");
+ writeFileSync(outputPath, cursorLines ? `${cursorLines}\n` : "");
+ return samples.length;
}
function renderTahoeCursorAtlas(workDir) {
- const rgbaPath = join(workDir, "tahoe-cursor-atlas.rgba");
- const metadataPath = join(workDir, "tahoe-cursor-atlas.tsv");
- const electronPath = require("electron");
- const render = run(electronPath, [
- join(scriptDir, "render-tahoe-cursor-atlas.cjs"),
- "--repo-root",
- repoRoot,
- "--output-rgba",
- rgbaPath,
- "--output-metadata",
- metadataPath,
- ], {
- env: {
- ...process.env,
- ELECTRON_DISABLE_SECURITY_WARNINGS: "1",
- },
- });
- const resultLine = render.stdout.trim().split(/\r?\n/).filter(Boolean).pop();
- if (!resultLine) {
- fail("Cursor atlas renderer did not report a result");
- }
- const result = JSON.parse(resultLine);
- if (result.error) {
- fail(`Cursor atlas renderer failed: ${result.error}`);
- }
- return {
- rgbaPath,
- metadataPath,
- width: result.width,
- height: result.height,
- entries: result.entries,
- elapsedMs: render.elapsedMs,
- };
+ const rgbaPath = join(workDir, "tahoe-cursor-atlas.rgba");
+ const metadataPath = join(workDir, "tahoe-cursor-atlas.tsv");
+ const electronPath = require("electron");
+ const render = run(
+ electronPath,
+ [
+ join(scriptDir, "render-tahoe-cursor-atlas.cjs"),
+ "--repo-root",
+ repoRoot,
+ "--output-rgba",
+ rgbaPath,
+ "--output-metadata",
+ metadataPath,
+ ],
+ {
+ env: {
+ ...process.env,
+ ELECTRON_DISABLE_SECURITY_WARNINGS: "1",
+ },
+ },
+ );
+ const resultLine = render.stdout.trim().split(/\r?\n/).filter(Boolean).pop();
+ if (!resultLine) {
+ fail("Cursor atlas renderer did not report a result");
+ }
+ const result = JSON.parse(resultLine);
+ if (result.error) {
+ fail(`Cursor atlas renderer failed: ${result.error}`);
+ }
+ return {
+ rgbaPath,
+ metadataPath,
+ width: result.width,
+ height: result.height,
+ entries: result.entries,
+ elapsedMs: render.elapsedMs,
+ };
}
function prepareExternalCursorAtlas(workDir, pngPath, metadataPath) {
- const startedAt = performance.now();
- const resolvedPngPath = resolve(pngPath);
- const resolvedMetadataPath = resolve(metadataPath);
- if (!existsSync(resolvedPngPath)) {
- fail(`Cursor atlas PNG does not exist: ${resolvedPngPath}`);
- }
- if (!existsSync(resolvedMetadataPath)) {
- fail(`Cursor atlas metadata does not exist: ${resolvedMetadataPath}`);
- }
-
- const json = ffprobeJson([
- "-select_streams",
- "v:0",
- "-show_entries",
- "stream=width,height",
- resolvedPngPath,
- ]);
- const stream = json.streams?.[0];
- const width = Number(stream?.width);
- const height = Number(stream?.height);
- if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
- fail(`Invalid cursor atlas dimensions: ${resolvedPngPath}`);
- }
-
- const rgbaPath = join(workDir, "external-cursor-atlas.rgba");
- run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-i",
- resolvedPngPath,
- "-f",
- "rawvideo",
- "-pix_fmt",
- "rgba",
- rgbaPath,
- ]);
- const entries = readFileSync(resolvedMetadataPath, "utf8")
- .split(/\r?\n/)
- .map((line) => line.trim())
- .filter(Boolean).length;
- if (entries === 0) {
- fail(`Cursor atlas metadata is empty: ${resolvedMetadataPath}`);
- }
-
- return {
- rgbaPath,
- metadataPath: resolvedMetadataPath,
- width,
- height,
- entries,
- elapsedMs: performance.now() - startedAt,
- };
+ const startedAt = performance.now();
+ const resolvedPngPath = resolve(pngPath);
+ const resolvedMetadataPath = resolve(metadataPath);
+ if (!existsSync(resolvedPngPath)) {
+ fail(`Cursor atlas PNG does not exist: ${resolvedPngPath}`);
+ }
+ if (!existsSync(resolvedMetadataPath)) {
+ fail(`Cursor atlas metadata does not exist: ${resolvedMetadataPath}`);
+ }
+
+ const json = ffprobeJson([
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "stream=width,height",
+ resolvedPngPath,
+ ]);
+ const stream = json.streams?.[0];
+ const width = Number(stream?.width);
+ const height = Number(stream?.height);
+ if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
+ fail(`Invalid cursor atlas dimensions: ${resolvedPngPath}`);
+ }
+
+ const rgbaPath = join(workDir, "external-cursor-atlas.rgba");
+ run(ffmpegCommand, [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-i",
+ resolvedPngPath,
+ "-f",
+ "rawvideo",
+ "-pix_fmt",
+ "rgba",
+ rgbaPath,
+ ]);
+ const entries = readFileSync(resolvedMetadataPath, "utf8")
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .filter(Boolean).length;
+ if (entries === 0) {
+ fail(`Cursor atlas metadata is empty: ${resolvedMetadataPath}`);
+ }
+
+ return {
+ rgbaPath,
+ metadataPath: resolvedMetadataPath,
+ width,
+ height,
+ entries,
+ elapsedMs: performance.now() - startedAt,
+ };
}
async function runWithGpuMonitor(command, args, sampleIntervalMs) {
- const samples = [];
- const startedAt = performance.now();
- const shouldSampleGpu = Number.isFinite(sampleIntervalMs) && sampleIntervalMs > 0;
- let stdout = "";
- let stderr = "";
-
- const child = spawn(command, args, {
- windowsHide: true,
- stdio: ["ignore", "pipe", "pipe"],
- });
- const priorityBoosted = raiseChildPriority(child, "native CUDA encoder");
-
- const collectSample = () => {
- if (!shouldSampleGpu) {
- return;
- }
- const sample = sampleGpu();
- if (sample) {
- samples.push({
- elapsedMs: Number((performance.now() - startedAt).toFixed(2)),
- ...sample,
- });
- }
- };
- collectSample();
- const interval = shouldSampleGpu ? setInterval(collectSample, sampleIntervalMs) : null;
-
- child.stdout.on("data", (chunk) => {
- stdout += chunk.toString();
- });
- child.stderr.on("data", (chunk) => {
- const text = chunk.toString();
- stderr += text;
- process.stderr.write(text);
- });
-
- const status = await new Promise((resolve, reject) => {
- child.on("error", reject);
- child.on("close", resolve);
- });
- if (interval) {
- clearInterval(interval);
- }
- collectSample();
-
- const elapsedMs = performance.now() - startedAt;
- if (status !== 0) {
- fail(
- `${command} exited with ${status}` +
- (stderr.trim() ? `\nSTDERR:\n${stderr.trim()}` : "") +
- (stdout.trim() ? `\nSTDOUT:\n${stdout.trim()}` : ""),
- );
- }
- return {
- elapsedMs,
- stdout,
- stderr,
- gpuSamples: samples,
- gpuSummary: summarizeGpuSamples(samples),
- priorityBoosted,
- };
+ const samples = [];
+ const startedAt = performance.now();
+ const shouldSampleGpu = Number.isFinite(sampleIntervalMs) && sampleIntervalMs > 0;
+ let stdout = "";
+ let stderr = "";
+
+ const child = spawn(command, args, {
+ windowsHide: true,
+ stdio: ["ignore", "pipe", "pipe"],
+ });
+ const priorityBoosted = raiseChildPriority(child, "native CUDA encoder");
+
+ const collectSample = () => {
+ if (!shouldSampleGpu) {
+ return;
+ }
+ const sample = sampleGpu();
+ if (sample) {
+ samples.push({
+ elapsedMs: Number((performance.now() - startedAt).toFixed(2)),
+ ...sample,
+ });
+ }
+ };
+ collectSample();
+ const interval = shouldSampleGpu ? setInterval(collectSample, sampleIntervalMs) : null;
+
+ child.stdout.on("data", (chunk) => {
+ stdout += chunk.toString();
+ });
+ child.stderr.on("data", (chunk) => {
+ const text = chunk.toString();
+ stderr += text;
+ process.stderr.write(text);
+ });
+
+ const status = await new Promise((resolve, reject) => {
+ child.on("error", reject);
+ child.on("close", resolve);
+ });
+ if (interval) {
+ clearInterval(interval);
+ }
+ collectSample();
+
+ const elapsedMs = performance.now() - startedAt;
+ if (status !== 0) {
+ fail(
+ `${command} exited with ${status}` +
+ (stderr.trim() ? `\nSTDERR:\n${stderr.trim()}` : "") +
+ (stdout.trim() ? `\nSTDOUT:\n${stdout.trim()}` : ""),
+ );
+ }
+ return {
+ elapsedMs,
+ stdout,
+ stderr,
+ gpuSamples: samples,
+ gpuSummary: summarizeGpuSamples(samples),
+ priorityBoosted,
+ };
}
function ffprobeJson(args) {
- const result = run("ffprobe", ["-v", "error", ...args, "-of", "json"]);
- return JSON.parse(result.stdout);
+ const result = run(ffprobeCommand, ["-v", "error", ...args, "-of", "json"]);
+ return JSON.parse(result.stdout);
}
-function ffprobeCsv(args) {
- const result = run("ffprobe", ["-v", "error", ...args, "-of", "csv=p=0"]);
- return result.stdout;
+async function ffprobeCsvAsync(args) {
+ const result = await runAsync(ffprobeCommand, [
+ "-v",
+ "error",
+ ...args,
+ "-of",
+ "csv=p=0",
+ ]);
+ return result.stdout;
}
function getVideoInfo(inputPath) {
- const json = ffprobeJson([
- "-select_streams",
- "v:0",
- "-show_entries",
- "stream=codec_name,width,height,duration,avg_frame_rate,nb_frames",
- inputPath,
- ]);
- let stream = json.streams?.[0];
- if (!stream) {
- fail(`No video stream found in ${inputPath}`);
- }
- if (stream.codec_name !== "h264") {
- fail(`The NVIDIA CUDA compositor currently expects H.264 input, got ${stream.codec_name}`);
- }
- const durationSec = Number(stream.duration);
- if (!Number.isFinite(durationSec) || durationSec <= 0) {
- fail("ffprobe did not return a valid video duration");
- }
- let sourceFrames = Number(stream.nb_frames);
- if (!Number.isFinite(sourceFrames) || sourceFrames <= 0) {
- const countedJson = ffprobeJson([
- "-count_frames",
- "-select_streams",
- "v:0",
- "-show_entries",
- "stream=codec_name,width,height,duration,avg_frame_rate,nb_frames,nb_read_frames",
- inputPath,
- ]);
- stream = countedJson.streams?.[0] ?? stream;
- sourceFrames = Number(stream.nb_read_frames || stream.nb_frames);
- }
- if (!Number.isFinite(sourceFrames) || sourceFrames <= 0) {
- fail("ffprobe did not return a valid source frame count");
- }
- return {
- codec: stream.codec_name,
- width: Number(stream.width),
- height: Number(stream.height),
- durationSec,
- sourceFrames,
- avgFrameRate: stream.avg_frame_rate,
- };
+ const json = ffprobeJson([
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "stream=codec_name,width,height,duration,avg_frame_rate,nb_frames",
+ inputPath,
+ ]);
+ let stream = json.streams?.[0];
+ if (!stream) {
+ fail(`No video stream found in ${inputPath}`);
+ }
+ if (stream.codec_name !== "h264") {
+ fail(`The NVIDIA CUDA compositor currently expects H.264 input, got ${stream.codec_name}`);
+ }
+ const durationSec = Number(stream.duration);
+ if (!Number.isFinite(durationSec) || durationSec <= 0) {
+ fail("ffprobe did not return a valid video duration");
+ }
+ let sourceFrames = Number(stream.nb_frames);
+ if (!Number.isFinite(sourceFrames) || sourceFrames <= 0) {
+ const countedJson = ffprobeJson([
+ "-count_frames",
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "stream=codec_name,width,height,duration,avg_frame_rate,nb_frames,nb_read_frames",
+ inputPath,
+ ]);
+ stream = countedJson.streams?.[0] ?? stream;
+ sourceFrames = Number(stream.nb_read_frames || stream.nb_frames);
+ }
+ if (!Number.isFinite(sourceFrames) || sourceFrames <= 0) {
+ fail("ffprobe did not return a valid source frame count");
+ }
+ return {
+ codec: stream.codec_name,
+ width: Number(stream.width),
+ height: Number(stream.height),
+ durationSec,
+ sourceFrames,
+ avgFrameRate: stream.avg_frame_rate,
+ };
}
function normalizeMonotonicTimestamps(timestamps) {
- if (timestamps.length < 2) {
- return [];
- }
-
- const first = timestamps[0];
- const normalized = timestamps
- .map((value) => Math.max(0, value - first))
- .filter((value, index, values) => index === 0 || value >= values[index - 1]);
- return normalized.length === timestamps.length ? normalized : [];
+ if (timestamps.length < 2) {
+ return [];
+ }
+
+ const first = timestamps[0];
+ const normalized = timestamps
+ .map((value) => Math.max(0, value - first))
+ .filter((value, index, values) => index === 0 || value >= values[index - 1]);
+ return normalized.length === timestamps.length ? normalized : [];
}
function parseTimestampCsv(csv) {
- return csv
- .split(/\r?\n/)
- .map((line) => line.trim())
- .filter(Boolean)
- .map((line) => {
- const columns = line.split(",");
- const value = columns
- .map((column) => Number(column.trim()))
- .find((candidate) => Number.isFinite(candidate));
- return value ?? Number.NaN;
- })
- .filter((value) => Number.isFinite(value));
+ return csv
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .filter(Boolean)
+ .map((line) => {
+ const columns = line.split(",");
+ const value = columns
+ .map((column) => Number(column.trim()))
+ .find((candidate) => Number.isFinite(candidate));
+ return value ?? Number.NaN;
+ })
+ .filter((value) => Number.isFinite(value));
}
-function getVideoPacketPts(inputPath, durationSec) {
- const csv = ffprobeCsv([
- "-select_streams",
- "v:0",
- "-read_intervals",
- `%+${durationSec}`,
- "-show_packets",
- "-show_entries",
- "packet=pts_time,dts_time",
- inputPath,
- ]);
- return normalizeMonotonicTimestamps(parseTimestampCsv(csv));
+function readTimelineSegments(timelineMapPath) {
+ if (!timelineMapPath) {
+ return [];
+ }
+ const resolvedPath = resolve(timelineMapPath);
+ if (!existsSync(resolvedPath)) {
+ fail(`Timeline map does not exist: ${resolvedPath}`);
+ }
+
+ const segments = readFileSync(resolvedPath, "utf8")
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .filter(Boolean)
+ .map((line) => {
+ const [sourceStartMs, sourceEndMs, outputStartMs, outputEndMs, speed] = line
+ .split(",")
+ .map((value) => Number(value.trim()));
+ return { sourceStartMs, sourceEndMs, outputStartMs, outputEndMs, speed };
+ });
+
+ let outputCursorMs = 0;
+ for (const segment of segments) {
+ if (
+ !Number.isFinite(segment.sourceStartMs) ||
+ !Number.isFinite(segment.sourceEndMs) ||
+ !Number.isFinite(segment.outputStartMs) ||
+ !Number.isFinite(segment.outputEndMs) ||
+ !Number.isFinite(segment.speed) ||
+ segment.sourceEndMs <= segment.sourceStartMs ||
+ segment.outputEndMs <= segment.outputStartMs ||
+ segment.speed <= 0
+ ) {
+ fail(`Invalid timeline map segment in ${resolvedPath}`);
+ }
+ if (Math.abs(segment.outputStartMs - outputCursorMs) > 2) {
+ fail(`Timeline map output ranges must be contiguous: ${resolvedPath}`);
+ }
+ outputCursorMs = segment.outputEndMs;
+ }
+
+ return segments;
}
-function getVideoFramePts(inputPath, durationSec) {
- const csv = ffprobeCsv([
- "-select_streams",
- "v:0",
- "-read_intervals",
- `%+${durationSec}`,
- "-show_frames",
- "-show_entries",
- "frame=best_effort_timestamp_time",
- inputPath,
- ]);
- return normalizeMonotonicTimestamps(parseTimestampCsv(csv));
+async function getVideoPacketPtsAsync(inputPath, durationSec) {
+ const csv = await ffprobeCsvAsync([
+ "-select_streams",
+ "v:0",
+ "-read_intervals",
+ `%+${durationSec}`,
+ "-show_packets",
+ "-show_entries",
+ "packet=pts_time,dts_time",
+ inputPath,
+ ]);
+ return normalizeMonotonicTimestamps(parseTimestampCsv(csv));
}
-function writeFramePtsSidecar(inputPath, durationSec, outputPath) {
- const startedAt = performance.now();
- let source = "packet-pts";
- let timestamps = getVideoPacketPts(inputPath, durationSec);
- if (timestamps.length === 0) {
- source = "frame-pts";
- timestamps = getVideoFramePts(inputPath, durationSec);
- }
- const elapsedMs = performance.now() - startedAt;
- if (timestamps.length === 0) {
- return { path: null, frames: 0, elapsedMs, source: "none" };
- }
-
- writeFileSync(outputPath, timestamps.map((value) => value.toFixed(9)).join("\n"));
- return { path: outputPath, frames: timestamps.length, elapsedMs, source };
+async function getVideoFramePtsAsync(inputPath, durationSec) {
+ const csv = await ffprobeCsvAsync([
+ "-select_streams",
+ "v:0",
+ "-read_intervals",
+ `%+${durationSec}`,
+ "-show_frames",
+ "-show_entries",
+ "frame=best_effort_timestamp_time",
+ inputPath,
+ ]);
+ return normalizeMonotonicTimestamps(parseTimestampCsv(csv));
+}
+
+async function writeFramePtsSidecarAsync(inputPath, durationSec, outputPath) {
+ const startedAt = performance.now();
+ let source = "packet-pts";
+ let timestamps = await getVideoPacketPtsAsync(inputPath, durationSec);
+ if (timestamps.length === 0) {
+ source = "frame-pts";
+ timestamps = await getVideoFramePtsAsync(inputPath, durationSec);
+ }
+ const elapsedMs = performance.now() - startedAt;
+ if (timestamps.length === 0) {
+ return { path: null, frames: 0, elapsedMs, source: "none" };
+ }
+
+ writeFileSync(outputPath, timestamps.map((value) => value.toFixed(9)).join("\n"));
+ return { path: outputPath, frames: timestamps.length, elapsedMs, source };
}
function roundedRectMaskExpression({ x, y, width, height, radius }) {
- const right = x + width;
- const bottom = y + height;
- const cornerRight = right - radius;
- const cornerBottom = bottom - radius;
- const radiusSquared = radius * radius;
- const centerBand = `between(X,${x + radius},${cornerRight})*between(Y,${y},${bottom})`;
- const middleBand = `between(X,${x},${right})*between(Y,${y + radius},${cornerBottom})`;
- const topLeft = `lte((X-${x + radius})*(X-${x + radius})+(Y-${y + radius})*(Y-${y + radius}),${radiusSquared})*lte(X,${x + radius})*lte(Y,${y + radius})`;
- const topRight = `lte((X-${cornerRight})*(X-${cornerRight})+(Y-${y + radius})*(Y-${y + radius}),${radiusSquared})*gte(X,${cornerRight})*lte(Y,${y + radius})`;
- const bottomLeft = `lte((X-${x + radius})*(X-${x + radius})+(Y-${cornerBottom})*(Y-${cornerBottom}),${radiusSquared})*lte(X,${x + radius})*gte(Y,${cornerBottom})`;
- const bottomRight = `lte((X-${cornerRight})*(X-${cornerRight})+(Y-${cornerBottom})*(Y-${cornerBottom}),${radiusSquared})*gte(X,${cornerRight})*gte(Y,${cornerBottom})`;
- return `${centerBand}+${middleBand}+${topLeft}+${topRight}+${bottomLeft}+${bottomRight}`;
+ const right = x + width;
+ const bottom = y + height;
+ const cornerRight = right - radius;
+ const cornerBottom = bottom - radius;
+ const radiusSquared = radius * radius;
+ const centerBand = `between(X,${x + radius},${cornerRight})*between(Y,${y},${bottom})`;
+ const middleBand = `between(X,${x},${right})*between(Y,${y + radius},${cornerBottom})`;
+ const topLeft = `lte((X-${x + radius})*(X-${x + radius})+(Y-${y + radius})*(Y-${y + radius}),${radiusSquared})*lte(X,${x + radius})*lte(Y,${y + radius})`;
+ const topRight = `lte((X-${cornerRight})*(X-${cornerRight})+(Y-${y + radius})*(Y-${y + radius}),${radiusSquared})*gte(X,${cornerRight})*lte(Y,${y + radius})`;
+ const bottomLeft = `lte((X-${x + radius})*(X-${x + radius})+(Y-${cornerBottom})*(Y-${cornerBottom}),${radiusSquared})*lte(X,${x + radius})*gte(Y,${cornerBottom})`;
+ const bottomRight = `lte((X-${cornerRight})*(X-${cornerRight})+(Y-${cornerBottom})*(Y-${cornerBottom}),${radiusSquared})*gte(X,${cornerRight})*gte(Y,${cornerBottom})`;
+ return `${centerBand}+${middleBand}+${topLeft}+${topRight}+${bottomLeft}+${bottomRight}`;
}
function createBackgroundFilter(videoInfo, shadowOptions) {
- const base = `[0:v]scale=${videoInfo.width}:${videoInfo.height}:force_original_aspect_ratio=increase,crop=${videoInfo.width}:${videoInfo.height},format=rgba[bg]`;
- if (!shadowOptions) {
- return {
- filterArgs: [
- "-vf",
- `scale=${videoInfo.width}:${videoInfo.height}:force_original_aspect_ratio=increase,crop=${videoInfo.width}:${videoInfo.height},format=nv12`,
- ],
- bakedShadow: false,
- };
- }
-
- const shadowAlpha = Math.round(255 * Math.min(0.5, shadowOptions.intensityPct / 200));
- const shadowBlur = Math.max(12, Math.round(shadowOptions.radius * 1.5));
- const mask = roundedRectMaskExpression({
- x: shadowOptions.x,
- y: shadowOptions.y,
- width: shadowOptions.width,
- height: shadowOptions.height,
- radius: shadowOptions.radius,
- });
- const shadow = `[1:v]format=rgba,geq=r='0':g='0':b='0':a='if(${mask},${shadowAlpha},0)',boxblur=luma_radius=${shadowBlur}:luma_power=1:chroma_radius=${shadowBlur}:chroma_power=1:alpha_radius=${shadowBlur}:alpha_power=1[shadow]`;
- return {
- filterArgs: [
- "-f",
- "lavfi",
- "-i",
- `[email protected]:s=${videoInfo.width}x${videoInfo.height}:d=1`,
- "-filter_complex",
- `${base};${shadow};[bg][shadow]overlay=format=auto,format=nv12[out]`,
- "-map",
- "[out]",
- ],
- bakedShadow: true,
- shadowAlpha,
- shadowBlur,
- };
+ const base = `[0:v]scale=${videoInfo.width}:${videoInfo.height}:force_original_aspect_ratio=increase,crop=${videoInfo.width}:${videoInfo.height},format=rgba[bg]`;
+ if (!shadowOptions) {
+ return {
+ filterArgs: [
+ "-vf",
+ `scale=${videoInfo.width}:${videoInfo.height}:force_original_aspect_ratio=increase,crop=${videoInfo.width}:${videoInfo.height},format=nv12`,
+ ],
+ bakedShadow: false,
+ };
+ }
+
+ const shadowAlpha = Math.round(255 * Math.min(0.5, shadowOptions.intensityPct / 200));
+ const shadowBlur = Math.max(12, Math.round(shadowOptions.radius * 1.5));
+ const mask = roundedRectMaskExpression({
+ x: shadowOptions.x,
+ y: shadowOptions.y,
+ width: shadowOptions.width,
+ height: shadowOptions.height,
+ radius: shadowOptions.radius,
+ });
+ const shadow = `[1:v]format=rgba,geq=r='0':g='0':b='0':a='if(${mask},${shadowAlpha},0)',boxblur=luma_radius=${shadowBlur}:luma_power=1:chroma_radius=${shadowBlur}:chroma_power=1:alpha_radius=${shadowBlur}:alpha_power=1[shadow]`;
+ return {
+ filterArgs: [
+ "-f",
+ "lavfi",
+ "-i",
+ `[email protected]:s=${videoInfo.width}x${videoInfo.height}:d=1`,
+ "-filter_complex",
+ `${base};${shadow};[bg][shadow]overlay=format=auto,format=nv12[out]`,
+ "-map",
+ "[out]",
+ ],
+ bakedShadow: true,
+ shadowAlpha,
+ shadowBlur,
+ };
}
function parseProbeSummary(stdout) {
- const lines = stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
- const jsonLine = lines.find((line) => line.startsWith("{") && line.includes("\"success\""));
- if (!jsonLine) {
- fail(`Native probe did not emit a JSON summary:\n${stdout}`);
- }
- try {
- return JSON.parse(jsonLine);
- } catch {
- // The helper currently prints raw Windows paths with backslashes.
- // Keep parsing resilient while this remains a throwaway benchmark tool.
- return JSON.parse(jsonLine.replace(/,"outputPath":".*"\}$/, "}"));
- }
+ const lines = stdout
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .filter(Boolean);
+ const jsonLine = lines.find((line) => line.startsWith("{") && line.includes('"success"'));
+ if (!jsonLine) {
+ fail(`Native probe did not emit a JSON summary:\n${stdout}`);
+ }
+ try {
+ return JSON.parse(jsonLine);
+ } catch {
+ // The helper currently prints raw Windows paths with backslashes.
+ // Keep parsing resilient while this remains a throwaway benchmark tool.
+ return JSON.parse(jsonLine.replace(/,"outputPath":".*"\}$/, "}"));
+ }
}
const inputPath = resolve(getArg("--input"));
-const outputPath = resolve(getArg("--output", join(scriptDir, "recordly-nvdec-nvenc-mp4-output.mp4")));
+const outputPath = resolve(
+ getArg("--output", join(scriptDir, "recordly-nvdec-nvenc-mp4-output.mp4")),
+);
const fps = Math.round(getNumberArg("--fps", 30));
const bitrateMbps = Math.round(getNumberArg("--bitrate-mbps", 18));
const workDir = resolve(getArg("--work-dir", join(scriptDir, "mp4-work")));
@@ -625,6 +957,7 @@ const webcamX = Math.round(getNonNegativeNumberArg("--webcam-x", 0));
const webcamY = Math.round(getNonNegativeNumberArg("--webcam-y", 0));
const webcamSize = Math.round(getNumberArg("--webcam-size", 0));
const webcamRadius = Math.round(getNonNegativeNumberArg("--webcam-radius", 0));
+const webcamTimeOffsetMs = getFiniteNumberArg("--webcam-time-offset-ms", 0);
const webcamMirror = hasArg("--webcam-mirror");
const webcamStream = hasArg("--webcam-stream");
const cursorJson = getArg("--cursor-json", "");
@@ -633,472 +966,582 @@ const cursorStyle = getArg("--cursor-style", "vector");
const cursorAtlasPng = getArg("--cursor-atlas-png", "");
const cursorAtlasMetadata = getArg("--cursor-atlas-metadata", "");
const zoomTelemetry = getArg("--zoom-telemetry", "");
+const timelineMap = getArg("--timeline-map", "");
if (!existsSync(inputPath)) {
- fail(`Input does not exist: ${inputPath}`);
+ fail(`Input does not exist: ${inputPath}`);
}
mkdirSync(workDir, { recursive: true });
mkdirSync(dirname(outputPath), { recursive: true });
function resolveNativeProbePath() {
- const configuredPath = process.env.RECORDLY_NVIDIA_CUDA_EXPORT_EXE;
- const platformArch = process.arch === "arm64" ? "win32-arm64" : "win32-x64";
- const candidates = [
- configuredPath,
- join(scriptDir, "build", "Release", "recordly-nvidia-cuda-compositor.exe"),
- join(repoRoot, "electron", "native", "bin", platformArch, "recordly-nvidia-cuda-compositor.exe"),
- // Backward-compatible legacy helper path while old work dirs are being retired.
- join(scriptDir, "build", "Release", "recordly-nvdec-nvenc-probe.exe"),
- ].filter(Boolean);
-
- for (const candidate of candidates) {
- if (existsSync(candidate)) {
- return candidate;
- }
- }
-
- fail(`Native NVIDIA CUDA compositor is not built. Checked: ${candidates.join(", ")}`);
+ const configuredPath = process.env.RECORDLY_NVIDIA_CUDA_EXPORT_EXE;
+ const platformArch = process.arch === "arm64" ? "win32-arm64" : "win32-x64";
+ const candidates = [
+ configuredPath,
+ join(scriptDir, "build", "Release", "recordly-nvidia-cuda-compositor.exe"),
+ join(
+ repoRoot,
+ "electron",
+ "native",
+ "bin",
+ platformArch,
+ "recordly-nvidia-cuda-compositor.exe",
+ ),
+ // Backward-compatible legacy helper path while old work dirs are being retired.
+ join(scriptDir, "build", "Release", "recordly-nvdec-nvenc-probe.exe"),
+ ].filter(Boolean);
+
+ for (const candidate of candidates) {
+ const unpackedCandidate = unpackAsarPath(candidate);
+ if (unpackedCandidate !== candidate && existsSync(unpackedCandidate)) {
+ return unpackedCandidate;
+ }
+ if (existsSync(candidate)) {
+ return candidate;
+ }
+ }
+
+ fail(`Native NVIDIA CUDA compositor is not built. Checked: ${candidates.join(", ")}`);
}
const nativeProbe = resolveNativeProbePath();
const baseName = basename(inputPath).replace(/\.[^.]+$/, "");
const webcamBaseName = webcamInput
- ? basename(webcamInput).replace(/\.[^.]+$/, "")
- : `${baseName}.webcam`;
+ ? basename(webcamInput).replace(/\.[^.]+$/, "")
+ : `${baseName}.webcam`;
const annexBPath = join(workDir, `${baseName}.annexb.h264`);
const webcamAnnexBPath = join(workDir, `${webcamBaseName}.annexb.h264`);
const cursorSamplesPath = join(workDir, `${baseName}.cursor.tsv`);
const encodedPath = join(workDir, `${baseName}.mapped-callback.h264`);
const shouldBakeStaticShadow =
- Boolean(backgroundImage) &&
- contentWidth > 0 &&
- contentHeight > 0 &&
- shadowOffsetY > 0 &&
- shadowIntensityPct > 0;
+ Boolean(backgroundImage) &&
+ contentWidth > 0 &&
+ contentHeight > 0 &&
+ shadowOffsetY > 0 &&
+ shadowIntensityPct > 0;
const backgroundSuffix = shouldBakeStaticShadow
- ? `.shadow-${shadowOffsetY}-${shadowIntensityPct}`
- : "";
-const generatedBackgroundNv12Path = join(
- workDir,
- `${baseName}${backgroundSuffix}.background.nv12`,
-);
+ ? `.shadow-${shadowOffsetY}-${shadowIntensityPct}`
+ : "";
+const generatedBackgroundNv12Path = join(workDir, `${baseName}${backgroundSuffix}.background.nv12`);
const generatedWebcamNv12Path = join(
- workDir,
- `${baseName}.webcam-${webcamSize}${webcamMirror ? "-mirror" : ""}.nv12`,
+ workDir,
+ `${baseName}.webcam-${webcamSize}${webcamMirror ? "-mirror" : ""}.nv12`,
);
const sourcePtsPath = join(workDir, `${baseName}.source-pts.csv`);
const videoInfo = getVideoInfo(inputPath);
const webcamInfo = webcamInput ? getVideoInfo(webcamInput) : null;
+const timelineSegments = readTimelineSegments(timelineMap);
+const timelineOutputDurationSec = timelineSegments.length
+ ? Math.max(...timelineSegments.map((segment) => segment.outputEndMs)) / 1000
+ : 0;
const durationSec =
- requestedDurationSec > 0 ? Math.min(videoInfo.durationSec, requestedDurationSec) : videoInfo.durationSec;
+ requestedDurationSec > 0
+ ? timelineSegments.length
+ ? requestedDurationSec
+ : Math.min(videoInfo.durationSec, requestedDurationSec)
+ : timelineSegments.length
+ ? timelineOutputDurationSec
+ : videoInfo.durationSec;
+if (timelineSegments.length && Math.abs(durationSec - timelineOutputDurationSec) > 0.05) {
+ fail(
+ `Timeline map output duration ${timelineOutputDurationSec.toFixed(
+ 3,
+ )}s does not match requested duration ${durationSec.toFixed(3)}s`,
+ );
+}
+const timelineSourceDurationSec = timelineSegments.length
+ ? Math.max(...timelineSegments.map((segment) => segment.sourceEndMs)) / 1000
+ : durationSec;
+if (timelineSourceDurationSec > videoInfo.durationSec + 0.1) {
+ fail(
+ `Timeline map source duration ${timelineSourceDurationSec.toFixed(
+ 3,
+ )}s exceeds input duration ${videoInfo.durationSec.toFixed(3)}s`,
+ );
+}
+const sourceDurationSec = Math.min(videoInfo.durationSec, timelineSourceDurationSec);
+const webcamSourceDurationSec = webcamInfo
+ ? Math.min(sourceDurationSec, webcamInfo.durationSec)
+ : sourceDurationSec;
const targetFrames = Math.ceil(durationSec * fps);
emitPreparationProgress(targetFrames, 1);
+const pipelineStartedAt = performance.now();
+const zeroElapsed = () => ({ elapsedMs: 0, stdout: "", stderr: "" });
let sourceWindowFrames = Math.max(
- 1,
- Math.min(videoInfo.sourceFrames, Math.ceil((videoInfo.sourceFrames * durationSec) / videoInfo.durationSec)),
+ 1,
+ Math.min(
+ videoInfo.sourceFrames,
+ Math.ceil((videoInfo.sourceFrames * sourceDurationSec) / videoInfo.durationSec),
+ ),
);
let webcamSourceWindowFrames = webcamInfo
- ? Math.max(
- 1,
- Math.min(webcamInfo.sourceFrames, Math.ceil((webcamInfo.sourceFrames * durationSec) / webcamInfo.durationSec)),
- )
- : 0;
-const backgroundNv12Path = backgroundImage
- ? generatedBackgroundNv12Path
- : backgroundNv12;
+ ? Math.max(
+ 1,
+ Math.min(
+ webcamInfo.sourceFrames,
+ Math.ceil((webcamInfo.sourceFrames * sourceDurationSec) / webcamInfo.durationSec),
+ ),
+ )
+ : 0;
+const backgroundNv12Path = backgroundImage ? generatedBackgroundNv12Path : backgroundNv12;
const backgroundFilter = createBackgroundFilter(
- videoInfo,
- shouldBakeStaticShadow
- ? {
- x: contentX,
- y: contentY + shadowOffsetY,
- width: contentWidth,
- height: contentHeight,
- radius: radius + 8,
- intensityPct: shadowIntensityPct,
- }
- : null,
+ videoInfo,
+ shouldBakeStaticShadow
+ ? {
+ x: contentX,
+ y: contentY + shadowOffsetY,
+ width: contentWidth,
+ height: contentHeight,
+ radius: radius + 8,
+ intensityPct: shadowIntensityPct,
+ }
+ : null,
);
-const backgroundConvert =
- backgroundImage && !(reuseIntermediates && existsSync(backgroundNv12Path))
- ? run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-i",
- resolve(backgroundImage),
- ...backgroundFilter.filterArgs,
- "-frames:v",
- "1",
- "-f",
- "rawvideo",
- backgroundNv12Path,
- ])
- : { elapsedMs: 0 };
-
-const webcamConvert =
- webcamInput && !webcamStream && webcamSize > 0 && !(reuseIntermediates && existsSync(generatedWebcamNv12Path))
- ? run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-i",
- resolve(webcamInput),
- "-vf",
- `${webcamMirror ? "hflip," : ""}scale=${webcamSize}:${webcamSize}:force_original_aspect_ratio=increase,crop=${webcamSize}:${webcamSize},format=nv12`,
- "-frames:v",
- "1",
- "-f",
- "rawvideo",
- generatedWebcamNv12Path,
- ])
- : { elapsedMs: 0 };
-
-const webcamDemux =
- webcamInput && webcamStream && !(reuseDemux && existsSync(webcamAnnexBPath))
- ? run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-stats",
- "-i",
- resolve(webcamInput),
- "-t",
- String(durationSec),
- "-map",
- "0:v:0",
- "-c:v",
- "copy",
- "-bsf:v",
- "h264_mp4toannexb",
- "-an",
- webcamAnnexBPath,
- ])
- : { elapsedMs: 0 };
+const backgroundConvertPromise =
+ backgroundImage && !(reuseIntermediates && existsSync(backgroundNv12Path))
+ ? runAsync(ffmpegCommand, [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-i",
+ resolve(backgroundImage),
+ ...backgroundFilter.filterArgs,
+ "-frames:v",
+ "1",
+ "-f",
+ "rawvideo",
+ backgroundNv12Path,
+ ])
+ : Promise.resolve(zeroElapsed());
+
+const webcamConvertPromise =
+ webcamInput &&
+ !webcamStream &&
+ webcamSize > 0 &&
+ !(reuseIntermediates && existsSync(generatedWebcamNv12Path))
+ ? runAsync(ffmpegCommand, [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-i",
+ resolve(webcamInput),
+ "-vf",
+ `${webcamMirror ? "hflip," : ""}scale=${webcamSize}:${webcamSize}:force_original_aspect_ratio=increase,crop=${webcamSize}:${webcamSize},format=nv12`,
+ "-frames:v",
+ "1",
+ "-f",
+ "rawvideo",
+ generatedWebcamNv12Path,
+ ])
+ : Promise.resolve(zeroElapsed());
+
+const webcamDemuxPromise =
+ webcamInput && webcamStream && !(reuseDemux && existsSync(webcamAnnexBPath))
+ ? runWithProgress(
+ ffmpegCommand,
+ [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-stats",
+ "-i",
+ resolve(webcamInput),
+ "-t",
+ String(sourceDurationSec),
+ "-map",
+ "0:v:0",
+ "-c:v",
+ "copy",
+ "-bsf:v",
+ "h264_mp4toannexb",
+ "-an",
+ webcamAnnexBPath,
+ ],
+ {
+ totalDurationSec: sourceDurationSec,
+ totalFrames: targetFrames,
+ startPercentage: 1,
+ endPercentage: 2,
+ },
+ )
+ : Promise.resolve(zeroElapsed());
+
+const demuxPromise =
+ reuseDemux && existsSync(annexBPath)
+ ? Promise.resolve(zeroElapsed())
+ : runWithProgress(
+ ffmpegCommand,
+ [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-stats",
+ "-i",
+ inputPath,
+ "-t",
+ String(sourceDurationSec),
+ "-map",
+ "0:v:0",
+ "-c:v",
+ "copy",
+ "-bsf:v",
+ "h264_mp4toannexb",
+ "-an",
+ annexBPath,
+ ],
+ {
+ totalDurationSec: sourceDurationSec,
+ totalFrames: targetFrames,
+ startPercentage: 1,
+ endPercentage: 2,
+ },
+ );
+const sourcePtsPromise = writeFramePtsSidecarAsync(inputPath, sourceDurationSec, sourcePtsPath);
if (cursorJson) {
- const cursorPayload = JSON.parse(readFileSync(resolve(cursorJson), "utf8"));
- writeCursorSamples(cursorPayload, cursorSamplesPath);
+ const cursorPayload = JSON.parse(readFileSync(resolve(cursorJson), "utf8"));
+ writeCursorSamples(cursorPayload, cursorSamplesPath);
}
const cursorAtlas =
- cursorJson && cursorHeight > 0 && cursorAtlasPng && cursorAtlasMetadata
- ? prepareExternalCursorAtlas(workDir, cursorAtlasPng, cursorAtlasMetadata)
- : cursorJson && cursorHeight > 0 && cursorStyle === "tahoe"
- ? renderTahoeCursorAtlas(workDir)
- : null;
-
-const demux = reuseDemux && existsSync(annexBPath)
- ? { elapsedMs: 0 }
- : run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-stats",
- "-i",
- inputPath,
- "-t",
- String(durationSec),
- "-map",
- "0:v:0",
- "-c:v",
- "copy",
- "-bsf:v",
- "h264_mp4toannexb",
- "-an",
- annexBPath,
- ]);
+ cursorJson && cursorHeight > 0 && cursorAtlasPng && cursorAtlasMetadata
+ ? prepareExternalCursorAtlas(workDir, cursorAtlasPng, cursorAtlasMetadata)
+ : cursorJson && cursorHeight > 0 && cursorStyle === "tahoe"
+ ? renderTahoeCursorAtlas(workDir)
+ : null;
+
+const [backgroundConvert, webcamConvert, webcamDemux, demux, sourcePts] = await Promise.all([
+ backgroundConvertPromise,
+ webcamConvertPromise,
+ webcamDemuxPromise,
+ demuxPromise,
+ sourcePtsPromise,
+]);
+const preparationWallMs = performance.now() - pipelineStartedAt;
const demuxFrameCount = parseFfmpegStatsFrameCount(demux.stderr);
if (demuxFrameCount > 0) {
- sourceWindowFrames = demuxFrameCount;
+ sourceWindowFrames = demuxFrameCount;
}
const webcamDemuxFrameCount = parseFfmpegStatsFrameCount(webcamDemux.stderr);
if (webcamDemuxFrameCount > 0) {
- webcamSourceWindowFrames = webcamDemuxFrameCount;
+ webcamSourceWindowFrames = webcamDemuxFrameCount;
+}
+if (timelineSegments.length && (!sourcePts.path || sourcePts.frames < sourceWindowFrames)) {
+ fail("Timeline-map CUDA export requires source frame PTS for the full source window");
}
-emitPreparationProgress(targetFrames, 2);
-const sourcePts = writeFramePtsSidecar(inputPath, durationSec, sourcePtsPath);
emitPreparationProgress(targetFrames, 3);
const encodeArgs = [
- "--input",
- annexBPath,
- "--output",
- encodedPath,
- "--fps",
- String(fps),
- "--input-frames",
- String(sourceWindowFrames),
- "--target-frames",
- String(targetFrames),
- "--bitrate-mbps",
- String(bitrateMbps),
- "--callback-encode",
- "--chunk-mb",
- String(chunkMb),
+ "--input",
+ annexBPath,
+ "--output",
+ encodedPath,
+ "--fps",
+ String(fps),
+ "--input-frames",
+ String(sourceWindowFrames),
+ "--target-frames",
+ String(targetFrames),
+ "--bitrate-mbps",
+ String(bitrateMbps),
+ "--callback-encode",
+ "--chunk-mb",
+ String(chunkMb),
];
if (sourcePts.path && sourcePts.frames >= sourceWindowFrames) {
- encodeArgs.push("--source-pts", sourcePts.path);
+ encodeArgs.push("--source-pts", sourcePts.path);
+}
+if (timelineMap) {
+ encodeArgs.push("--timeline-map", resolve(timelineMap));
}
if (maxOutputFrames > 0) {
- encodeArgs.push("--max-frames", String(maxOutputFrames));
+ encodeArgs.push("--max-frames", String(maxOutputFrames));
}
if (cursorJson && cursorHeight > 0) {
- encodeArgs.push("--cursor-samples", cursorSamplesPath, "--cursor-height", String(cursorHeight));
- if (cursorAtlas) {
- encodeArgs.push(
- "--cursor-atlas-rgba",
- cursorAtlas.rgbaPath,
- "--cursor-atlas-metadata",
- cursorAtlas.metadataPath,
- "--cursor-atlas-width",
- String(cursorAtlas.width),
- "--cursor-atlas-height",
- String(cursorAtlas.height),
- );
- }
+ encodeArgs.push("--cursor-samples", cursorSamplesPath, "--cursor-height", String(cursorHeight));
+ if (cursorAtlas) {
+ encodeArgs.push(
+ "--cursor-atlas-rgba",
+ cursorAtlas.rgbaPath,
+ "--cursor-atlas-metadata",
+ cursorAtlas.metadataPath,
+ "--cursor-atlas-width",
+ String(cursorAtlas.width),
+ "--cursor-atlas-height",
+ String(cursorAtlas.height),
+ );
+ }
}
if (streamSync) {
- encodeArgs.push("--stream-sync");
+ encodeArgs.push("--stream-sync");
}
if (prewarmMs > 0) {
- encodeArgs.push("--prewarm-ms", String(prewarmMs));
+ encodeArgs.push("--prewarm-ms", String(prewarmMs));
}
if (contentWidth > 0 && contentHeight > 0) {
- encodeArgs.push(
- "--content-x",
- String(contentX),
- "--content-y",
- String(contentY),
- "--content-width",
- String(contentWidth),
- "--content-height",
- String(contentHeight),
- "--radius",
- String(radius),
- "--background-y",
- String(backgroundY),
- "--background-u",
- String(backgroundU),
- "--background-v",
- String(backgroundV),
- );
- if (backgroundNv12Path) {
- encodeArgs.push("--background-nv12", backgroundNv12Path);
- }
- if (!shouldBakeStaticShadow && shadowOffsetY > 0 && shadowIntensityPct > 0) {
- encodeArgs.push(
- "--shadow-offset-y",
- String(shadowOffsetY),
- "--shadow-intensity-pct",
- String(shadowIntensityPct),
- );
- }
- if (webcamInput && webcamSize > 0) {
- encodeArgs.push(
- "--webcam-x",
- String(webcamX),
- "--webcam-y",
- String(webcamY),
- "--webcam-size",
- String(webcamSize),
- "--webcam-radius",
- String(webcamRadius),
- );
- if (webcamMirror) {
- encodeArgs.push("--webcam-mirror");
- }
- if (webcamStream) {
- encodeArgs.push(
- "--webcam-annexb",
- webcamAnnexBPath,
- "--webcam-input-frames",
- String(webcamSourceWindowFrames),
- "--webcam-target-frames",
- String(targetFrames),
- "--webcam-source-width",
- String(webcamInfo.width),
- "--webcam-source-height",
- String(webcamInfo.height),
- );
- } else {
- encodeArgs.push("--webcam-nv12", generatedWebcamNv12Path);
- }
- }
+ encodeArgs.push(
+ "--content-x",
+ String(contentX),
+ "--content-y",
+ String(contentY),
+ "--content-width",
+ String(contentWidth),
+ "--content-height",
+ String(contentHeight),
+ "--radius",
+ String(radius),
+ "--background-y",
+ String(backgroundY),
+ "--background-u",
+ String(backgroundU),
+ "--background-v",
+ String(backgroundV),
+ );
+ if (backgroundNv12Path) {
+ encodeArgs.push("--background-nv12", backgroundNv12Path);
+ }
+ if (!shouldBakeStaticShadow && shadowOffsetY > 0 && shadowIntensityPct > 0) {
+ encodeArgs.push(
+ "--shadow-offset-y",
+ String(shadowOffsetY),
+ "--shadow-intensity-pct",
+ String(shadowIntensityPct),
+ );
+ }
+ if (webcamInput && webcamSize > 0) {
+ encodeArgs.push(
+ "--webcam-x",
+ String(webcamX),
+ "--webcam-y",
+ String(webcamY),
+ "--webcam-size",
+ String(webcamSize),
+ "--webcam-radius",
+ String(webcamRadius),
+ "--webcam-time-offset-ms",
+ String(webcamTimeOffsetMs),
+ );
+ if (webcamMirror) {
+ encodeArgs.push("--webcam-mirror");
+ }
+ if (webcamStream) {
+ encodeArgs.push(
+ "--webcam-annexb",
+ webcamAnnexBPath,
+ "--webcam-input-frames",
+ String(webcamSourceWindowFrames),
+ "--webcam-target-frames",
+ String(targetFrames),
+ "--webcam-source-duration-ms",
+ String(webcamSourceDurationSec * 1000),
+ "--webcam-source-width",
+ String(webcamInfo.width),
+ "--webcam-source-height",
+ String(webcamInfo.height),
+ );
+ } else {
+ encodeArgs.push("--webcam-nv12", generatedWebcamNv12Path);
+ }
+ }
}
if (zoomTelemetry) {
- encodeArgs.push("--zoom-samples", resolve(zoomTelemetry));
+ encodeArgs.push("--zoom-samples", resolve(zoomTelemetry));
}
-const encode = reuseIntermediates && existsSync(encodedPath)
- ? { elapsedMs: 0, stdout: "", gpuSummary: null }
- : await runWithGpuMonitor(
- nativeProbe,
- encodeArgs,
- sampleGpuDuringEncode ? gpuSampleIntervalMs : 0,
- );
+const encode =
+ reuseIntermediates && existsSync(encodedPath)
+ ? { elapsedMs: 0, stdout: "", gpuSummary: null }
+ : await runWithGpuMonitor(
+ nativeProbe,
+ encodeArgs,
+ sampleGpuDuringEncode ? gpuSampleIntervalMs : 0,
+ );
const nativeSummary = encode.stdout ? parseProbeSummary(encode.stdout) : null;
const mux = skipMux
- ? { elapsedMs: 0 }
- : videoOnly
- ? run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-framerate",
- String(fps),
- "-i",
- encodedPath,
- "-map",
- "0:v:0",
- "-c:v",
- "copy",
- outputPath,
- ])
- : run("ffmpeg", [
- "-y",
- "-hide_banner",
- "-loglevel",
- "error",
- "-framerate",
- String(fps),
- "-i",
- encodedPath,
- "-i",
- inputPath,
- "-map",
- "0:v:0",
- "-map",
- "1:a?",
- "-c:v",
- "copy",
- "-c:a",
- "copy",
- "-t",
- String(durationSec),
- outputPath,
-]);
+ ? { elapsedMs: 0 }
+ : videoOnly
+ ? await runWithProgress(
+ ffmpegCommand,
+ [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-stats",
+ "-framerate",
+ String(fps),
+ "-i",
+ encodedPath,
+ "-map",
+ "0:v:0",
+ "-c:v",
+ "copy",
+ outputPath,
+ ],
+ {
+ totalDurationSec: durationSec,
+ totalFrames: targetFrames,
+ startPercentage: 96,
+ endPercentage: 97.25,
+ stage: "finalizing",
+ },
+ )
+ : await runWithProgress(
+ ffmpegCommand,
+ [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-stats",
+ "-framerate",
+ String(fps),
+ "-i",
+ encodedPath,
+ "-i",
+ inputPath,
+ "-map",
+ "0:v:0",
+ "-map",
+ "1:a?",
+ "-c:v",
+ "copy",
+ "-c:a",
+ "copy",
+ "-t",
+ String(durationSec),
+ outputPath,
+ ],
+ {
+ totalDurationSec: durationSec,
+ totalFrames: targetFrames,
+ startPercentage: 96,
+ endPercentage: 99,
+ stage: "finalizing",
+ },
+ );
const outputInfo = skipMux
- ? { streams: [] }
- : ffprobeJson([
- "-show_entries",
- "stream=index,codec_type,codec_name,width,height,duration,avg_frame_rate,nb_frames",
- outputPath,
- ]);
+ ? { streams: [] }
+ : ffprobeJson([
+ "-show_entries",
+ "stream=index,codec_type,codec_name,width,height,duration,avg_frame_rate,nb_frames",
+ outputPath,
+ ]);
const outputStreams = outputInfo.streams ?? [];
const outputVideo = outputStreams.find((stream) => stream.codec_type === "video") ?? null;
const outputAudio = outputStreams.find((stream) => stream.codec_type === "audio") ?? null;
console.log(
- JSON.stringify(
- {
- success: true,
- inputPath,
- outputPath,
- fps,
- bitrateMbps,
- streamSync,
- prewarmMs,
- maxOutputFrames,
- chunkMb,
- skipMux,
- videoOnly,
- durationSec,
- staticLayout:
- contentWidth > 0 && contentHeight > 0
- ? {
- contentX,
- contentY,
- contentWidth,
- contentHeight,
- radius,
- backgroundY,
- backgroundU,
- backgroundV,
- shadowOffsetY,
- shadowIntensityPct,
- shadowBakedIntoBackground: shouldBakeStaticShadow,
- backgroundShadowAlpha: backgroundFilter.shadowAlpha ?? null,
- backgroundShadowBlur: backgroundFilter.shadowBlur ?? null,
- webcam:
- webcamInput && webcamSize > 0
- ? {
- inputPath: resolve(webcamInput),
- x: webcamX,
- y: webcamY,
- size: webcamSize,
- radius: webcamRadius,
- mirror: webcamMirror,
- staticFrameOnly: !webcamStream,
- stream: webcamStream,
- }
- : null,
- cursor:
- cursorJson && cursorHeight > 0
- ? {
- inputPath: resolve(cursorJson),
- height: cursorHeight,
- style: cursorStyle,
- atlas: cursorAtlas
- ? {
- width: cursorAtlas.width,
- height: cursorAtlas.height,
- entries: cursorAtlas.entries,
- }
- : null,
- }
- : null,
- zoom: zoomTelemetry
- ? {
- inputPath: resolve(zoomTelemetry),
- }
- : null,
- }
- : null,
- gpuSampleIntervalMs: sampleGpuDuringEncode ? gpuSampleIntervalMs : null,
- videoInfo,
- sourceWindowFrames,
- sourcePtsFrames: sourcePts.frames,
- sourcePtsSource: sourcePts.source,
- targetFrames,
- timingsMs: {
- demux: Number(demux.elapsedMs.toFixed(2)),
- backgroundConvert: Number(backgroundConvert.elapsedMs.toFixed(2)),
- cursorAtlas: Number((cursorAtlas?.elapsedMs ?? 0).toFixed(2)),
- webcamConvert: Number(webcamConvert.elapsedMs.toFixed(2)),
- webcamDemux: Number(webcamDemux.elapsedMs.toFixed(2)),
- sourcePtsProbe: Number(sourcePts.elapsedMs.toFixed(2)),
- nativeEncode: Number(encode.elapsedMs.toFixed(2)),
- mux: Number(mux.elapsedMs.toFixed(2)),
- endToEnd: Number(
- (
- backgroundConvert.elapsedMs +
- (cursorAtlas?.elapsedMs ?? 0) +
- webcamConvert.elapsedMs +
- webcamDemux.elapsedMs +
- demux.elapsedMs +
- sourcePts.elapsedMs +
- encode.elapsedMs +
- mux.elapsedMs
- ).toFixed(2),
- ),
- },
- nativeSummary,
- nativeProcessPriorityBoosted: encode.priorityBoosted ?? false,
- gpuSamples: encode.gpuSamples ?? [],
- gpuSummary: encode.gpuSummary ?? null,
- outputVideo,
- outputAudio,
- outputStreams,
- },
- null,
- 2,
- ),
+ JSON.stringify(
+ {
+ success: true,
+ inputPath,
+ outputPath,
+ fps,
+ bitrateMbps,
+ streamSync,
+ prewarmMs,
+ maxOutputFrames,
+ chunkMb,
+ skipMux,
+ videoOnly,
+ durationSec,
+ sourceDurationSec,
+ timelineMap: timelineSegments.length
+ ? {
+ inputPath: resolve(timelineMap),
+ segments: timelineSegments.length,
+ sourceDurationSec,
+ outputDurationSec: durationSec,
+ }
+ : null,
+ staticLayout:
+ contentWidth > 0 && contentHeight > 0
+ ? {
+ contentX,
+ contentY,
+ contentWidth,
+ contentHeight,
+ radius,
+ backgroundY,
+ backgroundU,
+ backgroundV,
+ shadowOffsetY,
+ shadowIntensityPct,
+ shadowBakedIntoBackground: shouldBakeStaticShadow,
+ backgroundShadowAlpha: backgroundFilter.shadowAlpha ?? null,
+ backgroundShadowBlur: backgroundFilter.shadowBlur ?? null,
+ webcam:
+ webcamInput && webcamSize > 0
+ ? {
+ inputPath: resolve(webcamInput),
+ x: webcamX,
+ y: webcamY,
+ size: webcamSize,
+ radius: webcamRadius,
+ timeOffsetMs: webcamTimeOffsetMs,
+ sourceDurationSec: webcamSourceDurationSec,
+ mirror: webcamMirror,
+ staticFrameOnly: !webcamStream,
+ stream: webcamStream,
+ }
+ : null,
+ cursor:
+ cursorJson && cursorHeight > 0
+ ? {
+ inputPath: resolve(cursorJson),
+ height: cursorHeight,
+ style: cursorStyle,
+ atlas: cursorAtlas
+ ? {
+ width: cursorAtlas.width,
+ height: cursorAtlas.height,
+ entries: cursorAtlas.entries,
+ }
+ : null,
+ }
+ : null,
+ zoom: zoomTelemetry
+ ? {
+ inputPath: resolve(zoomTelemetry),
+ }
+ : null,
+ }
+ : null,
+ gpuSampleIntervalMs: sampleGpuDuringEncode ? gpuSampleIntervalMs : null,
+ videoInfo,
+ sourceWindowFrames,
+ sourcePtsFrames: sourcePts.frames,
+ sourcePtsSource: sourcePts.source,
+ targetFrames,
+ timingsMs: {
+ preparationWall: Number(preparationWallMs.toFixed(2)),
+ demux: Number(demux.elapsedMs.toFixed(2)),
+ backgroundConvert: Number(backgroundConvert.elapsedMs.toFixed(2)),
+ cursorAtlas: Number((cursorAtlas?.elapsedMs ?? 0).toFixed(2)),
+ webcamConvert: Number(webcamConvert.elapsedMs.toFixed(2)),
+ webcamDemux: Number(webcamDemux.elapsedMs.toFixed(2)),
+ sourcePtsProbe: Number(sourcePts.elapsedMs.toFixed(2)),
+ nativeEncode: Number(encode.elapsedMs.toFixed(2)),
+ mux: Number(mux.elapsedMs.toFixed(2)),
+ endToEnd: Number((performance.now() - pipelineStartedAt).toFixed(2)),
+ },
+ nativeSummary,
+ nativeProcessPriorityBoosted: encode.priorityBoosted ?? false,
+ gpuSamples: encode.gpuSamples ?? [],
+ gpuSummary: encode.gpuSummary ?? null,
+ outputVideo,
+ outputAudio,
+ outputStreams,
+ },
+ null,
+ 2,
+ ),
);
diff --git a/electron/native/nvidia-cuda-compositor/src/main.cu b/electron/native/nvidia-cuda-compositor/src/main.cu
index 385dc18b..9dccdaad 100644
--- a/electron/native/nvidia-cuda-compositor/src/main.cu
+++ b/electron/native/nvidia-cuda-compositor/src/main.cu
@@ -5,6 +5,7 @@
#include <chrono>
#include <cmath>
#include <cstdint>
+#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
@@ -24,10 +25,20 @@ simplelogger::Logger* logger = simplelogger::LoggerFactory::CreateConsoleLogger(
namespace {
+struct TimelineSegment {
+ double sourceStartMs = 0.0;
+ double sourceEndMs = 0.0;
+ double outputStartMs = 0.0;
+ double outputEndMs = 0.0;
+ double speed = 1.0;
+};
+
struct Options {
std::string inputPath;
std::string outputPath = "recordly-nvidia-cuda-compositor.h264";
std::string sourcePtsPath;
+ std::string timelineMapPath;
+ std::vector<TimelineSegment> timelineSegments;
int fps = 30;
int maxFrames = 0;
int inputFrames = 0;
@@ -53,12 +64,14 @@ struct Options {
std::string webcamAnnexbPath;
int webcamInputFrames = 0;
int webcamTargetFrames = 0;
+ double webcamSourceDurationMs = 0.0;
int webcamSourceWidth = 0;
int webcamSourceHeight = 0;
int webcamX = 0;
int webcamY = 0;
int webcamSize = 0;
int webcamRadius = 0;
+ double webcamTimeOffsetMs = 0.0;
bool webcamMirror = false;
std::string cursorSamplesPath;
int cursorHeight = 0;
@@ -119,6 +132,17 @@ int parseNonNegativeInt(const char* value, const char* name) {
return static_cast<int>(parsed);
}
+double parseFiniteDouble(const char* value, const char* name) {
+ char* end = nullptr;
+ const double parsed = std::strtod(value, &end);
+ if (!end || *end != '\0' || !std::isfinite(parsed)) {
+ std::ostringstream stream;
+ stream << "Invalid " << name << ": " << value;
+ fail(stream.str());
+ }
+ return parsed;
+}
+
Options parseOptions(int argc, char** argv) {
Options options;
for (int index = 1; index < argc; ++index) {
@@ -138,6 +162,8 @@ Options parseOptions(int argc, char** argv) {
options.outputPath = requireValue("--output");
} else if (arg == "--source-pts") {
options.sourcePtsPath = requireValue("--source-pts");
+ } else if (arg == "--timeline-map") {
+ options.timelineMapPath = requireValue("--timeline-map");
} else if (arg == "--fps") {
options.fps = parsePositiveInt(requireValue("--fps"), "--fps");
} else if (arg == "--max-frames") {
@@ -189,6 +215,9 @@ Options parseOptions(int argc, char** argv) {
} else if (arg == "--webcam-target-frames") {
options.webcamTargetFrames =
parsePositiveInt(requireValue("--webcam-target-frames"), "--webcam-target-frames");
+ } else if (arg == "--webcam-source-duration-ms") {
+ options.webcamSourceDurationMs =
+ parseFiniteDouble(requireValue("--webcam-source-duration-ms"), "--webcam-source-duration-ms");
} else if (arg == "--webcam-source-width") {
options.webcamSourceWidth = parsePositiveInt(requireValue("--webcam-source-width"), "--webcam-source-width");
} else if (arg == "--webcam-source-height") {
@@ -202,6 +231,9 @@ Options parseOptions(int argc, char** argv) {
options.webcamSize = parsePositiveInt(requireValue("--webcam-size"), "--webcam-size");
} else if (arg == "--webcam-radius") {
options.webcamRadius = parseNonNegativeInt(requireValue("--webcam-radius"), "--webcam-radius");
+ } else if (arg == "--webcam-time-offset-ms") {
+ options.webcamTimeOffsetMs =
+ parseFiniteDouble(requireValue("--webcam-time-offset-ms"), "--webcam-time-offset-ms");
} else if (arg == "--webcam-mirror") {
options.webcamMirror = true;
} else if (arg == "--cursor-samples") {
@@ -227,7 +259,8 @@ Options parseOptions(int argc, char** argv) {
"[--content-x N --content-y N --content-width N --content-height N --radius N] "
"[--background-nv12 background.nv12] [--shadow-offset-y N --shadow-intensity-pct N] "
"[--webcam-nv12 webcam.nv12 --webcam-x N --webcam-y N --webcam-size N --webcam-radius N] "
- "[--webcam-annexb webcam.h264 --webcam-input-frames N --webcam-target-frames N] "
+ "[--webcam-annexb webcam.h264 --webcam-input-frames N --webcam-target-frames N "
+ "--webcam-source-duration-ms N --webcam-time-offset-ms N] "
"[--cursor-samples cursor.tsv --cursor-height N] "
"[--cursor-atlas-rgba cursor.rgba --cursor-atlas-metadata cursor.tsv "
"--cursor-atlas-width N --cursor-atlas-height N] "
@@ -266,14 +299,6 @@ bool hasWebcamOverlay(const Options& options) {
return (!options.webcamNv12Path.empty() || !options.webcamAnnexbPath.empty()) && options.webcamSize > 0;
}
-int webcamFrameIndexForOutputFrame(int outputFrameIndex, const Options& options) {
- if (options.webcamInputFrames > 0 && options.webcamTargetFrames > 0) {
- return static_cast<int>(
- (static_cast<int64_t>(outputFrameIndex) * options.webcamInputFrames) / options.webcamTargetFrames);
- }
- return outputFrameIndex;
-}
-
std::vector<double> loadFramePts(const std::string& path) {
std::vector<double> timestamps;
if (path.empty()) {
@@ -303,6 +328,125 @@ std::vector<double> loadFramePts(const std::string& path) {
return timestamps;
}
+std::vector<TimelineSegment> loadTimelineMap(const std::string& path) {
+ std::vector<TimelineSegment> segments;
+ if (path.empty()) {
+ return segments;
+ }
+
+ std::ifstream input(path);
+ if (!input) {
+ fail("Failed to open timeline map: " + path);
+ }
+
+ std::string line;
+ double expectedOutputStartMs = 0.0;
+ while (std::getline(input, line)) {
+ if (line.empty()) {
+ continue;
+ }
+ TimelineSegment segment;
+ if (std::sscanf(
+ line.c_str(),
+ "%lf,%lf,%lf,%lf,%lf",
+ &segment.sourceStartMs,
+ &segment.sourceEndMs,
+ &segment.outputStartMs,
+ &segment.outputEndMs,
+ &segment.speed) != 5) {
+ fail("Invalid timeline map row: " + line);
+ }
+ if (
+ !std::isfinite(segment.sourceStartMs) ||
+ !std::isfinite(segment.sourceEndMs) ||
+ !std::isfinite(segment.outputStartMs) ||
+ !std::isfinite(segment.outputEndMs) ||
+ !std::isfinite(segment.speed) ||
+ segment.sourceEndMs <= segment.sourceStartMs ||
+ segment.outputEndMs <= segment.outputStartMs ||
+ segment.speed <= 0.0 ||
+ std::abs(segment.outputStartMs - expectedOutputStartMs) > 2.0) {
+ fail("Invalid timeline map segment: " + line);
+ }
+ expectedOutputStartMs = segment.outputEndMs;
+ segments.push_back(segment);
+ }
+
+ if (!path.empty() && segments.empty()) {
+ fail("Timeline map is empty: " + path);
+ }
+ return segments;
+}
+
+bool sourceToOutputMs(
+ const std::vector<TimelineSegment>& segments,
+ double sourceMs,
+ double& outputMs) {
+ if (segments.empty()) {
+ outputMs = sourceMs;
+ return true;
+ }
+
+ constexpr double kToleranceMs = 1.0;
+ for (const auto& segment : segments) {
+ if (sourceMs + kToleranceMs < segment.sourceStartMs) {
+ return false;
+ }
+ if (sourceMs <= segment.sourceEndMs + kToleranceMs) {
+ const double clampedSourceMs =
+ std::min(segment.sourceEndMs, std::max(segment.sourceStartMs, sourceMs));
+ outputMs = segment.outputStartMs + (clampedSourceMs - segment.sourceStartMs) / segment.speed;
+ outputMs = std::min(segment.outputEndMs, std::max(segment.outputStartMs, outputMs));
+ return true;
+ }
+ }
+
+ return false;
+}
+
+double outputToSourceMs(const std::vector<TimelineSegment>& segments, double outputMs) {
+ if (segments.empty()) {
+ return outputMs;
+ }
+
+ for (const auto& segment : segments) {
+ if (outputMs <= segment.outputEndMs + 1.0) {
+ const double clampedOutputMs =
+ std::min(segment.outputEndMs, std::max(segment.outputStartMs, outputMs));
+ return segment.sourceStartMs + (clampedOutputMs - segment.outputStartMs) * segment.speed;
+ }
+ }
+
+ return segments.back().sourceEndMs;
+}
+
+int webcamFrameIndexForSourceTimeMs(double sourceTimeMs, const Options& options) {
+ const double adjustedSourceMs = std::max(0.0, sourceTimeMs - options.webcamTimeOffsetMs);
+ if (options.webcamInputFrames > 0) {
+ const double durationMs = options.webcamSourceDurationMs > 0.0
+ ? options.webcamSourceDurationMs
+ : (options.webcamTargetFrames > 0 && options.fps > 0
+ ? (static_cast<double>(options.webcamTargetFrames) * 1000.0) / options.fps
+ : 0.0);
+ if (durationMs > 0.0) {
+ const double ratio = std::min(1.0, std::max(0.0, adjustedSourceMs / durationMs));
+ const int index = ratio >= 1.0
+ ? options.webcamInputFrames - 1
+ : static_cast<int>(std::floor(ratio * options.webcamInputFrames));
+ return std::max(0, std::min(options.webcamInputFrames - 1, index));
+ }
+ }
+ return std::max(0, static_cast<int>(std::floor(adjustedSourceMs * options.fps / 1000.0)));
+}
+
+int webcamFrameIndexForOutputFrame(int outputFrameIndex, const Options& options) {
+ const double outputTimeMs =
+ static_cast<double>(std::max(0, outputFrameIndex)) * 1000.0 / static_cast<double>(options.fps);
+ return webcamFrameIndexForSourceTimeMs(
+ outputToSourceMs(options.timelineSegments, outputTimeMs),
+ options);
+}
+
int maxSelectedFramesForTimeline(int targetFrames, int maxFrames) {
if (targetFrames <= 0) {
return maxFrames > 0 ? maxFrames : std::numeric_limits<int>::max();
@@ -316,14 +460,24 @@ int expectedOutputFramesForSourceFrame(
int targetFrames,
int maxFrames,
int fps,
- const std::vector<double>* sourcePts) {
+ const std::vector<double>* sourcePts,
+ const std::vector<TimelineSegment>* timelineSegments) {
const int maxOutputFrames = maxSelectedFramesForTimeline(targetFrames, maxFrames);
if (sourcePts && sourceFrameIndex >= 0 && sourceFrameIndex < static_cast<int>(sourcePts->size())) {
- if (inputFrames > 0 && sourceFrameIndex + 1 >= inputFrames) {
+ const bool hasTimelineMap = timelineSegments && !timelineSegments->empty();
+ if (!hasTimelineMap && inputFrames > 0 && sourceFrameIndex + 1 >= inputFrames) {
+ return maxOutputFrames;
+ }
+ if (hasTimelineMap && inputFrames > 0 && sourceFrameIndex + 1 >= inputFrames) {
return maxOutputFrames;
}
const double frameTimeSec = std::max(0.0, (*sourcePts)[sourceFrameIndex]);
- const int64_t expected = static_cast<int64_t>(std::floor(frameTimeSec * fps)) + 1;
+ double outputTimeMs = frameTimeSec * 1000.0;
+ if (hasTimelineMap && !sourceToOutputMs(*timelineSegments, outputTimeMs, outputTimeMs)) {
+ return 0;
+ }
+ const int64_t expected =
+ static_cast<int64_t>(std::floor((outputTimeMs / 1000.0) * fps)) + 1;
return static_cast<int>(std::min<int64_t>(std::max<int64_t>(1, expected), maxOutputFrames));
}
if (inputFrames <= 0 || targetFrames <= 0) {
@@ -347,6 +501,7 @@ struct FrameSelectionState {
int selectedFrames = 0;
int fps = 30;
const std::vector<double>* sourcePts = nullptr;
+ const std::vector<TimelineSegment>* timelineSegments = nullptr;
};
bool shouldCopyDisplayFrame(int displayFrameIndex, void* userData) {
@@ -367,7 +522,8 @@ bool shouldCopyDisplayFrame(int displayFrameIndex, void* userData) {
state->targetFrames,
state->maxFrames,
state->fps,
- state->sourcePts);
+ state->sourcePts,
+ state->timelineSegments);
if (state->selectedFrames >= expectedSelectedFrames) {
return false;
}
@@ -1824,8 +1980,11 @@ public:
const NvEncInputFrame* inputFrame = encoder_.GetNextInputFrame();
const dim3 block(16, 16);
const dim3 grid((width_ + block.x - 1) / block.x, (height_ + block.y - 1) / block.y);
- const unsigned char* webcamFrame = selectWebcamFrame(outputFrameIndex);
- const double frameTimeMs = static_cast<double>(outputFrameIndex) * 1000.0 / static_cast<double>(fps_);
+ const double outputFrameTimeMs =
+ static_cast<double>(outputFrameIndex) * 1000.0 / static_cast<double>(fps_);
+ const double frameTimeMs =
+ outputToSourceMs(layoutOptions_.timelineSegments, outputFrameTimeMs);
+ const unsigned char* webcamFrame = selectWebcamFrame(frameTimeMs);
const ZoomSample zoomSample = zoomTrack_ ? zoomTrack_->sampleAt(frameTimeMs) : ZoomSample{};
const bool zoomEnabled = zoomTrack_ && zoomSample.scale > 0.01;
const CursorPosition cursorPosition = cursorTrack_
@@ -2341,9 +2500,9 @@ private:
layoutOptions_.shadowIntensityPct == 0;
}
- const unsigned char* selectWebcamFrame(int outputFrameIndex) const {
+ const unsigned char* selectWebcamFrame(double sourceTimeMs) const {
if (webcamCache_ && !webcamCache_->frames.empty()) {
- return webcamCache_->frameAt(webcamFrameIndexForOutputFrame(outputFrameIndex, layoutOptions_));
+ return webcamCache_->frameAt(webcamFrameIndexForSourceTimeMs(sourceTimeMs, layoutOptions_));
}
return webcamDevice_;
}
@@ -2513,6 +2672,7 @@ struct CallbackEncodeState {
bool oneFramePerMappedDisplayFrame = false;
int mappedFrames = 0;
const std::vector<double>* sourcePts = nullptr;
+ const std::vector<TimelineSegment>* timelineSegments = nullptr;
};
ProgressCounters collectProgressCounters(
@@ -2555,14 +2715,16 @@ bool shouldContinueEncoding(int encodedFrames, const Options& options) {
int expectedCallbackOutputFramesForSourceFrame(
int sourceFrameIndex,
const Options& options,
- const std::vector<double>* sourcePts) {
+ const std::vector<double>* sourcePts,
+ const std::vector<TimelineSegment>* timelineSegments) {
return expectedOutputFramesForSourceFrame(
sourceFrameIndex,
options.inputFrames,
options.targetFrames,
options.maxFrames,
options.fps,
- sourcePts);
+ sourcePts,
+ timelineSegments);
}
void encodeMappedDisplayFrame(
@@ -2585,7 +2747,8 @@ void encodeMappedDisplayFrame(
: expectedCallbackOutputFramesForSourceFrame(
sourceFrameIndex,
*state->options,
- state->sourcePts);
+ state->sourcePts,
+ state->timelineSegments);
if (*state->encodedFrames >= expectedOutputFrames) {
return;
}
@@ -2701,7 +2864,11 @@ void reportEncodingProgress(
int main(int argc, char** argv) {
try {
- const Options options = parseOptions(argc, argv);
+ Options options = parseOptions(argc, argv);
+ options.timelineSegments = loadTimelineMap(options.timelineMapPath);
+ if (!options.timelineSegments.empty() && !options.callbackEncode) {
+ fail("Timeline-map CUDA export requires --callback-encode");
+ }
const uint32_t bitrate = static_cast<uint32_t>(options.bitrateMbps) * 1000U * 1000U;
checkCuda(cudaSetDevice(0), "cudaSetDevice");
@@ -2728,6 +2895,9 @@ int main(int argc, char** argv) {
const bool useSourcePts =
options.inputFrames > 0 &&
sourcePts.size() >= static_cast<size_t>(options.inputFrames);
+ if (!options.timelineSegments.empty() && !useSourcePts) {
+ fail("Timeline-map CUDA export requires source frame PTS");
+ }
auto decoder = std::make_unique<NvDecoder>(context, 0, 0, true, cudaVideoCodec_H264, nullptr, true, true);
std::unique_ptr<NvencSink> sink;
const bool useDecoderFramePolicy =
@@ -2744,6 +2914,7 @@ int main(int argc, char** argv) {
0,
options.fps,
useSourcePts ? &sourcePts : nullptr,
+ options.timelineSegments.empty() ? nullptr : &options.timelineSegments,
};
if (useDecoderFramePolicy) {
decoder->SetDisplayFramePolicy(shouldCopyDisplayFrame, &selectionState);
@@ -2778,6 +2949,7 @@ int main(int argc, char** argv) {
useDecoderFramePolicy,
0,
useSourcePts ? &sourcePts : nullptr,
+ options.timelineSegments.empty() ? nullptr : &options.timelineSegments,
};
if (options.callbackEncode) {
decoder->SetMappedFrameHandler(encodeMappedDisplayFrame, &callbackState);
@@ -2938,6 +3110,8 @@ int main(int argc, char** argv) {
: (useDecoderFramePolicy ? "decoder" : "post"))
<< "\","
<< "\"sourceTimestampMode\":\"" << (useSourcePts ? "pts" : "ordinal") << "\","
+ << "\"timelineMap\":" << (!options.timelineSegments.empty() ? "true" : "false") << ","
+ << "\"timelineSegments\":" << options.timelineSegments.size() << ","
<< "\"syncMode\":\"" << (options.streamSync ? "stream" : "device") << "\","
<< "\"prewarmMs\":" << options.prewarmMs << ","
<< "\"chunkMb\":" << options.chunkMb << ","
@@ -2960,6 +3134,8 @@ int main(int argc, char** argv) {
<< "\"webcamRadius\":" << options.webcamRadius << ","
<< "\"webcamStream\":" << (!options.webcamAnnexbPath.empty() ? "true" : "false") << ","
<< "\"webcamMirror\":" << (options.webcamMirror ? "true" : "false") << ","
+ << "\"webcamTimeOffsetMs\":" << options.webcamTimeOffsetMs << ","
+ << "\"webcamSourceDurationMs\":" << options.webcamSourceDurationMs << ","
<< "\"webcamCachedFrames\":" << (webcamCachePtr ? webcamCachePtr->frames.size() : 0) << ","
<< "\"webcamPeakCachedFrames\":" << (webcamCachePtr ? webcamCachePtr->peakFrames : 0) << ","
<< "\"webcamCacheBaseFrame\":" << (webcamCachePtr ? webcamCachePtr->baseFrameIndex : 0) << ","
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index 24fbc015..5a7d0d48 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -18,7 +18,6 @@
static std::atomic<bool> g_stopRequested{false};
static std::atomic<bool> g_pauseRequested{false};
-static std::atomic<bool> g_resumePending{false};
static std::atomic<int64_t> g_lastFrameTimestampHns{0};
static std::atomic<int64_t> g_pauseStartTimestampHns{0};
static std::atomic<int64_t> g_accumulatedPausedHns{0};
@@ -157,6 +156,47 @@ static std::wstring utf8ToWide(const std::string& str) {
return wstr;
}
+static int64_t queryPerformanceCounterHns() {
+ LARGE_INTEGER counter;
+ LARGE_INTEGER frequency;
+ if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&frequency) || frequency.QuadPart <= 0) {
+ return g_lastFrameTimestampHns.load();
+ }
+
+ return static_cast<int64_t>(
+ (static_cast<long double>(counter.QuadPart) * 10000000.0L) /
+ static_cast<long double>(frequency.QuadPart));
+}
+
+static int64_t adjustedVideoTimestampHns(int64_t timestampHns) {
+ int64_t accumulatedPausedHns = g_accumulatedPausedHns.load();
+ if (g_pauseRequested.load()) {
+ const int64_t pauseStart = g_pauseStartTimestampHns.load();
+ if (pauseStart > 0 && timestampHns > pauseStart) {
+ accumulatedPausedHns += (timestampHns - pauseStart);
+ }
+ }
+
+ int64_t adjustedTimestampHns = timestampHns - accumulatedPausedHns;
+ if (adjustedTimestampHns < 0) {
+ adjustedTimestampHns = 0;
+ }
+ return adjustedTimestampHns;
+}
+
+static void openActivePauseAt(int64_t timestampHns) {
+ g_pauseStartTimestampHns.store(timestampHns);
+ g_pauseRequested.store(true);
+}
+
+static void closeActivePauseAt(int64_t timestampHns) {
+ const int64_t pauseStart = g_pauseStartTimestampHns.exchange(0);
+ if (pauseStart > 0 && timestampHns > pauseStart) {
+ g_accumulatedPausedHns.fetch_add(timestampHns - pauseStart);
+ }
+ g_pauseRequested.store(false);
+}
+
static void writeCompanionAudioTimingMetadata(
const std::string& audioPath,
int64_t firstVideoTimestampHns,
@@ -205,14 +245,12 @@ static void stdinListenerThread() {
}
if (line == "pause") {
- g_pauseRequested = true;
- g_pauseStartTimestampHns = g_lastFrameTimestampHns.load();
+ openActivePauseAt(queryPerformanceCounterHns());
continue;
}
if (line == "resume") {
- g_pauseRequested = false;
- g_resumePending = true;
+ closeActivePauseAt(queryPerformanceCounterHns());
continue;
}
@@ -300,18 +338,7 @@ int main(int argc, char* argv[]) {
if (g_pauseRequested) return;
- int64_t adjustedTimestampHns = timestampHns;
- if (g_resumePending.exchange(false)) {
- const int64_t pauseStart = g_pauseStartTimestampHns.load();
- if (pauseStart > 0 && timestampHns > pauseStart) {
- g_accumulatedPausedHns += (timestampHns - pauseStart);
- }
- }
-
- adjustedTimestampHns -= g_accumulatedPausedHns.load();
- if (adjustedTimestampHns < 0) {
- adjustedTimestampHns = 0;
- }
+ const int64_t adjustedTimestampHns = adjustedVideoTimestampHns(timestampHns);
if (encoder.writeFrame(texture, adjustedTimestampHns)) {
const int64_t writtenFrames = frameCount.fetch_add(1) + 1;
@@ -376,6 +403,7 @@ int main(int argc, char* argv[]) {
}
// Stop capture and finalize
+ const int64_t adjustedStopTimestampHns = adjustedVideoTimestampHns(queryPerformanceCounterHns());
session.stopCapture();
if (audioActive) loopback.stop();
if (micActive) micCapture.stop();
@@ -399,6 +427,10 @@ int main(int argc, char* argv[]) {
return 1;
}
+ if (!encoder.extendLastFrameTo(adjustedStopTimestampHns)) {
+ std::cerr << "WARNING: Failed to extend the last video frame to the stop timestamp" << std::endl;
+ }
+
if (!encoder.finalize()) {
std::cerr << "ERROR: Failed to finalize Media Foundation encoder" << std::endl;
return 1;
diff --git a/electron/native/wgc-capture/src/mf_encoder.cpp b/electron/native/wgc-capture/src/mf_encoder.cpp
index 00eea688..30969e39 100644
--- a/electron/native/wgc-capture/src/mf_encoder.cpp
+++ b/electron/native/wgc-capture/src/mf_encoder.cpp
@@ -120,6 +120,8 @@ bool MFEncoder::initialize(const std::wstring& outputPath, int width, int height
const int ySize = width_ * height_;
const int uvSize = (width_ / 2) * (height_ / 2) * 2;
nv12Buffer_.resize(ySize + uvSize);
+ lastFrameBuffer_.clear();
+ lastSampleTimeHns_ = -1;
initialized_ = true;
return true;
@@ -167,17 +169,47 @@ bool MFEncoder::writeFrame(ID3D11Texture2D* texture, int64_t timestampHns) {
context_->Unmap(stagingTexture_.Get(), 0);
+ bool wroteSample = writeNv12SampleLocked(nv12Buffer_, timestampHns);
+ if (wroteSample) {
+ lastFrameBuffer_ = nv12Buffer_;
+ lastSampleTimeHns_ = timestampHns;
+ }
+ return wroteSample;
+}
+
+bool MFEncoder::extendLastFrameTo(int64_t timestampHns) {
+ std::lock_guard<std::mutex> lock(mutex_);
+
+ if (!initialized_ || !sinkWriter_) return false;
+ if (lastFrameBuffer_.empty()) return false;
+
+ const int64_t frameDurationHns = 10000000LL / fps_;
+ if (lastSampleTimeHns_ >= 0 && timestampHns <= lastSampleTimeHns_ + frameDurationHns) {
+ return true;
+ }
+
+ if (!writeNv12SampleLocked(lastFrameBuffer_, timestampHns)) {
+ return false;
+ }
+
+ lastSampleTimeHns_ = timestampHns;
+ return true;
+}
+
+bool MFEncoder::writeNv12SampleLocked(const std::vector<uint8_t>& frameBuffer, int64_t timestampHns) {
+ if (frameBuffer.empty()) return false;
+
// Create MF sample
- DWORD bufferSize = static_cast<DWORD>(nv12Buffer_.size());
+ DWORD bufferSize = static_cast<DWORD>(frameBuffer.size());
ComPtr<IMFMediaBuffer> buffer;
- hr = MFCreateMemoryBuffer(bufferSize, &buffer);
+ HRESULT hr = MFCreateMemoryBuffer(bufferSize, &buffer);
if (FAILED(hr)) return false;
BYTE* bufferData = nullptr;
hr = buffer->Lock(&bufferData, nullptr, nullptr);
if (FAILED(hr)) return false;
- std::memcpy(bufferData, nv12Buffer_.data(), bufferSize);
+ std::memcpy(bufferData, frameBuffer.data(), bufferSize);
buffer->Unlock();
buffer->SetCurrentLength(bufferSize);
@@ -211,7 +243,10 @@ bool MFEncoder::finalize() {
sinkWriter_.Reset();
stagingTexture_.Reset();
nv12Buffer_.clear();
+ lastFrameBuffer_.clear();
nv12Buffer_.shrink_to_fit();
+ lastFrameBuffer_.shrink_to_fit();
+ lastSampleTimeHns_ = -1;
MFShutdown();
return SUCCEEDED(hr);
}
diff --git a/electron/native/wgc-capture/src/mf_encoder.h b/electron/native/wgc-capture/src/mf_encoder.h
index c0855713..21f49032 100644
--- a/electron/native/wgc-capture/src/mf_encoder.h
+++ b/electron/native/wgc-capture/src/mf_encoder.h
@@ -20,18 +20,23 @@ public:
bool initialize(const std::wstring& outputPath, int width, int height, int fps,
ID3D11Device* device, ID3D11DeviceContext* context);
bool writeFrame(ID3D11Texture2D* texture, int64_t timestampHns);
+ bool extendLastFrameTo(int64_t timestampHns);
bool finalize();
private:
+ bool writeNv12SampleLocked(const std::vector<uint8_t>& frameBuffer, int64_t timestampHns);
+
ComPtr<IMFSinkWriter> sinkWriter_;
ID3D11Device* device_ = nullptr;
ID3D11DeviceContext* context_ = nullptr;
ComPtr<ID3D11Texture2D> stagingTexture_;
std::vector<uint8_t> nv12Buffer_;
+ std::vector<uint8_t> lastFrameBuffer_;
DWORD streamIndex_ = 0;
int width_ = 0;
int height_ = 0;
int fps_ = 60;
+ int64_t lastSampleTimeHns_ = -1;
bool initialized_ = false;
std::mutex mutex_;
};
diff --git a/electron/native/wgc-capture/src/wasapi_loopback.cpp b/electron/native/wgc-capture/src/wasapi_loopback.cpp
index 547423e3..745f86e3 100644
--- a/electron/native/wgc-capture/src/wasapi_loopback.cpp
+++ b/electron/native/wgc-capture/src/wasapi_loopback.cpp
@@ -10,6 +10,18 @@ namespace {
constexpr int64_t kHundredNanosecondsPerSecond = 10000000;
constexpr uint64_t kSilenceWriteChunkFrames = 4096;
+int64_t queryPerformanceCounterHns() {
+ LARGE_INTEGER counter;
+ LARGE_INTEGER frequency;
+ if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&frequency) || frequency.QuadPart <= 0) {
+ return 0;
+ }
+
+ return static_cast<int64_t>(
+ (static_cast<long double>(counter.QuadPart) * 10000000.0L) /
+ static_cast<long double>(frequency.QuadPart));
+}
+
bool isFloatFormat(const WAVEFORMATEX* format) {
if (!format) return false;
if (format->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) return true;
@@ -171,6 +183,8 @@ bool WasapiCapture::start() {
totalDataBytes_ = 0;
framesWritten_ = 0;
firstPacketQpcHns_ = -1;
+ pauseStartQpcHns_ = 0;
+ accumulatedPausedQpcHns_ = 0;
dataDiscontinuityCount_ = 0;
timestampErrorCount_ = 0;
writeWavHeader(outputFile_, 0);
@@ -190,14 +204,21 @@ bool WasapiCapture::start() {
bool WasapiCapture::pause() {
if (!capturing_ || paused_) return true;
+ pauseStartQpcHns_ = queryPerformanceCounterHns();
paused_ = true;
return audioClient_ ? SUCCEEDED(audioClient_->Stop()) : false;
}
bool WasapiCapture::resume() {
if (!capturing_ || !paused_) return true;
+ const int64_t resumedQpcHns = queryPerformanceCounterHns();
+ const int64_t pauseStartQpcHns = pauseStartQpcHns_.load();
HRESULT hr = audioClient_ ? audioClient_->Start() : E_FAIL;
if (FAILED(hr)) return false;
+ if (pauseStartQpcHns > 0 && resumedQpcHns > pauseStartQpcHns) {
+ accumulatedPausedQpcHns_.fetch_add(resumedQpcHns - pauseStartQpcHns);
+ }
+ pauseStartQpcHns_ = 0;
paused_ = false;
return true;
}
@@ -219,6 +240,8 @@ void WasapiCapture::stop() {
}
paused_ = false;
+ pauseStartQpcHns_ = 0;
+ accumulatedPausedQpcHns_ = 0;
}
static int16_t floatToInt16(float v) {
@@ -362,8 +385,11 @@ void WasapiCapture::captureThread() {
) {
const int64_t elapsedHns =
static_cast<int64_t>(qpcPosition) - firstPacketQpcHns;
+ const int64_t adjustedElapsedHns = std::max<int64_t>(
+ 0,
+ elapsedHns - accumulatedPausedQpcHns_.load());
const uint64_t expectedStartFrame =
- (static_cast<uint64_t>(elapsedHns) * mixFormat_->nSamplesPerSec +
+ (static_cast<uint64_t>(adjustedElapsedHns) * mixFormat_->nSamplesPerSec +
kHundredNanosecondsPerSecond / 2) /
kHundredNanosecondsPerSecond;
const uint64_t writtenFrames = framesWritten_.load();
diff --git a/electron/native/wgc-capture/src/wasapi_loopback.h b/electron/native/wgc-capture/src/wasapi_loopback.h
index b925f0d3..1c5555e9 100644
--- a/electron/native/wgc-capture/src/wasapi_loopback.h
+++ b/electron/native/wgc-capture/src/wasapi_loopback.h
@@ -50,6 +50,8 @@ private:
UINT32 bufferFrameCount_ = 0;
std::atomic<int64_t> firstPacketQpcHns_{-1};
+ std::atomic<int64_t> pauseStartQpcHns_{0};
+ std::atomic<int64_t> accumulatedPausedQpcHns_{0};
std::atomic<uint32_t> dataDiscontinuityCount_{0};
std::atomic<uint32_t> timestampErrorCount_{0};
};
diff --git a/electron/preload.ts b/electron/preload.ts
index c774ac44..082be8e7 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -69,6 +69,7 @@ type NativeStaticLayoutMetrics = NativeVideoAudioMuxMetrics & {
type NativeStaticLayoutProgress = {
sessionId?: string;
backend?: NativeStaticLayoutChunkMetric["backend"];
+ stage?: "preparing" | "finalizing";
elapsedMs?: number;
averageFps?: number;
currentFrame: number;
@@ -244,6 +245,13 @@ contextBridge.exposeInMainWorld("electronAPI", {
aspectRatio: number;
}>;
zoomTelemetry?: Array<{ timeMs: number; scale: number; x: number; y: number }>;
+ timelineSegments?: Array<{
+ sourceStartMs: number;
+ sourceEndMs: number;
+ outputStartMs: number;
+ outputEndMs: number;
+ speed: number;
+ }>;
chunkDurationSec?: number;
experimentalWindowsGpuCompositor?: boolean;
audioOptions?: {
@@ -821,8 +829,8 @@ contextBridge.exposeInMainWorld("electronAPI", {
},
isNativeWindowsCaptureAvailable: () =>
ipcRenderer.invoke("is-native-windows-capture-available"),
- muxNativeWindowsRecording: (pauseSegments?: Array<{ startMs: number; endMs: number }>) =>
- ipcRenderer.invoke("mux-native-windows-recording", pauseSegments),
+ muxNativeWindowsRecording: (expectedDurationMs?: number) =>
+ ipcRenderer.invoke("mux-native-windows-recording", expectedDurationMs),
hideOsCursor: () => ipcRenderer.invoke("hide-cursor"),
getAppVersion: () => ipcRenderer.invoke("app:getVersion"),
getRecordingPreferences: () => ipcRenderer.invoke("get-recording-preferences"),
diff --git a/package-lock.json b/package-lock.json
index 6c7e1afd..599e93a3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,18 +1,19 @@
{
"name": "recordly",
- "version": "1.2.0-beta.2",
+ "version": "1.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "recordly",
- "version": "1.2.0-beta.2",
+ "version": "1.2.0",
"hasInstallScript": true,
"dependencies": {
"@phosphor-icons/react": "^2.1.10",
"capturekit": "^1.0.13",
"electron-updater": "^6.8.3",
"ffmpeg-static": "^5.3.0",
+ "ffprobe-static": "^3.1.0",
"uiohook-napi": "^1.5.4"
},
"devDependencies": {
@@ -7474,6 +7475,12 @@
"node": ">=16"
}
},
+ "node_modules/ffprobe-static": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/ffprobe-static/-/ffprobe-static-3.1.0.tgz",
+ "integrity": "sha512-Dvpa9uhVMOYivhHKWLGDoa512J751qN1WZAIO+Xw4L/mrUSPxS4DApzSUDbCFE/LUq2+xYnznEahTd63AqBSpA==",
+ "license": "MIT"
+ },
"node_modules/file-type": {
"version": "16.5.4",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-16.5.4.tgz",
diff --git a/package.json b/package.json
index f2dcda9f..7efc2451 100644
--- a/package.json
+++ b/package.json
@@ -46,10 +46,12 @@
"capturekit": "^1.0.13",
"electron-updater": "^6.8.3",
"ffmpeg-static": "^5.3.0",
+ "ffprobe-static": "^3.1.0",
"uiohook-napi": "^1.5.4"
},
"devDependencies": {
"@biomejs/biome": "2.3.13",
+ "@electron/rebuild": "^4.0.3",
"@fix-webm-duration/fix": "^1.0.1",
"@pixi/filter-drop-shadow": "^5.2.0",
"@pixi/filter-motion-blur": "^5.1.1",
@@ -79,7 +81,6 @@
"electron": "^39.2.7",
"electron-builder": "^26.7.0",
"electron-icon-builder": "^2.0.1",
- "@electron/rebuild": "^4.0.3",
"emoji-picker-react": "^4.16.1",
"fast-check": "^4.5.2",
"fix-webm-duration": "^1.0.6",
diff --git a/scripts/launch-recordly-cuda-auto.ps1 b/scripts/launch-recordly-cuda-auto.ps1
index 02d3dff2..5ce72ae3 100644
--- a/scripts/launch-recordly-cuda-auto.ps1
+++ b/scripts/launch-recordly-cuda-auto.ps1
@@ -53,8 +53,10 @@ $env:RECORDLY_NVIDIA_CUDA_SAMPLE_GPU = "1"
if ($AllowCudaAudio) {
$env:RECORDLY_NVIDIA_CUDA_ALLOW_AUDIO_EXPORT = "1"
+ Remove-Item Env:\RECORDLY_NVIDIA_CUDA_FORCE_VIDEO_ONLY -ErrorAction SilentlyContinue
} else {
Remove-Item Env:\RECORDLY_NVIDIA_CUDA_ALLOW_AUDIO_EXPORT -ErrorAction SilentlyContinue
+ $env:RECORDLY_NVIDIA_CUDA_FORCE_VIDEO_ONLY = "1"
}
if ($NoDiagnostics) {
@@ -66,14 +68,15 @@ if ($NoDiagnostics) {
$resolvedAppPath = (Resolve-Path $AppPath).Path
$appDirectory = Split-Path $resolvedAppPath -Parent
if ($AllowCudaAudio) {
- $cudaAudioMode = "candidate only; app still requires timestamp-aligned CUDA output"
+ $cudaAudioMode = "inline CUDA audio candidate; app still requires timestamp-aligned CUDA output"
} else {
- $cudaAudioMode = "guarded; audio exports fall back to Windows D3D11"
+ $cudaAudioMode = "CUDA video-only, then shared app audio mux"
}
Write-Host "Launching Recordly with guarded NVIDIA CUDA/NVENC auto export enabled:"
Write-Host " App: $resolvedAppPath"
Write-Host " CUDA wrapper: $env:RECORDLY_NVIDIA_CUDA_EXPORT_SCRIPT"
+Write-Host " Force CUDA video-only: $($env:RECORDLY_NVIDIA_CUDA_FORCE_VIDEO_ONLY -eq '1')"
Write-Host " CUDA audio exports: $cudaAudioMode"
Write-Host " Diagnostics: $($env:RECORDLY_NVIDIA_CUDA_EXPORT_DIAGNOSTICS -eq '1')"
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index 7b97d8b2..e8e8ce98 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -72,17 +72,18 @@ import { resolveMediaElementSource } from "@/lib/exporter/localMediaSource";
import { resolveSourceAudioFallbackPaths } from "@/lib/exporter/sourceAudioFallback";
import {
clampMediaTimeToDuration,
+ enablePitchPreservingPlayback,
estimateCompanionAudioStartDelaySeconds,
getMediaSyncPlaybackRate,
} from "@/lib/mediaTiming";
import { matchesShortcut } from "@/lib/shortcuts";
+import { cn } from "@/lib/utils";
import {
ASPECT_RATIOS,
type AspectRatio,
getAspectRatioLabel,
getAspectRatioValue,
} from "@/utils/aspectRatioUtils";
-import { cn } from "@/lib/utils";
import { ExtensionIcon } from "./ExtensionIcon";
const PhCursorFill = (props: { className?: string; weight?: "fill" | "regular" }) => (
@@ -110,13 +111,13 @@ import { CropControl } from "./CropControl";
import { ExportSettingsMenu } from "./ExportSettingsMenu";
import ExtensionManager from "./ExtensionManager";
import {
+ type EditorPreset,
+ type EditorPresetSnapshot,
loadEditorPreferences,
loadEditorPresets,
saveEditorPreferences,
saveEditorPresets,
serializeEditorPresetSnapshot,
- type EditorPreset,
- type EditorPresetSnapshot,
} from "./editorPreferences";
import ProjectBrowserDialog, { type ProjectLibraryEntry } from "./ProjectBrowserDialog";
import {
@@ -330,18 +331,6 @@ const DEFAULT_MP4_EXPORT_FRAME_RATE: ExportMp4FrameRate = 30;
const SOURCE_AUDIO_FALLBACK_TOAST_ID = "source-audio-fallback-error";
const PROJECT_AUTOSAVE_DELAY_MS = 1000;
-function getEncodingModeBitrateMultiplier(encodingMode: ExportEncodingMode): number {
- switch (encodingMode) {
- case "fast":
- return 0.1;
- case "quality":
- return 0.9;
- case "balanced":
- default:
- return 0.7;
- }
-}
-
function summarizeErrorMessage(message: string): string {
const firstLine = message
.split(/\r?\n/)
@@ -4194,6 +4183,7 @@ export default function VideoEditor() {
const isInRegion = currentTimeMs >= region.startMs && currentTimeMs < region.endMs;
if (isPlaying && isInRegion) {
+ enablePitchPreservingPlayback(audio);
const audioOffset = (currentTimeMs - region.startMs) / 1000;
// Only seek if significantly out of sync (> 200ms)
if (Math.abs(audio.currentTime - audioOffset) > 0.2) {
@@ -4234,6 +4224,7 @@ export default function VideoEditor() {
const driftThreshold = isPlaying ? 0.35 : 0.01;
for (const audio of sourceAudioElementsRef.current.values()) {
+ enablePitchPreservingPlayback(audio);
const audioDuration = Number.isFinite(audio.duration) ? audio.duration : null;
const startDelaySeconds = estimateCompanionAudioStartDelaySeconds(
duration,
@@ -4876,6 +4867,7 @@ export default function VideoEditor() {
smokeExportConfig.encodingMode,
smokeExportConfig.fps,
smokeExportConfig.quality,
+ saveBlobExport,
],
);
@@ -5166,6 +5158,8 @@ export default function VideoEditor() {
}, [t]);
const isExportSaving = exportProgress?.phase === "saving";
+ const isExportPreparing =
+ isExporting && (!exportProgress || exportProgress.phase === "preparing");
const isExportFinalizing = exportProgress?.phase === "finalizing";
const isRenderingAudio =
isExportFinalizing && typeof exportProgress?.audioProgress === "number";
@@ -5188,6 +5182,9 @@ export default function VideoEditor() {
exportPipelineModel === "legacy" &&
(isExporting || exportProgress !== null);
const exportRenderSpeedLabel =
+ !isExportPreparing &&
+ !isExportFinalizing &&
+ !isExportSaving &&
typeof exportProgress?.renderFps === "number" &&
Number.isFinite(exportProgress.renderFps) &&
exportProgress.renderFps > 0
@@ -5227,16 +5224,26 @@ export default function VideoEditor() {
? `Native skipped: ${exportProgress.nativeStaticLayoutSkipReason}`
: null;
const exportPercentLabel = exportProgress
- ? isExportSaving
+ ? isExportPreparing
+ ? t("editor.exportStatus.preparing", "Preparing export...")
+ : isExportSaving
? t("editor.exportStatus.saving", "Opening save dialog...")
: isRenderingAudio
? t("editor.exportStatus.renderingAudio", "Rendering audio {{percent}}%", {
percent: Math.round((exportProgress.audioProgress ?? 0) * 100),
})
: isExportFinalizing
- ? t("editor.exportStatus.finalizingPercent", "Finalizing {{percent}}%", {
- percent: Math.round(exportFinalizingProgress ?? 100),
- })
+ ? exportFormat === "mp4" && exportPipelineModel === "modern"
+ ? t(
+ "editor.exportStatus.muxingAndSavingPercent",
+ "Muxing and saving {{percent}}%",
+ {
+ percent: Math.round(exportFinalizingProgress ?? 100),
+ },
+ )
+ : t("editor.exportStatus.finalizingPercent", "Finalizing {{percent}}%", {
+ percent: Math.round(exportFinalizingProgress ?? 100),
+ })
: t("editor.exportStatus.completePercent", "{{percent}}% complete", {
percent: Math.round(exportProgress.percentage),
})
@@ -5577,7 +5584,7 @@ export default function VideoEditor() {
</Button>
</div>
<div className="h-2 overflow-hidden rounded-full border border-foreground/5 bg-foreground/5">
- {isExportSaving ? (
+ {isExportPreparing || isExportSaving ? (
<div className="indeterminate-progress h-full rounded-full bg-transparent" />
) : (
<div
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index f55cda90..8f6778b1 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -12,7 +12,11 @@ import {
useState,
} from "react";
import { getAssetPath, getRenderableAssetUrl, getRenderableVideoUrl } from "@/lib/assetPath";
-import { clampMediaTimeToDuration, getMediaSyncPlaybackRate } from "@/lib/mediaTiming";
+import {
+ clampMediaTimeToDuration,
+ enablePitchPreservingPlayback,
+ getMediaSyncPlaybackRate,
+} from "@/lib/mediaTiming";
import {
DEFAULT_WALLPAPER_PATH,
DEFAULT_WALLPAPER_RELATIVE_PATH,
@@ -895,6 +899,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
const video = videoRef.current;
if (!video) return;
+ enablePitchPreservingPlayback(video);
const nextVolume = Math.max(0, Math.min(1, volume));
video.volume = nextVolume;
video.muted = nextVolume <= 0.001;
@@ -1222,6 +1227,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
currentTime * 1000 >= region.startMs && currentTime * 1000 < region.endMs,
);
const targetPlaybackRate = activeSpeedRegion ? activeSpeedRegion.speed : 1;
+ enablePitchPreservingPlayback(bgVideo);
const syncedPlaybackRate = getMediaSyncPlaybackRate({
basePlaybackRate: targetPlaybackRate,
currentTime: bgVideo.currentTime,
@@ -1512,6 +1518,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
(region) => targetTime * 1000 >= region.startMs && targetTime * 1000 < region.endMs,
);
const targetPlaybackRate = activeSpeedRegion ? activeSpeedRegion.speed : 1;
+ enablePitchPreservingPlayback(webcamVideo);
if (Math.abs(webcamVideo.playbackRate - targetPlaybackRate) > 0.001) {
webcamVideo.playbackRate = targetPlaybackRate;
}
diff --git a/src/components/video-editor/videoPlayback/videoEventHandlers.ts b/src/components/video-editor/videoPlayback/videoEventHandlers.ts
index 113d420b..b85bf00d 100644
--- a/src/components/video-editor/videoPlayback/videoEventHandlers.ts
+++ b/src/components/video-editor/videoPlayback/videoEventHandlers.ts
@@ -1,5 +1,6 @@
import type React from "react";
import { extensionHost } from "@/lib/extensions";
+import { enablePitchPreservingPlayback } from "@/lib/mediaTiming";
import type { SpeedRegion, TrimRegion } from "../types";
interface PresentedFrameMetadata {
@@ -41,6 +42,7 @@ export function createVideoEventHandlers(params: VideoEventHandlersParams) {
} = params;
const presentedFrameVideo = video as PresentedFrameVideoElement;
let videoFrameRequestId: number | null = null;
+ enablePitchPreservingPlayback(video);
const emitTime = (timeValue: number) => {
currentTimeRef.current = timeValue * 1000;
@@ -135,6 +137,7 @@ export function createVideoEventHandlers(params: VideoEventHandlersParams) {
} else {
// Apply playback speed from active speed region
const activeSpeedRegion = findActiveSpeedRegion(currentTimeMs);
+ enablePitchPreservingPlayback(video);
video.playbackRate = activeSpeedRegion ? activeSpeedRegion.speed : 1;
emitTime(presentedTime);
}
diff --git a/src/hooks/useScreenRecorder.test.ts b/src/hooks/useScreenRecorder.test.ts
index 597198c6..4395a9b1 100644
--- a/src/hooks/useScreenRecorder.test.ts
+++ b/src/hooks/useScreenRecorder.test.ts
@@ -131,6 +131,33 @@ async function resumeNativeRecording(
return true;
}
+async function stopNativeRecordingWithCompanions({
+ getRecordingDurationMs,
+ markRecordingResumed,
+ now,
+ stopMicFallbackRecorder,
+ stopNativeScreenRecording,
+ stopWebcamRecorder,
+}: {
+ getRecordingDurationMs: (timestampMs: number) => number;
+ markRecordingResumed: (timestampMs: number) => void;
+ now: () => number;
+ stopMicFallbackRecorder: () => Promise<Blob | null>;
+ stopNativeScreenRecording: () => Promise<{ success: boolean; path?: string }>;
+ stopWebcamRecorder: () => Promise<string | null>;
+}) {
+ const stoppedAtMs = now();
+ markRecordingResumed(stoppedAtMs);
+ const expectedDurationMs = getRecordingDurationMs(stoppedAtMs);
+ const micFallbackBlobPromise = stopMicFallbackRecorder();
+ const webcamPathPromise = stopWebcamRecorder();
+ const result = await stopNativeScreenRecording();
+ const webcamPath = await webcamPathPromise;
+ const micFallbackBlob = await micFallbackBlobPromise;
+
+ return { expectedDurationMs, micFallbackBlob, result, webcamPath };
+}
+
function cancelRecording(
recorder: ReturnType<typeof createMockMediaRecorder>,
isNativeRecording: boolean,
@@ -484,6 +511,57 @@ describe("useScreenRecorder state machine", () => {
expect(webcam.pause).not.toHaveBeenCalled();
});
+ it("stops native capture before awaiting webcam finalization", async () => {
+ const callOrder: string[] = [];
+ let resolveWebcam: (path: string | null) => void = () => {};
+ const webcamPathPromise = new Promise<string | null>((resolve) => {
+ resolveWebcam = resolve;
+ });
+ const stopWebcamRecorder = vi.fn(() => {
+ callOrder.push("stop-webcam-started");
+ return webcamPathPromise;
+ });
+ const stopNativeScreenRecording = vi.fn(async () => {
+ callOrder.push("stop-native");
+ return { success: true, path: "screen.mp4" };
+ });
+ const markRecordingResumed = vi.fn((timestampMs: number) => {
+ callOrder.push(`mark-resumed-${timestampMs}`);
+ });
+ const getRecordingDurationMs = vi.fn((timestampMs: number) => {
+ callOrder.push(`duration-${timestampMs}`);
+ return 35000;
+ });
+
+ let finalized = false;
+ const stopped = stopNativeRecordingWithCompanions({
+ getRecordingDurationMs,
+ markRecordingResumed,
+ now: () => 123456,
+ stopMicFallbackRecorder: vi.fn(async () => null),
+ stopNativeScreenRecording,
+ stopWebcamRecorder,
+ }).then((result) => {
+ finalized = true;
+ return result;
+ });
+
+ await Promise.resolve();
+ expect(callOrder).toEqual([
+ "mark-resumed-123456",
+ "duration-123456",
+ "stop-webcam-started",
+ "stop-native",
+ ]);
+ expect(finalized).toBe(false);
+
+ resolveWebcam("webcam.webm");
+ await expect(stopped).resolves.toMatchObject({
+ expectedDurationMs: 35000,
+ webcamPath: "webcam.webm",
+ });
+ });
+
it("cancel discards both screen and webcam recordings", () => {
const webcam = createMockMediaRecorder("recording");
const chunks = { current: [new Blob(["screen"])] };
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 671a69a5..562e7f3c 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -51,11 +51,6 @@ const LINUX_PORTAL_SOURCE: ProcessedDesktopSource = {
sourceType: "screen",
};
-type PauseSegment = {
- startMs: number;
- endMs: number;
-};
-
type DesktopCaptureMediaDevices = {
getUserMedia: (constraints: unknown) => Promise<MediaStream>;
getDisplayMedia: (constraints: unknown) => Promise<MediaStream>;
@@ -155,7 +150,6 @@ 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 micFallbackRecorder = useRef<MediaRecorder | null>(null);
const micFallbackChunks = useRef<Blob[]>([]);
const micFallbackStartDelayMs = useRef<number | null>(null);
@@ -219,7 +213,6 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
startTime.current = startedAt;
accumulatedPausedDurationMs.current = 0;
pauseStartedAtMs.current = null;
- pauseSegmentsRef.current = [];
}, []);
const markRecordingPaused = useCallback((pausedAt: number) => {
@@ -236,9 +229,6 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
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;
}, []);
@@ -693,15 +683,17 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
void (async () => {
const fallbackStartDelayMs = micFallbackStartDelayMs.current;
+ const stoppedAtMs = Date.now();
+ markRecordingResumed(stoppedAtMs);
+ const expectedDurationMs = getRecordingDurationMs(stoppedAtMs);
const micFallbackBlobPromise = stopMicFallbackRecorder();
- const webcamPath = await stopWebcamRecorder();
+ const webcamPathPromise = stopWebcamRecorder();
const isNativeWindows = nativeWindowsRecording.current;
- markRecordingResumed(Date.now());
- const pauseSegments = pauseSegmentsRef.current.slice();
nativeWindowsRecording.current = false;
const result = await window.electronAPI.stopNativeScreenRecording();
await window.electronAPI?.setRecordingState(false);
+ const webcamPath = await webcamPathPromise;
if (!result.success || !result.path) {
console.error(
@@ -735,7 +727,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
if (isNativeWindows) {
const muxResult =
- await window.electronAPI.muxNativeWindowsRecording(pauseSegments);
+ await window.electronAPI.muxNativeWindowsRecording(expectedDurationMs);
if (!muxResult?.success || !muxResult.path) {
void logNativeCaptureDiagnostics("mux-native-windows-recording");
const fallbackPath = muxResult?.path ?? finalPath;
diff --git a/src/lib/exporter/modernVideoExporter.nativeStaticLayout.test.ts b/src/lib/exporter/modernVideoExporter.nativeStaticLayout.test.ts
new file mode 100644
index 00000000..4ca3b0c5
--- /dev/null
+++ b/src/lib/exporter/modernVideoExporter.nativeStaticLayout.test.ts
@@ -0,0 +1,177 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import type { AudioRegion, SpeedRegion } from "@/components/video-editor/types";
+import { ModernVideoExporter } from "./modernVideoExporter";
+import type { DecodedVideoInfo } from "./streamingDecoder";
+
+const videoInfo: DecodedVideoInfo = {
+ width: 1920,
+ height: 1080,
+ duration: 60,
+ streamDuration: 60,
+ frameRate: 30,
+ codec: "h264",
+ hasAudio: true,
+ audioCodec: "aac",
+ audioSampleRate: 48_000,
+};
+
+function createExporter(overrides: Record<string, unknown> = {}) {
+ vi.stubGlobal("window", {
+ electronAPI: {
+ nativeStaticLayoutExport: vi.fn(),
+ nativeStaticLayoutExportCancel: vi.fn(),
+ },
+ });
+
+ return new ModernVideoExporter({
+ videoUrl: "file:///recording.mp4",
+ width: 1920,
+ height: 1080,
+ frameRate: 30,
+ bitrate: 8_000_000,
+ wallpaper: "#101010",
+ padding: 0,
+ borderRadius: 0,
+ backgroundBlur: 0,
+ shadowIntensity: 0,
+ showShadow: false,
+ cropRegion: { x: 0, y: 0, width: 1, height: 1 },
+ experimentalNativeExport: true,
+ ...overrides,
+ } as never) as unknown as {
+ getNativeStaticLayoutSkipReason: (
+ audioPlan: unknown,
+ videoInfo: DecodedVideoInfo,
+ effectiveDurationSec: number,
+ ) => string | null;
+ };
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("ModernVideoExporter native static-layout eligibility", () => {
+ it("allows native video when only the audio track needs offline editing", () => {
+ const audioRegions: AudioRegion[] = [
+ {
+ id: "audio-1",
+ audioPath: "file:///overlay.wav",
+ startMs: 1_000,
+ endMs: 4_000,
+ volume: 0.85,
+ },
+ ];
+ const exporter = createExporter({ audioRegions });
+
+ expect(
+ exporter.getNativeStaticLayoutSkipReason(
+ {
+ audioMode: "edited-track",
+ strategy: "offline-render-fallback",
+ },
+ videoInfo,
+ 60,
+ ),
+ ).toBeNull();
+ });
+
+ it("rejects native static-layout when speed edits do not have a native timeline map", () => {
+ const speedRegions: SpeedRegion[] = [
+ { id: "speed-1", startMs: 1_000, endMs: 4_000, speed: 1.5 },
+ ];
+ const exporter = createExporter({ speedRegions });
+
+ expect(
+ exporter.getNativeStaticLayoutSkipReason(
+ {
+ audioMode: "edited-track",
+ strategy: "offline-render-fallback",
+ },
+ videoInfo,
+ 60,
+ ),
+ ).toBe("unsupported-native-speed-timeline");
+ });
+
+ it("allows speed-only native static-layout when audio and video share filtergraph segments", () => {
+ const speedRegions: SpeedRegion[] = [
+ { id: "speed-1", startMs: 1_000, endMs: 4_000, speed: 1.5 },
+ ];
+ const exporter = createExporter({ speedRegions });
+
+ expect(
+ exporter.getNativeStaticLayoutSkipReason(
+ {
+ audioMode: "edited-track",
+ strategy: "filtergraph-fast-path",
+ audioSourcePath: "recording.mp4",
+ audioSourceSampleRate: 48_000,
+ editedTrackSegments: [
+ { startMs: 0, endMs: 1_000, speed: 1 },
+ { startMs: 1_000, endMs: 4_000, speed: 1.5 },
+ { startMs: 4_000, endMs: 60_000, speed: 1 },
+ ],
+ },
+ videoInfo,
+ 59,
+ ),
+ ).toBeNull();
+ });
+
+ it("allows slow-speed native static-layout when a timeline map is available", () => {
+ const speedRegions: SpeedRegion[] = [
+ { id: "speed-1", startMs: 1_000, endMs: 4_000, speed: 0.5 },
+ ];
+ const exporter = createExporter({ speedRegions });
+
+ expect(
+ exporter.getNativeStaticLayoutSkipReason(
+ {
+ audioMode: "edited-track",
+ strategy: "filtergraph-fast-path",
+ audioSourcePath: "recording.mp4",
+ audioSourceSampleRate: 48_000,
+ editedTrackSegments: [
+ { startMs: 0, endMs: 1_000, speed: 1 },
+ { startMs: 1_000, endMs: 4_000, speed: 0.5 },
+ { startMs: 4_000, endMs: 60_000, speed: 1 },
+ ],
+ },
+ videoInfo,
+ 63,
+ ),
+ ).toBeNull();
+ });
+
+ it("allows native speed timelines with a resolvable webcam source", () => {
+ const speedRegions: SpeedRegion[] = [
+ { id: "speed-1", startMs: 1_000, endMs: 4_000, speed: 1.5 },
+ ];
+ const exporter = createExporter({
+ speedRegions,
+ webcam: {
+ enabled: true,
+ sourcePath: "C:\\recordly\\webcam.mp4",
+ },
+ });
+
+ expect(
+ exporter.getNativeStaticLayoutSkipReason(
+ {
+ audioMode: "edited-track",
+ strategy: "filtergraph-fast-path",
+ audioSourcePath: "recording.mp4",
+ audioSourceSampleRate: 48_000,
+ editedTrackSegments: [
+ { startMs: 0, endMs: 1_000, speed: 1 },
+ { startMs: 1_000, endMs: 4_000, speed: 1.5 },
+ { startMs: 4_000, endMs: 60_000, speed: 1 },
+ ],
+ },
+ videoInfo,
+ 59,
+ ),
+ ).toBeNull();
+ });
+});
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index 09824840..b2cf7ab2 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -158,6 +158,54 @@ type NativeAudioPlan =
editedTrackSegments: Array<{ startMs: number; endMs: number; speed: number }>;
};
+type NativeStaticLayoutTimelineSegment = {
+ sourceStartMs: number;
+ sourceEndMs: number;
+ outputStartMs: number;
+ outputEndMs: number;
+ speed: number;
+};
+
+function buildNativeStaticLayoutTimelineSegments(
+ segments: Array<{ startMs: number; endMs: number; speed: number }>,
+): NativeStaticLayoutTimelineSegment[] {
+ const timelineSegments: NativeStaticLayoutTimelineSegment[] = [];
+ let outputCursorMs = 0;
+
+ for (const segment of segments) {
+ const sourceStartMs = Math.max(0, segment.startMs);
+ const sourceEndMs = Math.max(sourceStartMs, segment.endMs);
+ const speed = segment.speed;
+ if (
+ !Number.isFinite(sourceStartMs) ||
+ !Number.isFinite(sourceEndMs) ||
+ !Number.isFinite(speed) ||
+ sourceEndMs - sourceStartMs <= 0.5 ||
+ speed <= 0
+ ) {
+ return [];
+ }
+
+ const outputDurationMs = (sourceEndMs - sourceStartMs) / speed;
+ if (!Number.isFinite(outputDurationMs) || outputDurationMs <= 0.5) {
+ return [];
+ }
+
+ const outputStartMs = outputCursorMs;
+ const outputEndMs = outputStartMs + outputDurationMs;
+ timelineSegments.push({
+ sourceStartMs,
+ sourceEndMs,
+ outputStartMs,
+ outputEndMs,
+ speed,
+ });
+ outputCursorMs = outputEndMs;
+ }
+
+ return timelineSegments;
+}
+
type NativeStaticLayoutBackground =
| {
backgroundColor: string;
@@ -380,10 +428,14 @@ export class ModernVideoExporter {
const videoInfo = await this.streamingDecoder.loadMetadata(this.config.videoUrl);
this.metadataLoadTimeMs = this.getNowMs() - stageStartedAt;
const nativeAudioPlan = this.buildNativeAudioPlan(videoInfo);
+ const shouldUsePitchPreservingFfmpegAudio =
+ nativeAudioPlan.audioMode === "edited-track" &&
+ nativeAudioPlan.strategy === "filtergraph-fast-path";
const shouldUseFfmpegAudioFallback =
!useNativeEncoder &&
nativeAudioPlan.audioMode !== "none" &&
- !(await isAacAudioEncodingSupported());
+ (shouldUsePitchPreservingFfmpegAudio ||
+ !(await isAacAudioEncodingSupported()));
const effectiveDuration = this.streamingDecoder.getEffectiveDuration(
this.config.trimRegions,
this.config.speedRegions,
@@ -663,7 +715,9 @@ export class ModernVideoExporter {
if (shouldUseFfmpegAudioFallback) {
console.warn(
- `[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.`,
+ shouldUsePitchPreservingFfmpegAudio
+ ? "[VideoExporter] Using FFmpeg audio muxing for pitch-preserving speed edits."
+ : "[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.",
);
const muxedResult = await this.finalizeExportWithFfmpegAudio(
muxerResult,
@@ -1076,6 +1130,25 @@ export class ModernVideoExporter {
);
}
+ private canUseNativeStaticLayoutAudioPlan(
+ audioPlan: NativeAudioPlan,
+ videoInfo: DecodedVideoInfo,
+ effectiveDurationSec: number,
+ ): boolean {
+ switch (audioPlan.audioMode) {
+ case "none":
+ case "copy-source":
+ return true;
+ case "trim-source":
+ return this.canUseNativeStaticTailTrim(videoInfo, effectiveDurationSec);
+ case "edited-track":
+ return (
+ audioPlan.strategy === "offline-render-fallback" ||
+ audioPlan.strategy === "filtergraph-fast-path"
+ );
+ }
+ }
+
private getNativeStaticLayoutSkipReason(
audioPlan: NativeAudioPlan,
videoInfo: DecodedVideoInfo,
@@ -1093,27 +1166,29 @@ export class ModernVideoExporter {
return "odd-output-dimensions";
}
- if (
- audioPlan.audioMode !== "none" &&
- audioPlan.audioMode !== "copy-source" &&
- !(
- audioPlan.audioMode === "trim-source" &&
- this.canUseNativeStaticTailTrim(videoInfo, effectiveDurationSec)
- )
- ) {
+ if (!this.canUseNativeStaticLayoutAudioPlan(audioPlan, videoInfo, effectiveDurationSec)) {
return `unsupported-audio-mode:${audioPlan.audioMode}`;
}
const trimRegions = this.config.trimRegions ?? [];
+ const speedRegions = this.config.speedRegions ?? [];
const hasZoomRegions = (this.config.zoomRegions ?? []).length > 0;
+ if (
+ speedRegions.length > 0 &&
+ !(
+ audioPlan.audioMode === "edited-track" &&
+ audioPlan.strategy === "filtergraph-fast-path" &&
+ audioPlan.editedTrackSegments.length > 0
+ )
+ ) {
+ return "unsupported-native-speed-timeline";
+ }
if (
(trimRegions.length > 0 &&
!this.canUseNativeStaticTailTrim(videoInfo, effectiveDurationSec)) ||
- (this.config.speedRegions ?? []).length > 0 ||
(hasZoomRegions && this.config.experimentalNativeExport !== true) ||
(this.config.annotationRegions ?? []).length > 0 ||
- (this.config.autoCaptions ?? []).length > 0 ||
- (this.config.audioRegions ?? []).length > 0
+ (this.config.autoCaptions ?? []).length > 0
) {
return "timeline-edits-present";
}
@@ -1209,20 +1284,72 @@ export class ModernVideoExporter {
return getLocalFilePath(assetUrl);
}
- private getNativeStaticLayoutAudioOptions(audioPlan: NativeAudioPlan) {
- if (audioPlan.audioMode === "none") {
- return { audioMode: "none" as const };
- }
+ private async renderEditedAudioForNativeMux(
+ description: string,
+ onProgress: (progress: number) => void,
+ ) {
+ this.audioProcessor = new AudioProcessor();
+ this.audioProcessor.setOnProgress(onProgress);
+ const audioBlob = await this.measureFinalizationStage("editedAudioRenderMs", async () =>
+ this.awaitWithFinalizationTimeout(
+ this.audioProcessor!.renderEditedAudioTrack(
+ this.config.videoUrl,
+ this.config.trimRegions,
+ this.config.speedRegions,
+ this.config.audioRegions,
+ this.config.sourceAudioFallbackPaths,
+ this.config.sourceAudioFallbackStartDelayMsByPath,
+ ),
+ description,
+ "audio",
+ true,
+ ),
+ );
- if (audioPlan.audioMode === "copy-source" || audioPlan.audioMode === "trim-source") {
- return {
- audioMode: audioPlan.audioMode,
- audioSourcePath: audioPlan.audioSourcePath,
- trimSegments: audioPlan.trimSegments,
- };
- }
+ return {
+ editedAudioData: await audioBlob.arrayBuffer(),
+ editedAudioMimeType: audioBlob.type || null,
+ };
+ }
- return null;
+ private async getNativeStaticLayoutAudioOptions(
+ audioPlan: NativeAudioPlan,
+ totalFrames: number,
+ ) {
+ switch (audioPlan.audioMode) {
+ case "none":
+ return { audioMode: "none" as const };
+ case "copy-source":
+ case "trim-source":
+ return {
+ audioMode: audioPlan.audioMode,
+ audioSourcePath: audioPlan.audioSourcePath,
+ trimSegments: audioPlan.trimSegments,
+ };
+ case "edited-track": {
+ if (audioPlan.strategy === "filtergraph-fast-path") {
+ return {
+ audioMode: audioPlan.audioMode,
+ audioSourcePath: audioPlan.audioSourcePath,
+ audioSourceSampleRate: audioPlan.audioSourceSampleRate,
+ editedTrackStrategy: audioPlan.strategy,
+ editedTrackSegments: audioPlan.editedTrackSegments,
+ };
+ }
+
+ const renderedAudio = await this.renderEditedAudioForNativeMux(
+ "Native static-layout edited audio rendering",
+ (progress) =>
+ this.reportProgress(0, totalFrames, "preparing", undefined, progress),
+ );
+
+ return {
+ audioMode: audioPlan.audioMode,
+ editedTrackStrategy: audioPlan.strategy,
+ ...renderedAudio,
+ };
+ }
+ }
}
private getNativeStaticLayoutWebcamOverlay(): NativeStaticLayoutWebcamOverlay | null {
@@ -1502,7 +1629,10 @@ export class ModernVideoExporter {
}
const sourcePath = this.getNativeVideoSourcePath();
- const audioOptions = this.getNativeStaticLayoutAudioOptions(audioPlan);
+ const audioOptions = await this.getNativeStaticLayoutAudioOptions(
+ audioPlan,
+ totalFrames,
+ );
if (!sourcePath || !audioOptions) {
this.nativeStaticLayoutSkipReason = !sourcePath
? "missing-source-path"
@@ -1562,6 +1692,16 @@ export class ModernVideoExporter {
totalFrames,
cursorTelemetry,
);
+ const timelineSegments =
+ (this.config.speedRegions ?? []).length > 0 &&
+ audioPlan.audioMode === "edited-track" &&
+ audioPlan.strategy === "filtergraph-fast-path"
+ ? buildNativeStaticLayoutTimelineSegments(audioPlan.editedTrackSegments)
+ : undefined;
+ if ((this.config.speedRegions ?? []).length > 0 && !timelineSegments?.length) {
+ this.nativeStaticLayoutSkipReason = "invalid-native-speed-timeline";
+ return null;
+ }
const cursorAtlas =
cursorTelemetry && cursorTelemetry.length > 0
? await buildNativeCursorAtlas(this.config.cursorStyle ?? "tahoe").catch(
@@ -1602,7 +1742,7 @@ export class ModernVideoExporter {
this.config.experimentalNativeExport === true && runtimePlatform === "win32"
? "windows-native-compositor"
: "static-layout-h264-nvenc";
- this.reportProgress(0, totalFrames, "extracting");
+ this.reportProgress(0, totalFrames, "preparing");
let unsubscribeNativeProgress: (() => void) | undefined;
unsubscribeNativeProgress = window.electronAPI.onNativeStaticLayoutExportProgress?.(
(progress) => {
@@ -1617,14 +1757,42 @@ export class ModernVideoExporter {
}
const nativeTotalFrames = Math.max(1, Math.floor(progress.totalFrames));
+ const rawNativeCurrentFrame = Math.max(0, Math.floor(progress.currentFrame));
+ const rawNativePercentage = Number.isFinite(progress.percentage)
+ ? Math.max(0, progress.percentage)
+ : 0;
+ if (
+ progress.backend === "nvidia-cuda-compositor" ||
+ progress.backend === "windows-d3d11-compositor"
+ ) {
+ this.encoderName = progress.backend;
+ }
+ if (
+ progress.stage === "preparing" ||
+ (progress.stage !== "finalizing" &&
+ rawNativeCurrentFrame === 0 &&
+ rawNativePercentage <= 3)
+ ) {
+ this.nativeStaticLayoutAverageFps = null;
+ this.processedFrameCount = 0;
+ this.reportProgress(0, totalFrames, "preparing");
+ return;
+ }
const progressPercentFrame = Number.isFinite(progress.percentage)
? Math.floor((nativeTotalFrames * progress.percentage) / 100)
: 0;
const nativeCurrentFrame = Math.max(
- Math.floor(progress.currentFrame),
+ rawNativeCurrentFrame,
progressPercentFrame,
);
const nativeFramesComplete = nativeCurrentFrame >= nativeTotalFrames;
+ const nativeFinalizingProgress =
+ progress.stage === "finalizing" && Number.isFinite(progress.percentage)
+ ? Math.max(
+ NATIVE_STATIC_LAYOUT_FRAME_COMPLETE_PROGRESS,
+ Math.min(99, progress.percentage),
+ )
+ : NATIVE_STATIC_LAYOUT_FRAME_COMPLETE_PROGRESS;
const maxExtractingFrame = Math.max(
0,
Math.min(
@@ -1636,16 +1804,12 @@ export class ModernVideoExporter {
maxExtractingFrame,
Math.max(this.processedFrameCount, nativeCurrentFrame),
);
- if (
- progress.backend === "nvidia-cuda-compositor" ||
- progress.backend === "windows-d3d11-compositor"
- ) {
- this.encoderName = progress.backend;
- }
this.nativeStaticLayoutAverageFps =
- typeof progress.instantFps === "number" &&
- Number.isFinite(progress.instantFps) &&
- progress.instantFps > 0
+ progress.stage === "finalizing"
+ ? null
+ : typeof progress.instantFps === "number" &&
+ Number.isFinite(progress.instantFps) &&
+ progress.instantFps > 0
? progress.instantFps
: typeof progress.averageFps === "number" &&
Number.isFinite(progress.averageFps) &&
@@ -1653,10 +1817,10 @@ export class ModernVideoExporter {
? progress.averageFps
: null;
this.processedFrameCount = currentFrame;
- if (nativeFramesComplete) {
+ if (progress.stage === "finalizing" || nativeFramesComplete) {
this.reportFinalizingProgress(
totalFrames,
- NATIVE_STATIC_LAYOUT_FRAME_COMPLETE_PROGRESS,
+ nativeFinalizingProgress,
);
} else {
this.reportProgress(currentFrame, totalFrames, "extracting");
@@ -1695,6 +1859,7 @@ export class ModernVideoExporter {
cursorAtlasPngDataUrl: cursorAtlas?.dataUrl ?? null,
cursorAtlasEntries: cursorAtlas?.entries,
zoomTelemetry,
+ timelineSegments,
chunkDurationSec: STATIC_LAYOUT_CHUNK_DURATION_SEC,
experimentalWindowsGpuCompositor: this.config.experimentalNativeExport === true,
audioOptions: {
@@ -1967,27 +2132,12 @@ export class ModernVideoExporter {
audioPlan.audioMode === "edited-track" &&
audioPlan.strategy === "offline-render-fallback"
) {
- this.audioProcessor = new AudioProcessor();
- this.audioProcessor.setOnProgress((progress) => {
- this.reportFinalizingProgress(this.processedFrameCount, 99, progress);
- });
- const audioBlob = await this.measureFinalizationStage("editedAudioRenderMs", async () =>
- this.awaitWithFinalizationTimeout(
- this.audioProcessor!.renderEditedAudioTrack(
- this.config.videoUrl,
- this.config.trimRegions,
- this.config.speedRegions,
- this.config.audioRegions,
- this.config.sourceAudioFallbackPaths,
- this.config.sourceAudioFallbackStartDelayMsByPath,
- ),
- `${NATIVE_EXPORT_ENGINE_NAME} edited audio rendering`,
- "audio",
- true,
- ),
+ const renderedAudio = await this.renderEditedAudioForNativeMux(
+ `${NATIVE_EXPORT_ENGINE_NAME} edited audio rendering`,
+ (progress) => this.reportFinalizingProgress(this.processedFrameCount, 99, progress),
);
- editedAudioBuffer = await audioBlob.arrayBuffer();
- editedAudioMimeType = audioBlob.type || null;
+ editedAudioBuffer = renderedAudio.editedAudioData;
+ editedAudioMimeType = renderedAudio.editedAudioMimeType;
}
const sessionId = this.nativeExportSessionId;
@@ -2079,27 +2229,12 @@ export class ModernVideoExporter {
audioPlan.audioMode === "edited-track" &&
audioPlan.strategy === "offline-render-fallback"
) {
- this.audioProcessor = new AudioProcessor();
- this.audioProcessor.setOnProgress((progress) => {
- this.reportFinalizingProgress(this.processedFrameCount, 99, progress);
- });
- const audioBlob = await this.measureFinalizationStage("editedAudioRenderMs", async () =>
- this.awaitWithFinalizationTimeout(
- this.audioProcessor!.renderEditedAudioTrack(
- this.config.videoUrl,
- this.config.trimRegions,
- this.config.speedRegions,
- this.config.audioRegions,
- this.config.sourceAudioFallbackPaths,
- this.config.sourceAudioFallbackStartDelayMsByPath,
- ),
- "FFmpeg edited audio rendering",
- "audio",
- true,
- ),
+ const renderedAudio = await this.renderEditedAudioForNativeMux(
+ "FFmpeg edited audio rendering",
+ (progress) => this.reportFinalizingProgress(this.processedFrameCount, 99, progress),
);
- editedAudioBuffer = await audioBlob.arrayBuffer();
- editedAudioMimeType = audioBlob.type || null;
+ editedAudioBuffer = renderedAudio.editedAudioData;
+ editedAudioMimeType = renderedAudio.editedAudioMimeType;
}
const muxOptions = {
@@ -2296,7 +2431,9 @@ export class ModernVideoExporter {
const safeRenderProgress =
phase === "finalizing" ? Math.max(0, Math.min(renderProgress ?? 100, 100)) : undefined;
const percentage =
- phase === "finalizing"
+ phase === "preparing"
+ ? 0
+ : phase === "finalizing"
? (safeRenderProgress ?? 100)
: totalFrames > 0
? (currentFrame / totalFrames) * 100
diff --git a/src/lib/exporter/types.ts b/src/lib/exporter/types.ts
index 9cc7508a..3312ecbc 100644
--- a/src/lib/exporter/types.ts
+++ b/src/lib/exporter/types.ts
@@ -30,7 +30,7 @@ export interface ExportProgress {
encodeBackend?: ExportEncodeBackend;
encoderName?: string;
nativeStaticLayoutSkipReason?: string;
- phase?: "extracting" | "finalizing" | "saving"; // Phase of export
+ phase?: "preparing" | "extracting" | "finalizing" | "saving"; // Phase of export
renderProgress?: number; // 0-100, progress of GIF rendering phase
audioProgress?: number; // 0-1, progress of real-time audio rendering (speed/audio regions)
}
diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts
index 47926eb7..6ba44e63 100644
--- a/src/lib/exporter/videoExporter.ts
+++ b/src/lib/exporter/videoExporter.ts
@@ -178,10 +178,14 @@ export class VideoExporter {
let useNativeEncoder = shouldUseExperimentalNativeExport
? await this.tryStartNativeVideoExport()
: false;
+ const shouldUsePitchPreservingFfmpegAudio =
+ audioPlan.audioMode === "edited-track" &&
+ audioPlan.strategy === "filtergraph-fast-path";
const shouldUseFfmpegAudioFallback =
!useNativeEncoder &&
audioPlan.audioMode !== "none" &&
- !(await isAacAudioEncodingSupported());
+ (shouldUsePitchPreservingFfmpegAudio ||
+ !(await isAacAudioEncodingSupported()));
if (!useNativeEncoder) {
await this.initializeEncoder();
@@ -406,7 +410,9 @@ export class VideoExporter {
if (shouldUseFfmpegAudioFallback) {
console.warn(
- "[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.",
+ shouldUsePitchPreservingFfmpegAudio
+ ? "[VideoExporter] Using FFmpeg audio muxing for pitch-preserving speed edits."
+ : "[VideoExporter] Browser AAC encoding is unavailable; falling back to FFmpeg audio muxing.",
);
const result = await this.finalizeExportWithFfmpegAudio(
muxerResult,
diff --git a/src/lib/mediaTiming.test.ts b/src/lib/mediaTiming.test.ts
index a6f20479..9934aaab 100644
--- a/src/lib/mediaTiming.test.ts
+++ b/src/lib/mediaTiming.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
clampMediaTimeToDuration,
+ enablePitchPreservingPlayback,
estimateCompanionAudioStartDelaySeconds,
getEffectiveRecordingDurationMs,
getEffectiveVideoStreamDurationSeconds,
@@ -101,6 +102,22 @@ describe("getMediaSyncPlaybackRate", () => {
});
});
+describe("enablePitchPreservingPlayback", () => {
+ it("enables standard and vendor pitch-preserve switches", () => {
+ const media = {} as HTMLMediaElement & {
+ preservesPitch?: boolean;
+ mozPreservesPitch?: boolean;
+ webkitPreservesPitch?: boolean;
+ };
+
+ enablePitchPreservingPlayback(media);
+
+ expect(media.preservesPitch).toBe(true);
+ expect(media.mozPreservesPitch).toBe(true);
+ expect(media.webkitPreservesPitch).toBe(true);
+ });
+});
+
describe("getEffectiveVideoStreamDurationSeconds", () => {
it("prefers the video stream duration when present", () => {
expect(
diff --git a/src/lib/mediaTiming.ts b/src/lib/mediaTiming.ts
index d9dcf673..fe10ba98 100644
--- a/src/lib/mediaTiming.ts
+++ b/src/lib/mediaTiming.ts
@@ -74,6 +74,19 @@ export function getMediaSyncPlaybackRate({
return Math.max(0.1, safeBasePlaybackRate + adjustment);
}
+type PitchPreservingMediaElement = HTMLMediaElement & {
+ preservesPitch?: boolean;
+ mozPreservesPitch?: boolean;
+ webkitPreservesPitch?: boolean;
+};
+
+export function enablePitchPreservingPlayback(media: HTMLMediaElement) {
+ const pitchMedia = media as PitchPreservingMediaElement;
+ pitchMedia.preservesPitch = true;
+ pitchMedia.mozPreservesPitch = true;
+ pitchMedia.webkitPreservesPitch = true;
+}
+
export function getEffectiveVideoStreamDurationSeconds({
duration,
streamDuration,