Skip to content

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

38.3 KB
Raw Blame
import { getLocalMediaServerPath } from "@/lib/localMediaUrl";
import type { SourceAudioTrackSettings } from "@/components/video-editor/audio/audioTypes";
import { DEFAULT_WALLPAPER_PATH } from "@/lib/wallpapers";
import { ASPECT_RATIOS, type AspectRatio, isCustomAspectRatio } from "@/utils/aspectRatioUtils";
import { closeClipGaps, rippleRegionAnchors, rippleRegions } from "./clipSequence";
import { CURSOR_MOTION_PRESETS, resolveCursorMotionPresetId } from "./cursorMotionPresets";
import {
	ADVANCED_VERTICAL_PADDING_MAX,
	type AnnotationRegion,
	type AudioRegion,
	type AutoCaptionAnimation,
	type AutoCaptionSettings,
	type CaptionCue,
	type CaptionCueWord,
	type ClipRegion,
	type CropRegion,
	type CursorClickEffectStyle,
	type CursorStyle,
	DEFAULT_ANNOTATION_POSITION,
	DEFAULT_ANNOTATION_SIZE,
	DEFAULT_ANNOTATION_STYLE,
	DEFAULT_AUTO_CAPTION_SETTINGS,
	DEFAULT_CONNECTED_ZOOM_DURATION_MS,
	DEFAULT_CONNECTED_ZOOM_EASING,
	DEFAULT_CONNECTED_ZOOM_GAP_MS,
	DEFAULT_CROP_REGION,
	DEFAULT_CURSOR_CLICK_EFFECT,
	DEFAULT_CURSOR_CLICK_EFFECT_COLOR,
	DEFAULT_CURSOR_CLICK_EFFECT_DURATION_MS,
	DEFAULT_CURSOR_CLICK_EFFECT_OPACITY,
	DEFAULT_CURSOR_CLICK_EFFECT_SCALE,
	DEFAULT_CURSOR_MOTION_BLUR,
	DEFAULT_CURSOR_STYLE,
	DEFAULT_CURSOR_SWAY,
	DEFAULT_FIGURE_DATA,
	DEFAULT_PADDING,
	DEFAULT_PLAYBACK_SPEED,
	DEFAULT_WEBCAM_MARGIN,
	DEFAULT_WEBCAM_OVERLAY,
	DEFAULT_WEBCAM_POSITION_PRESET,
	DEFAULT_WEBCAM_POSITION_X,
	DEFAULT_WEBCAM_POSITION_Y,
	DEFAULT_WEBCAM_REACT_TO_ZOOM,
	DEFAULT_WEBCAM_ROUNDNESS,
	DEFAULT_WEBCAM_SHADOW,
	DEFAULT_WEBCAM_SIZE,
	DEFAULT_WEBCAM_TIME_OFFSET_MS,
	DEFAULT_ZOOM_DEPTH,
	DEFAULT_ZOOM_IN_EASING,
	DEFAULT_ZOOM_IN_OVERLAP_MS,
	DEFAULT_ZOOM_MOTION_BLUR,
	DEFAULT_ZOOM_MOTION_BLUR_TUNING,
	DEFAULT_ZOOM_OUT_EASING,
	DEFAULT_ZOOM_SMOOTHNESS,
	getDefaultCaptionFontFamily,
	normalizeCursorClickEffectColor,
	normalizeCursorClickEffectStyle,
	type Padding,
	type SpeedRegion,
	type TrimRegion,
	type WebcamOverlaySettings,
	type ZoomMotionBlurTuning,
	type ZoomRegion,
	type ZoomTransitionEasing,
} from "./types";
import { convertLegacyWebcamRadiusToRoundness, normalizeWebcamCropRegion } from "./webcamOverlay";

export const PROJECT_VERSION = 2;
export const MACOS_DEFAULT_BORDER_RADIUS_PERCENT = 8;
const LEGACY_BORDER_RADIUS_REFERENCE_PX = 1080;

export function getDefaultBorderRadiusPercent(
	platform = typeof navigator === "undefined" ? "" : navigator.platform,
): number {
	return /mac/i.test(platform) ? MACOS_DEFAULT_BORDER_RADIUS_PERCENT : 0;
}

export function legacyBorderRadiusPixelsToPercent(value: number): number {
	return (value / LEGACY_BORDER_RADIUS_REFERENCE_PX) * 100;
}

const DEFAULT_MOTION_PRESET = CURSOR_MOTION_PRESETS.focused;

export interface ProjectEditorState {
	wallpaper: string;
	shadowIntensity: number;
	backgroundBlur: number;
	zoomMotionBlur: number;
	zoomMotionBlurTuning: ZoomMotionBlurTuning;
	connectZooms: boolean;
	zoomInDurationMs: number;
	zoomInOverlapMs: number;
	zoomOutDurationMs: number;
	connectedZoomGapMs: number;
	connectedZoomDurationMs: number;
	zoomInEasing: ZoomTransitionEasing;
	zoomOutEasing: ZoomTransitionEasing;
	connectedZoomEasing: ZoomTransitionEasing;
	showCursor: boolean;
	loopCursor: boolean;
	cursorStyle: CursorStyle;
	cursorClickEffect: CursorClickEffectStyle;
	cursorClickEffectColor: string;
	cursorClickEffectScale: number;
	cursorClickEffectOpacity: number;
	cursorClickEffectDurationMs: number;
	cursorSize: number;
	cursorSmoothing: number;
	cursorSpringStiffnessMultiplier: number;
	cursorSpringDampingMultiplier: number;
	cursorSpringMassMultiplier: number;
	cameraSpringStiffnessMultiplier: number;
	cameraSpringDampingMultiplier: number;
	cameraSpringMassMultiplier: number;
	zoomSmoothness: number;
	zoomClassicMode: boolean;
	cursorMotionBlur: number;
	cursorClickBounce: number;
	cursorClickBounceDuration: number;
	cursorSway: number;
	borderRadius: number;
	padding: Padding;
	cropRegion: CropRegion;
	zoomRegions: ZoomRegion[];
	trimRegions: TrimRegion[];
	clipRegions: ClipRegion[];
	autoFullTrackClipId?: string | null;
	autoFullTrackClipEndMs?: number | null;
	speedRegions: SpeedRegion[];
	annotationRegions: AnnotationRegion[];
	audioRegions: AudioRegion[];
	autoCaptions: CaptionCue[];
	autoCaptionSettings: AutoCaptionSettings;
	webcam: WebcamOverlaySettings;
	aspectRatio: AspectRatio;
	sourceAudioTrackSettingsByClip?: Record<string, SourceAudioTrackSettings>;
	defaultSourceAudioTrackSettings?: SourceAudioTrackSettings;
}

export interface EditorProjectData {
	version: number;
	projectId?: string;
	videoPath: string;
	editor: Partial<ProjectEditorState>;
}

function isFiniteNumber(value: unknown): value is number {
	return typeof value === "number" && Number.isFinite(value);
}

function clamp(value: number, min: number, max: number) {
	return Math.min(max, Math.max(min, value));
}

type PersistedDevMotionBlurSettings = {
	zoomMotionBlurTuning?: unknown;
};

export function stripPersistedDevMotionBlurSettings<T extends PersistedDevMotionBlurSettings>(
	editor: T,
): Omit<T, keyof PersistedDevMotionBlurSettings> {
	const { zoomMotionBlurTuning: _zoomMotionBlurTuning, ...persistedEditor } = editor;

	return persistedEditor;
}

function normalizeZoomTransitionEasing(
	value: unknown,
	fallback: ZoomTransitionEasing,
): ZoomTransitionEasing {
	return value === "recordly" ||
		value === "glide" ||
		value === "smooth" ||
		value === "snappy" ||
		value === "linear"
		? value
		: fallback;
}

function normalizeAutoCaptionAnimation(
	value: unknown,
	fallback: AutoCaptionAnimation,
): AutoCaptionAnimation {
	return value === "none" || value === "fade" || value === "rise" || value === "pop"
		? value
		: fallback;
}

function isFileUrl(value: string): boolean {
	return /^file:\/\//i.test(value);
}

function encodePathSegments(pathname: string, keepWindowsDrive = false): string {
	return pathname
		.split("/")
		.map((segment, index) => {
			if (!segment) return "";
			if (keepWindowsDrive && index === 1 && /^[a-zA-Z]:$/.test(segment)) {
				return segment;
			}
			return encodeURIComponent(segment);
		})
		.join("/");
}

export function toFileUrl(filePath: string): string {
	const normalized = filePath.replace(/\\/g, "/");

	// Windows drive path: C:/Users/...
	if (/^[a-zA-Z]:\//.test(normalized)) {
		return `file://${encodePathSegments(`/${normalized}`, true)}`;
	}

	// UNC path: //server/share/...
	if (normalized.startsWith("//")) {
		const [host, ...pathParts] = normalized.replace(/^\/+/, "").split("/");
		const encodedPath = pathParts.map((part) => encodeURIComponent(part)).join("/");
		return encodedPath ? `file://${host}/${encodedPath}` : `file://${host}/`;
	}

	const absolutePath = normalized.startsWith("/") ? normalized : `/${normalized}`;
	return `file://${encodePathSegments(absolutePath)}`;
}

export function fromFileUrl(fileUrl: string): string {
	const value = fileUrl.trim();
	const serverPath = getLocalMediaServerPath(value);
	if (serverPath) return serverPath;
	if (!isFileUrl(value)) {
		return fileUrl;
	}

	try {
		const url = new URL(value);
		const pathname = decodeURIComponent(url.pathname);

		if (url.host && url.host !== "localhost") {
			const uncPath = `//${url.host}${pathname.startsWith("/") ? pathname : `/${pathname}`}`;
			return uncPath.replace(/\//g, "\\");
		}

		if (/^\/[A-Za-z]:/.test(pathname)) {
			return pathname.slice(1);
		}

		return pathname;
	} catch {
		const rawFallbackPath = value.replace(/^file:\/\//i, "");
		let fallbackPath = rawFallbackPath;
		try {
			fallbackPath = decodeURIComponent(rawFallbackPath);
		} catch {
			// Keep raw best-effort path if percent decoding fails.
		}
		return fallbackPath.replace(/^\/([a-zA-Z]:)/, "$1");
	}
}

export function deriveNextId(prefix: string, ids: string[]): number {
	const max = ids.reduce((acc, id) => {
		const match = id.match(new RegExp(`^${prefix}-(\\d+)$`));
		if (!match) return acc;
		const value = Number(match[1]);
		return Number.isFinite(value) ? Math.max(acc, value) : acc;
	}, 0);
	return max + 1;
}

/**
 * Resolve a local file path to a URL the `<video>` element can load.
 *
 * Prefers the local media HTTP server (works on all platforms regardless of
 * Chromium's `file://` restrictions).  Falls back to a `file://` URL if the
 * media server is unavailable.
 */
export async function resolveVideoUrl(sourcePath: string): Promise<string> {
	const trimmedSourcePath = fromFileUrl(sourcePath.trim());
	if (/^(?:https?:|blob:|data:)/i.test(trimmedSourcePath)) {
		return trimmedSourcePath;
	}
	const localPath = fromFileUrl(trimmedSourcePath);
	try {
		const result = await window.electronAPI.getLocalMediaUrl(localPath);
		if (result.success) {
			return result.url;
		}
	} catch {
		// Media server unavailable — fall through to file:// URL.
	}
	return toFileUrl(localPath);
}

export function validateProjectData(candidate: unknown): candidate is EditorProjectData {
	if (!candidate || typeof candidate !== "object") return false;
	const project = candidate as Partial<EditorProjectData>;
	if (typeof project.version !== "number") return false;
	if (project.projectId !== undefined && typeof project.projectId !== "string") return false;
	if (typeof project.videoPath !== "string" || !project.videoPath) return false;
	if (!project.editor || typeof project.editor !== "object") return false;
	return true;
}

export function normalizeProjectEditor(editor: Partial<ProjectEditorState>): ProjectEditorState {
	const validAspectRatios = new Set<AspectRatio>(ASPECT_RATIOS);
	const legacyMotionBlurEnabled = (editor as Partial<{ motionBlurEnabled: boolean }>)
		.motionBlurEnabled;
	const legacyShowBlur = (editor as Partial<{ showBlur: boolean }>).showBlur;
	const normalizedZoomMotionBlur = isFiniteNumber(
		(editor as Partial<ProjectEditorState>).zoomMotionBlur,
	)
		? clamp((editor as Partial<ProjectEditorState>).zoomMotionBlur as number, 0, 2)
		: legacyMotionBlurEnabled
			? 0.35
			: DEFAULT_ZOOM_MOTION_BLUR;
	const rawZoomMotionBlurTuning =
		(editor as Partial<ProjectEditorState>).zoomMotionBlurTuning &&
		typeof (editor as Partial<ProjectEditorState>).zoomMotionBlurTuning === "object"
			? ((editor as Partial<ProjectEditorState>)
					.zoomMotionBlurTuning as Partial<ZoomMotionBlurTuning>)
			: {};
	const normalizedZoomMotionBlurTuning: ZoomMotionBlurTuning = {
		panVelocityThreshold: isFiniteNumber(rawZoomMotionBlurTuning.panVelocityThreshold)
			? clamp(rawZoomMotionBlurTuning.panVelocityThreshold, 0, 240)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.panVelocityThreshold,
		zoomVelocityThreshold: isFiniteNumber(rawZoomMotionBlurTuning.zoomVelocityThreshold)
			? clamp(rawZoomMotionBlurTuning.zoomVelocityThreshold, 0, 0.4)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.zoomVelocityThreshold,
		maxDirectionalBlurPx: isFiniteNumber(rawZoomMotionBlurTuning.maxDirectionalBlurPx)
			? clamp(rawZoomMotionBlurTuning.maxDirectionalBlurPx, 0, 96)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.maxDirectionalBlurPx,
		maxRadialBlurStrength: isFiniteNumber(rawZoomMotionBlurTuning.maxRadialBlurStrength)
			? clamp(rawZoomMotionBlurTuning.maxRadialBlurStrength, 0, 1.5)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.maxRadialBlurStrength,
		panResponsePerSecond: isFiniteNumber(rawZoomMotionBlurTuning.panResponsePerSecond)
			? clamp(rawZoomMotionBlurTuning.panResponsePerSecond, 1, 30)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.panResponsePerSecond,
		zoomResponsePerSecond: isFiniteNumber(rawZoomMotionBlurTuning.zoomResponsePerSecond)
			? clamp(rawZoomMotionBlurTuning.zoomResponsePerSecond, 1, 30)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.zoomResponsePerSecond,
		zoomSafeZoneRadiusPx: isFiniteNumber(rawZoomMotionBlurTuning.zoomSafeZoneRadiusPx)
			? clamp(rawZoomMotionBlurTuning.zoomSafeZoneRadiusPx, 0, 80)
			: DEFAULT_ZOOM_MOTION_BLUR_TUNING.zoomSafeZoneRadiusPx,
	};
	const normalizedBackgroundBlur = isFiniteNumber(
		(editor as Partial<ProjectEditorState>).backgroundBlur,
	)
		? clamp((editor as Partial<ProjectEditorState>).backgroundBlur as number, 0, 8)
		: legacyShowBlur
			? 2
			: 0;
	const normalizedZoomInDurationMs = isFiniteNumber(editor.zoomInDurationMs)
		? clamp(editor.zoomInDurationMs, 60, 4000)
		: DEFAULT_MOTION_PRESET.zoomInDurationMs;
	const normalizedZoomInOverlapMs = isFiniteNumber(editor.zoomInOverlapMs)
		? clamp(editor.zoomInOverlapMs, 0, normalizedZoomInDurationMs)
		: DEFAULT_ZOOM_IN_OVERLAP_MS;
	const normalizedZoomOutDurationMs = isFiniteNumber(editor.zoomOutDurationMs)
		? clamp(editor.zoomOutDurationMs, 60, 4000)
		: DEFAULT_MOTION_PRESET.zoomOutDurationMs;
	const normalizedConnectedZoomGapMs = isFiniteNumber(editor.connectedZoomGapMs)
		? clamp(editor.connectedZoomGapMs, 0, 5000)
		: DEFAULT_CONNECTED_ZOOM_GAP_MS;
	const normalizedConnectedZoomDurationMs = isFiniteNumber(editor.connectedZoomDurationMs)
		? clamp(editor.connectedZoomDurationMs, 60, 4000)
		: DEFAULT_CONNECTED_ZOOM_DURATION_MS;

	const normalizedZoomRegions: ZoomRegion[] = Array.isArray(editor.zoomRegions)
		? editor.zoomRegions
				.filter((region): region is ZoomRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);

					return {
						id: region.id,
						startMs,
						endMs,
						depth: [1, 2, 3, 4, 5, 6].includes(region.depth)
							? region.depth
							: DEFAULT_ZOOM_DEPTH,
						focus: {
							cx: clamp(
								isFiniteNumber(region.focus?.cx) ? region.focus.cx : 0.5,
								0,
								1,
							),
							cy: clamp(
								isFiniteNumber(region.focus?.cy) ? region.focus.cy : 0.5,
								0,
								1,
							),
						},
						mode:
							region.mode === "auto" || region.mode === "manual"
								? region.mode
								: undefined,
					};
				})
		: [];

	const normalizedTrimRegions: TrimRegion[] = Array.isArray(editor.trimRegions)
		? editor.trimRegions
				.filter((region): region is TrimRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);
					return {
						id: region.id,
						startMs,
						endMs,
					};
				})
		: [];

	const normalizedClipRegions: ClipRegion[] = Array.isArray(editor.clipRegions)
		? editor.clipRegions
				.filter((region): region is ClipRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);
					const sourceStartMs = isFiniteNumber(region.sourceStartMs)
						? Math.max(0, Math.round(region.sourceStartMs))
						: undefined;
					let sourceMinMs = isFiniteNumber(region.sourceMinMs)
						? Math.max(0, Math.round(region.sourceMinMs))
						: undefined;
					let sourceMaxMs = isFiniteNumber(region.sourceMaxMs)
						? Math.max(0, Math.round(region.sourceMaxMs))
						: undefined;
					if (
						sourceMaxMs !== undefined &&
						sourceMaxMs < Math.max(sourceMinMs ?? 0, sourceStartMs ?? startMs)
					) {
						sourceMinMs = undefined;
						sourceMaxMs = undefined;
					}
					return {
						id: region.id,
						startMs,
						endMs,
						sourceStartMs,
						sourceMinMs,
						sourceMaxMs,
						speed: isFiniteNumber(region.speed) && region.speed > 0 ? region.speed : 1,
						muted: typeof region.muted === "boolean" ? region.muted : false,
						showSourceAudio:
							typeof region.showSourceAudio === "boolean"
								? region.showSourceAudio
								: false,
					};
				})
		: [];

	// Migrate before applying editor state, so loaded history and the saved baseline
	// both begin with the same canonical sequence rather than recording a repair edit.
	const sequenceClips = closeClipGaps(normalizedClipRegions);
	const sequenceChanged = sequenceClips.some(
		(clip, index) => clip !== normalizedClipRegions[index],
	);

	const normalizedAutoFullTrackClipId =
		typeof editor.autoFullTrackClipId === "string" ? editor.autoFullTrackClipId : null;
	const normalizedAutoFullTrackClipEndMs = isFiniteNumber(editor.autoFullTrackClipEndMs)
		? Math.round(editor.autoFullTrackClipEndMs)
		: null;

	const normalizedSpeedRegions: SpeedRegion[] = Array.isArray(editor.speedRegions)
		? editor.speedRegions
				.filter((region): region is SpeedRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);

					const speed =
						region.speed === 0.25 ||
						region.speed === 0.5 ||
						region.speed === 0.75 ||
						region.speed === 1.25 ||
						region.speed === 1.5 ||
						region.speed === 1.75 ||
						region.speed === 2
							? region.speed
							: DEFAULT_PLAYBACK_SPEED;

					return {
						id: region.id,
						startMs,
						endMs,
						speed,
					};
				})
		: [];

	const normalizedAnnotationRegions: AnnotationRegion[] = Array.isArray(editor.annotationRegions)
		? editor.annotationRegions
				.filter((region): region is AnnotationRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region, index) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);

					return {
						id: region.id,
						startMs,
						endMs,
						type:
							region.type === "image" ||
							region.type === "figure" ||
							region.type === "blur"
								? region.type
								: "text",
						content: typeof region.content === "string" ? region.content : "",
						textContent:
							typeof region.textContent === "string" ? region.textContent : undefined,
						imageContent:
							typeof region.imageContent === "string"
								? region.imageContent
								: undefined,
						position: {
							x: isFiniteNumber(region.position?.x)
								? region.position.x
								: DEFAULT_ANNOTATION_POSITION.x,
							y: isFiniteNumber(region.position?.y)
								? region.position.y
								: DEFAULT_ANNOTATION_POSITION.y,
						},
						size: {
							width: clamp(
								isFiniteNumber(region.size?.width)
									? region.size.width
									: DEFAULT_ANNOTATION_SIZE.width,
								1,
								10000,
							),
							height: clamp(
								isFiniteNumber(region.size?.height)
									? region.size.height
									: DEFAULT_ANNOTATION_SIZE.height,
								1,
								10000,
							),
						},
						style: {
							...DEFAULT_ANNOTATION_STYLE,
							...(region.style && typeof region.style === "object"
								? region.style
								: {}),
						},
						zIndex: isFiniteNumber(region.zIndex) ? region.zIndex : index + 1,
						figureData: region.figureData
							? {
									...DEFAULT_FIGURE_DATA,
									...region.figureData,
								}
							: undefined,
						blurIntensity: isFiniteNumber(region.blurIntensity)
							? clamp(region.blurIntensity, 1, 100)
							: 20,
						blurColor:
							typeof region.blurColor === "string" ? region.blurColor : undefined,
						trackIndex: isFiniteNumber(region.trackIndex)
							? Math.max(0, Math.floor(region.trackIndex))
							: 0,
					};
				})
		: [];

	const normalizedAudioRegions: AudioRegion[] = Array.isArray(
		(editor as Partial<ProjectEditorState>).audioRegions,
	)
		? ((editor as Partial<ProjectEditorState>).audioRegions as AudioRegion[])
				.filter((region): region is AudioRegion =>
					Boolean(region && typeof region.id === "string"),
				)
				.map((region) => {
					const rawStart = isFiniteNumber(region.startMs)
						? Math.round(region.startMs)
						: 0;
					const rawEnd = isFiniteNumber(region.endMs)
						? Math.round(region.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);

					return {
						id: region.id,
						startMs,
						endMs,
						audioPath: typeof region.audioPath === "string" ? region.audioPath : "",
						volume: isFiniteNumber(region.volume) ? clamp(region.volume, 0, 1) : 1,
						normalize: Boolean(region.normalize),
						trackIndex: isFiniteNumber(region.trackIndex)
							? Math.max(0, Math.floor(region.trackIndex))
							: 0,
					};
				})
		: [];

	const normalizedAutoCaptions: CaptionCue[] = Array.isArray(
		(editor as Partial<ProjectEditorState>).autoCaptions,
	)
		? ((editor as Partial<ProjectEditorState>).autoCaptions as CaptionCue[])
				.filter((cue): cue is CaptionCue => Boolean(cue && typeof cue.id === "string"))
				.map((cue) => {
					const rawStart = isFiniteNumber(cue.startMs) ? Math.round(cue.startMs) : 0;
					const rawEnd = isFiniteNumber(cue.endMs)
						? Math.round(cue.endMs)
						: rawStart + 1000;
					const startMs = Math.max(0, Math.min(rawStart, rawEnd));
					const endMs = Math.max(startMs + 1, rawEnd);
					const words: CaptionCueWord[] | undefined = Array.isArray(cue.words)
						? cue.words
								.filter((word): word is CaptionCueWord =>
									Boolean(word && typeof word.text === "string"),
								)
								.map((word) => {
									const rawWordStart = isFiniteNumber(word.startMs)
										? Math.round(word.startMs)
										: startMs;
									const rawWordEnd = isFiniteNumber(word.endMs)
										? Math.round(word.endMs)
										: rawWordStart + 1;
									const normalizedWordStart = clamp(
										rawWordStart,
										startMs,
										endMs - 1,
									);
									const normalizedWordEnd = clamp(
										rawWordEnd,
										normalizedWordStart + 1,
										endMs,
									);

									return {
										text: word.text.trim(),
										startMs: normalizedWordStart,
										endMs: normalizedWordEnd,
										...(word.leadingSpace ? { leadingSpace: true } : {}),
									};
								})
								.filter((word) => word.text.length > 0)
						: undefined;

					return {
						id: cue.id,
						startMs,
						endMs,
						text: typeof cue.text === "string" ? cue.text.trim() : "",
						...(words && words.length > 0 ? { words } : {}),
					};
				})
				.filter((cue) => cue.text.length > 0)
		: [];

	const rawAutoCaptionSettings: Partial<AutoCaptionSettings> =
		editor.autoCaptionSettings && typeof editor.autoCaptionSettings === "object"
			? (editor.autoCaptionSettings as Partial<AutoCaptionSettings>)
			: {};
	const normalizedAutoCaptionSettings: AutoCaptionSettings = {
		enabled:
			typeof rawAutoCaptionSettings.enabled === "boolean"
				? rawAutoCaptionSettings.enabled
				: DEFAULT_AUTO_CAPTION_SETTINGS.enabled,
		timelineQuickAdd:
			typeof rawAutoCaptionSettings.timelineQuickAdd === "boolean"
				? rawAutoCaptionSettings.timelineQuickAdd
				: DEFAULT_AUTO_CAPTION_SETTINGS.timelineQuickAdd,
		language:
			typeof rawAutoCaptionSettings.language === "string" &&
			rawAutoCaptionSettings.language.trim()
				? rawAutoCaptionSettings.language.trim()
				: DEFAULT_AUTO_CAPTION_SETTINGS.language,
		fontFamily: getDefaultCaptionFontFamily(),
		fontSize: isFiniteNumber(rawAutoCaptionSettings.fontSize)
			? clamp(rawAutoCaptionSettings.fontSize, 16, 72)
			: DEFAULT_AUTO_CAPTION_SETTINGS.fontSize,
		bottomOffset: isFiniteNumber(rawAutoCaptionSettings.bottomOffset)
			? clamp(rawAutoCaptionSettings.bottomOffset, 0, 30)
			: DEFAULT_AUTO_CAPTION_SETTINGS.bottomOffset,
		maxWidth: isFiniteNumber(rawAutoCaptionSettings.maxWidth)
			? clamp(rawAutoCaptionSettings.maxWidth, 40, 95)
			: DEFAULT_AUTO_CAPTION_SETTINGS.maxWidth,
		maxRows: isFiniteNumber(rawAutoCaptionSettings.maxRows)
			? clamp(Math.round(rawAutoCaptionSettings.maxRows), 1, 4)
			: DEFAULT_AUTO_CAPTION_SETTINGS.maxRows,
		animationStyle: normalizeAutoCaptionAnimation(
			rawAutoCaptionSettings.animationStyle,
			DEFAULT_AUTO_CAPTION_SETTINGS.animationStyle,
		),
		boxRadius: isFiniteNumber(rawAutoCaptionSettings.boxRadius)
			? clamp(rawAutoCaptionSettings.boxRadius, 0, 40)
			: DEFAULT_AUTO_CAPTION_SETTINGS.boxRadius,
		textColor:
			typeof rawAutoCaptionSettings.textColor === "string" &&
			rawAutoCaptionSettings.textColor.trim()
				? rawAutoCaptionSettings.textColor
				: DEFAULT_AUTO_CAPTION_SETTINGS.textColor,
		inactiveTextColor:
			typeof rawAutoCaptionSettings.inactiveTextColor === "string" &&
			rawAutoCaptionSettings.inactiveTextColor.trim()
				? rawAutoCaptionSettings.inactiveTextColor
				: DEFAULT_AUTO_CAPTION_SETTINGS.inactiveTextColor,
		backgroundOpacity: isFiniteNumber(rawAutoCaptionSettings.backgroundOpacity)
			? clamp(rawAutoCaptionSettings.backgroundOpacity, 0, 1)
			: DEFAULT_AUTO_CAPTION_SETTINGS.backgroundOpacity,
	};

	const rawCropX = isFiniteNumber(editor.cropRegion?.x)
		? editor.cropRegion.x
		: DEFAULT_CROP_REGION.x;
	const rawCropY = isFiniteNumber(editor.cropRegion?.y)
		? editor.cropRegion.y
		: DEFAULT_CROP_REGION.y;
	const rawCropWidth = isFiniteNumber(editor.cropRegion?.width)
		? editor.cropRegion.width
		: DEFAULT_CROP_REGION.width;
	const rawCropHeight = isFiniteNumber(editor.cropRegion?.height)
		? editor.cropRegion.height
		: DEFAULT_CROP_REGION.height;

	const cropX = clamp(rawCropX, 0, 1);
	const cropY = clamp(rawCropY, 0, 1);
	const cropWidth = clamp(rawCropWidth, 0.01, 1 - cropX);
	const cropHeight = clamp(rawCropHeight, 0.01, 1 - cropY);

	const webcam: Partial<WebcamOverlaySettings> =
		editor.webcam && typeof editor.webcam === "object" ? editor.webcam : {};
	const webcamSourcePath = typeof webcam.sourcePath === "string" ? webcam.sourcePath : null;
	const legacyZoomScaleEffect = isFiniteNumber(
		(webcam as Partial<{ zoomScaleEffect: number }>).zoomScaleEffect,
	)
		? (webcam as Partial<{ zoomScaleEffect: number }>).zoomScaleEffect
		: null;
	const normalizedCursorStyle =
		typeof editor.cursorStyle === "string" && editor.cursorStyle.trim().length > 0
			? editor.cursorStyle === "mono"
				? "tahoe-inverted"
				: editor.cursorStyle
			: DEFAULT_CURSOR_STYLE;
	const normalizedMotionValues = {
		zoomInDurationMs: normalizedZoomInDurationMs,
		zoomOutDurationMs: normalizedZoomOutDurationMs,
		cursorSize: isFiniteNumber(editor.cursorSize)
			? clamp(editor.cursorSize, 0.5, 10)
			: DEFAULT_MOTION_PRESET.cursorSize,
		cursorSmoothing: isFiniteNumber(editor.cursorSmoothing)
			? clamp(editor.cursorSmoothing, 0, 2)
			: DEFAULT_MOTION_PRESET.cursorSmoothing,
		cursorSpringStiffnessMultiplier: isFiniteNumber(editor.cursorSpringStiffnessMultiplier)
			? clamp(editor.cursorSpringStiffnessMultiplier, 0.25, 3)
			: DEFAULT_MOTION_PRESET.cursorSpringStiffnessMultiplier,
		cursorSpringDampingMultiplier: isFiniteNumber(editor.cursorSpringDampingMultiplier)
			? clamp(editor.cursorSpringDampingMultiplier, 0.25, 3)
			: DEFAULT_MOTION_PRESET.cursorSpringDampingMultiplier,
		cursorSpringMassMultiplier: isFiniteNumber(editor.cursorSpringMassMultiplier)
			? clamp(editor.cursorSpringMassMultiplier, 0.25, 3)
			: DEFAULT_MOTION_PRESET.cursorSpringMassMultiplier,
		cursorClickBounce: isFiniteNumber((editor as Partial<ProjectEditorState>).cursorClickBounce)
			? clamp((editor as Partial<ProjectEditorState>).cursorClickBounce as number, 0, 5)
			: DEFAULT_MOTION_PRESET.cursorClickBounce,
		cursorClickBounceDuration: isFiniteNumber(
			(editor as Partial<ProjectEditorState>).cursorClickBounceDuration,
		)
			? clamp(
					(editor as Partial<ProjectEditorState>).cursorClickBounceDuration as number,
					60,
					500,
				)
			: DEFAULT_MOTION_PRESET.cursorClickBounceDuration,
	};
	const normalizedCursorClickEffectScale = isFiniteNumber(
		(editor as Partial<ProjectEditorState>).cursorClickEffectScale,
	)
		? clamp((editor as Partial<ProjectEditorState>).cursorClickEffectScale as number, 0.5, 2)
		: DEFAULT_CURSOR_CLICK_EFFECT_SCALE;
	const normalizedCursorClickEffectOpacity = isFiniteNumber(
		(editor as Partial<ProjectEditorState>).cursorClickEffectOpacity,
	)
		? clamp((editor as Partial<ProjectEditorState>).cursorClickEffectOpacity as number, 0, 1)
		: DEFAULT_CURSOR_CLICK_EFFECT_OPACITY;
	const normalizedCursorClickEffectDurationMs = isFiniteNumber(
		(editor as Partial<ProjectEditorState>).cursorClickEffectDurationMs,
	)
		? clamp(
				(editor as Partial<ProjectEditorState>).cursorClickEffectDurationMs as number,
				120,
				1200,
			)
		: DEFAULT_CURSOR_CLICK_EFFECT_DURATION_MS;
	const normalizedCursorClickEffectColor = normalizeCursorClickEffectColor(
		(editor as Partial<ProjectEditorState>).cursorClickEffectColor,
		DEFAULT_CURSOR_CLICK_EFFECT_COLOR,
	);
	const normalizedMotionPreset =
		CURSOR_MOTION_PRESETS[resolveCursorMotionPresetId(normalizedMotionValues)];

	return {
		wallpaper: typeof editor.wallpaper === "string" ? editor.wallpaper : DEFAULT_WALLPAPER_PATH,
		shadowIntensity: typeof editor.shadowIntensity === "number" ? editor.shadowIntensity : 0.67,
		backgroundBlur: normalizedBackgroundBlur,
		zoomMotionBlur: normalizedZoomMotionBlur,
		zoomMotionBlurTuning: normalizedZoomMotionBlurTuning,
		connectZooms: typeof editor.connectZooms === "boolean" ? editor.connectZooms : true,
		zoomInDurationMs: normalizedMotionPreset.zoomInDurationMs,
		zoomInOverlapMs: normalizedZoomInOverlapMs,
		zoomOutDurationMs: normalizedMotionPreset.zoomOutDurationMs,
		connectedZoomGapMs: normalizedConnectedZoomGapMs,
		connectedZoomDurationMs: normalizedConnectedZoomDurationMs,
		zoomInEasing: normalizeZoomTransitionEasing(editor.zoomInEasing, DEFAULT_ZOOM_IN_EASING),
		zoomOutEasing: normalizeZoomTransitionEasing(editor.zoomOutEasing, DEFAULT_ZOOM_OUT_EASING),
		connectedZoomEasing: normalizeZoomTransitionEasing(
			editor.connectedZoomEasing,
			DEFAULT_CONNECTED_ZOOM_EASING,
		),
		showCursor: typeof editor.showCursor === "boolean" ? editor.showCursor : true,
		loopCursor: typeof editor.loopCursor === "boolean" ? editor.loopCursor : false,
		cursorStyle: normalizedCursorStyle,
		cursorClickEffect: normalizeCursorClickEffectStyle(
			(editor as Partial<ProjectEditorState>).cursorClickEffect,
			DEFAULT_CURSOR_CLICK_EFFECT,
		),
		cursorClickEffectColor: normalizedCursorClickEffectColor,
		cursorClickEffectScale: normalizedCursorClickEffectScale,
		cursorClickEffectOpacity: normalizedCursorClickEffectOpacity,
		cursorClickEffectDurationMs: normalizedCursorClickEffectDurationMs,
		cursorSize: normalizedMotionPreset.cursorSize,
		cursorSmoothing: normalizedMotionPreset.cursorSmoothing,
		cursorSpringStiffnessMultiplier: normalizedMotionPreset.cursorSpringStiffnessMultiplier,
		cursorSpringDampingMultiplier: normalizedMotionPreset.cursorSpringDampingMultiplier,
		cursorSpringMassMultiplier: normalizedMotionPreset.cursorSpringMassMultiplier,
		cameraSpringStiffnessMultiplier: isFiniteNumber(editor.cameraSpringStiffnessMultiplier)
			? clamp(editor.cameraSpringStiffnessMultiplier, 0.25, 3)
			: 1,
		cameraSpringDampingMultiplier: isFiniteNumber(editor.cameraSpringDampingMultiplier)
			? clamp(editor.cameraSpringDampingMultiplier, 0.25, 3)
			: 1.13,
		cameraSpringMassMultiplier: isFiniteNumber(editor.cameraSpringMassMultiplier)
			? clamp(editor.cameraSpringMassMultiplier, 0.25, 3)
			: 1.12,
		zoomSmoothness: DEFAULT_ZOOM_SMOOTHNESS,
		zoomClassicMode:
			typeof editor.zoomClassicMode === "boolean" ? editor.zoomClassicMode : false,
		cursorMotionBlur: DEFAULT_CURSOR_MOTION_BLUR,
		cursorClickBounce: normalizedMotionPreset.cursorClickBounce,
		cursorClickBounceDuration: normalizedMotionPreset.cursorClickBounceDuration,
		cursorSway: isFiniteNumber((editor as Partial<ProjectEditorState>).cursorSway)
			? clamp((editor as Partial<ProjectEditorState>).cursorSway as number, 0, 2)
			: DEFAULT_CURSOR_SWAY,
		borderRadius: isFiniteNumber(editor.borderRadius)
			? clamp(editor.borderRadius, 0, 50)
			: getDefaultBorderRadiusPercent(),
		padding: (() => {
			const p = editor.padding;
			if (p && typeof p === "object") {
				const linked = typeof p.linked === "boolean" ? p.linked : true;
				const verticalMax = linked ? 100 : ADVANCED_VERTICAL_PADDING_MAX;
				const top = isFiniteNumber(p.top)
					? clamp(p.top, 0, verticalMax)
					: DEFAULT_PADDING.top;
				if (linked) {
					return { top, bottom: top, left: top, right: top, linked: true };
				}
				return {
					top,
					bottom: isFiniteNumber(p.bottom)
						? clamp(p.bottom, 0, verticalMax)
						: DEFAULT_PADDING.bottom,
					left: isFiniteNumber(p.left) ? clamp(p.left, 0, 100) : DEFAULT_PADDING.left,
					right: isFiniteNumber(p.right) ? clamp(p.right, 0, 100) : DEFAULT_PADDING.right,
					linked: false,
				};
			}
			if (typeof p === "number" && isFiniteNumber(p)) {
				const val = clamp(p, 0, 100);
				return { top: val, bottom: val, left: val, right: val, linked: true };
			}
			return { ...DEFAULT_PADDING };
		})(),
		cropRegion: {
			x: cropX,
			y: cropY,
			width: cropWidth,
			height: cropHeight,
		},
		zoomRegions: sequenceChanged
			? rippleRegions(normalizedZoomRegions, normalizedClipRegions, sequenceClips)
			: normalizedZoomRegions,
		trimRegions: normalizedTrimRegions,
		clipRegions: sequenceClips,
		autoFullTrackClipId: normalizedAutoFullTrackClipId,
		autoFullTrackClipEndMs: normalizedAutoFullTrackClipEndMs,
		speedRegions: normalizedSpeedRegions,
		annotationRegions: sequenceChanged
			? rippleRegions(normalizedAnnotationRegions, normalizedClipRegions, sequenceClips)
			: normalizedAnnotationRegions,
		audioRegions: sequenceChanged
			? rippleRegionAnchors(normalizedAudioRegions, normalizedClipRegions, sequenceClips)
			: normalizedAudioRegions,
		autoCaptions: normalizedAutoCaptions,
		autoCaptionSettings: normalizedAutoCaptionSettings,
		webcam: {
			visibleRanges: Array.isArray(webcam.visibleRanges)
				? webcam.visibleRanges
						.filter(
							(range) =>
								isFiniteNumber(range?.startMs) &&
								isFiniteNumber(range?.endMs) &&
								range.startMs >= 0 &&
								range.endMs > range.startMs,
						)
						.map(({ startMs, endMs }) => ({ startMs, endMs }))
				: undefined,
			enabled:
				typeof webcam.enabled === "boolean"
					? webcam.enabled
					: DEFAULT_WEBCAM_OVERLAY.enabled,
			sourcePath: webcamSourcePath,
			mirror:
				typeof webcam.mirror === "boolean" ? webcam.mirror : DEFAULT_WEBCAM_OVERLAY.mirror,
			cropRegion: normalizeWebcamCropRegion(webcam.cropRegion),
			positionPreset:
				webcam.positionPreset === "top-left" ||
				webcam.positionPreset === "top-center" ||
				webcam.positionPreset === "top-right" ||
				webcam.positionPreset === "center-left" ||
				webcam.positionPreset === "center" ||
				webcam.positionPreset === "center-right" ||
				webcam.positionPreset === "bottom-left" ||
				webcam.positionPreset === "bottom-center" ||
				webcam.positionPreset === "bottom-right" ||
				webcam.positionPreset === "custom"
					? webcam.positionPreset
					: webcam.corner === "top-left" ||
							webcam.corner === "top-right" ||
							webcam.corner === "bottom-left" ||
							webcam.corner === "bottom-right"
						? webcam.corner
						: DEFAULT_WEBCAM_POSITION_PRESET,
			positionX: isFiniteNumber(webcam.positionX)
				? clamp(webcam.positionX, 0, 1)
				: DEFAULT_WEBCAM_POSITION_X,
			positionY: isFiniteNumber(webcam.positionY)
				? clamp(webcam.positionY, 0, 1)
				: DEFAULT_WEBCAM_POSITION_Y,
			corner:
				webcam.corner === "top-left" ||
				webcam.corner === "top-right" ||
				webcam.corner === "bottom-left" ||
				webcam.corner === "bottom-right"
					? webcam.corner
					: DEFAULT_WEBCAM_OVERLAY.corner,
			size: isFiniteNumber(webcam.size) ? clamp(webcam.size, 10, 100) : DEFAULT_WEBCAM_SIZE,
			width: isFiniteNumber(webcam.width)
				? clamp(webcam.width, 10, 100)
				: isFiniteNumber(webcam.size)
					? clamp(webcam.size, 10, 100)
					: DEFAULT_WEBCAM_SIZE,
			height: isFiniteNumber(webcam.height)
				? clamp(webcam.height, 10, 100)
				: isFiniteNumber(webcam.size)
					? clamp(webcam.size, 10, 100)
					: DEFAULT_WEBCAM_SIZE,
			reactToZoom:
				typeof webcam.reactToZoom === "boolean"
					? webcam.reactToZoom
					: legacyZoomScaleEffect != null
						? legacyZoomScaleEffect > 0
						: DEFAULT_WEBCAM_REACT_TO_ZOOM,
			roundness: isFiniteNumber(webcam.roundness)
				? clamp(webcam.roundness, 0, 100)
				: isFiniteNumber(webcam.cornerRadius)
					? convertLegacyWebcamRadiusToRoundness(
							webcam.cornerRadius,
							isFiniteNumber(webcam.width)
								? webcam.width
								: isFiniteNumber(webcam.size)
									? webcam.size
									: DEFAULT_WEBCAM_SIZE,
							isFiniteNumber(webcam.height)
								? webcam.height
								: isFiniteNumber(webcam.size)
									? webcam.size
									: DEFAULT_WEBCAM_SIZE,
						)
					: DEFAULT_WEBCAM_ROUNDNESS,
			shadow: isFiniteNumber(webcam.shadow)
				? clamp(webcam.shadow, 0, 1)
				: DEFAULT_WEBCAM_SHADOW,
			timeOffsetMs: isFiniteNumber(webcam.timeOffsetMs)
				? Math.round(webcam.timeOffsetMs)
				: DEFAULT_WEBCAM_TIME_OFFSET_MS,
			margin: isFiniteNumber(webcam.margin)
				? clamp(webcam.margin, 0, 96)
				: DEFAULT_WEBCAM_MARGIN,
		},
		sourceAudioTrackSettingsByClip:
			editor.sourceAudioTrackSettingsByClip &&
			typeof editor.sourceAudioTrackSettingsByClip === "object"
				? editor.sourceAudioTrackSettingsByClip
				: {},
		defaultSourceAudioTrackSettings:
			editor.defaultSourceAudioTrackSettings &&
			typeof editor.defaultSourceAudioTrackSettings === "object"
				? editor.defaultSourceAudioTrackSettings
				: {},
		aspectRatio:
			typeof editor.aspectRatio === "string" &&
			(validAspectRatios.has(editor.aspectRatio as AspectRatio) ||
				isCustomAspectRatio(editor.aspectRatio))
				? (editor.aspectRatio as AspectRatio)
				: "16:9",
	};
}

export function createProjectData(
	videoPath: string,
	editor: Partial<ProjectEditorState>,
	projectId?: string | null,
): EditorProjectData {
	return {
		version: PROJECT_VERSION,
		...(typeof projectId === "string" && projectId.trim().length > 0 ? { projectId } : {}),
		videoPath,
		editor: stripProjectUserPreferences(editor),
	};
}

// Older project files may contain app preferences. Never write or compare them as edits.
export function stripProjectUserPreferences<T extends object>(editor: T): T {
	const result = { ...editor };
	for (const key of [
		"exportEncodingMode",
		"exportBackendPreference",
		"exportPipelineModel",
		"exportQuality",
		"mp4FrameRate",
		"exportFormat",
		"gifFrameRate",
		"gifLoop",
		"gifSizePreset",
		"publishDestination",
		"includeCaptionSidecar",
		"whisperExecutablePath",
		"whisperModelPath",
	]) {
		delete (result as Record<string, unknown>)[key];
	}
	return result;
}