Repository navigation
Expand file tree
/
Copy pathloadCliProject.ts
More file actions
165 lines (158 loc) · 6 KB
/
Copy pathloadCliProject.ts
File metadata and controls
165 lines (158 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// What `openscreen export` and `openscreen captions` read from a project file, in the
// hidden runner window: either shape the file was saved in, as one AxcutDocument. The
// renderer half of `exportProject.ts`, since probing a video needs a DOM.
import {
type EditorProjectData,
resolveProjectMedia,
toFileUrl,
validateProjectData,
} from "@/components/video-editor/projectPersistence";
import { migrateProjectDataToAxcutDocument } from "@/lib/ai-edition/document/migrate";
import { applyProbedDuration } from "@/lib/ai-edition/document/timeline";
import { isAxcutDocumentFile } from "@/lib/ai-edition/schema";
import { nativeBridgeClient } from "@/native";
import { type ExportProject, legacyProjectEditor, loadDocumentProject } from "./exportProject";
export function probeVideoDimensions(
url: string,
): Promise<{ width: number; height: number; durationMs: number }> {
return new Promise((resolve, reject) => {
const video = document.createElement("video");
video.preload = "metadata";
video.muted = true;
const cleanup = () => {
clearTimeout(timer);
video.removeAttribute("src");
video.load();
};
// A stalled load fires neither event; without a deadline the CLI hangs.
const timer = setTimeout(() => {
cleanup();
reject(new Error(`Timed out reading video metadata: ${url}`));
}, 30_000);
video.onloadedmetadata = () => {
const width = video.videoWidth;
const height = video.videoHeight;
const durationMs = Number.isFinite(video.duration) ? Math.round(video.duration * 1000) : 0;
cleanup();
resolve({ width, height, durationMs });
};
video.onerror = () => {
cleanup();
reject(new Error(`Failed to load video metadata: ${url}`));
};
video.src = url;
});
}
/**
* A legacy v2 project (`{ version, media, editor }`), as written before the editor saved the
* document itself: migrated to an AxcutDocument, with what the migration cannot know probed.
*/
async function loadLegacyProject(project: EditorProjectData): Promise<ExportProject> {
const media = resolveProjectMedia(project);
if (!media) {
throw new Error("Project file does not reference any recorded media");
}
// Prefer the main process's approved session paths: they carry the
// packed-project sibling fallback when the stored absolute paths are stale.
try {
const sessionResult = await window.electronAPI.getCurrentRecordingSession();
const session = sessionResult?.session;
if (session?.screenVideoPath) {
media.screenVideoPath = session.screenVideoPath;
if (media.webcamVideoPath && session.webcamVideoPath) {
media.webcamVideoPath = session.webcamVideoPath;
}
}
} catch {
// Fall back to the paths stored in the project file.
}
const editor = legacyProjectEditor(project.editor);
const probed = await probeVideoDimensions(toFileUrl(media.screenVideoPath));
// Migrate the .openscreen project onto the AxcutDocument the native
// compositor consumes. The migration is pure and carries zooms, annotations,
// trims and the legacy editor settings; the clip's duration is unknown until
// probed, so applyProbedDuration must run or the export is a single frame.
let axcutDocument = migrateProjectDataToAxcutDocument({
...project,
media,
editor,
});
const primaryAssetId = axcutDocument.project.primaryAssetId ?? axcutDocument.assets[0]?.id;
if (!primaryAssetId) {
throw new Error("Project migration produced no media asset");
}
if (probed.durationMs > 0) {
axcutDocument = applyProbedDuration(axcutDocument, primaryAssetId, probed.durationMs / 1000);
}
// The screen's dimensions, for the same reason as the camera's below: the pure migration
// cannot know them, and the scene's screen box, the output frame and Auto all read them off
// `asset.video`. Without them the CLI laid every recording out as 1920x1080 while sizing the
// export from the probe, two answers to one question.
if (probed.width > 0 && probed.height > 0) {
axcutDocument = {
...axcutDocument,
assets: axcutDocument.assets.map((asset) =>
asset.id === primaryAssetId
? {
...asset,
video: {
codec: "unknown",
fps: 0,
...asset.video,
width: probed.width,
height: probed.height,
},
}
: asset,
),
};
}
// The camera's dimensions decide the PiP's layout box, and this is the one caller the
// document cannot answer for: there is no editor session here to have probed and saved
// them, so without this the CLI lays the box out from a hardcoded 4:3 and a 16:9 camera
// exports framed differently from the same project opened in the app.
//
// Failure-tolerant on purpose, unlike the screen probe above which is allowed to reject:
// a camera file that has gone missing should cost the export its camera, not the export.
if (media.webcamVideoPath) {
const camera = await probeVideoDimensions(toFileUrl(media.webcamVideoPath)).catch(() => null);
if (camera) {
axcutDocument = {
...axcutDocument,
assets: axcutDocument.assets.map((asset) =>
asset.cameraTrack
? {
...asset,
cameraTrack: {
...asset.cameraTrack,
width: camera.width,
height: camera.height,
},
}
: asset,
),
};
}
}
return {
document: axcutDocument,
editor,
screenVideoPath: media.screenVideoPath,
durationMs: probed.durationMs,
sourceDims: { width: probed.width, height: probed.height },
};
}
/** The project at `projectPath`, whichever shape it was saved in. */
export async function loadCliProject(projectPath: string): Promise<ExportProject> {
const loaded = await nativeBridgeClient.project.loadProjectFileFromPath(projectPath);
if (!loaded.success || loaded.project === undefined) {
throw new Error(loaded.error ?? loaded.message ?? "Failed to load project file");
}
if (isAxcutDocumentFile(loaded.project)) {
return loadDocumentProject(loaded.project, (videoPath) =>
probeVideoDimensions(toFileUrl(videoPath)),
);
}
if (validateProjectData(loaded.project)) return loadLegacyProject(loaded.project);
throw new Error("Project file is not a valid .openscreen project");
}