Skip to content

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

Stabilize Lightning export frame pacing

webadderall8187c715fc1f
diff --git a/src/lib/exporter/modernFrameRenderer.ts b/src/lib/exporter/modernFrameRenderer.ts
index 11342a83..82d9b530 100644
--- a/src/lib/exporter/modernFrameRenderer.ts
+++ b/src/lib/exporter/modernFrameRenderer.ts
@@ -749,11 +749,20 @@ export class FrameRenderer {
layer.container.visible = true;
}
- private shouldUseStartupVideoFrameStaging(): boolean {
- return (
- this.rendererBackend === "webgpu" &&
- this.currentVideoTime < VIDEO_FRAME_STARTUP_STAGING_WINDOW_SEC
- );
+ private shouldUseVideoFrameStaging(kind: "scene" | "background" | "webcam"): boolean {
+ if (this.rendererBackend !== "webgpu") {
+ return false;
+ }
+
+ // Decoder-owned VideoFrames can be closed immediately after renderFrame()
+ // returns. WebGPU texture uploads are not reliably complete by then, so
+ // stage export frames through a stable canvas source instead of letting
+ // Pixi hold the ephemeral VideoFrame directly.
+ if (kind === "scene") {
+ return true;
+ }
+
+ return this.currentVideoTime < VIDEO_FRAME_STARTUP_STAGING_WINDOW_SEC;
}
private ensureVideoFrameStagingCanvas(
@@ -815,7 +824,7 @@ export class FrameRenderer {
fallbackWidth: number,
fallbackHeight: number,
): CanvasImageSource | VideoFrame {
- if (!this.shouldUseStartupVideoFrameStaging()) {
+ if (!this.shouldUseVideoFrameStaging(kind)) {
return frame;
}
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index ab12aa01..64405cd6 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -191,6 +191,7 @@ export class ModernVideoExporter {
private lastFinalizationAudioProgress = INITIAL_FINALIZATION_PROGRESS_STATE.lastAudioProgress;
private lastProgressSampleTimeMs = 0;
private lastProgressSampleFrame = 0;
+ private displayedRenderFps = 0;
constructor(config: VideoExporterConfig) {
this.config = config;
@@ -404,6 +405,7 @@ export class ModernVideoExporter {
this.lastThroughputLogTimeMs = this.exportStartTimeMs;
this.lastProgressSampleTimeMs = this.exportStartTimeMs;
this.lastProgressSampleFrame = 0;
+ this.displayedRenderFps = 0;
const decodeLoopStartedAt = this.getNowMs();
await this.streamingDecoder.decodeAll(
@@ -1445,6 +1447,14 @@ export class ModernVideoExporter {
const sampleElapsedMs = Math.max(nowMs - this.lastProgressSampleTimeMs, 1);
const sampleFrameDelta = Math.max(currentFrame - this.lastProgressSampleFrame, 0);
const sampleRenderFps = (sampleFrameDelta * 1000) / sampleElapsedMs;
+ if (sampleElapsedMs >= 500 || currentFrame === totalFrames) {
+ this.displayedRenderFps =
+ this.displayedRenderFps > 0
+ ? this.displayedRenderFps * 0.35 + sampleRenderFps * 0.65
+ : sampleRenderFps;
+ } else if (this.displayedRenderFps <= 0) {
+ this.displayedRenderFps = averageRenderFps;
+ }
const remainingFrames = Math.max(totalFrames - currentFrame, 0);
const estimatedTimeRemaining =
averageRenderFps > 0 ? remainingFrames / averageRenderFps : 0;
@@ -1510,7 +1520,7 @@ export class ModernVideoExporter {
totalFrames,
percentage,
estimatedTimeRemaining,
- renderFps: sampleRenderFps,
+ renderFps: this.displayedRenderFps,
renderBackend: this.renderBackend ?? undefined,
encodeBackend: this.encodeBackend ?? undefined,
encoderName: this.encoderName ?? undefined,
@@ -1896,6 +1906,7 @@ export class ModernVideoExporter {
this.lastFinalizationAudioProgress = INITIAL_FINALIZATION_PROGRESS_STATE.lastAudioProgress;
this.lastProgressSampleTimeMs = 0;
this.lastProgressSampleFrame = 0;
+ this.displayedRenderFps = 0;
this.nativeWritePromises = new Set();
this.nativeWriteError = null;
this.pendingNativeWriteChunks = [];