Skip to content

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

Refine clip timing and recover safely from preview and export failures

webadderall33cf184249fe
diff --git a/src/components/video-editor/SettingsPanel.tsx b/src/components/video-editor/SettingsPanel.tsx
index 586e09d2..8b289e3f 100644
--- a/src/components/video-editor/SettingsPanel.tsx
+++ b/src/components/video-editor/SettingsPanel.tsx
@@ -91,6 +91,7 @@ import {
} from "./types";
import { fromCursorSwaySliderValue, toCursorSwaySliderValue } from "./videoPlayback/cursorSway";
import { isZeroPadding } from "./videoPlayback/layoutUtils";
+import { supportsPreviewPlaybackRate } from "./videoPlayback/playbackRate";
import {
cursorSetAssets,
getCursorStyleSizeMultiplier,
@@ -3057,6 +3058,15 @@ export function SettingsPanel({
key={option.speed}
type="button"
onClick={() => onClipSpeedChange?.(option.speed)}
+ disabled={!supportsPreviewPlaybackRate(option.speed)}
+ title={
+ !supportsPreviewPlaybackRate(option.speed)
+ ? tSettings(
+ "speed.unsupported",
+ "Not supported for preview on this device",
+ )
+ : undefined
+ }
className={cn(
"h-auto w-full rounded-lg border px-0.5 py-2 text-center shadow-sm transition-all duration-200 ease-out cursor-pointer",
isActive
diff --git a/src/components/video-editor/VideoEditor.tsx b/src/components/video-editor/VideoEditor.tsx
index e3f98e1a..6d22666a 100644
--- a/src/components/video-editor/VideoEditor.tsx
+++ b/src/components/video-editor/VideoEditor.tsx
@@ -94,7 +94,11 @@ export default function VideoEditor() {
const effectiveShowCursor = sessionShowCursorOverride ?? showCursor;
const headerLeftControlsPaddingClass = appPlatform === "darwin" ? "pl-[76px]" : "";
const { cursorTelemetrySourcePath, autoCaptions, autoCaptionSettings } = timeline;
- const exportSettings = useExportSettings(initialEditorPreferences, autoCaptions);
+ const exportSettings = useExportSettings(
+ initialEditorPreferences,
+ autoCaptions,
+ timeline.clipRegions,
+ );
const {
includeCaptionSidecar,
mp4FrameRate,
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index 33b1c679..afdc56e7 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -91,7 +91,7 @@ import {
isAnnotationActiveAtTime,
shouldClearSelectedAnnotation,
} from "./videoPlayback/annotationVisibility";
-import { createClipPlayback } from "./videoPlayback/clipPlayback";
+import { createClipPlayback, findPreviewClipAtTimelineTime } from "./videoPlayback/clipPlayback";
import { DEFAULT_FOCUS } from "./videoPlayback/constants";
import {
type CursorFollowCameraState,
@@ -113,6 +113,7 @@ import {
stepSpringValue,
} from "./videoPlayback/motionSmoothing";
import { updateOverlayIndicator } from "./videoPlayback/overlayUtils";
+import { supportsPreviewPlaybackRate } from "./videoPlayback/playbackRate";
import { PreviewVideoSource } from "./videoPlayback/previewVideoSource";
import { getSceneEffectMetrics } from "./videoPlayback/sceneEffects";
import {
@@ -446,7 +447,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
null,
);
const currentTime = mapTimelineTimeToSourceTime(timelineTime * 1000, clipRegions) / 1000;
- const isGap = !findClipAtTimelineTime(timelineTime * 1000, clipRegions);
+ const isGap = !findPreviewClipAtTimelineTime(timelineTime * 1000, clipRegions);
const clipRegionsRef = useRef(clipRegions);
const clipPlaybackRef = useRef<ReturnType<typeof createClipPlayback> | null>(null);
const onPlaybackErrorRef = useRef(onError);
@@ -1717,6 +1718,10 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
const targetPlaybackRate =
findClipAtTimelineTime(timelineTime * 1000, clipRegions)?.speed ?? 1;
+ if (!supportsPreviewPlaybackRate(targetPlaybackRate)) {
+ webcamVideo.pause();
+ return;
+ }
enablePitchPreservingPlayback(webcamVideo);
if (Math.abs(webcamVideo.playbackRate - targetPlaybackRate) > 0.001) {
webcamVideo.playbackRate = targetPlaybackRate;
diff --git a/src/components/video-editor/audio/useAudioPreviewSync.ts b/src/components/video-editor/audio/useAudioPreviewSync.ts
index c1e3a259..78c1b02c 100644
--- a/src/components/video-editor/audio/useAudioPreviewSync.ts
+++ b/src/components/video-editor/audio/useAudioPreviewSync.ts
@@ -9,6 +9,7 @@ import {
resolvePreviewMediaDuration,
} from "@/lib/mediaTiming";
import type { AudioRegion } from "../types";
+import { supportsPreviewPlaybackRate } from "../videoPlayback/playbackRate";
import {
getAudioResourceVersionKey,
getVersionedAudioResourceUrl,
@@ -395,6 +396,10 @@ export function useAudioPreviewSync({
}
for (const audio of sourceAudioElementsRef.current.values()) {
+ if (!supportsPreviewPlaybackRate(sourcePlaybackRate)) {
+ audio.pause();
+ continue;
+ }
const sourceAudioPath = audio.dataset.sourceAudioPath ?? "";
audio.volume = Math.max(
0,
diff --git a/src/components/video-editor/captionTimeline.test.ts b/src/components/video-editor/captionTimeline.test.ts
new file mode 100644
index 00000000..6b77a4d2
--- /dev/null
+++ b/src/components/video-editor/captionTimeline.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it } from "vitest";
+import { captionSpanToSource, projectCaptionCues, retimeCaptionFragment } from "./captionTimeline";
+
+const clips = [
+ { id: "a", startMs: 0, endMs: 1000, sourceStartMs: 0, speed: 3 },
+ { id: "b", startMs: 2000, endMs: 3000, sourceStartMs: 6000, speed: 3 },
+];
+describe("caption timeline projection", () => {
+ it("omits deleted captions and splits crossing cues around gaps with distinct identities", () => {
+ const fragments = projectCaptionCues(
+ [
+ { id: "deleted", startMs: 3100, endMs: 5900, text: "gone" },
+ { id: "crossing", startMs: 1500, endMs: 7500, text: "kept" },
+ ],
+ clips,
+ );
+ expect(
+ fragments.map(({ sourceCueId, startMs, endMs }) => ({ sourceCueId, startMs, endMs })),
+ ).toEqual([
+ { sourceCueId: "crossing", startMs: 500, endMs: 1000 },
+ { sourceCueId: "crossing", startMs: 2000, endMs: 2500 },
+ ]);
+ expect(new Set(fragments.map(({ id }) => id)).size).toBe(2);
+ });
+ it("projects reused footage at every timeline position", () => {
+ const cue = { id: "cue", startMs: 0, endMs: 1500, text: "hello" };
+ const fragments = projectCaptionCues([cue], [clips[0], { ...clips[1], sourceStartMs: 0 }]);
+ expect(fragments.map(({ startMs, endMs }) => [startMs, endMs])).toEqual([
+ [0, 500],
+ [2000, 2500],
+ ]);
+ expect(projectCaptionCues([cue], [])).toEqual([]);
+ });
+ it("retimes within the selected clip even at its cut boundary", () => {
+ expect(captionSpanToSource(clips[0], { start: 500, end: 1000 })).toEqual({
+ startMs: 1500,
+ endMs: 3000,
+ });
+ expect(captionSpanToSource(clips[1], { start: 2000, end: 4000 })).toEqual({
+ startMs: 6000,
+ endMs: 9000,
+ });
+ });
+ it("preserves the other fragment when resizing one edge of a crossing cue", () => {
+ const fragments = projectCaptionCues(
+ [{ id: "cue", startMs: 1500, endMs: 7500, text: "hello" }],
+ clips,
+ );
+ expect(retimeCaptionFragment(fragments[0], { start: 600, end: 1000 })).toEqual({
+ startMs: 1800,
+ endMs: 7500,
+ });
+ expect(retimeCaptionFragment(fragments[1], { start: 2000, end: 2600 })).toEqual({
+ startMs: 1500,
+ endMs: 7800,
+ });
+ });
+});
diff --git a/src/components/video-editor/captionTimeline.ts b/src/components/video-editor/captionTimeline.ts
new file mode 100644
index 00000000..3ba237f2
--- /dev/null
+++ b/src/components/video-editor/captionTimeline.ts
@@ -0,0 +1,51 @@
+import {
+ type CaptionCue,
+ type ClipRegion,
+ getClipSourceEndMs,
+ getClipSourceStartMs,
+ sortClipRegions,
+} from "./types";
+
+export function captionSpanToSource(clip: ClipRegion, span: { start: number; end: number }) {
+ const sourceTime = (time: number) =>
+ Math.round(
+ getClipSourceStartMs(clip) +
+ (Math.max(clip.startMs, Math.min(clip.endMs, time)) - clip.startMs) * clip.speed,
+ );
+ return { startMs: sourceTime(span.start), endMs: sourceTime(span.end) };
+}
+
+export function projectCaptionCues(cues: CaptionCue[], clips: ClipRegion[]) {
+ return sortClipRegions(clips).flatMap((clip) => {
+ const sourceStart = getClipSourceStartMs(clip);
+ const sourceEnd = getClipSourceEndMs(clip);
+ return cues.flatMap((cue) => {
+ const start = Math.max(cue.startMs, sourceStart);
+ const end = Math.min(cue.endMs, sourceEnd);
+ if (start >= end) return [];
+ return [
+ {
+ ...cue,
+ id: JSON.stringify([cue.id, clip.id]),
+ sourceCueId: cue.id,
+ sourceCue: cue,
+ clip,
+ startMs: clip.startMs + (start - sourceStart) / clip.speed,
+ endMs: clip.startMs + (end - sourceStart) / clip.speed,
+ },
+ ];
+ });
+ });
+}
+
+export function retimeCaptionFragment(
+ fragment: ReturnType<typeof projectCaptionCues>[number],
+ span: { start: number; end: number },
+) {
+ const mapped = captionSpanToSource(fragment.clip, span);
+ // An unchanged clipped edge must not truncate the rest of the source cue.
+ return {
+ startMs: span.start === fragment.startMs ? fragment.sourceCue.startMs : mapped.startMs,
+ endMs: span.end === fragment.endMs ? fragment.sourceCue.endMs : mapped.endMs,
+ };
+}
diff --git a/src/components/video-editor/export/useExportSettings.ts b/src/components/video-editor/export/useExportSettings.ts
index 7cd88330..1f3384d6 100644
--- a/src/components/video-editor/export/useExportSettings.ts
+++ b/src/components/video-editor/export/useExportSettings.ts
@@ -9,12 +9,17 @@ import type {
GifFrameRate,
GifSizePreset,
} from "@/lib/exporter";
+import { projectCaptionCues } from "../captionTimeline";
import type { EditorPreferences } from "../editorPreferences";
-import type { CaptionCue } from "../types";
+import type { CaptionCue, ClipRegion } from "../types";
const DEFAULT_MP4_EXPORT_FRAME_RATE: ExportMp4FrameRate = 30;
-export function useExportSettings(preferences: EditorPreferences, autoCaptions: CaptionCue[]) {
+export function useExportSettings(
+ preferences: EditorPreferences,
+ autoCaptions: CaptionCue[],
+ clips: ClipRegion[],
+) {
const [includeCaptionSidecar, setIncludeCaptionSidecar] = useState(false);
const [exportQuality, setExportQuality] = useState<ExportQuality>(preferences.exportQuality);
const [exportEncodingMode, setExportEncodingMode] = useState<ExportEncodingMode>(
@@ -35,7 +40,7 @@ export function useExportSettings(preferences: EditorPreferences, autoCaptions:
const [gifSizePreset, setGifSizePreset] = useState<GifSizePreset>(preferences.gifSizePreset);
const captionSidecarCues = useMemo(
() =>
- autoCaptions
+ projectCaptionCues(autoCaptions, clips)
.filter(
(cue) =>
Number.isFinite(cue.startMs) &&
@@ -45,7 +50,7 @@ export function useExportSettings(preferences: EditorPreferences, autoCaptions:
cue.text.trim().length > 0,
)
.map(({ startMs, endMs, text }) => ({ startMs, endMs, text })),
- [autoCaptions],
+ [autoCaptions, clips],
);
return {
diff --git a/src/components/video-editor/hooks/useCaptionCommands.ts b/src/components/video-editor/hooks/useCaptionCommands.ts
index d541cbcb..5326070d 100644
--- a/src/components/video-editor/hooks/useCaptionCommands.ts
+++ b/src/components/video-editor/hooks/useCaptionCommands.ts
@@ -15,10 +15,18 @@ import {
retimeCue,
splitCue,
} from "../captionOps";
-import type { AutoCaptionSettings, CaptionCue, EditorEffectSection } from "../types";
+import { captionSpanToSource } from "../captionTimeline";
+import {
+ type AutoCaptionSettings,
+ type CaptionCue,
+ type ClipRegion,
+ type EditorEffectSection,
+ findClipAtTimelineTime,
+} from "../types";
import type { VideoPlaybackRef } from "../VideoPlayback";
interface UseCaptionCommandsParams {
+ clipRegions: ClipRegion[];
autoCaptions: CaptionCue[];
setAutoCaptions: Dispatch<SetStateAction<CaptionCue[]>>;
setAutoCaptionSettings: Dispatch<SetStateAction<AutoCaptionSettings>>;
@@ -30,11 +38,11 @@ interface UseCaptionCommandsParams {
setActiveEffectSection: Dispatch<SetStateAction<EditorEffectSection>>;
videoPlaybackRef: RefObject<VideoPlaybackRef>;
mapSourceTimeToTimelineTime: (timeMs: number) => number;
- mapTimelineTimeToSourceTime: (timeMs: number) => number;
handleSeek: (time: number, options?: { pause?: boolean }) => void;
}
export function useCaptionCommands({
+ clipRegions,
autoCaptions,
setAutoCaptions,
setAutoCaptionSettings,
@@ -46,7 +54,6 @@ export function useCaptionCommands({
setActiveEffectSection,
videoPlaybackRef,
mapSourceTimeToTimelineTime,
- mapTimelineTimeToSourceTime,
handleSeek,
}: UseCaptionCommandsParams) {
const handleSelectCaption = useCallback(
@@ -157,11 +164,10 @@ export function useCaptionCommands({
const handleCaptionAdded = useCallback(
(span: Span) => {
+ const clip = findClipAtTimelineTime(span.start, clipRegions);
+ if (!clip) return;
cancelEdit();
- const newCue = createCaptionCue({
- startMs: mapTimelineTimeToSourceTime(span.start),
- endMs: mapTimelineTimeToSourceTime(span.end),
- });
+ const newCue = createCaptionCue(captionSpanToSource(clip, span));
setAutoCaptions((captions) => addCue(captions, newCue));
setSelectedCaptionId(newCue.id);
setActiveEffectSection("caption");
@@ -174,7 +180,7 @@ export function useCaptionCommands({
[
cancelEdit,
handleSeek,
- mapTimelineTimeToSourceTime,
+ clipRegions,
setActiveEffectSection,
setAutoCaptions,
setSelectedAnnotationId,
diff --git a/src/components/video-editor/hooks/useClipRegionCommands.ts b/src/components/video-editor/hooks/useClipRegionCommands.ts
index 3470aa07..f605789e 100644
--- a/src/components/video-editor/hooks/useClipRegionCommands.ts
+++ b/src/components/video-editor/hooks/useClipRegionCommands.ts
@@ -1,10 +1,11 @@
import type { Span } from "dnd-timeline";
import { type Dispatch, type MutableRefObject, type SetStateAction, useCallback } from "react";
import { toast } from "sonner";
-import { planClipSpeedChange } from "../clipSpeedChange";
import { changeClipSpan } from "../clipSpanChange";
+import { planClipSpeedChange } from "../clipSpeedChange";
import { planClipSplit } from "../clipSplit";
import type { ClipRegion, EditorEffectSection, ZoomRegion } from "../types";
+import { supportsPreviewPlaybackRate } from "../videoPlayback/playbackRate";
type Translator = (
key: string,
@@ -123,6 +124,15 @@ export function useClipRegionCommands({
const handleClipSpeedChange = useCallback(
(speed: number) => {
if (!selectedClipId || !Number.isFinite(speed) || speed <= 0) return;
+ if (!supportsPreviewPlaybackRate(speed)) {
+ toast.error(
+ t(
+ "editor.timeline.unsupportedSpeed",
+ "This speed is not supported for preview on this device.",
+ ),
+ );
+ return;
+ }
const plan = planClipSpeedChange({ clipRegions, zoomRegions, selectedClipId, speed });
if (!plan) return;
if ("blockedReason" in plan) {
diff --git a/src/components/video-editor/hooks/useTimelineEditingController.ts b/src/components/video-editor/hooks/useTimelineEditingController.ts
index cc42f864..083340f9 100644
--- a/src/components/video-editor/hooks/useTimelineEditingController.ts
+++ b/src/components/video-editor/hooks/useTimelineEditingController.ts
@@ -109,6 +109,7 @@ export function useTimelineEditingController(input: Input) {
timelineDuration: projection.timelineDuration,
});
const captionCommands = useCaptionCommands({
+ clipRegions: timeline.clipRegions,
autoCaptions: timeline.autoCaptions,
setAutoCaptions: timeline.setAutoCaptions,
setAutoCaptionSettings: timeline.setAutoCaptionSettings,
@@ -120,7 +121,6 @@ export function useTimelineEditingController(input: Input) {
setActiveEffectSection: input.setActiveEffectSection,
videoPlaybackRef: input.videoPlaybackRef,
mapSourceTimeToTimelineTime: projection.mapSourceTimeToTimelineTime,
- mapTimelineTimeToSourceTime: projection.mapTimelineTimeToSourceTime,
handleSeek: playback.handleSeek,
});
const zoomCommands = useZoomRegionCommands({
diff --git a/src/components/video-editor/hooks/useTimelineProjection.ts b/src/components/video-editor/hooks/useTimelineProjection.ts
index a455c29e..7d88ffa7 100644
--- a/src/components/video-editor/hooks/useTimelineProjection.ts
+++ b/src/components/video-editor/hooks/useTimelineProjection.ts
@@ -1,9 +1,9 @@
/* biome-ignore-all lint/correctness/useExhaustiveDependencies: mutable timeline bootstrap refs intentionally do not trigger effects. */
import { type MutableRefObject, useCallback, useEffect, useMemo } from "react";
+import { projectCaptionCues } from "../captionTimeline";
import { deriveNextId } from "../projectPersistence";
import type { useTimelineState } from "../state/useTimelineState";
import {
- type CaptionCue,
clipsToTrims,
extendAutoFullTrackClip,
getClipSourceEndMs,
@@ -90,14 +90,9 @@ export function useTimelineProjection({
[clipRegions],
);
const effectiveZoomRegions: ZoomRegion[] = zoomRegions;
- const effectiveCaptionRegions = useMemo<CaptionCue[]>(
- () =>
- autoCaptions.map((cue) => ({
- ...cue,
- startMs: toTimelineTime(cue.startMs),
- endMs: toTimelineTime(cue.endMs),
- })),
- [autoCaptions, toTimelineTime],
+ const effectiveCaptionRegions = useMemo(
+ () => projectCaptionCues(autoCaptions, clipRegions),
+ [autoCaptions, clipRegions],
);
const timelinePlayheadTime = currentTime;
const timelineDuration = useMemo(
diff --git a/src/components/video-editor/layout/EditorExportMenu.tsx b/src/components/video-editor/layout/EditorExportMenu.tsx
index b39a8521..7f557a70 100644
--- a/src/components/video-editor/layout/EditorExportMenu.tsx
+++ b/src/components/video-editor/layout/EditorExportMenu.tsx
@@ -1,4 +1,5 @@
import { DownloadSimple as Download } from "@phosphor-icons/react";
+import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -190,10 +191,32 @@ export function EditorExportMenu(props: Props) {
Path: {exportRuntimeLabel}
</p>
) : null}
- <p className="mt-1 whitespace-pre-line text-xs leading-relaxed text-muted-foreground">
+ <p className="mt-1 select-text whitespace-pre-wrap break-words text-xs leading-relaxed text-muted-foreground">
{exportError}
</p>
<div className="mt-4 flex gap-2">
+ <Button
+ type="button"
+ variant="outline"
+ className="h-8 text-xs"
+ onClick={async () => {
+ try {
+ await navigator.clipboard.writeText(exportError);
+ toast.success(
+ t("editor.exportStatus.errorCopied", "Error copied"),
+ );
+ } catch {
+ toast.error(
+ t(
+ "editor.exportStatus.errorCopyFailed",
+ "Couldn't copy. Select the error text and copy it manually.",
+ ),
+ );
+ }
+ }}
+ >
+ {t("editor.exportStatus.copyError", "Copy error")}
+ </Button>
{hasPendingExportSave ? (
<Button
type="button"
diff --git a/src/components/video-editor/layout/EditorTimelinePanel.tsx b/src/components/video-editor/layout/EditorTimelinePanel.tsx
index 78bfca53..6f960f9f 100644
--- a/src/components/video-editor/layout/EditorTimelinePanel.tsx
+++ b/src/components/video-editor/layout/EditorTimelinePanel.tsx
@@ -1,5 +1,6 @@
import type { RefObject } from "react";
import type { useVideoEditorAudio } from "../audio/useVideoEditorAudio";
+import { retimeCaptionFragment } from "../captionTimeline";
import type { useAnnotationRegionCommands } from "../hooks/useAnnotationRegionCommands";
import type { useAudioRegionCommands } from "../hooks/useAudioRegionCommands";
import type { useCaptionCommands } from "../hooks/useCaptionCommands";
@@ -90,15 +91,37 @@ export function EditorTimelinePanel(props: Props) {
selectedAudioId={timeline.selectedAudioId}
onSelectAudio={audioCommands.handleSelectAudio}
captionRegions={projection.effectiveCaptionRegions}
- onCaptionSpanChange={(id, span) =>
- captionCommands.handleCaptionRetime(id, {
- startMs: projection.mapTimelineTimeToSourceTime(span.start),
- endMs: projection.mapTimelineTimeToSourceTime(span.end),
- })
+ onCaptionSpanChange={(id, span) => {
+ const fragment = projection.effectiveCaptionRegions.find(
+ (cue) => cue.id === id,
+ );
+ if (!fragment) return;
+ captionCommands.handleCaptionRetime(
+ fragment.sourceCueId,
+ retimeCaptionFragment(fragment, span),
+ );
+ }}
+ selectedCaptionId={
+ projection.effectiveCaptionRegions.find(
+ (cue) =>
+ cue.sourceCueId === timeline.selectedCaptionId &&
+ currentTime * 1000 >= cue.startMs &&
+ currentTime * 1000 <= cue.endMs,
+ )?.id ?? null
}
- selectedCaptionId={timeline.selectedCaptionId}
- onSelectCaption={captionCommands.handleSelectCaption}
- onCaptionDelete={captionCommands.handleCaptionDelete}
+ onSelectCaption={(id) => {
+ const fragment = projection.effectiveCaptionRegions.find(
+ (cue) => cue.id === id,
+ );
+ captionCommands.handleSelectCaption(fragment?.sourceCueId ?? null);
+ if (fragment) playback.handleTimelineSeek(fragment.startMs / 1000);
+ }}
+ onCaptionDelete={(id) => {
+ const fragment = projection.effectiveCaptionRegions.find(
+ (cue) => cue.id === id,
+ );
+ if (fragment) captionCommands.handleCaptionDelete(fragment.sourceCueId);
+ }}
onCaptionAdded={captionCommands.handleCaptionAdded}
captionsEnabled={timeline.autoCaptionSettings.enabled}
captionQuickAddEnabled={timeline.autoCaptionSettings.timelineQuickAdd}
diff --git a/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.test.ts b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.test.ts
new file mode 100644
index 00000000..d408c89b
--- /dev/null
+++ b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.test.ts
@@ -0,0 +1,67 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { useTimelineKeyboardShortcuts } from "./useTimelineKeyboardShortcuts";
+
+vi.mock("react", () => ({ useEffect: (effect: () => void) => effect() }));
+
+class Element {
+ isContentEditable = false;
+}
+class Input extends Element {}
+class Textarea extends Element {}
+class Select extends Element {}
+
+afterEach(() => vi.unstubAllGlobals());
+
+function setup(selectedClipId: string | null = "clip") {
+ vi.stubGlobal("HTMLElement", Element);
+ vi.stubGlobal("HTMLInputElement", Input);
+ vi.stubGlobal("HTMLTextAreaElement", Textarea);
+ vi.stubGlobal("HTMLSelectElement", Select);
+ const addEventListener = vi.fn();
+ vi.stubGlobal("window", { addEventListener, removeEventListener: vi.fn() });
+ const deleteSelectedClip = vi.fn();
+ useTimelineKeyboardShortcuts({
+ isTimelineFocusedRef: { current: false },
+ selectedClipId,
+ deleteSelectedClip,
+ } as unknown as Parameters<typeof useTimelineKeyboardShortcuts>[0]);
+ const handler = addEventListener.mock.calls[0][1] as (event: KeyboardEvent) => void;
+ const press = (options: Record<string, unknown> = {}) => {
+ const event = {
+ key: "Backspace",
+ target: new Element(),
+ preventDefault: vi.fn(),
+ ...options,
+ };
+ handler(event as unknown as KeyboardEvent);
+ return event;
+ };
+ return { press, deleteSelectedClip };
+}
+
+describe("selected clip Backspace", () => {
+ it("deletes the selected clip without requiring timeline focus", () => {
+ const { press, deleteSelectedClip } = setup();
+ expect(press().preventDefault).toHaveBeenCalledOnce();
+ expect(deleteSelectedClip).toHaveBeenCalledOnce();
+ });
+ it.each([
+ new Input(),
+ new Textarea(),
+ new Select(),
+ Object.assign(new Element(), { isContentEditable: true }),
+ ])("does not delete a clip while editing text or a form control", (target) => {
+ const { press, deleteSelectedClip } = setup();
+ expect(press({ target }).preventDefault).not.toHaveBeenCalled();
+ expect(deleteSelectedClip).not.toHaveBeenCalled();
+ });
+ it("ignores consumed events, modifier shortcuts and missing selection", () => {
+ const { press, deleteSelectedClip } = setup();
+ for (const option of ["defaultPrevented", "ctrlKey", "metaKey", "altKey"])
+ press({ [option]: true });
+ expect(deleteSelectedClip).not.toHaveBeenCalled();
+ const unselected = setup(null);
+ unselected.press();
+ expect(unselected.deleteSelectedClip).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
index 94cb3973..4669d6b5 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineKeyboardShortcuts.ts
@@ -58,6 +58,7 @@ export function useTimelineKeyboardShortcuts({
}: UseTimelineKeyboardShortcutsParams) {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.defaultPrevented) return;
const eventTarget = e.target;
if (
eventTarget instanceof HTMLInputElement ||
@@ -68,6 +69,12 @@ export function useTimelineKeyboardShortcuts({
return;
}
+ if (selectedClipId && e.key === "Backspace" && !e.ctrlKey && !e.metaKey && !e.altKey) {
+ e.preventDefault();
+ deleteSelectedClip();
+ return;
+ }
+
if (!isTimelineFocusedRef.current) {
return;
}
diff --git a/src/components/video-editor/types.test.ts b/src/components/video-editor/types.test.ts
index 0d95a9da..6ea4ad6b 100644
--- a/src/components/video-editor/types.test.ts
+++ b/src/components/video-editor/types.test.ts
@@ -117,6 +117,20 @@ describe("extendAutoFullTrackClip", () => {
});
describe("clip timeline mapping", () => {
+ it("selects the next source at an exact cut, but keeps fractional times before it in the previous clip", () => {
+ const clips = [
+ { id: "a", startMs: 0, endMs: 1000, sourceStartMs: 0, speed: 3 },
+ { id: "b", startMs: 1000, endMs: 2000, sourceStartMs: 6000, speed: 1 },
+ ];
+ expect(mapTimelineTimeToSourceTime(1000, clips)).toBe(6000);
+ expect(mapTimelineTimeToSourceTime(999.9, clips)).toBe(3000);
+ expect(mapTimelineTimeToSourceTime(2000, clips)).toBe(7000);
+ const sourceAdjacent = [
+ clips[0],
+ { ...clips[1], startMs: 2000, endMs: 3000, sourceStartMs: 3000 },
+ ];
+ expect(mapSourceTimeToTimelineTime(3000, sourceAdjacent)).toBe(2000);
+ });
const clips = [
{ id: "clip-1", startMs: 0, endMs: 4_000, speed: 1 },
{ id: "clip-2", startMs: 6_000, endMs: 8_000, speed: 2 },
diff --git a/src/components/video-editor/types.ts b/src/components/video-editor/types.ts
index bfa3cb14..d5dc8714 100644
--- a/src/components/video-editor/types.ts
+++ b/src/components/video-editor/types.ts
@@ -299,7 +299,7 @@ export function mapTimelineTimeToSourceTime(timeMs: number, clips: ClipRegion[])
const sortedClips = sortClipRegions(clips);
for (const clip of sortedClips) {
- if (roundedTimeMs < clip.startMs || roundedTimeMs > clip.endMs) {
+ if (timeMs < clip.startMs || timeMs >= clip.endMs) {
continue;
}
@@ -322,7 +322,7 @@ export function mapSourceTimeToTimelineTime(timeMs: number, clips: ClipRegion[])
for (const clip of sortedClips) {
const sourceStartMs = getClipSourceStartMs(clip);
const sourceEndMs = getClipSourceEndMs(clip);
- if (roundedTimeMs < sourceStartMs || roundedTimeMs > sourceEndMs) {
+ if (timeMs < sourceStartMs || timeMs >= sourceEndMs) {
continue;
}
diff --git a/src/components/video-editor/videoPlayback/clipPlayback.test.ts b/src/components/video-editor/videoPlayback/clipPlayback.test.ts
index 99f35a4b..858e1851 100644
--- a/src/components/video-editor/videoPlayback/clipPlayback.test.ts
+++ b/src/components/video-editor/videoPlayback/clipPlayback.test.ts
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ClipRegion } from "../types";
-import { createClipPlayback } from "./clipPlayback";
+import { createClipPlayback, findPreviewClipAtTimelineTime } from "./clipPlayback";
describe("clip timeline playback", () => {
let now = 0;
@@ -87,6 +87,28 @@ describe("clip timeline playback", () => {
advance(250);
expect(onTime).toHaveBeenLastCalledWith(1.75, null);
});
+ it("does not skip a short gap when a media tick overshoots the cut", async () => {
+ const { video, playback, onTime } = setup([
+ { id: "a", startMs: 0, endMs: 1000, sourceStartMs: 0, speed: 3 },
+ { id: "b", startMs: 1010, endMs: 2000, sourceStartMs: 6000, speed: 3 },
+ ]);
+ await playback.play();
+ video.currentTime = 3.15;
+ advance(1050);
+ expect(onTime).toHaveBeenLastCalledWith(1, null);
+ advance(10);
+ expect(onTime).toHaveBeenLastCalledWith(1.01, 6);
+ });
+ it("leaves a clip at source EOF even when metadata rounding extends its timeline end", async () => {
+ const { video, playback, onTime } = setup([
+ { id: "a", startMs: 0, endMs: 4000, sourceStartMs: 0, speed: 3 },
+ ]);
+ await playback.play();
+ Object.assign(video, { currentTime: 11.999, ended: true });
+ advance(4000);
+ expect(onTime).toHaveBeenLastCalledWith(4, 12);
+ expect(playback.isPlaying).toBe(false);
+ });
it("does not skip footage when source playback stalls or a seek is pending", async () => {
const { video, playback, onTime } = setup();
await playback.play();
@@ -116,4 +138,54 @@ describe("clip timeline playback", () => {
expect(onError).toHaveBeenCalledWith(error);
expect(playback.isPlaying).toBe(false);
});
+ it.each([
+ 20, 30,
+ ])("reports unsupported %sx without crashing or scheduling playback", async (speed) => {
+ const clips = [{ id: "fast", startMs: 0, endMs: 1000, speed }];
+ const { video, playback, onError } = setup(clips);
+ Object.defineProperty(video, "playbackRate", {
+ get: () => 1,
+ set: (rate: number) => {
+ if (rate > 16)
+ throw new DOMException("Unsupported playback rate", "NotSupportedError");
+ },
+ });
+ expect(() => playback.refresh()).not.toThrow();
+ expect(() => playback.seek(0.5)).not.toThrow();
+ await playback.play();
+ expect(playback.isPlaying).toBe(false);
+ expect(tick).toBeUndefined();
+ expect(video.play).not.toHaveBeenCalled();
+ expect(onError).toHaveBeenCalledWith(
+ expect.objectContaining({ name: "NotSupportedError" }),
+ );
+ expect(clips[0].speed).toBe(speed);
+ clips[0].speed = 2;
+ playback.refresh();
+ await playback.play();
+ expect(playback.isPlaying).toBe(true);
+ });
+ it("holds the final clip when seeking to the timeline end and restarts on play", async () => {
+ const { playback, video, onTime } = setup();
+ playback.seek(4);
+ expect(onTime).toHaveBeenLastCalledWith(4, 12);
+ expect(video.currentTime).toBe(12);
+ expect(video.play).not.toHaveBeenCalled();
+ await playback.play();
+ expect(onTime).toHaveBeenLastCalledWith(0, 0);
+ });
+ it("keeps real gaps black, chooses the next clip at cuts, and holds only the final endpoint", () => {
+ const clips = [
+ { id: "a", startMs: 0, endMs: 1000, speed: 1 },
+ { id: "b", startMs: 1000, endMs: 2000, sourceStartMs: 4000, speed: 1 },
+ { id: "c", startMs: 3000, endMs: 4000, sourceStartMs: 6000, speed: 1 },
+ ];
+ expect(findPreviewClipAtTimelineTime(999.9, clips)?.id).toBe("a");
+ expect(findPreviewClipAtTimelineTime(1000, clips)?.id).toBe("b");
+ expect(findPreviewClipAtTimelineTime(2000, clips)).toBeNull();
+ expect(findPreviewClipAtTimelineTime(2500, clips)).toBeNull();
+ expect(findPreviewClipAtTimelineTime(4000, clips)?.id).toBe("c");
+ expect(findPreviewClipAtTimelineTime(4001, clips)).toBeNull();
+ expect(findPreviewClipAtTimelineTime(0, [])).toBeNull();
+ });
});
diff --git a/src/components/video-editor/videoPlayback/clipPlayback.ts b/src/components/video-editor/videoPlayback/clipPlayback.ts
index bf804ea8..58b76ce8 100644
--- a/src/components/video-editor/videoPlayback/clipPlayback.ts
+++ b/src/components/video-editor/videoPlayback/clipPlayback.ts
@@ -4,8 +4,20 @@ import {
findClipAtTimelineTime,
getClipSourceStartMs,
getTimelineDurationMs,
+ sortClipRegions,
} from "../types";
+/** The playhead may stop at the timeline end; that is not a black gap. */
+export function findPreviewClipAtTimelineTime(
+ timeMs: number,
+ clips: ClipRegion[],
+): ClipRegion | null {
+ const active = findClipAtTimelineTime(timeMs, clips);
+ if (active) return active;
+ const last = sortClipRegions(clips)[clips.length - 1];
+ return last && timeMs === last.endMs ? last : null;
+}
+
/** Timeline time advances at 1x; only the source media uses the clip's speed. */
export function createClipPlayback({
video,
@@ -46,13 +58,19 @@ export function createClipPlayback({
});
};
const sync = (seek = false) => {
- const clip = findClipAtTimelineTime(timeMs, getClips());
+ const clip = findPreviewClipAtTimelineTime(timeMs, getClips());
const sourceMs = clip
? getClipSourceStartMs(clip) + (timeMs - clip.startMs) * clip.speed
: null;
if (clip && sourceMs !== null) {
enablePitchPreservingPlayback(video);
- video.playbackRate = clip.speed;
+ try {
+ video.playbackRate = clip.speed;
+ } catch (error) {
+ pause();
+ onError(error);
+ return;
+ }
if (seek || clip !== activeClip) video.currentTime = sourceMs / 1000;
if (playing && (seek || clip !== activeClip)) playSource();
} else {
@@ -69,13 +87,19 @@ export function createClipPlayback({
// A gap has no source clock, so advance it with elapsed real time.
if (!activeClip) timeMs += now - lastTick;
else if (!video.seeking) {
- timeMs =
- activeClip.startMs +
- (video.currentTime * 1000 - getClipSourceStartMs(activeClip)) / activeClip.speed;
+ timeMs = video.ended
+ ? activeClip.endMs
+ : Math.min(
+ activeClip.endMs,
+ activeClip.startMs +
+ (video.currentTime * 1000 - getClipSourceStartMs(activeClip)) /
+ activeClip.speed,
+ );
}
timeMs = Math.min(duration(), timeMs);
lastTick = now;
sync();
+ if (!playing) return;
if (timeMs >= duration()) pause();
else request = requestAnimationFrame(tick);
};
@@ -90,7 +114,7 @@ export function createClipPlayback({
onPlaying(true);
lastTick = performance.now();
sync(true);
- request = requestAnimationFrame(tick);
+ if (playing) request = requestAnimationFrame(tick);
},
pause,
seek: (seconds: number) => {
diff --git a/src/components/video-editor/videoPlayback/playbackRate.test.ts b/src/components/video-editor/videoPlayback/playbackRate.test.ts
new file mode 100644
index 00000000..42ed9305
--- /dev/null
+++ b/src/components/video-editor/videoPlayback/playbackRate.test.ts
@@ -0,0 +1,22 @@
+import { afterEach, expect, it, vi } from "vitest";
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+});
+
+it("probes and caches the runtime's accepted rates", async () => {
+ const createElement = vi.fn(() => ({
+ set playbackRate(rate: number) {
+ if (rate > 16) throw new DOMException("Unsupported rate", "NotSupportedError");
+ },
+ }));
+ vi.stubGlobal("document", { createElement });
+ const { supportsPreviewPlaybackRate } = await import("./playbackRate");
+ for (const speed of [0.25, 1, 3, 15, 16]) expect(supportsPreviewPlaybackRate(speed)).toBe(true);
+ for (const speed of [20, 30]) expect(supportsPreviewPlaybackRate(speed)).toBe(false);
+ expect(supportsPreviewPlaybackRate(20)).toBe(false);
+ expect(createElement).toHaveBeenCalledTimes(7);
+ for (const speed of [0, -1, NaN, Infinity])
+ expect(supportsPreviewPlaybackRate(speed)).toBe(false);
+});
diff --git a/src/components/video-editor/videoPlayback/playbackRate.ts b/src/components/video-editor/videoPlayback/playbackRate.ts
new file mode 100644
index 00000000..98dcefa3
--- /dev/null
+++ b/src/components/video-editor/videoPlayback/playbackRate.ts
@@ -0,0 +1,18 @@
+const supportedRates = new Map<number, boolean>();
+
+/** Ask the runtime rather than assuming every platform accepts the same rates. */
+export function supportsPreviewPlaybackRate(rate: number): boolean {
+ if (!Number.isFinite(rate) || rate <= 0) return false;
+ const cached = supportedRates.get(rate);
+ if (cached !== undefined) return cached;
+ const video = document.createElement("video");
+ try {
+ video.playbackRate = rate;
+ supportedRates.set(rate, true);
+ return true;
+ } catch (error) {
+ if (!(error instanceof DOMException) || error.name !== "NotSupportedError") throw error;
+ supportedRates.set(rate, false);
+ return false;
+ }
+}
diff --git a/src/lib/exporter/audioEncoder.test.ts b/src/lib/exporter/audioEncoder.test.ts
index 92a1a887..4238b902 100644
--- a/src/lib/exporter/audioEncoder.test.ts
+++ b/src/lib/exporter/audioEncoder.test.ts
@@ -53,6 +53,19 @@ function fakeAudioBuffer(channels: Float32Array[]): AudioBuffer {
}
describe("AudioProcessor offline render preparation", () => {
+ it("rejects a cancelled chunked render instead of returning a partial WAV", async () => {
+ const processor = new AudioProcessor() as unknown as OfflineRenderTestHarness;
+ vi.spyOn(processor, "prepareOfflineRender").mockResolvedValue({
+ mainBufferEntry: null,
+ companionEntries: [],
+ outputDurationMs: 1000,
+ numChannels: 2,
+ } as never);
+ vi.spyOn(processor, "renderChunked").mockImplementation(async () => processor.cancel());
+ await expect(processor.renderEditedAudioTrack("source.mp4")).rejects.toThrow(
+ "Export cancelled",
+ );
+ });
it("keeps embedded source audio separate from external companion sidecars", async () => {
const processor = new AudioProcessor() as unknown as OfflineRenderTestHarness;
const mainBuffer = { duration: 10, numberOfChannels: 2 } as AudioBuffer;
diff --git a/src/lib/exporter/frameRenderer.ts b/src/lib/exporter/frameRenderer.ts
index 41cc9d62..349819ec 100644
--- a/src/lib/exporter/frameRenderer.ts
+++ b/src/lib/exporter/frameRenderer.ts
@@ -1417,6 +1417,7 @@ export class FrameRenderer {
this.currentVideoTime = timestamp / 1000000;
this.cameraContainer.visible = videoFrame !== null;
if (!videoFrame) {
+ this.updateAnimationState(backgroundTimelineTimestamp / 1000, cursorTimestamp / 1000);
this.compositeCtx.fillStyle = "#000000";
this.compositeCtx.fillRect(0, 0, this.config.width, this.config.height);
return;
diff --git a/src/lib/exporter/modernFrameRenderer.test.ts b/src/lib/exporter/modernFrameRenderer.test.ts
index 456519fb..c4809ad8 100644
--- a/src/lib/exporter/modernFrameRenderer.test.ts
+++ b/src/lib/exporter/modernFrameRenderer.test.ts
@@ -317,45 +317,6 @@ describe("ModernFrameRenderer blur export path", () => {
expect(renderer.capturePixelsForNativeExport()).not.toBeNull();
});
- it("uses the sampled scene transform for blur annotations during temporal blur", async () => {
- const renderer = createRenderer() as any;
- renderer.config.zoomTemporalMotionBlur = 1;
- renderer.config.zoomMotionBlurSampleCount = 3;
- renderer.config.zoomMotionBlurShutterFraction = 0.5;
- renderer.app = { canvas: createMockCanvas() };
- renderer.annotationScaleFactor = 1;
- renderer.annotationAssets = { imageCache: new Map() };
- renderer.updateCaptionLayer = vi.fn();
- renderer.renderSceneSample = vi.fn(async (sampleTimestamp: number) => ({
- timeMs: sampleTimestamp / 1000,
- cursorTimeMs: sampleTimestamp / 1000,
- backgroundTimelineTimeMs: sampleTimestamp / 1000,
- sceneTransform: { scale: 1.75, x: 120, y: -48 },
- zoom: { scale: 1, focusX: 0.5, focusY: 0.5, progress: 0 },
- }));
-
- await renderer.renderTemporalMotionBlurFrame(1_000_000, 1_000_000, 1_000_000, 33_333, {
- stageSize: { width: 1920, height: 1080 },
- videoSize: { width: 1920, height: 1080 },
- baseScale: 1,
- baseOffset: { x: 0, y: 0 },
- maskRect: {
- x: 0,
- y: 0,
- width: 1920,
- height: 1080,
- sourceCrop: { x: 0, y: 0, width: 1, height: 1 },
- },
- });
-
- expect(renderAnnotations).toHaveBeenCalled();
- expect(vi.mocked(renderAnnotations).mock.lastCall?.[7]).toEqual({
- scale: 1.75,
- x: 120,
- y: -48,
- });
- });
-
it("prefers decoder-backed sync for video wallpapers during export", async () => {
vi.clearAllMocks();
const renderer = new FrameRenderer({
@@ -853,43 +814,77 @@ describe("ModernFrameRenderer webcam export fallback", () => {
});
});
-describe("ModernFrameRenderer temporal webcam sync", () => {
- it("pins webcam sync to the frame center during temporal blur sampling", async () => {
- const renderer = createRenderer() as any;
- renderer.config.annotationRegions = [];
- renderer.config.zoomTemporalMotionBlur = 1;
- renderer.config.zoomMotionBlurSampleCount = 3;
- renderer.config.zoomMotionBlurShutterFraction = 0.5;
- renderer.app = { canvas: createMockCanvas() };
- renderer.updateCaptionLayer = vi.fn();
- renderer.renderSceneSample = vi.fn(async (sampleTimestamp: number) => ({
- timeMs: sampleTimestamp / 1000,
- cursorTimeMs: sampleTimestamp / 1000,
- backgroundTimelineTimeMs: sampleTimestamp / 1000,
- sceneTransform: { scale: 1, x: 0, y: 0 },
- zoom: { scale: 1, focusX: 0.5, focusY: 0.5, progress: 0 },
- }));
-
- await renderer.renderTemporalMotionBlurFrame(1_000_000, 1_000_000, 1_000_000, 33_333, {
- stageSize: { width: 1920, height: 1080 },
- videoSize: { width: 1920, height: 1080 },
- baseScale: 1,
- baseOffset: { x: 0, y: 0 },
- maskRect: {
- x: 0,
- y: 0,
+describe("ModernFrameRenderer frame sequencing", () => {
+ it.each([
+ 0, 1,
+ ])("resumes visual layers after a black gap with temporal blur set to %s", async (temporalBlur) => {
+ const renderer = createRenderer();
+ const sceneCanvas = createMockCanvas();
+ const gapCanvas = createMockCanvas();
+ gapCanvas.width = 1920;
+ gapCanvas.height = 1080;
+ const render = vi.fn();
+ const syncWebcam = vi.fn();
+ const syncBackground = vi.fn();
+ const updateAnnotations = vi.fn();
+ const updateCaptions = vi.fn();
+ const updateWebcam = vi.fn();
+ const updateAnimation = vi.fn();
+ const camera = { visible: true };
+ Object.assign(renderer, {
+ config: {
width: 1920,
height: 1080,
- sourceCrop: { x: 0, y: 0, width: 1, height: 1 },
+ timelineEffects: true,
+ zoomTemporalMotionBlur: temporalBlur,
+ zoomMotionBlur: 0.5,
+ },
+ app: { canvas: sceneCanvas, render },
+ videoContainer: {},
+ videoMaskGraphics: {},
+ cameraContainer: camera,
+ videoSprite: {},
+ videoTextureSource: { update: vi.fn() },
+ layoutCache: {
+ stageSize: { width: 1920, height: 1080 },
+ maskRect: { x: 0, y: 0, width: 1920, height: 1080 },
},
+ resolveDetachedVideoFrameSource: async (frame: VideoFrame) => frame,
+ ensureExportCompositeCanvas: () => ({ canvas: gapCanvas, context: gapCanvas.context }),
+ webcamForwardFrameSource: {},
+ backgroundForwardFrameSource: {},
+ syncWebcamFrame: syncWebcam,
+ syncBackgroundFrame: syncBackground,
+ updateAnimationState: updateAnimation,
+ updateAnnotationLayer: updateAnnotations,
+ updateCaptionLayer: updateCaptions,
+ updateWebcamOverlay: updateWebcam,
});
+ await renderer.renderFrame({} as VideoFrame, 0, 0, 33_333, 0);
+ expect(renderer.getCanvas()).toBe(sceneCanvas);
+
+ await renderer.renderFrame(null, 0, 0, 33_333, 1_500_000);
+ expect(updateAnimation).toHaveBeenLastCalledWith(1500, 0);
+ expect(renderer.getCanvas()).toBe(gapCanvas);
+ expect(camera.visible).toBe(false);
+ for (const callback of [
+ render,
+ syncWebcam,
+ syncBackground,
+ updateAnnotations,
+ updateCaptions,
+ updateWebcam,
+ ]) {
+ expect(callback).toHaveBeenCalledTimes(1);
+ }
- expect(renderer.renderSceneSample).toHaveBeenCalledTimes(3);
- expect(renderer.renderSceneSample.mock.calls.map((call: unknown[]) => call[6])).toEqual([
- 1, 1, 1,
- ]);
- expect(
- new Set(renderer.renderSceneSample.mock.calls.map((call: unknown[]) => call[0])).size,
- ).toBeGreaterThan(1);
+ await renderer.renderFrame({} as VideoFrame, 6_000_000, 6_000_000, 33_333, 2_000_000);
+ expect(renderer.getCanvas()).toBe(sceneCanvas);
+ expect(camera.visible).toBe(true);
+ expect(render).toHaveBeenCalledTimes(2);
+ expect(syncWebcam).toHaveBeenLastCalledWith(6);
+ expect(syncBackground).toHaveBeenLastCalledWith(2);
+ expect(updateAnnotations).toHaveBeenLastCalledWith(2000);
+ expect(updateCaptions).toHaveBeenLastCalledWith(6000);
});
});
diff --git a/src/lib/exporter/modernFrameRenderer.ts b/src/lib/exporter/modernFrameRenderer.ts
index 24a6f86d..17841979 100644
--- a/src/lib/exporter/modernFrameRenderer.ts
+++ b/src/lib/exporter/modernFrameRenderer.ts
@@ -94,10 +94,6 @@ import {
VIDEO_SHADOW_LAYER_PROFILES,
WEBCAM_SHADOW_LAYER_PROFILES,
} from "./shadowProfile";
-import { buildTemporalSamplePlanUs, getTemporalMotionBlurConfig } from "./temporalMotionBlur";
-
-const TEMPORAL_ZOOM_MOTION_BLUR_ENABLED = false;
-
import type { ExportRenderBackend } from "./types";
interface FrameRenderConfig {
@@ -282,14 +278,6 @@ function isKnownRendererUnavailableError(error: unknown): boolean {
);
}
-interface RenderSnapshot {
- timeMs: number;
- cursorTimeMs: number;
- backgroundTimelineTimeMs: number;
- sceneTransform: { scale: number; x: number; y: number };
- zoom: { scale: number; focusX: number; focusY: number; progress: number };
-}
-
function createAnimationState(): AnimationState {
return {
scale: 1,
@@ -432,7 +420,6 @@ export class FrameRenderer {
private captionTextureSource: MutableVideoTextureSource | null = null;
private captionRenderKey: string | null = null;
private exportCompositeCanvas: ExportCompositeCanvasState | null = null;
- private temporalCompositeCanvas: ExportCompositeCanvasState | null = null;
private outputCanvasOverride: HTMLCanvasElement | null = null;
private config: FrameRenderConfig;
private animationState: AnimationState;
@@ -1473,35 +1460,6 @@ export class FrameRenderer {
return this.exportCompositeCanvas;
}
- private ensureTemporalCompositeCanvas(): ExportCompositeCanvasState | null {
- const targetWidth = Math.max(1, Math.ceil(this.config.width));
- const targetHeight = Math.max(1, Math.ceil(this.config.height));
-
- if (
- this.temporalCompositeCanvas &&
- this.temporalCompositeCanvas.canvas.width === targetWidth &&
- this.temporalCompositeCanvas.canvas.height === targetHeight
- ) {
- return this.temporalCompositeCanvas;
- }
-
- const canvas = document.createElement("canvas");
- canvas.width = targetWidth;
- canvas.height = targetHeight;
-
- const context = configureHighQuality2DContext(canvas.getContext("2d"));
- if (!context) {
- return null;
- }
-
- this.temporalCompositeCanvas = {
- canvas,
- context,
- };
-
- return this.temporalCompositeCanvas;
- }
-
private drawCaptionOverlay(context: CanvasRenderingContext2D): void {
if (
!this.captionContainer?.visible ||
@@ -1522,11 +1480,7 @@ export class FrameRenderer {
context.restore();
}
- private async composeBlurAnnotationFrame(
- timeMs: number,
- sceneTransform?: { scale: number; x: number; y: number },
- sourceCanvas?: CanvasImageSource,
- ): Promise<void> {
+ private async composeBlurAnnotationFrame(timeMs: number): Promise<void> {
if (!this.app) {
this.outputCanvasOverride = null;
return;
@@ -1540,7 +1494,7 @@ export class FrameRenderer {
const { canvas, context } = compositeState;
context.clearRect(0, 0, canvas.width, canvas.height);
- context.drawImage(sourceCanvas ?? (this.app.canvas as HTMLCanvasElement), 0, 0);
+ context.drawImage(this.app.canvas as HTMLCanvasElement, 0, 0);
await renderAnnotations(
context,
@@ -1550,7 +1504,7 @@ export class FrameRenderer {
timeMs,
this.annotationScaleFactor,
this.annotationAssets ?? undefined,
- sceneTransform ?? {
+ {
scale: this.animationState.appliedScale,
x: this.animationState.x,
y: this.animationState.y,
@@ -2315,20 +2269,6 @@ export class FrameRenderer {
private async setupWebcamSource(): Promise<void> {
const webcamUrl = this.config.webcamUrl;
- if (!this.config.webcam?.enabled || !webcamUrl) {
- this.webcamForwardFrameSource?.cancel();
- void this.webcamForwardFrameSource?.destroy();
- this.webcamForwardFrameSource = null;
- this.closeWebcamDecodedFrame();
- this.clearWebcamMediaElement();
- this.webcamFrameCacheCanvas = null;
- this.webcamFrameCacheCtx = null;
- this.lastSyncedWebcamTime = null;
- this.webcamLayoutCache = null;
- this.webcamRenderMode = "hidden";
- return;
- }
-
this.webcamForwardFrameSource?.cancel();
void this.webcamForwardFrameSource?.destroy();
this.webcamForwardFrameSource = null;
@@ -2338,6 +2278,9 @@ export class FrameRenderer {
this.webcamFrameCacheCtx = null;
this.webcamLayoutCache = null;
this.webcamRenderMode = "hidden";
+ this.lastSyncedWebcamTime = null;
+
+ if (!this.config.webcam?.enabled || !webcamUrl) return;
try {
const frameSource = new ForwardFrameSource();
@@ -2904,211 +2847,11 @@ export class FrameRenderer {
}
}
- private async renderSceneSample(
- timestamp: number,
- cursorTimestamp: number,
- backgroundTimelineTimestamp: number,
- layoutCache: LayoutCache,
- useVelocityMotionBlur: boolean,
- includeOverlayLayers = true,
- webcamTimeSecondsOverride?: number,
- ): Promise<RenderSnapshot> {
- if (!this.app || !this.cameraContainer || !this.videoMaskGraphics) {
- throw new Error("Renderer not initialized");
- }
-
- this.currentVideoTime = timestamp / 1_000_000;
- const webcamRenderTimeSeconds = Math.max(
- 0,
- webcamTimeSecondsOverride ?? this.currentVideoTime,
- );
-
- if (this.webcamForwardFrameSource || this.webcamVideoElement) {
- await this.syncWebcamFrame(webcamRenderTimeSeconds);
- }
-
- if (this.backgroundForwardFrameSource || this.backgroundVideoElement) {
- await this.syncBackgroundFrame(Math.max(0, backgroundTimelineTimestamp / 1_000_000));
- }
-
- const timeMs = this.config.timelineEffects
- ? backgroundTimelineTimestamp / 1000
- : timestamp / 1000;
- const cursorTimeMs = cursorTimestamp / 1000;
-
- if (this.cursorOverlay) {
- this.cursorOverlay.update(
- this.config.cursorTelemetry ?? [],
- cursorTimeMs,
- layoutCache.maskRect,
- this.config.showCursor ?? true,
- false,
- );
- }
-
- this.updateAnimationState(timeMs, cursorTimeMs);
-
- applyZoomTransform({
- cameraContainer: this.cameraContainer,
- zoomBlurFilter: this.zoomBlurFilter,
- motionBlurFilter: this.motionBlurFilter,
- stageSize: layoutCache.stageSize,
- baseMask: layoutCache.maskRect,
- zoomScale: this.animationState.scale,
- zoomProgress: this.animationState.progress,
- focusX: this.animationState.focusX,
- focusY: this.animationState.focusY,
- isPlaying: true,
- motionBlurAmount: useVelocityMotionBlur ? (this.config.zoomMotionBlur ?? 0) : 0,
- motionBlurTuning: this.config.zoomMotionBlurTuning,
- transformOverride: {
- scale: this.animationState.appliedScale,
- x: this.animationState.x,
- y: this.animationState.y,
- },
- motionBlurState: this.motionBlurState,
- frameTimeMs: timeMs,
- });
-
- if (includeOverlayLayers) {
- this.updateAnnotationLayer(timeMs);
- this.updateCaptionLayer(timestamp / 1000);
- }
- this.updateWebcamOverlay(webcamRenderTimeSeconds);
-
- const annotationContainerVisible = this.annotationContainer?.visible ?? true;
- const captionContainerVisible = this.captionContainer?.visible ?? true;
- if (!includeOverlayLayers) {
- if (this.annotationContainer) {
- this.annotationContainer.visible = false;
- }
- if (this.captionContainer) {
- this.captionContainer.visible = false;
- }
- }
-
- this.app.render();
-
- if (!includeOverlayLayers) {
- if (this.annotationContainer) {
- this.annotationContainer.visible = annotationContainerVisible;
- }
- if (this.captionContainer) {
- this.captionContainer.visible = captionContainerVisible;
- }
- }
-
- return {
- timeMs,
- cursorTimeMs,
- backgroundTimelineTimeMs: backgroundTimelineTimestamp / 1000,
- sceneTransform: {
- scale: this.animationState.appliedScale,
- x: this.animationState.x,
- y: this.animationState.y,
- },
- zoom: {
- scale: this.animationState.scale,
- focusX: this.animationState.focusX,
- focusY: this.animationState.focusY,
- progress: this.animationState.progress,
- },
- };
- }
-
- private async renderTemporalMotionBlurFrame(
- timestamp: number,
- cursorTimestamp: number,
- backgroundTimelineTimestamp: number,
- frameDurationUs: number,
- layoutCache: LayoutCache,
- ): Promise<RenderSnapshot | null> {
- if (!this.app) {
- return null;
- }
-
- const blurConfig = getTemporalMotionBlurConfig(this.config.zoomTemporalMotionBlur, {
- sampleCount: this.config.zoomMotionBlurSampleCount,
- shutterFraction: this.config.zoomMotionBlurShutterFraction,
- });
- if (!blurConfig) {
- return null;
- }
-
- const compositeState = this.ensureTemporalCompositeCanvas();
- if (!compositeState) {
- return null;
- }
-
- const samplePlan = buildTemporalSamplePlanUs(frameDurationUs, blurConfig);
- const webcamReferenceTimeSeconds = Math.max(0, timestamp / 1_000_000);
-
- compositeState.context.clearRect(
- 0,
- 0,
- compositeState.canvas.width,
- compositeState.canvas.height,
- );
-
- let centerSnapshot: RenderSnapshot | null = null;
- let lastSnapshot: RenderSnapshot | null = null;
-
- for (const { offsetUs: sampleOffsetUs, weight } of samplePlan) {
- const sampleTimestamp = Math.max(0, timestamp + sampleOffsetUs);
- const sampleCursorTimestamp = Math.max(0, cursorTimestamp + sampleOffsetUs);
- const sampleBackgroundTimelineTimestamp = Math.max(
- 0,
- backgroundTimelineTimestamp + sampleOffsetUs,
- );
- const snapshot = await this.renderSceneSample(
- sampleTimestamp,
- sampleCursorTimestamp,
- sampleBackgroundTimelineTimestamp,
- layoutCache,
- false,
- false,
- webcamReferenceTimeSeconds,
- );
- lastSnapshot = snapshot;
- if (Math.abs(sampleOffsetUs) < 0.0001) {
- centerSnapshot = snapshot;
- }
-
- compositeState.context.save();
- compositeState.context.globalCompositeOperation = "lighter";
- compositeState.context.globalAlpha = weight;
- compositeState.context.drawImage(this.app.canvas as HTMLCanvasElement, 0, 0);
- compositeState.context.restore();
- }
-
- const resolvedSnapshot = centerSnapshot ?? lastSnapshot;
- if (!resolvedSnapshot) {
- return null;
- }
-
- this.updateCaptionLayer(timestamp / 1000);
-
- const hasOverlayCanvasWork =
- (this.config.annotationRegions?.length ?? 0) > 0 ||
- Boolean(this.captionCanvas && this.captionSprite?.visible);
- if (hasOverlayCanvasWork) {
- await this.composeBlurAnnotationFrame(
- resolvedSnapshot.timeMs,
- resolvedSnapshot.sceneTransform,
- compositeState.canvas,
- );
- } else {
- this.outputCanvasOverride = compositeState.canvas;
- }
-
- return resolvedSnapshot;
- }
-
async renderFrame(
videoFrame: VideoFrame | null,
timestamp: number,
cursorTimestamp = timestamp,
- frameDurationUs?: number,
+ _frameDurationUs?: number,
backgroundTimelineTimestamp = timestamp,
): Promise<void> {
if (!this.app || !this.videoContainer || !this.cameraContainer || !this.videoMaskGraphics) {
@@ -3118,6 +2861,7 @@ export class FrameRenderer {
this.currentVideoTime = timestamp / 1_000_000;
this.cameraContainer.visible = videoFrame !== null;
if (!videoFrame) {
+ this.updateAnimationState(backgroundTimelineTimestamp / 1000, cursorTimestamp / 1000);
const output = this.ensureExportCompositeCanvas();
if (!output) throw new Error("Failed to create gap frame canvas");
output.context.fillStyle = "#000000";
@@ -3160,24 +2904,6 @@ export class FrameRenderer {
throw new Error("Renderer layout cache is unavailable");
}
- const temporalSnapshot =
- TEMPORAL_ZOOM_MOTION_BLUR_ENABLED &&
- (this.config.zoomTemporalMotionBlur ?? 0) > 0 &&
- typeof frameDurationUs === "number" &&
- frameDurationUs > 0
- ? await this.renderTemporalMotionBlurFrame(
- timestamp,
- cursorTimestamp,
- backgroundTimelineTimestamp,
- frameDurationUs,
- layoutCache,
- )
- : null;
-
- if (temporalSnapshot) {
- return;
- }
-
if (this.webcamForwardFrameSource || this.webcamVideoElement) {
await this.syncWebcamFrame(Math.max(0, this.currentVideoTime));
}
@@ -3488,27 +3214,15 @@ export class FrameRenderer {
destroy(): void {
const texturesToDestroy = new Set<Texture>();
- if (this.videoSprite?.texture) {
- texturesToDestroy.add(this.videoSprite.texture);
- }
- if (this.backgroundSprite?.texture) {
- texturesToDestroy.add(this.backgroundSprite.texture);
- }
- if (this.webcamSprite?.texture) {
- texturesToDestroy.add(this.webcamSprite.texture);
- }
- if (this.captionSprite?.texture) {
- texturesToDestroy.add(this.captionSprite.texture);
- }
- for (const layer of this.videoShadowLayers) {
- if (layer.sprite?.texture) {
- texturesToDestroy.add(layer.sprite.texture);
- }
- }
- for (const layer of this.webcamShadowLayers) {
- if (layer.sprite?.texture) {
- texturesToDestroy.add(layer.sprite.texture);
- }
+ for (const sprite of [
+ this.videoSprite,
+ this.backgroundSprite,
+ this.webcamSprite,
+ this.captionSprite,
+ ...this.videoShadowLayers.map((layer) => layer.sprite),
+ ...this.webcamShadowLayers.map((layer) => layer.sprite),
+ ]) {
+ if (sprite?.texture) texturesToDestroy.add(sprite.texture);
}
for (const entry of this.annotationSprites) {
texturesToDestroy.add(entry.texture);
@@ -3614,7 +3328,6 @@ export class FrameRenderer {
this.captionTextureSource = null;
this.captionRenderKey = null;
this.exportCompositeCanvas = null;
- this.temporalCompositeCanvas = null;
this.outputCanvasOverride = null;
this.annotationScaleFactor = 1;
diff --git a/src/lib/exporter/modernVideoExporter.cancellation.test.ts b/src/lib/exporter/modernVideoExporter.cancellation.test.ts
new file mode 100644
index 00000000..17222055
--- /dev/null
+++ b/src/lib/exporter/modernVideoExporter.cancellation.test.ts
@@ -0,0 +1,62 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { AudioProcessor } from "./audioEncoder";
+import { ModernVideoExporter } from "./modernVideoExporter";
+import type { MuxerFinalizeResult } from "./muxer";
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+describe("modern export audio cancellation", () => {
+ it.each([
+ "audio-render",
+ "audio-buffer",
+ "video-buffer",
+ ])("does not start FFmpeg after cancellation during %s", async (stage) => {
+ const mux = vi.fn();
+ vi.stubGlobal("window", { electronAPI: { muxExportedVideoAudio: mux } });
+ const exporter = new ModernVideoExporter({
+ videoUrl: "file:///source.mp4",
+ width: 320,
+ height: 180,
+ frameRate: 30,
+ bitrate: 1_000_000,
+ wallpaper: "#000000",
+ zoomRegions: [],
+ showShadow: false,
+ shadowIntensity: 0,
+ backgroundBlur: 0,
+ cropRegion: { x: 0, y: 0, width: 1, height: 1 },
+ });
+ const audio = new Blob([], { type: "audio/wav" });
+ vi.spyOn(AudioProcessor.prototype, "renderEditedAudioTrack").mockImplementation(
+ async () => {
+ if (stage === "audio-render") exporter.cancel();
+ return audio;
+ },
+ );
+ vi.spyOn(audio, "arrayBuffer").mockImplementation(async () => {
+ if (stage === "audio-buffer") exporter.cancel();
+ return new ArrayBuffer(0);
+ });
+ const video = new Blob([], { type: "video/mp4" });
+ vi.spyOn(video, "arrayBuffer").mockImplementation(async () => {
+ if (stage === "video-buffer") exporter.cancel();
+ return new ArrayBuffer(0);
+ });
+ const access = exporter as unknown as {
+ finalizeExportWithFfmpegAudio(
+ video: MuxerFinalizeResult,
+ plan: unknown,
+ ): Promise<unknown>;
+ };
+ await expect(
+ access.finalizeExportWithFfmpegAudio(
+ { mode: "buffer", blob: video },
+ { audioMode: "edited-track", strategy: "offline-render-fallback" },
+ ),
+ ).rejects.toThrow("Export cancelled");
+ expect(mux).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/lib/exporter/modernVideoExporter.fallback.test.ts b/src/lib/exporter/modernVideoExporter.fallback.test.ts
index 252d4280..f4d291ab 100644
--- a/src/lib/exporter/modernVideoExporter.fallback.test.ts
+++ b/src/lib/exporter/modernVideoExporter.fallback.test.ts
@@ -76,9 +76,83 @@ describe("ModernVideoExporter native fallback routing", () => {
afterEach(() => {
vi.clearAllMocks();
+ if (vi.isMockFunction(console.error)) console.error.mockRestore();
vi.unstubAllGlobals();
});
+ it("removes failed native writes without creating an unhandled rejection", async () => {
+ const exporter = new ModernVideoExporter({} as never) as unknown as {
+ trackNativeWritePromise: (write: Promise<void>) => void;
+ nativeWritePromises: Set<Promise<void>>;
+ };
+ const write = Promise.reject(new Error("Native write failed"));
+ exporter.trackNativeWritePromise(write);
+ await expect(write).rejects.toThrow("Native write failed");
+ expect(exporter.nativeWritePromises.size).toBe(0);
+ });
+
+ it.each([
+ "returned",
+ "thrown",
+ "write",
+ "cancelled",
+ "both-fail",
+ "decode",
+ ])("handles a native runtime failure: %s", async (failure) => {
+ vi.stubGlobal("navigator", { platform: "Win32" });
+ const exporter = new ModernVideoExporter({
+ videoUrl: "file:///recording.mp4",
+ width: 1920,
+ height: 1080,
+ frameRate: 30,
+ bitrate: 8_000_000,
+ wallpaper: "#000000",
+ backendPreference: "auto",
+ } as never) as unknown as {
+ export: () => Promise<{ success: boolean; error?: string }>;
+ cancel: () => void;
+ initializeEncoder: () => Promise<unknown>;
+ tryStartNativeVideoExport: () => Promise<boolean>;
+ finishNativeVideoExport: () => Promise<unknown>;
+ nativeEncoderError: Error | null;
+ };
+ const log = vi.spyOn(console, "error").mockImplementation(() => {});
+ const start = vi.spyOn(exporter, "tryStartNativeVideoExport").mockResolvedValue(true);
+ const initialize = vi.spyOn(exporter, "initializeEncoder").mockImplementation(async () => {
+ if (failure === "both-fail") throw new Error("WebCodecs unavailable");
+ return { hardwareAcceleration: "prefer-hardware" };
+ });
+ vi.spyOn(exporter, "finishNativeVideoExport").mockImplementation(async () => {
+ if (failure === "cancelled") exporter.cancel();
+ if (failure === "thrown" || failure === "cancelled")
+ throw new Error("Native finish failed");
+ return { success: false, error: "Native finish failed" };
+ });
+ if (failure === "write" || failure === "decode") {
+ mocks.streamingDecoderDecodeAll.mockImplementationOnce(async () => {
+ const error = new Error(
+ failure === "write" ? "Native write failed" : "Source decode failed",
+ );
+ if (failure === "write") exporter.nativeEncoderError = error;
+ throw error;
+ });
+ }
+ const result = await exporter.export();
+ expect(start).toHaveBeenCalledTimes(1);
+ const shouldRetry = failure !== "cancelled" && failure !== "decode";
+ expect(initialize).toHaveBeenCalledTimes(shouldRetry ? 1 : 0);
+ expect(result.success).toBe(shouldRetry && failure !== "both-fail");
+ if (shouldRetry)
+ expect(log).toHaveBeenCalledWith(
+ expect.stringContaining("restarting once with WebCodecs"),
+ );
+ if (failure === "both-fail") {
+ expect(result.error).toContain("Native finish failed");
+ expect(result.error).toContain("WebCodecs unavailable");
+ }
+ if (failure === "cancelled") expect(result.error).toBe("Export cancelled");
+ });
+
it("falls back to WebCodecs instead of surfacing a native error when Breeze is unavailable", async () => {
const exporter = new ModernVideoExporter({
videoUrl: "file:///recording.mp4",
diff --git a/src/lib/exporter/modernVideoExporter.ts b/src/lib/exporter/modernVideoExporter.ts
index 1b9326e2..86b7ecc9 100644
--- a/src/lib/exporter/modernVideoExporter.ts
+++ b/src/lib/exporter/modernVideoExporter.ts
@@ -1,4 +1,3 @@
-import { requiresClipTimelineRendering } from "./clipTimeline";
import type {
AnnotationRegion,
AudioRegion,
@@ -54,6 +53,7 @@ import {
shouldPreferNativeAutoBackend,
shouldPreferNativeStaticLayoutBeforeBreeze,
} from "./backendPolicy";
+import { requiresClipTimelineRendering } from "./clipTimeline";
import { buildEditedTrackSourceSegments, classifyEditedTrackStrategy } from "./editedTrackStrategy";
import {
type ExportBackpressureProfile,
@@ -383,11 +383,13 @@ export class ModernVideoExporter {
async export(): Promise<ExportResult> {
let useFallbackMediaSource = false;
let retriedWithFallbackMediaSource = false;
+ let nativeFailure: string | null = null;
this.mediaSourceRetryAttempted = false;
this.runtimeDiagnostics = await this.collectRuntimeDiagnostics();
while (true) {
- let shouldRetryWithFallbackMediaSource = false;
+ let retryExport = false;
+ let inNativeStage = false;
try {
this.cleanup();
this.cancelled = false;
@@ -398,21 +400,21 @@ export class ModernVideoExporter {
this.nativeStaticLayoutBackgroundSkipReason = null;
this.sourceVideoInfo = null;
this.totalExportStartTimeMs = this.getNowMs();
- const backendPreference = this.config.backendPreference ?? "auto";
+ const backendPreference = nativeFailure
+ ? "webcodecs"
+ : (this.config.backendPreference ?? "auto");
const runtimePlatform = this.getRuntimePlatform();
let useNativeEncoder = false;
let triedNativeStaticLayoutWithProbe = false;
const prefersNativeStaticLayoutBeforeBreeze =
shouldPreferNativeStaticLayoutBeforeBreeze(runtimePlatform, backendPreference);
const shouldTryNativeStaticLayout =
- backendPreference === "breeze" ||
- this.config.experimentalNvidiaCudaExport === true ||
- prefersNativeStaticLayoutBeforeBreeze;
- let shouldDeferNativeEncoderStart =
- backendPreference === "breeze" ||
- this.config.experimentalNvidiaCudaExport === true ||
- prefersNativeStaticLayoutBeforeBreeze;
- this.lastNativeExportError = null;
+ !nativeFailure &&
+ (backendPreference === "breeze" ||
+ this.config.experimentalNvidiaCudaExport === true ||
+ prefersNativeStaticLayoutBeforeBreeze);
+ let shouldDeferNativeEncoderStart = shouldTryNativeStaticLayout;
+ this.lastNativeExportError = nativeFailure;
let stageStartedAt = this.getNowMs();
if (shouldDeferNativeEncoderStart) {
@@ -724,11 +726,13 @@ export class ModernVideoExporter {
}
if (useNativeEncoder) {
+ inNativeStage = true;
await this.encodeRenderedFrameNative(
timestamp,
frameDuration,
frameIndex,
);
+ inNativeStage = false;
} else {
await this.encodeRenderedFrame(timestamp, frameDuration, frameIndex);
}
@@ -760,6 +764,7 @@ export class ModernVideoExporter {
this.reportFinalizingProgress(totalFrames, 96);
if (useNativeEncoder) {
+ inNativeStage = true;
stageStartedAt = this.getNowMs();
this.reportFinalizingProgress(totalFrames, 99);
if (this.nativeH264Encoder) {
@@ -773,12 +778,9 @@ export class ModernVideoExporter {
!finishResult.success ||
(!finishResult.tempFilePath && !finishResult.blob)
) {
- return {
- success: false,
- error:
- finishResult.error || `${NATIVE_EXPORT_ENGINE_NAME} export failed`,
- metrics: this.buildExportMetrics(),
- };
+ throw new Error(
+ finishResult.error || `${NATIVE_EXPORT_ENGINE_NAME} export failed`,
+ );
}
return {
@@ -907,6 +909,20 @@ export class ModernVideoExporter {
};
} catch (error) {
if (
+ !this.cancelled &&
+ !nativeFailure &&
+ (inNativeStage || this.nativeEncoderError)
+ ) {
+ nativeFailure = this.buildLightningExportError(
+ this.nativeEncoderError ?? error,
+ );
+ console.error(
+ "[VideoExporter] Native export failed; restarting once with WebCodecs.\n" +
+ nativeFailure,
+ );
+ retryExport = true;
+ } else if (
+ !this.cancelled &&
!useFallbackMediaSource &&
!retriedWithFallbackMediaSource &&
this.shouldRetryWithFallbackMediaSource(error)
@@ -914,7 +930,7 @@ export class ModernVideoExporter {
retriedWithFallbackMediaSource = true;
this.mediaSourceRetryAttempted = true;
useFallbackMediaSource = true;
- shouldRetryWithFallbackMediaSource = true;
+ retryExport = true;
console.warn(
"[VideoExporter] Primary decode path failed; retrying export once with a fresh media source.",
error,
@@ -937,7 +953,7 @@ export class ModernVideoExporter {
};
}
} finally {
- if (!shouldRetryWithFallbackMediaSource && this.totalExportStartTimeMs > 0) {
+ if (!retryExport && this.totalExportStartTimeMs > 0) {
console.log(
`[VideoExporter] Final metrics ${JSON.stringify(this.buildExportMetrics())}`,
);
@@ -945,7 +961,7 @@ export class ModernVideoExporter {
this.cleanup();
}
- if (shouldRetryWithFallbackMediaSource) {
+ if (retryExport) {
continue;
}
}
@@ -2141,12 +2157,19 @@ export class ModernVideoExporter {
),
);
+ this.throwIfCancelled();
+ const editedAudioData = await audioBlob.arrayBuffer();
+ this.throwIfCancelled();
return {
- editedAudioData: await audioBlob.arrayBuffer(),
+ editedAudioData,
editedAudioMimeType: audioBlob.type || null,
};
}
+ private throwIfCancelled(): void {
+ if (this.cancelled) throw new Error("Export cancelled");
+ }
+
private async getNativeStaticLayoutAudioOptions(
audioPlan: NativeAudioPlan,
totalFrames: number,
@@ -2616,6 +2639,7 @@ export class ModernVideoExporter {
);
try {
+ this.throwIfCancelled();
const result = await window.electronAPI.nativeStaticLayoutExport({
sessionId,
inputPath: sourcePath,
@@ -2826,6 +2850,7 @@ export class ModernVideoExporter {
this.queueNativeWriteChunk(sessionId, new Uint8Array(buffer));
},
error: (error) => {
+ if (this.nativeExportSessionId !== sessionId) return;
this.nativeEncoderError = error;
this.notifyEncodeCapacityAvailable();
},
@@ -2889,8 +2914,11 @@ export class ModernVideoExporter {
duration: frameDuration,
colorSpace: EXPORT_CANVAS_COLOR_SPACE,
});
- this.nativeH264Encoder.encode(frame, { keyFrame: frameIndex % 300 === 0 });
- frame.close();
+ try {
+ this.nativeH264Encoder.encode(frame, { keyFrame: frameIndex % 300 === 0 });
+ } finally {
+ frame.close();
+ }
}
private async finishNativeVideoExport(audioPlan: NativeAudioPlan): Promise<ExportResult> {
@@ -2928,6 +2956,7 @@ export class ModernVideoExporter {
this.flushPendingNativeWriteBatch(sessionId);
await this.awaitPendingNativeWrites();
+ this.throwIfCancelled();
const result = await this.measureFinalizationStage("nativeExportFinalizeMs", async () =>
this.awaitWithFinalizationTimeout(
@@ -3015,6 +3044,7 @@ export class ModernVideoExporter {
editedAudioMimeType = renderedAudio.editedAudioMimeType;
}
+ this.throwIfCancelled();
const muxOptions = {
audioMode: audioPlan.audioMode,
audioSourcePath:
@@ -3079,6 +3109,7 @@ export class ModernVideoExporter {
};
}
const videoBuffer = await videoSource.blob.arrayBuffer();
+ this.throwIfCancelled();
const result = await this.measureFinalizationStage("ffmpegAudioMuxMs", async () =>
this.awaitWithFinalizationTimeout(
window.electronAPI.muxExportedVideoAudio(videoBuffer, muxOptions),
@@ -3216,7 +3247,7 @@ export class ModernVideoExporter {
}
})
.catch((error) => {
- if (!this.cancelled) {
+ if (!this.cancelled && this.nativeExportSessionId === sessionId) {
const resolvedError = error instanceof Error ? error : new Error(String(error));
if (!this.nativeEncoderError) {
this.nativeEncoderError = resolvedError;
@@ -3420,9 +3451,10 @@ export class ModernVideoExporter {
this.nativeWritePromises.size,
);
- void writePromise.finally(() => {
+ const removeWrite = () => {
this.nativeWritePromises.delete(writePromise);
- });
+ };
+ void writePromise.then(removeWrite, removeWrite);
}
private async awaitOldestNativeWrite(): Promise<void> {
diff --git a/src/lib/exporter/offlineAudioProcessor.ts b/src/lib/exporter/offlineAudioProcessor.ts
index 4e60597d..e1080db3 100644
--- a/src/lib/exporter/offlineAudioProcessor.ts
+++ b/src/lib/exporter/offlineAudioProcessor.ts
@@ -1,15 +1,15 @@
import { SOURCE_AUDIO_NORMALIZE_GAIN } from "@/components/video-editor/audio/audioTypes";
-import {
- getClipSourceStartMs,
- getClipSourceEndMs,
- getTimelineDurationMs,
-} from "@/components/video-editor/types";
import type {
AudioRegion,
ClipRegion,
SourceAudioTrackSettings,
SpeedRegion,
} from "@/components/video-editor/types";
+import {
+ getClipSourceEndMs,
+ getClipSourceStartMs,
+ getTimelineDurationMs,
+} from "@/components/video-editor/types";
import { buildResolvedAudioPlan } from "@/lib/exporter/audioRoutingEngine";
import { estimateCompanionAudioStartDelaySeconds } from "@/lib/mediaTiming";
import { AudioMediaProcessor } from "./audioMediaProcessor";
@@ -291,6 +291,7 @@ export class OfflineAudioProcessor extends AudioMediaProcessor {
await this.renderChunked(prepared, totalOutputSec, async (rendered) => {
pcmParts.push(...this.audioBufferToPcmParts(rendered));
});
+ if (this.cancelled) throw new Error("Export cancelled");
return new Blob(pcmParts, { type: "audio/wav" });
}
diff --git a/src/lib/exporter/streamingDecodePipeline.ts b/src/lib/exporter/streamingDecodePipeline.ts
index 6ed2298d..acd24c80 100644
--- a/src/lib/exporter/streamingDecodePipeline.ts
+++ b/src/lib/exporter/streamingDecodePipeline.ts
@@ -1,18 +1,18 @@
+import type { WebDemuxer } from "web-demuxer";
import type { SpeedRegion, TrimRegion } from "@/components/video-editor/types";
import { getEffectiveVideoStreamDurationSeconds } from "@/lib/mediaTiming";
-import type { WebDemuxer } from "web-demuxer";
import {
buildVideoDecodeFailure,
+ type DecodedVideoInfo,
getDecodedFrameTimelineOffsetUs,
preserveFirstVideoDecodeFailure,
- type DecodedVideoInfo,
type VideoDecodeFailureContext,
} from "./streamingDecoderSupport";
import {
computeVideoSegments,
- splitVideoSegmentsBySpeed,
segmentFrameCount,
segmentSourceTime,
+ splitVideoSegmentsBySpeed,
type VideoSegment,
} from "./videoTimelineSegments";
@@ -322,14 +322,6 @@ export async function decodeVideoStream(
segmentIdx++;
segmentFrameIndex = 0;
- if (
- heldFrame &&
- segmentIdx < segments.length &&
- heldFrameSec < segments[segmentIdx].startSec - epsilonSec
- ) {
- heldFrame.close();
- heldFrame = null;
- }
}
if (segmentIdx >= segments.length) {
@@ -339,9 +331,11 @@ export async function decodeVideoStream(
const currentSegment = segments[segmentIdx];
- // Before current segment (trimmed region or pre-roll).
+ // Keep the nearest pre-roll frame for cuts between source frames.
if (frameTimeSec < currentSegment.startSec - epsilonSec) {
- frame.close();
+ heldFrame?.close();
+ heldFrame = frame;
+ heldFrameSec = frameTimeSec;
continue;
}
diff --git a/src/lib/exporter/streamingDecoder.test.ts b/src/lib/exporter/streamingDecoder.test.ts
index 542653a2..90b43020 100644
--- a/src/lib/exporter/streamingDecoder.test.ts
+++ b/src/lib/exporter/streamingDecoder.test.ts
@@ -204,9 +204,10 @@ describe("StreamingVideoDecoder decode failures", () => {
});
it.each([
- false,
- true,
- ])("emits real gap frames on the output clock (reordered: %s)", async (reordered) => {
+ [false, 3, 0, 2400],
+ [true, 3, 0, 2400],
+ [false, 0.1, 5, 2405],
+ ])("emits real gap frames (reordered: %s, speed: %s)", async (reordered, speed, firstSource, secondSource) => {
mockDemuxerRead.mockImplementation(
() =>
new ReadableStream({
@@ -239,16 +240,29 @@ describe("StreamingVideoDecoder decode failures", () => {
const decoder = new StreamingVideoDecoder();
await decoder.loadMetadata("/tmp/clip-timeline.mp4");
const clips = [
- { id: "a", startMs: 0, endMs: 400, sourceStartMs: reordered ? 2400 : 0, speed: 3 },
- { id: "b", startMs: 800, endMs: 1200, sourceStartMs: reordered ? 0 : 2400, speed: 3 },
+ {
+ id: "a",
+ startMs: 0,
+ endMs: 400,
+ sourceStartMs: reordered ? secondSource : firstSource,
+ speed,
+ },
+ {
+ id: "b",
+ startMs: 800,
+ endMs: 1200,
+ sourceStartMs: reordered ? firstSource : secondSource,
+ speed,
+ },
];
- const frames: Array<{ gap: boolean; timestamp: number; source: number }> = [];
+ const frames: Array<{ gap: boolean; timestamp: number; source: number; decoded?: number }> =
+ [];
await decoder.decodeAll(
30,
undefined,
undefined,
async (frame, timestamp, source) => {
- frames.push({ gap: frame === null, timestamp, source });
+ frames.push({ gap: frame === null, timestamp, source, decoded: frame?.timestamp });
},
clips,
);
@@ -258,7 +272,10 @@ describe("StreamingVideoDecoder decode failures", () => {
expect(frames[i].timestamp).toBeCloseTo((i * 1_000_000) / 30, 5);
expect(frames[i].gap).toBe(i >= 12 && i < 24);
}
- expect(frames[24].source).toBeCloseTo(reordered ? 0 : 2400);
+ expect(frames[24].source).toBeCloseTo(reordered ? firstSource : secondSource);
+ expect(frames[0].decoded).toBeCloseTo(
+ (Math.round(((reordered ? secondSource : firstSource) * 30) / 1000) * 1_000_000) / 30,
+ );
expect(decoder.getEffectiveDuration(undefined, undefined, clips)).toBe(1.2);
});
});