Skip to content

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

fix: address export pipeline review feedback

webadderall77af3e58f6b2
diff --git a/electron/extensions/errorUtils.ts b/electron/extensions/errorUtils.ts
new file mode 100644
index 00000000..ae5ccfda
--- /dev/null
+++ b/electron/extensions/errorUtils.ts
@@ -0,0 +1,3 @@
+export function getErrorMessage(error: unknown): string {
+ return error instanceof Error ? error.message : String(error);
+}
\ No newline at end of file
diff --git a/electron/extensions/extensionIpc.ts b/electron/extensions/extensionIpc.ts
index abc49c8f..d1a32b67 100644
--- a/electron/extensions/extensionIpc.ts
+++ b/electron/extensions/extensionIpc.ts
@@ -23,12 +23,9 @@ import {
submitExtensionForReview,
updateReviewStatus,
} from "./extensionMarketplace";
+import { getErrorMessage } from "./errorUtils";
import type { ExtensionInfo, MarketplaceReviewStatus } from "./extensionTypes";
-function getErrorMessage(error: unknown): string {
- return error instanceof Error ? error.message : String(error);
-}
-
/**
* Serialize extension info for IPC transfer (strip non-serializable fields).
*/
diff --git a/electron/extensions/extensionMarketplace.ts b/electron/extensions/extensionMarketplace.ts
index 3d6bc8d7..e8bc66dd 100644
--- a/electron/extensions/extensionMarketplace.ts
+++ b/electron/extensions/extensionMarketplace.ts
@@ -12,6 +12,7 @@ import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import type { ReadableStream as NodeReadableStream } from "node:stream/web";
import { app } from "electron";
+import { getErrorMessage } from "./errorUtils";
import { getRegisteredExtensions, installExtensionFromPath } from "./extensionLoader";
import type {
ExtensionReview,
@@ -20,10 +21,6 @@ import type {
MarketplaceSearchResult,
} from "./extensionTypes";
-function getErrorMessage(error: unknown): string {
- return error instanceof Error ? error.message : String(error);
-}
-
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
@@ -286,7 +283,7 @@ export async function downloadAndInstallExtension(
}
// Track download count (fire-and-forget — CDN may cache the GET, so POST separately)
- fetch(`${MARKETPLACE_API_BASE}/extensions/${encodeURIComponent(extensionId)}/download`, {
+ fetch(`${getMarketplaceUrl()}/extensions/${encodeURIComponent(extensionId)}/download`, {
method: "POST",
headers: { "X-Recordly-Version": app.getVersion() },
}).catch(() => undefined);
diff --git a/electron/ipc/nativeVideoExport.ts b/electron/ipc/nativeVideoExport.ts
index 2bb913f7..1861bce2 100644
--- a/electron/ipc/nativeVideoExport.ts
+++ b/electron/ipc/nativeVideoExport.ts
@@ -10,6 +10,7 @@ export interface NativeVideoExportStartOptions {
frameRate: number;
bitrate: number;
encodingMode: NativeExportEncodingMode;
+ inputMode?: "rawvideo" | "h264-stream";
}
export interface NativeVideoExportAudioSegment {
diff --git a/electron/ipc/windowsCaptureSelection.ts b/electron/ipc/windowsCaptureSelection.ts
index 6b68c77e..5d1157d0 100644
--- a/electron/ipc/windowsCaptureSelection.ts
+++ b/electron/ipc/windowsCaptureSelection.ts
@@ -26,16 +26,17 @@ export function resolveWindowsCaptureDisplay(
): ResolvedWindowsCaptureDisplay {
const requestedDisplayId = Number(source?.display_id);
const primaryDisplayId = Number(primaryDisplay.id);
- const displayId =
+ const requestedOrPrimaryDisplayId =
Number.isFinite(requestedDisplayId) && requestedDisplayId > 0
? requestedDisplayId
: primaryDisplayId;
const matchedDisplay =
- allDisplays.find((display) => String(display.id) === String(displayId)) ?? primaryDisplay;
+ allDisplays.find((display) => String(display.id) === String(requestedOrPrimaryDisplayId)) ??
+ primaryDisplay;
return {
- displayId,
+ displayId: Number(matchedDisplay.id),
bounds: matchedDisplay.bounds,
};
}
diff --git a/src/lib/exporter/annotationRenderer.ts b/src/lib/exporter/annotationRenderer.ts
index 2a672f51..a8822a81 100644
--- a/src/lib/exporter/annotationRenderer.ts
+++ b/src/lib/exporter/annotationRenderer.ts
@@ -11,8 +11,8 @@ export interface AnnotationRenderAssets {
const annotationImagePromiseCache = new Map<string, Promise<HTMLImageElement | null>>();
let blurBufferCanvas: HTMLCanvasElement | null = null;
-function getBlurBufferCanvas(): HTMLCanvasElement {
- if (typeof document === "undefined") return {} as HTMLCanvasElement;
+function getBlurBufferCanvas(): HTMLCanvasElement | null {
+ if (typeof document === "undefined") return null;
if (!blurBufferCanvas) {
blurBufferCanvas = document.createElement("canvas");
}
@@ -391,19 +391,21 @@ export async function renderAnnotations(
if (sw > 0 && sh > 0) {
const buffer = getBlurBufferCanvas();
- buffer.width = sw;
- buffer.height = sh;
- const bCtx = buffer.getContext("2d");
- if (bCtx) {
- bCtx.drawImage(ctx.canvas, sx, sy, sw, sh, 0, 0, sw, sh);
-
- ctx.filter = `blur(${blurStrength}px)`;
- ctx.drawImage(buffer, sx, sy);
-
- if (annotation.blurColor && annotation.blurColor !== "transparent") {
- ctx.filter = "none";
- ctx.fillStyle = annotation.blurColor;
- ctx.fillRect(x, y, width, height);
+ if (buffer) {
+ buffer.width = sw;
+ buffer.height = sh;
+ const bCtx = buffer.getContext("2d");
+ if (bCtx) {
+ bCtx.drawImage(ctx.canvas, sx, sy, sw, sh, 0, 0, sw, sh);
+
+ ctx.filter = `blur(${blurStrength}px)`;
+ ctx.drawImage(buffer, sx, sy);
+
+ if (annotation.blurColor && annotation.blurColor !== "transparent") {
+ ctx.filter = "none";
+ ctx.fillStyle = annotation.blurColor;
+ ctx.fillRect(x, y, width, height);
+ }
}
}
}
diff --git a/src/lib/exporter/audioEncoder.ts b/src/lib/exporter/audioEncoder.ts
index fd13a2ba..2604fbd2 100644
--- a/src/lib/exporter/audioEncoder.ts
+++ b/src/lib/exporter/audioEncoder.ts
@@ -519,19 +519,9 @@ export class AudioProcessor {
pitchMedia.mozPreservesPitch = true;
pitchMedia.webkitPreservesPitch = true;
- await this.waitForLoadedMetadata(timelineMedia);
- if (this.cancelled) {
- throw new Error("Export cancelled");
- }
-
- const audioContext = new AudioContext();
- const destinationNode = audioContext.createMediaStreamDestination();
-
+ let audioContext: AudioContext | null = null;
+ let destinationNode: MediaStreamAudioDestinationNode | null = null;
let timelineAudioSourceNode: MediaElementAudioSourceNode | null = null;
- if (sourceAudioFallbackPaths.length === 0) {
- timelineAudioSourceNode = audioContext.createMediaElementSource(timelineMedia);
- timelineAudioSourceNode.connect(destinationNode);
- }
const sourceAudioElements: {
media: HTMLAudioElement;
@@ -540,37 +530,6 @@ export class AudioProcessor {
cleanup: () => void;
}[] = [];
- for (const sourceAudioPath of sourceAudioFallbackPaths) {
- const sourceFileSource = await resolveMediaElementSource(sourceAudioPath);
- const audioEl = document.createElement("audio");
- audioEl.src = sourceFileSource.src;
- audioEl.preload = "auto";
- try {
- await this.waitForLoadedMetadata(audioEl);
- } catch {
- sourceFileSource.revoke();
- console.warn(
- "[AudioProcessor] Failed to load source audio fallback:",
- sourceAudioPath,
- );
- continue;
- }
- if (this.cancelled) throw new Error("Export cancelled");
-
- const sourceNode = audioContext.createMediaElementSource(audioEl);
- sourceNode.connect(destinationNode);
-
- sourceAudioElements.push({
- media: audioEl,
- sourceNode,
- startDelaySeconds: estimateCompanionAudioStartDelaySeconds(
- timelineMedia.duration,
- audioEl.duration,
- ),
- cleanup: sourceFileSource.revoke,
- });
- }
-
// Prepare external audio region elements
const audioRegionElements: {
media: HTMLAudioElement;
@@ -580,39 +539,89 @@ export class AudioProcessor {
cleanup: () => void;
}[] = [];
- for (const region of audioRegions) {
- const regionFileSource = await resolveMediaElementSource(region.audioPath);
- const audioEl = document.createElement("audio");
- audioEl.src = regionFileSource.src;
- audioEl.preload = "auto";
- try {
- await this.waitForLoadedMetadata(audioEl);
- } catch {
- regionFileSource.revoke();
- console.warn("[AudioProcessor] Failed to load audio region:", region.audioPath);
- continue;
- }
- if (this.cancelled) throw new Error("Export cancelled");
-
- const regionSourceNode = audioContext.createMediaElementSource(audioEl);
- const gainNode = audioContext.createGain();
- gainNode.gain.value = Math.max(0, Math.min(1, region.volume));
- regionSourceNode.connect(gainNode);
- gainNode.connect(destinationNode);
-
- audioRegionElements.push({
- media: audioEl,
- sourceNode: regionSourceNode,
- gainNode,
- region,
- cleanup: regionFileSource.revoke,
- });
- }
-
- const { recorder, recordedBlobPromise } = this.startAudioRecording(destinationNode.stream);
+ let recorder: MediaRecorder | null = null;
+ let recordedBlobPromise: Promise<Blob> | null = null;
let tickTimerId: ReturnType<typeof setTimeout> | null = null;
try {
+ await this.waitForLoadedMetadata(timelineMedia);
+ if (this.cancelled) {
+ throw new Error("Export cancelled");
+ }
+
+ audioContext = new AudioContext();
+ const currentDestinationNode = audioContext.createMediaStreamDestination();
+ destinationNode = currentDestinationNode;
+
+ if (sourceAudioFallbackPaths.length === 0) {
+ timelineAudioSourceNode = audioContext.createMediaElementSource(timelineMedia);
+ timelineAudioSourceNode.connect(currentDestinationNode);
+ }
+
+ for (const sourceAudioPath of sourceAudioFallbackPaths) {
+ const sourceFileSource = await resolveMediaElementSource(sourceAudioPath);
+ const audioEl = document.createElement("audio");
+ audioEl.src = sourceFileSource.src;
+ audioEl.preload = "auto";
+ try {
+ await this.waitForLoadedMetadata(audioEl);
+ } catch {
+ sourceFileSource.revoke();
+ console.warn(
+ "[AudioProcessor] Failed to load source audio fallback:",
+ sourceAudioPath,
+ );
+ continue;
+ }
+ if (this.cancelled) throw new Error("Export cancelled");
+
+ const sourceNode = audioContext.createMediaElementSource(audioEl);
+ sourceNode.connect(currentDestinationNode);
+
+ sourceAudioElements.push({
+ media: audioEl,
+ sourceNode,
+ startDelaySeconds: estimateCompanionAudioStartDelaySeconds(
+ timelineMedia.duration,
+ audioEl.duration,
+ ),
+ cleanup: sourceFileSource.revoke,
+ });
+ }
+
+ for (const region of audioRegions) {
+ const regionFileSource = await resolveMediaElementSource(region.audioPath);
+ const audioEl = document.createElement("audio");
+ audioEl.src = regionFileSource.src;
+ audioEl.preload = "auto";
+ try {
+ await this.waitForLoadedMetadata(audioEl);
+ } catch {
+ regionFileSource.revoke();
+ console.warn("[AudioProcessor] Failed to load audio region:", region.audioPath);
+ continue;
+ }
+ if (this.cancelled) throw new Error("Export cancelled");
+
+ const regionSourceNode = audioContext.createMediaElementSource(audioEl);
+ const gainNode = audioContext.createGain();
+ gainNode.gain.value = Math.max(0, Math.min(1, region.volume));
+ regionSourceNode.connect(gainNode);
+ gainNode.connect(currentDestinationNode);
+
+ audioRegionElements.push({
+ media: audioEl,
+ sourceNode: regionSourceNode,
+ gainNode,
+ region,
+ cleanup: regionFileSource.revoke,
+ });
+ }
+
+ const recording = this.startAudioRecording(currentDestinationNode.stream);
+ recorder = recording.recorder;
+ recordedBlobPromise = recording.recordedBlobPromise;
+
if (audioContext.state === "suspended") {
await audioContext.resume();
}
@@ -793,16 +802,22 @@ export class AudioProcessor {
entry.media.load();
entry.cleanup();
}
- if (recorder.state !== "inactive") {
+ if (recorder && recorder.state !== "inactive") {
recorder.stop();
}
- destinationNode.stream.getTracks().forEach((track) => track.stop());
- timelineAudioSourceNode?.disconnect();
- destinationNode.disconnect();
- await audioContext.close();
- timelineMedia.src = "";
- timelineMedia.load();
- timelineMediaSource.revoke();
+ destinationNode?.stream.getTracks().forEach((track) => track.stop());
+ destinationNode?.disconnect();
+ if (audioContext && audioContext.state !== "closed") {
+ try {
+ await audioContext.close();
+ } catch {
+ // Ignore teardown failures during export cleanup.
+ }
+ }
+ }
+
+ if (!recordedBlobPromise) {
+ throw new Error("Failed to record mixed timeline audio");
}
const recordedBlob = await recordedBlobPromise;
diff --git a/src/lib/exporter/forwardFrameSource.ts b/src/lib/exporter/forwardFrameSource.ts
index fdd006ec..d73a96f9 100644
--- a/src/lib/exporter/forwardFrameSource.ts
+++ b/src/lib/exporter/forwardFrameSource.ts
@@ -111,6 +111,11 @@ export class ForwardFrameSource {
return videoUrl;
}
+ if (/^[A-Za-z]:[\\/]/.test(videoUrl)) {
+ const normalized = videoUrl.replace(/\\/g, "/");
+ return `file:///${encodeURI(normalized)}`;
+ }
+
if (videoUrl.startsWith("/")) {
return `file://${encodeURI(videoUrl)}`;
}
@@ -261,6 +266,10 @@ export class ForwardFrameSource {
return Promise.resolve(null);
}
+ if (this.frameResolve) {
+ throw new Error("Concurrent getFrameAtTime() calls are not supported");
+ }
+
return new Promise((resolve) => {
this.frameResolve = resolve;
});
diff --git a/src/lib/exporter/frameRenderer.test.ts b/src/lib/exporter/frameRenderer.test.ts
index 81261498..f3e87ac8 100644
--- a/src/lib/exporter/frameRenderer.test.ts
+++ b/src/lib/exporter/frameRenderer.test.ts
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
-import { DEFAULT_WEBCAM_OVERLAY } from "@/components/video-editor/types";
+import { DEFAULT_WEBCAM_OVERLAY } from "../../components/video-editor/types";
vi.mock("pixi.js", () => ({
Application: vi.fn(),
@@ -132,9 +132,13 @@ class FakeVideoElement {
);
}
- load() {}
+ load() {
+ // Intentional no-op for the mock video element.
+ }
- pause() {}
+ pause() {
+ // Intentional no-op for the mock video element.
+ }
private dispatch(name: string) {
const listeners = [...(this.listeners.get(name) ?? [])];
@@ -174,7 +178,7 @@ function createMockCanvas() {
width: 0,
height: 0,
context,
- getContext: vi.fn(() => context),
+ getContext: vi.fn((_type?: string) => context),
};
}
diff --git a/src/lib/exporter/gifExporter.test.ts b/src/lib/exporter/gifExporter.test.ts
index 70701ccf..174112e8 100644
--- a/src/lib/exporter/gifExporter.test.ts
+++ b/src/lib/exporter/gifExporter.test.ts
@@ -1,6 +1,6 @@
import * as fc from "fast-check";
import { describe, expect, it } from "vitest";
-import { calculateOutputDimensions } from "./gifExporter";
+import { calculateOutputDimensions, getGifRepeat } from "./gifExporter";
import { GIF_SIZE_PRESETS, GifSizePreset } from "./types";
/**
@@ -23,8 +23,7 @@ describe("GIF Exporter", () => {
it("should map loop=true to repeat=0 (infinite) and loop=false to repeat=1 (once)", () => {
fc.assert(
fc.property(fc.boolean(), (loopEnabled: boolean) => {
- // This is the logic used in GifExporter constructor
- const repeat = loopEnabled ? 0 : 1;
+ const repeat = getGifRepeat(loopEnabled);
if (loopEnabled) {
// When loop is enabled, repeat should be 0 (infinite loop)
@@ -41,7 +40,7 @@ describe("GIF Exporter", () => {
it("should always produce valid repeat values (0 or 1)", () => {
fc.assert(
fc.property(fc.boolean(), (loopEnabled: boolean) => {
- const repeat = loopEnabled ? 0 : 1;
+ const repeat = getGifRepeat(loopEnabled);
expect([0, 1]).toContain(repeat);
}),
{ numRuns: 100 },
@@ -83,7 +82,7 @@ describe("GIF Exporter", () => {
// Aspect ratio should be preserved within 0.01 tolerance
// (small deviation allowed due to rounding to even numbers)
expect(Math.abs(originalAspectRatio - outputAspectRatio)).toBeLessThan(
- 0.02,
+ 0.01,
);
},
),
diff --git a/src/lib/exporter/gifExporter.ts b/src/lib/exporter/gifExporter.ts
index 49d5a422..789e9dff 100644
--- a/src/lib/exporter/gifExporter.ts
+++ b/src/lib/exporter/gifExporter.ts
@@ -112,6 +112,10 @@ export function calculateOutputDimensions(
};
}
+export function getGifRepeat(loop: boolean): 0 | 1 {
+ return loop ? 0 : 1;
+}
+
export class GifExporter {
private config: GifExporterConfig;
private streamingDecoder: StreamingVideoDecoder | null = null;
@@ -192,7 +196,7 @@ export class GifExporter {
// Initialize GIF encoder
// Loop: 0 = infinite loop, 1 = play once (no loop)
- const repeat = this.config.loop ? 0 : 1;
+ const repeat = getGifRepeat(this.config.loop);
const cores = navigator.hardwareConcurrency || 4;
const WORKER_COUNT = Math.max(1, Math.min(8, cores - 1));
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index b7a79604..05a17054 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -13,7 +13,7 @@ import type {
ZoomTransitionEasing,
} from "@/components/video-editor/types";
import { extensionHost } from "@/lib/extensions";
-import { AudioProcessor } from "./audioEncoder";
+import { AudioProcessor, isAacAudioEncodingSupported } from "./audioEncoder";
import { normalizeLightningRuntimePlatform } from "./backendPolicy";
import {
type ExportBackpressureProfile,
@@ -103,6 +103,8 @@ const NATIVE_EXPORT_ENGINE_NAME = "Breeze";
const LIGHTNING_PIPELINE_NAME = "Lightning (Beta)";
export class ModernVideoExporter {
+ private static readonly NATIVE_ENCODER_QUEUE_LIMIT = 32;
+
private config: VideoExporterConfig;
private streamingDecoder: StreamingVideoDecoder | null = null;
private renderer: ModernFrameRenderer | null = null;
@@ -124,7 +126,7 @@ export class ModernVideoExporter {
private encoderName: string | null = null;
private backpressureProfile: ExportBackpressureProfile | null = null;
private nativeExportSessionId: string | null = null;
- private nativeWritePromises: Promise<void>[] = [];
+ private nativeWritePromises = new Set<Promise<void>>();
private nativeWriteError: Error | null = null;
private maxNativeWriteInFlight = 1;
private lastNativeExportError: string | null = null;
@@ -775,74 +777,124 @@ export class ModernVideoExporter {
return false;
}
+ if (typeof VideoEncoder === "undefined" || typeof VideoEncoder.isConfigSupported !== "function") {
+ this.lastNativeExportError = `${NATIVE_EXPORT_ENGINE_NAME} export requires WebCodecs VideoEncoder support.`;
+ return false;
+ }
+
+ const encoderConfig: VideoEncoderConfig = {
+ codec: "avc1.640034",
+ width: this.config.width,
+ height: this.config.height,
+ bitrate: this.config.bitrate,
+ framerate: this.config.frameRate,
+ hardwareAcceleration: "prefer-hardware",
+ avc: { format: "annexb" },
+ };
+
+ try {
+ const support = await VideoEncoder.isConfigSupported(encoderConfig);
+ if (!support.supported) {
+ this.lastNativeExportError = `H.264 Annex B encoding is not supported at ${this.config.width}x${this.config.height}.`;
+ return false;
+ }
+ } catch (error) {
+ this.lastNativeExportError =
+ error instanceof Error ? error.message : String(error);
+ console.warn(
+ `[VideoExporter] ${NATIVE_EXPORT_ENGINE_NAME} encoder support check failed`,
+ error,
+ );
+ return false;
+ }
+
const result = await window.electronAPI.nativeVideoExportStart({
width: this.config.width,
height: this.config.height,
frameRate: this.config.frameRate,
bitrate: this.config.bitrate,
encodingMode: this.config.encodingMode ?? "balanced",
- inputMode: "h264-stream",
- });
-
- if (!result.success || !result.sessionId) {
- this.lastNativeExportError =
- result.error ||
- `${NATIVE_EXPORT_ENGINE_NAME} export could not be started on this system.`;
- console.warn(
- `[VideoExporter] ${NATIVE_EXPORT_ENGINE_NAME} export unavailable`,
- result.error,
- );
- return false;
- }
-
- this.nativeExportSessionId = result.sessionId;
- this.lastNativeExportError = null;
- this.encodeBackend = "ffmpeg";
- this.encoderName = "h264-stream-copy";
-
- // Initialize browser-side H.264 encoder. Hardware-accelerated where available.
- // Encoded Annex B chunks go over IPC; FFmpeg stream-copies them into MP4.
- const sessionId = result.sessionId;
- const encoder = new VideoEncoder({
- output: (chunk) => {
- if (this.cancelled || !this.nativeExportSessionId) return;
- const buffer = new ArrayBuffer(chunk.byteLength);
- chunk.copyTo(buffer);
- const writePromise = window.electronAPI
- .nativeVideoExportWriteFrame(sessionId, new Uint8Array(buffer))
- .then((writeResult) => {
- if (!writeResult.success && !this.cancelled) {
- this.nativeEncoderError = new Error(
- writeResult.error || "Failed to write H.264 chunk to native encoder",
- );
- }
- })
- .catch((e) => {
- if (!this.cancelled && !this.nativeEncoderError) {
- this.nativeEncoderError = e instanceof Error ? e : new Error(String(e));
- }
- });
- // Track promise so finishNativeVideoExport waits for all IPC writes to land
- // before closing the FFmpeg pipe. encoder.flush() does NOT await async callbacks.
- this.nativeWritePromises.push(writePromise);
- void writePromise.finally(() => {
- this.nativeWritePromises = this.nativeWritePromises.filter((p) => p !== writePromise);
- });
- },
- error: (e) => {
- this.nativeEncoderError = e;
- },
- });
- encoder.configure({
- codec: "avc1.640034",
- width: this.config.width,
- height: this.config.height,
- bitrate: this.config.bitrate,
- framerate: this.config.frameRate,
- hardwareAcceleration: "prefer-hardware",
- avc: { format: "annexb" },
- });
- this.nativeH264Encoder = encoder;
+ inputMode: "h264-stream",
+ });
+
+ if (!result.success || !result.sessionId) {
+ this.lastNativeExportError =
+ result.error ||
+ `${NATIVE_EXPORT_ENGINE_NAME} export could not be started on this system.`;
+ console.warn(
+ `[VideoExporter] ${NATIVE_EXPORT_ENGINE_NAME} export unavailable`,
+ result.error,
+ );
+ return false;
+ }
+
+ this.nativeExportSessionId = result.sessionId;
+ this.lastNativeExportError = null;
+ this.encodeBackend = "ffmpeg";
+ this.encoderName = "h264-stream-copy";
+
+ const sessionId = result.sessionId;
+ const encoder = new VideoEncoder({
+ output: (chunk) => {
+ if (this.cancelled || !this.nativeExportSessionId) {
+ return;
+ }
+
+ const buffer = new ArrayBuffer(chunk.byteLength);
+ chunk.copyTo(buffer);
+ const writePromise = window.electronAPI
+ .nativeVideoExportWriteFrame(sessionId, new Uint8Array(buffer))
+ .then((writeResult) => {
+ if (!writeResult.success && !this.cancelled) {
+ throw new Error(
+ writeResult.error || "Failed to write H.264 chunk to native encoder",
+ );
+ }
+ })
+ .catch((error) => {
+ if (!this.cancelled) {
+ const resolvedError =
+ error instanceof Error ? error : new Error(String(error));
+ if (!this.nativeEncoderError) {
+ this.nativeEncoderError = resolvedError;
+ }
+ if (!this.nativeWriteError) {
+ this.nativeWriteError = resolvedError;
+ }
+ }
+ throw error;
+ });
+
+ this.trackNativeWritePromise(writePromise);
+ },
+ error: (error) => {
+ this.nativeEncoderError = error;
+ },
+ });
+
+ try {
+ encoder.configure(encoderConfig);
+ } catch (error) {
+ this.lastNativeExportError =
+ error instanceof Error ? error.message : String(error);
+ try {
+ encoder.close();
+ } catch (closeError) {
+ console.debug(
+ "[VideoExporter] Ignoring error closing native H.264 encoder after startup failure:",
+ closeError,
+ );
+ }
+ this.nativeExportSessionId = null;
+ await window.electronAPI.nativeVideoExportCancel?.(sessionId);
+ console.warn(
+ `[VideoExporter] ${NATIVE_EXPORT_ENGINE_NAME} encoder configure failed`,
+ error,
+ );
+ return false;
+ }
+
+ this.nativeH264Encoder = encoder;
console.log(`[VideoExporter] ${NATIVE_EXPORT_ENGINE_NAME} session ready (H264-stream)`, {
sessionId: result.sessionId,
@@ -860,7 +912,10 @@ export class ModernVideoExporter {
throw new Error(`${NATIVE_EXPORT_ENGINE_NAME} export session is not active`);
}
if (this.nativeEncoderError) throw this.nativeEncoderError;
- while (this.nativeH264Encoder.encodeQueueSize >= 32) {
+ while (
+ this.nativeH264Encoder.encodeQueueSize >=
+ ModernVideoExporter.NATIVE_ENCODER_QUEUE_LIMIT
+ ) {
await new Promise<void>((r) => setTimeout(r, 2));
if (this.cancelled) return;
if (this.nativeEncoderError) throw this.nativeEncoderError;
@@ -1121,7 +1176,7 @@ export class ModernVideoExporter {
pendingEncodeQueue: this.encodeQueue,
encodeBacklog: this.getCurrentEncodeBacklog(),
peakEncodeQueueSize: this.peakEncodeQueueSize,
- nativeWriteInFlight: this.nativeWritePromises.length,
+ nativeWriteInFlight: this.nativeWritePromises.size,
peakNativeWriteInFlight: this.peakNativeWriteInFlight,
averageFrameCallbackMs: Number(
(this.frameCallbackTimeMs / safeFrameCount).toFixed(3),
@@ -1210,8 +1265,20 @@ export class ModernVideoExporter {
return Math.max(this.encoder?.encodeQueueSize ?? 0, this.encodeQueue);
}
+ private trackNativeWritePromise(writePromise: Promise<void>): void {
+ this.nativeWritePromises.add(writePromise);
+ this.peakNativeWriteInFlight = Math.max(
+ this.peakNativeWriteInFlight,
+ this.nativeWritePromises.size,
+ );
+
+ void writePromise.finally(() => {
+ this.nativeWritePromises.delete(writePromise);
+ });
+ }
+
private async awaitOldestNativeWrite(): Promise<void> {
- const oldestWritePromise = this.nativeWritePromises[0];
+ const oldestWritePromise = this.nativeWritePromises.values().next().value;
if (!oldestWritePromise) {
return;
}
@@ -1224,7 +1291,7 @@ export class ModernVideoExporter {
}
private async awaitPendingNativeWrites(): Promise<void> {
- while (this.nativeWritePromises.length > 0) {
+ while (this.nativeWritePromises.size > 0) {
await this.awaitOldestNativeWrite();
}
@@ -1233,6 +1300,20 @@ export class ModernVideoExporter {
}
}
+ private disposeNativeH264Encoder(): void {
+ if (!this.nativeH264Encoder) {
+ return;
+ }
+
+ try {
+ this.nativeH264Encoder.close();
+ } catch (error) {
+ console.debug("[VideoExporter] Ignoring error closing native H.264 encoder:", error);
+ }
+
+ this.nativeH264Encoder = null;
+ }
+
private getNowMs(): number {
if (typeof performance !== "undefined" && typeof performance.now === "function") {
return performance.now();
@@ -1414,10 +1495,7 @@ export class ModernVideoExporter {
if (this.audioProcessor) {
this.audioProcessor.cancel();
}
- if (this.nativeH264Encoder) {
- try { this.nativeH264Encoder.close(); } catch (_) {}
- this.nativeH264Encoder = null;
- }
+ this.disposeNativeH264Encoder();
const nativeExportSessionId = this.nativeExportSessionId;
this.nativeExportSessionId = null;
@@ -1458,10 +1536,7 @@ export class ModernVideoExporter {
this.muxer = null;
this.audioProcessor?.cancel();
this.audioProcessor = null;
- if (this.nativeH264Encoder) {
- try { this.nativeH264Encoder.close(); } catch (_) {}
- this.nativeH264Encoder = null;
- }
+ this.disposeNativeH264Encoder();
const nativeExportSessionId = this.nativeExportSessionId;
this.nativeExportSessionId = null;
if (nativeExportSessionId && typeof window !== "undefined") {
@@ -1490,7 +1565,7 @@ export class ModernVideoExporter {
this.processedFrameCount = 0;
this.lastProgressSampleTimeMs = 0;
this.lastProgressSampleFrame = 0;
- this.nativeWritePromises = [];
+ this.nativeWritePromises = new Set();
this.nativeWriteError = null;
this.maxNativeWriteInFlight = 1;
this.videoDescription = undefined;
diff --git a/src/lib/exporter/mp4Support.ts b/src/lib/exporter/mp4Support.ts
index 9eaa842c..e1f9a384 100644
--- a/src/lib/exporter/mp4Support.ts
+++ b/src/lib/exporter/mp4Support.ts
@@ -167,16 +167,20 @@ export async function probeSupportedMp4Dimensions(
const codec = options.codec ?? DEFAULT_MP4_CODEC;
const normalizedWidth = normalizeEvenDimension(options.width);
const normalizedHeight = normalizeEvenDimension(options.height);
- const dimensionCacheKey = [codec, normalizedWidth, normalizedHeight, options.frameRate].join(
- ":",
- );
+ const requestedBitrate = options.getBitrate(normalizedWidth, normalizedHeight);
+ const dimensionCacheKey = [
+ codec,
+ normalizedWidth,
+ normalizedHeight,
+ options.frameRate,
+ requestedBitrate,
+ ].join(":");
const cachedResult = supportedDimensionCache.get(dimensionCacheKey);
if (cachedResult) {
return cachedResult;
}
- const requestedBitrate = options.getBitrate(normalizedWidth, normalizedHeight);
const directPath = await resolveSupportedMp4EncoderPath({
width: normalizedWidth,
height: normalizedHeight,
diff --git a/src/lib/exporter/streamingDecoder.ts b/src/lib/exporter/streamingDecoder.ts
index 2eeb3f66..cfc1a9d9 100644
--- a/src/lib/exporter/streamingDecoder.ts
+++ b/src/lib/exporter/streamingDecoder.ts
@@ -156,6 +156,26 @@ export class StreamingVideoDecoder {
}
async loadMetadata(videoUrl: string): Promise<DecodedVideoInfo> {
+ if (this.decoder) {
+ try {
+ if (this.decoder.state === "configured") {
+ this.decoder.close();
+ }
+ } catch {
+ // Ignore cleanup errors while reloading metadata.
+ }
+ this.decoder = null;
+ }
+
+ if (this.demuxer) {
+ try {
+ this.demuxer.destroy();
+ } catch {
+ // Ignore cleanup errors while reloading metadata.
+ }
+ this.demuxer = null;
+ }
+
const resourceUrl = this.resolveVideoResourceUrl(videoUrl);
// Relative URL so it resolves correctly in both dev (http) and packaged (file://) builds
@@ -576,7 +596,7 @@ export class StreamingVideoDecoder {
if (cursor < trimStart) {
segments.push({ startSec: cursor, endSec: trimStart });
}
- cursor = trimEnd;
+ cursor = Math.max(cursor, trimEnd);
}
if (cursor < totalDuration) {
@@ -624,9 +644,14 @@ export class StreamingVideoDecoder {
for (const sr of overlapping) {
const srStart = Math.max(sr.startMs / 1000, segment.startSec);
const srEnd = Math.min(sr.endMs / 1000, segment.endSec);
- if (cursor < srStart) result.push({ startSec: cursor, endSec: srStart, speed: 1 });
- result.push({ startSec: srStart, endSec: srEnd, speed: sr.speed });
- cursor = srEnd;
+ if (cursor < srStart) {
+ result.push({ startSec: cursor, endSec: srStart, speed: 1 });
+ }
+ const effectiveStart = Math.max(cursor, srStart);
+ if (srEnd > effectiveStart) {
+ result.push({ startSec: effectiveStart, endSec: srEnd, speed: sr.speed });
+ }
+ cursor = Math.max(cursor, srEnd);
}
if (cursor < segment.endSec)
result.push({ startSec: cursor, endSec: segment.endSec, speed: 1 });
diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts
index 86ec0a30..2ba64712 100644
--- a/src/lib/exporter/videoExporter.ts
+++ b/src/lib/exporter/videoExporter.ts
@@ -102,6 +102,7 @@ export class VideoExporter {
private encoderError: Error | null = null;
private nativeExportSessionId: string | null = null;
private nativeH264Encoder: VideoEncoder | null = null;
+ private nativePendingWrite: Promise<void> = Promise.resolve();
private nativeEncoderError: Error | null = null;
constructor(config: VideoExporterConfig) {
@@ -114,6 +115,7 @@ export class VideoExporter {
this.cancelled = false;
this.encoderError = null;
this.nativeEncoderError = null;
+ this.nativePendingWrite = Promise.resolve();
this.exportStartTimeMs = this.getNowMs();
this.progressSampleStartTimeMs = this.exportStartTimeMs;
this.progressSampleStartFrame = 0;
@@ -258,6 +260,10 @@ export class VideoExporter {
if (useNativeEncoder && nativeAudioPlan) {
if (this.nativeH264Encoder) {
await this.nativeH264Encoder.flush();
+ await this.nativePendingWrite;
+ if (this.nativeEncoderError) {
+ throw this.nativeEncoderError;
+ }
this.nativeH264Encoder.close();
this.nativeH264Encoder = null;
}
@@ -335,8 +341,15 @@ export class VideoExporter {
}
private shouldUseExperimentalNativeExport(): boolean {
- return typeof window !== "undefined" &&
- typeof window.electronAPI?.nativeVideoExportStart === "function";
+ return (
+ typeof window !== "undefined" &&
+ typeof VideoEncoder !== "undefined" &&
+ typeof VideoEncoder.isConfigSupported === "function" &&
+ typeof window.electronAPI?.nativeVideoExportStart === "function" &&
+ typeof window.electronAPI?.nativeVideoExportWriteFrame === "function" &&
+ typeof window.electronAPI?.nativeVideoExportFinish === "function" &&
+ typeof window.electronAPI?.nativeVideoExportCancel === "function"
+ );
}
private async awaitWithFinalizationTimeout<T>(promise: Promise<T>, stage: string): Promise<T> {
@@ -478,7 +491,7 @@ export class VideoExporter {
}
private async tryStartNativeVideoExport(): Promise<boolean> {
- if (typeof window === "undefined" || !window.electronAPI?.nativeVideoExportStart) {
+ if (!this.shouldUseExperimentalNativeExport()) {
return false;
}
@@ -489,6 +502,29 @@ export class VideoExporter {
return false;
}
+ const encoderConfig: VideoEncoderConfig = {
+ codec: "avc1.640034",
+ width: this.config.width,
+ height: this.config.height,
+ bitrate: this.config.bitrate,
+ framerate: this.config.frameRate,
+ hardwareAcceleration: "prefer-hardware",
+ avc: { format: "annexb" },
+ };
+
+ try {
+ const support = await VideoEncoder.isConfigSupported(encoderConfig);
+ if (!support.supported) {
+ console.warn(
+ `[VideoExporter] Native H.264 Annex B encoding is unsupported at ${this.config.width}x${this.config.height}`,
+ );
+ return false;
+ }
+ } catch (error) {
+ console.warn("[VideoExporter] Native encoder support check failed:", error);
+ return false;
+ }
+
const result = await window.electronAPI.nativeVideoExportStart({
width: this.config.width,
height: this.config.height,
@@ -504,38 +540,58 @@ export class VideoExporter {
}
this.nativeExportSessionId = result.sessionId;
+ this.nativePendingWrite = Promise.resolve();
// Initialize the browser-side H.264 encoder (hardware-accelerated where available).
// Encoded Annex B chunks are sent over IPC and FFmpeg stream-copies them into MP4.
const sessionId = result.sessionId;
const encoder = new VideoEncoder({
- output: async (chunk) => {
+ output: (chunk) => {
if (this.cancelled || !this.nativeExportSessionId) return;
const buffer = new ArrayBuffer(chunk.byteLength);
chunk.copyTo(buffer);
- const writeResult = await window.electronAPI.nativeVideoExportWriteFrame(
- sessionId,
- new Uint8Array(buffer),
- );
- if (!writeResult.success && !this.cancelled) {
- this.nativeEncoderError = new Error(
- writeResult.error || "Failed to write H.264 chunk to native encoder",
- );
- }
+ this.nativePendingWrite = this.nativePendingWrite
+ .then(async () => {
+ const writeResult = await window.electronAPI.nativeVideoExportWriteFrame(
+ sessionId,
+ new Uint8Array(buffer),
+ );
+ if (!writeResult.success && !this.cancelled) {
+ throw new Error(
+ writeResult.error || "Failed to write H.264 chunk to native encoder",
+ );
+ }
+ })
+ .catch((error) => {
+ if (!this.cancelled && !this.nativeEncoderError) {
+ this.nativeEncoderError =
+ error instanceof Error ? error : new Error(String(error));
+ }
+ });
},
error: (e) => {
this.nativeEncoderError = e;
},
});
- encoder.configure({
- codec: "avc1.640034",
- width: this.config.width,
- height: this.config.height,
- bitrate: this.config.bitrate,
- framerate: this.config.frameRate,
- hardwareAcceleration: "prefer-hardware",
- avc: { format: "annexb" },
- });
+
+ try {
+ encoder.configure(encoderConfig);
+ } catch (error) {
+ this.nativeEncoderError =
+ error instanceof Error ? error : new Error(String(error));
+ try {
+ encoder.close();
+ } catch (closeError) {
+ console.debug(
+ "[VideoExporter] Ignoring error closing native encoder after startup failure:",
+ closeError,
+ );
+ }
+ this.nativeExportSessionId = null;
+ await window.electronAPI.nativeVideoExportCancel(sessionId);
+ return false;
+ }
+
this.nativeH264Encoder = encoder;
return true;
}
@@ -986,6 +1042,7 @@ export class VideoExporter {
this.audioProcessor = null;
this.encodeQueue = 0;
this.pendingMuxing = Promise.resolve();
+ this.nativePendingWrite = Promise.resolve();
this.chunkCount = 0;
this.encoderError = null;
this.videoDescription = undefined;