Skip to content

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

fix(export): preserve container duration on large stream mismatch

wiiiii12310d78335411f
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index aabbbea0..750edaac 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -14,6 +14,7 @@ import type {
ZoomTransitionEasing,
} from "@/components/video-editor/types";
import { extensionHost } from "@/lib/extensions";
+import { getEffectiveVideoStreamDurationSeconds } from "@/lib/mediaTiming";
import { AudioProcessor, isAacAudioEncodingSupported } from "./audioEncoder";
import { normalizeLightningRuntimePlatform } from "./backendPolicy";
import { buildEditedTrackSourceSegments, classifyEditedTrackStrategy } from "./editedTrackStrategy";
@@ -787,7 +788,12 @@ export class ModernVideoExporter {
) {
const sourceDurationMs = Math.max(
0,
- Math.round((videoInfo.streamDuration ?? videoInfo.duration) * 1000),
+ Math.round(
+ getEffectiveVideoStreamDurationSeconds({
+ duration: videoInfo.duration,
+ streamDuration: videoInfo.streamDuration,
+ }) * 1000,
+ ),
);
const trimRegions = this.config.trimRegions ?? [];
const strategy =
@@ -846,7 +852,12 @@ export class ModernVideoExporter {
if ((this.config.trimRegions ?? []).length > 0) {
const sourceDurationMs = Math.max(
0,
- Math.round((videoInfo.streamDuration ?? videoInfo.duration) * 1000),
+ Math.round(
+ getEffectiveVideoStreamDurationSeconds({
+ duration: videoInfo.duration,
+ streamDuration: videoInfo.streamDuration,
+ }) * 1000,
+ ),
);
const trimSegments = this.buildNativeTrimSegments(sourceDurationMs);
if (trimSegments.length === 0) {
diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts
index 47926eb7..1e459181 100644
--- a/src/lib/exporter/videoExporter.ts
+++ b/src/lib/exporter/videoExporter.ts
@@ -13,6 +13,7 @@ import type {
ZoomRegion,
ZoomTransitionEasing,
} from "@/components/video-editor/types";
+import { getEffectiveVideoStreamDurationSeconds } from "@/lib/mediaTiming";
import { AudioProcessor, isAacAudioEncodingSupported } from "./audioEncoder";
import { buildEditedTrackSourceSegments, classifyEditedTrackStrategy } from "./editedTrackStrategy";
import {
@@ -544,7 +545,12 @@ export class VideoExporter {
) {
const sourceDurationMs = Math.max(
0,
- Math.round((videoInfo.streamDuration ?? videoInfo.duration) * 1000),
+ Math.round(
+ getEffectiveVideoStreamDurationSeconds({
+ duration: videoInfo.duration,
+ streamDuration: videoInfo.streamDuration,
+ }) * 1000,
+ ),
);
const trimRegions = this.config.trimRegions ?? [];
const strategy =
@@ -603,7 +609,12 @@ export class VideoExporter {
if ((this.config.trimRegions ?? []).length > 0) {
const sourceDurationMs = Math.max(
0,
- Math.round((videoInfo.streamDuration ?? videoInfo.duration) * 1000),
+ Math.round(
+ getEffectiveVideoStreamDurationSeconds({
+ duration: videoInfo.duration,
+ streamDuration: videoInfo.streamDuration,
+ }) * 1000,
+ ),
);
const trimSegments = this.buildNativeTrimSegments(sourceDurationMs);
if (trimSegments.length === 0) {
diff --git a/src/lib/mediaTiming.test.ts b/src/lib/mediaTiming.test.ts
index a6f20479..28b865bf 100644
--- a/src/lib/mediaTiming.test.ts
+++ b/src/lib/mediaTiming.test.ts
@@ -111,6 +111,15 @@ describe("getEffectiveVideoStreamDurationSeconds", () => {
).toBe(11.2);
});
+ it("uses the container duration when the video stream is much shorter", () => {
+ expect(
+ getEffectiveVideoStreamDurationSeconds({
+ duration: 60,
+ streamDuration: 40,
+ }),
+ ).toBe(60);
+ });
+
it("falls back to the container duration when stream duration is missing", () => {
expect(
getEffectiveVideoStreamDurationSeconds({
diff --git a/src/lib/mediaTiming.ts b/src/lib/mediaTiming.ts
index d9dcf673..1e8a840c 100644
--- a/src/lib/mediaTiming.ts
+++ b/src/lib/mediaTiming.ts
@@ -81,12 +81,29 @@ export function getEffectiveVideoStreamDurationSeconds({
duration?: number | null;
streamDuration?: number | null;
}): number {
- if (Number.isFinite(streamDuration) && (streamDuration ?? 0) > 0) {
- return Math.max(0, streamDuration ?? 0);
+ const safeDuration =
+ Number.isFinite(duration) && (duration ?? 0) > 0 ? Math.max(0, duration ?? 0) : 0;
+ const safeStreamDuration =
+ Number.isFinite(streamDuration) && (streamDuration ?? 0) > 0
+ ? Math.max(0, streamDuration ?? 0)
+ : 0;
+
+ if (safeDuration > 0 && safeStreamDuration > 0) {
+ const gapSeconds = safeDuration - safeStreamDuration;
+ const largeMismatchThresholdSeconds = Math.max(2, safeDuration * 0.1);
+ if (gapSeconds > largeMismatchThresholdSeconds) {
+ return safeDuration;
+ }
+
+ return safeStreamDuration;
+ }
+
+ if (safeStreamDuration > 0) {
+ return safeStreamDuration;
}
- if (Number.isFinite(duration) && (duration ?? 0) > 0) {
- return Math.max(0, duration ?? 0);
+ if (safeDuration > 0) {
+ return safeDuration;
}
return 0;