@morshoto/framekit 0.1.14 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist-package/adapters/final-cut/typescript/src/background-materialization.js +28 -0
  2. package/dist-package/adapters/final-cut/typescript/src/canonical.js +144 -35
  3. package/dist-package/adapters/final-cut/typescript/src/connection.js +8 -5
  4. package/dist-package/adapters/final-cut/typescript/src/disposable-native.js +36 -7
  5. package/dist-package/adapters/final-cut/typescript/src/fcpxml.js +21 -7
  6. package/dist-package/adapters/final-cut/typescript/src/index.js +2 -0
  7. package/dist-package/adapters/final-cut/typescript/src/live.js +56 -1
  8. package/dist-package/adapters/final-cut/typescript/src/native.js +311 -20
  9. package/dist-package/adapters/final-cut/typescript/src/pasteboard-snapshot.js +312 -0
  10. package/dist-package/adapters/final-cut/typescript/src/publisher.js +68 -8
  11. package/dist-package/adapters/final-cut/typescript/src/sqlite-wal-observation.js +263 -0
  12. package/dist-package/adapters/final-cut/typescript/src/timeline-ir-fcpxml.js +179 -25
  13. package/dist-package/adapters/headless/typescript/src/ffmpeg-media-probe.js +62 -0
  14. package/dist-package/adapters/headless/typescript/src/ffmpeg-renderer.js +551 -0
  15. package/dist-package/adapters/headless/typescript/src/ffmpeg-verifier.js +181 -0
  16. package/dist-package/adapters/headless/typescript/src/index.js +3 -0
  17. package/dist-package/apps/mcp-server/src/headless-projects.js +370 -0
  18. package/dist-package/apps/mcp-server/src/headless-sessions.js +3 -0
  19. package/dist-package/apps/mcp-server/src/main.js +28 -4
  20. package/dist-package/apps/mcp-server/src/materialization-jobs.js +233 -4
  21. package/dist-package/apps/mcp-server/src/readback-policy.js +26 -0
  22. package/dist-package/apps/mcp-server/src/routing.js +134 -5
  23. package/dist-package/apps/mcp-server/src/server.js +311 -34
  24. package/dist-package/packages/runtime/src/application/local-media.js +157 -0
  25. package/dist-package/packages/runtime/src/application/media-analysis-service.js +347 -27
  26. package/dist-package/packages/runtime/src/application/project-store.js +234 -0
  27. package/dist-package/packages/runtime/src/application/project-transactions.js +227 -0
  28. package/dist-package/packages/runtime/src/application/semantic-media-index-store.js +86 -0
  29. package/dist-package/packages/runtime/src/index.js +7 -0
  30. package/dist-package/packages/runtime/src/planning/rough-cut.js +103 -7
  31. package/dist-package/packages/runtime/src/render/render-and-verify.js +126 -0
  32. package/dist-package/packages/runtime/src/render/render-contract.js +146 -0
  33. package/dist-package/packages/runtime/src/runtime.js +5 -2
  34. package/dist-package/packages/runtime/src/timeline/editing-session.js +277 -9
  35. package/dist-package/packages/runtime/src/timeline/fast-observation.js +141 -0
  36. package/package.json +2 -1
@@ -0,0 +1,551 @@
1
+ import { createHash } from "node:crypto";
2
+ import { execFile } from "node:child_process";
3
+ import { createReadStream } from "node:fs";
4
+ import { access, link, lstat, mkdtemp, rename, rm, unlink } from "node:fs/promises";
5
+ import { existsSync } from "node:fs";
6
+ import { dirname, join, resolve } from "node:path";
7
+ import { promisify } from "node:util";
8
+ import { createFramekitRenderResult, } from "../../../../packages/runtime/src/index.js";
9
+ const execFileAsync = promisify(execFile);
10
+ export class FfmpegRenderError extends Error {
11
+ code;
12
+ constructor(code, message) {
13
+ super(`${code}: ${message}`);
14
+ this.code = code;
15
+ this.name = "FfmpegRenderError";
16
+ }
17
+ }
18
+ /**
19
+ * The first provider-neutral concrete renderer. It consumes only a validated
20
+ * Timeline IR render plan and treats local source files as immutable inputs.
21
+ */
22
+ export class FfmpegTimelineRenderer {
23
+ id = "ffmpeg-headless";
24
+ version;
25
+ ffmpegPath;
26
+ environment;
27
+ fontFile;
28
+ constructor(options = {}) {
29
+ this.ffmpegPath = options.ffmpegPath ?? process.env.FFMPEG_BIN ?? "ffmpeg";
30
+ this.version = options.version ?? "0.1.0";
31
+ this.environment = { ...process.env, ...options.env };
32
+ this.fontFile = resolveFontFile(options.env?.FRAMEKIT_RENDER_FONT ?? process.env.FRAMEKIT_RENDER_FONT);
33
+ }
34
+ capabilities(request) {
35
+ const timeline = request.timeline;
36
+ return {
37
+ renderer: { id: this.id, version: this.version },
38
+ features: {
39
+ "local-media": supportsLocalMedia(timeline) ? "supported" : "unsupported",
40
+ "structural-edits": supportsStructuralEdits(timeline) ? "supported" : "unsupported",
41
+ "audio-gain": supportsAudioGain(timeline) ? "supported" : "unsupported",
42
+ "audio-mixing": supportsAudioMixing(timeline) ? "supported" : "unsupported",
43
+ transform: supportsTransforms(timeline) ? "supported" : "unsupported",
44
+ titles: supportsTitles(timeline) ? "supported" : "unsupported",
45
+ "cross-dissolve": supportsCrossDissolve(timeline) ? "supported" : "unsupported",
46
+ },
47
+ };
48
+ }
49
+ async render(plan) {
50
+ if (plan.renderer.id !== this.id || plan.renderer.version !== this.version) {
51
+ throw new FfmpegRenderError("HEADLESS_RENDER_PLAN_MISMATCH", "render plan is bound to another renderer version");
52
+ }
53
+ const unavailable = plan.requiredFeatures.filter((feature) => plan.capabilities.features[feature] !== "supported");
54
+ if (unavailable.length > 0) {
55
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `required features are unavailable: ${unavailable.join(", ")}`);
56
+ }
57
+ const model = await prepareRenderModel(plan);
58
+ const outputPath = resolve(plan.parameters.outputPath);
59
+ await assertOutputPath(outputPath, plan.parameters.overwrite ?? false, model.sourcePaths);
60
+ const outputDirectory = dirname(outputPath);
61
+ await access(outputDirectory).catch(() => {
62
+ throw new FfmpegRenderError("HEADLESS_RENDER_OUTPUT_INVALID", `output directory does not exist: ${outputDirectory}`);
63
+ });
64
+ const stagingDirectory = await mkdtemp(join(outputDirectory, ".framekit-render-"));
65
+ const stagingPath = join(stagingDirectory, `render.${plan.parameters.format}`);
66
+ try {
67
+ const args = buildFfmpegArguments(model, stagingPath, this.fontFile);
68
+ try {
69
+ await execFileAsync(this.ffmpegPath, args, {
70
+ cwd: process.cwd(),
71
+ env: this.environment,
72
+ maxBuffer: 8 * 1024 * 1024,
73
+ });
74
+ }
75
+ catch (error) {
76
+ const detail = error instanceof Error ? error.message : String(error);
77
+ throw new FfmpegRenderError("HEADLESS_RENDER_FAILED", detail);
78
+ }
79
+ if (!(await isRegularFile(stagingPath))) {
80
+ throw new FfmpegRenderError("HEADLESS_RENDER_FAILED", "FFmpeg completed without producing an output file");
81
+ }
82
+ await assertSourcesUnchanged(model);
83
+ if (plan.parameters.overwrite ?? false) {
84
+ await rename(stagingPath, outputPath);
85
+ }
86
+ else {
87
+ try {
88
+ await link(stagingPath, outputPath);
89
+ }
90
+ catch (error) {
91
+ if (error.code === "EEXIST") {
92
+ throw new FfmpegRenderError("HEADLESS_RENDER_OUTPUT_EXISTS", `render output already exists: ${outputPath}`);
93
+ }
94
+ throw error;
95
+ }
96
+ await unlink(stagingPath);
97
+ }
98
+ return createFramekitRenderResult(plan, {
99
+ path: plan.parameters.outputPath,
100
+ format: plan.parameters.format,
101
+ });
102
+ }
103
+ finally {
104
+ await rm(stagingDirectory, { recursive: true, force: true });
105
+ }
106
+ }
107
+ }
108
+ async function prepareRenderModel(plan) {
109
+ const timeline = structuredClone(plan.timeline);
110
+ const sequence = timeline.sequence;
111
+ const occurrences = sequence.occurrences
112
+ .filter(isPrimaryVideoOccurrence)
113
+ .sort((left, right) => compareSeconds(left.startTime, right.startTime));
114
+ const audioOccurrences = sequence.occurrences
115
+ .filter(isIndependentAudioOccurrence)
116
+ .sort((left, right) => compareSeconds(left.startTime, right.startTime));
117
+ if (!supportsStructuralEdits(timeline)) {
118
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "markers, captions, story elements, disabled clips, and non-primary tracks are not supported");
119
+ }
120
+ if (!supportsTransforms(timeline) || !supportsTitles(timeline) || !supportsCrossDissolve(timeline)) {
121
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "Timeline IR contains unsupported renderer semantics");
122
+ }
123
+ if (occurrences.length === 0)
124
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "at least one video occurrence is required");
125
+ if (occurrences.length + audioOccurrences.length !== sequence.occurrences.length) {
126
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "occurrence roles and lanes do not form a primary video storyline with connected audio layers");
127
+ }
128
+ const duration = rationalSeconds(sequence.durationTime, "sequence duration");
129
+ const frameRate = 1 / rationalSeconds(sequence.frameDuration, "sequence frame duration");
130
+ if (!Number.isFinite(duration) || duration <= 0 || !Number.isFinite(frameRate) || frameRate <= 0) {
131
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "sequence duration and frame rate must be finite and positive");
132
+ }
133
+ if (!Number.isSafeInteger(plan.parameters.width) || !Number.isSafeInteger(plan.parameters.height)) {
134
+ throw new FfmpegRenderError("HEADLESS_RENDER_OUTPUT_INVALID", "output dimensions must be safe integers");
135
+ }
136
+ const resources = new Map(timeline.resources.map((resource) => [resource.id, resource]));
137
+ const sourcePaths = [];
138
+ const sourceIdentities = [];
139
+ const audioAvailability = new Set();
140
+ let previousEnd = 0;
141
+ for (const occurrence of occurrences) {
142
+ if (occurrence.enabled === false || occurrence.attachedTo) {
143
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `occurrence has unsupported state: ${occurrence.id}`);
144
+ }
145
+ if (occurrence.fadeIn !== undefined || occurrence.fadeOut !== undefined) {
146
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `audio fades are not renderable: ${occurrence.id}`);
147
+ }
148
+ const start = rationalSeconds(occurrence.startTime, `${occurrence.id}.startTime`);
149
+ const clipDuration = rationalSeconds(occurrence.durationTime, `${occurrence.id}.durationTime`);
150
+ if (Math.abs(start - previousEnd) > 1e-7) {
151
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `primary occurrences must be contiguous: ${occurrence.id}`);
152
+ }
153
+ previousEnd = start + clipDuration;
154
+ const resource = occurrence.mediaId ? resources.get(occurrence.mediaId) : undefined;
155
+ if (!resource)
156
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `occurrence has no registered media: ${occurrence.id}`);
157
+ if (resource.mediaKind !== "video")
158
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `primary occurrence does not reference video media: ${occurrence.id}`);
159
+ const source = await verifyResource(resource, occurrence, clipDuration);
160
+ const sourcePath = source.path;
161
+ audioAvailability.add(resource.metadata?.streams.some((stream) => stream.kind === "audio") ?? false);
162
+ if (!sourcePaths.includes(sourcePath))
163
+ sourcePaths.push(sourcePath);
164
+ if (!sourceIdentities.some((identity) => identity.path === source.path && identity.digest === source.digest)) {
165
+ sourceIdentities.push(source);
166
+ }
167
+ }
168
+ for (const occurrence of audioOccurrences) {
169
+ if (occurrence.enabled === false || occurrence.attachedTo) {
170
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `audio occurrence has unsupported state: ${occurrence.id}`);
171
+ }
172
+ if (occurrence.fadeIn !== undefined || occurrence.fadeOut !== undefined) {
173
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `audio fades are not renderable: ${occurrence.id}`);
174
+ }
175
+ const start = rationalSeconds(occurrence.startTime, `${occurrence.id}.startTime`);
176
+ const clipDuration = rationalSeconds(occurrence.durationTime, `${occurrence.id}.durationTime`);
177
+ if (clipDuration <= 0 || start + clipDuration > duration + 1e-7) {
178
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `audio occurrence exceeds the sequence duration: ${occurrence.id}`);
179
+ }
180
+ const resource = occurrence.mediaId ? resources.get(occurrence.mediaId) : undefined;
181
+ if (!resource)
182
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `audio occurrence has no registered media: ${occurrence.id}`);
183
+ const source = await verifyAudioResource(resource, occurrence, clipDuration);
184
+ if (!sourcePaths.includes(source.path))
185
+ sourcePaths.push(source.path);
186
+ if (!sourceIdentities.some((identity) => identity.path === source.path && identity.digest === source.digest)) {
187
+ sourceIdentities.push(source);
188
+ }
189
+ }
190
+ if (audioAvailability.size > 1) {
191
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "all primary occurrences must agree on audio availability");
192
+ }
193
+ if (Math.abs(previousEnd - duration) > 1e-7) {
194
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "occurrences do not cover the sequence duration");
195
+ }
196
+ const transitions = timeline.sequence.transitions ?? [];
197
+ if (transitions.length > 1) {
198
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "the first FFmpeg provider supports one canonical cross-dissolve");
199
+ }
200
+ return {
201
+ timeline,
202
+ occurrences,
203
+ audioOccurrences,
204
+ resources,
205
+ sourcePaths,
206
+ duration,
207
+ frameRate,
208
+ width: plan.parameters.width,
209
+ height: plan.parameters.height,
210
+ format: plan.parameters.format,
211
+ sourceIdentities,
212
+ };
213
+ }
214
+ async function verifyResource(resource, occurrence, duration) {
215
+ if (resource.sourceKind !== "local-file" || !resource.source || !resource.sourceDigest) {
216
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `resource is not a registered local file: ${resource.id}`);
217
+ }
218
+ const sourcePath = resolve(resource.source);
219
+ let details;
220
+ try {
221
+ details = await lstat(sourcePath);
222
+ }
223
+ catch (error) {
224
+ if (error.code === "ENOENT") {
225
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_MISSING", `registered source is missing: ${sourcePath}`);
226
+ }
227
+ throw error;
228
+ }
229
+ if (!details.isFile() || details.isSymbolicLink()) {
230
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_MISSING", `registered source is not a regular file: ${sourcePath}`);
231
+ }
232
+ const actualDigest = await digestFile(sourcePath);
233
+ if (actualDigest !== resource.sourceDigest) {
234
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_CHANGED", `registered source changed: ${sourcePath}`);
235
+ }
236
+ const video = resource.metadata?.streams.find((stream) => stream.kind === "video");
237
+ if (!video?.width || !video.height) {
238
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `resource is missing video dimensions: ${resource.id}`);
239
+ }
240
+ const sourceStart = occurrence.sourceStartTime ? rationalSeconds(occurrence.sourceStartTime, `${occurrence.id}.sourceStartTime`) : 0;
241
+ const sourceDuration = resource.metadata ? rationalSeconds(resource.metadata.durationTime, `${resource.id}.durationTime`) : 0;
242
+ if (sourceStart < 0 || duration <= 0 || sourceStart + duration > sourceDuration + 1e-7) {
243
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `source range exceeds registered media: ${occurrence.id}`);
244
+ }
245
+ return { path: sourcePath, digest: actualDigest };
246
+ }
247
+ async function verifyAudioResource(resource, occurrence, duration) {
248
+ if (resource.mediaKind !== "audio" || resource.sourceKind !== "local-file" || !resource.source || !resource.sourceDigest) {
249
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `resource is not registered local audio: ${resource.id}`);
250
+ }
251
+ const sourcePath = resolve(resource.source);
252
+ let details;
253
+ try {
254
+ details = await lstat(sourcePath);
255
+ }
256
+ catch (error) {
257
+ if (error.code === "ENOENT") {
258
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_MISSING", `registered source is missing: ${sourcePath}`);
259
+ }
260
+ throw error;
261
+ }
262
+ if (!details.isFile() || details.isSymbolicLink()) {
263
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_MISSING", `registered source is not a regular file: ${sourcePath}`);
264
+ }
265
+ const actualDigest = await digestFile(sourcePath);
266
+ if (actualDigest !== resource.sourceDigest) {
267
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_CHANGED", `registered source changed: ${sourcePath}`);
268
+ }
269
+ if (!resource.metadata?.streams.some((stream) => stream.kind === "audio")) {
270
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `resource is missing audio metadata: ${resource.id}`);
271
+ }
272
+ const sourceStart = occurrence.sourceStartTime ? rationalSeconds(occurrence.sourceStartTime, `${occurrence.id}.sourceStartTime`) : 0;
273
+ const sourceDuration = resource.metadata ? rationalSeconds(resource.metadata.durationTime, `${resource.id}.durationTime`) : 0;
274
+ if (sourceStart < 0 || duration <= 0 || sourceStart + duration > sourceDuration + 1e-7) {
275
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `source audio range exceeds registered media: ${occurrence.id}`);
276
+ }
277
+ return { path: sourcePath, digest: actualDigest };
278
+ }
279
+ async function assertSourcesUnchanged(model) {
280
+ for (const identity of model.sourceIdentities) {
281
+ let details;
282
+ try {
283
+ details = await lstat(identity.path);
284
+ }
285
+ catch {
286
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_CHANGED", `registered source changed during rendering: ${identity.path}`);
287
+ }
288
+ if (!details.isFile() || details.isSymbolicLink() || await digestFile(identity.path) !== identity.digest) {
289
+ throw new FfmpegRenderError("HEADLESS_RENDER_MEDIA_CHANGED", `registered source changed during rendering: ${identity.path}`);
290
+ }
291
+ }
292
+ }
293
+ function buildFfmpegArguments(model, stagingPath, fontFile) {
294
+ const filters = [];
295
+ const inputArguments = [];
296
+ const videoLabels = [];
297
+ const audioLabels = [];
298
+ const musicLabels = [];
299
+ const fps = formatNumber(model.frameRate);
300
+ const inputIndices = new Map();
301
+ for (const [index, occurrence] of [...model.occurrences, ...model.audioOccurrences].entries()) {
302
+ const resource = model.resources.get(occurrence.mediaId);
303
+ const inputPath = resolve(resource.source);
304
+ inputArguments.push("-i", inputPath);
305
+ inputIndices.set(occurrence.id, index);
306
+ }
307
+ for (const occurrence of model.occurrences) {
308
+ const index = inputIndices.get(occurrence.id);
309
+ const resource = model.resources.get(occurrence.mediaId);
310
+ const sourceStart = occurrence.sourceStartTime ? rationalSeconds(occurrence.sourceStartTime, `${occurrence.id}.sourceStartTime`) : 0;
311
+ const duration = rationalSeconds(occurrence.durationTime, `${occurrence.id}.durationTime`);
312
+ const videoInput = `[${index}:v]`;
313
+ const videoLabel = `clipv${index}`;
314
+ filters.push(`${videoInput}trim=start=${formatNumber(sourceStart)}:duration=${formatNumber(duration)},setpts=PTS-STARTPTS,settb=AVTB,fps=${fps},${transformFilter(occurrence, resource, model.width, model.height)}[${videoLabel}]`);
315
+ videoLabels.push(`[${videoLabel}]`);
316
+ const hasAudio = resource.metadata?.streams.some((stream) => stream.kind === "audio") ?? false;
317
+ if (hasAudio) {
318
+ const audioLabel = `clipa${index}`;
319
+ const gain = occurrence.gainDb ?? 0;
320
+ filters.push(`${`[${index}:a]`}atrim=start=${formatNumber(sourceStart)}:duration=${formatNumber(duration)},asetpts=PTS-STARTPTS,aresample=48000,volume=${formatNumber(Math.pow(10, gain / 20))}[${audioLabel}]`);
321
+ audioLabels.push(`[${audioLabel}]`);
322
+ }
323
+ }
324
+ for (const occurrence of model.audioOccurrences) {
325
+ const index = inputIndices.get(occurrence.id);
326
+ const sourceStart = occurrence.sourceStartTime ? rationalSeconds(occurrence.sourceStartTime, `${occurrence.id}.sourceStartTime`) : 0;
327
+ const start = rationalSeconds(occurrence.startTime, `${occurrence.id}.startTime`);
328
+ const duration = rationalSeconds(occurrence.durationTime, `${occurrence.id}.durationTime`);
329
+ const gain = occurrence.gainDb ?? 0;
330
+ const delaySamples = Math.round(start * 48_000);
331
+ const audioLabel = `bed-audio${index}`;
332
+ filters.push(`[${index}:a]atrim=start=${formatNumber(sourceStart)}:duration=${formatNumber(duration)},asetpts=PTS-STARTPTS,aresample=48000,aformat=channel_layouts=mono,volume=${formatNumber(Math.pow(10, gain / 20))},adelay=${delaySamples}S:all=1,apad,atrim=duration=${formatNumber(model.duration)},asetpts=PTS-STARTPTS[${audioLabel}]`);
333
+ musicLabels.push(`[${audioLabel}]`);
334
+ }
335
+ const transition = model.timeline.sequence.transitions?.[0];
336
+ const videoBase = addVideoComposition(filters, videoLabels, model, transition);
337
+ const videoOutput = addTitleOverlays(filters, videoBase, model, fontFile);
338
+ let audioOutput;
339
+ if (audioLabels.length > 0)
340
+ audioOutput = addAudioComposition(filters, audioLabels, model, transition);
341
+ if (musicLabels.length > 0) {
342
+ const inputs = [...(audioOutput ? [`[${audioOutput}]`] : []), ...musicLabels];
343
+ audioOutput = addAudioMix(filters, inputs, model.duration);
344
+ }
345
+ const args = ["-hide_banner", "-loglevel", "error", "-nostdin", "-y", ...inputArguments, "-filter_complex", filters.join(";")];
346
+ args.push("-map", `[${videoOutput}]`, "-c:v", "libx264", "-preset", "ultrafast", "-threads", "1", "-pix_fmt", "yuv420p", "-r", fps);
347
+ if (audioOutput) {
348
+ args.push("-map", `[${audioOutput}]`, "-c:a", "aac", "-b:a", "128k", "-ar", "48000", "-ac", "1");
349
+ }
350
+ args.push("-map_metadata", "-1", "-t", formatNumber(model.duration), "-movflags", "+faststart", stagingPath);
351
+ return args;
352
+ }
353
+ function addAudioMix(filters, labels, duration) {
354
+ if (labels.length === 1)
355
+ return labels[0].slice(1, -1);
356
+ const output = "audio-mix";
357
+ filters.push(`${labels.join("")}amix=inputs=${labels.length}:duration=longest:dropout_transition=0:normalize=0,apad,atrim=duration=${formatNumber(duration)},asetpts=PTS-STARTPTS[${output}]`);
358
+ return output;
359
+ }
360
+ function addVideoComposition(filters, labels, model, transition) {
361
+ if (labels.length === 1) {
362
+ const output = "video-base";
363
+ filters.push(`${labels[0]}tpad=stop_mode=clone:stop_duration=0[${output}]`);
364
+ return output;
365
+ }
366
+ if (transition) {
367
+ const duration = rationalSeconds(transition.durationTime, `${transition.id}.durationTime`);
368
+ const offset = rationalSeconds(model.occurrences[0].durationTime, "transition offset") - duration;
369
+ if (offset < 0)
370
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", "transition duration exceeds the first occurrence duration");
371
+ const output = "video-xfade";
372
+ filters.push(`${labels[0]}${labels[1]}xfade=transition=fade:duration=${formatNumber(duration)}:offset=${formatNumber(offset)}[${output}]`);
373
+ const padding = Math.max(0, model.duration - (model.duration - duration));
374
+ filters.push(`[${output}]tpad=stop_mode=clone:stop_duration=${formatNumber(padding)}[${output}-padded]`);
375
+ return `${output}-padded`;
376
+ }
377
+ const output = "video-concat";
378
+ filters.push(`${labels.join("")}concat=n=${labels.length}:v=1:a=0[${output}]`);
379
+ return output;
380
+ }
381
+ function addAudioComposition(filters, labels, model, transition) {
382
+ if (labels.length === 1) {
383
+ const output = "audio-base";
384
+ filters.push(`${labels[0]}apad,atrim=duration=${formatNumber(model.duration)},asetpts=PTS-STARTPTS[${output}]`);
385
+ return output;
386
+ }
387
+ if (transition) {
388
+ const duration = rationalSeconds(transition.durationTime, `${transition.id}.durationTime`);
389
+ const output = "audio-crossfade";
390
+ filters.push(`${labels[0]}${labels[1]}acrossfade=d=${formatNumber(duration)}:c1=tri:c2=tri[${output}]`);
391
+ filters.push(`[${output}]apad,atrim=duration=${formatNumber(model.duration)},asetpts=PTS-STARTPTS[${output}-padded]`);
392
+ return `${output}-padded`;
393
+ }
394
+ const output = "audio-concat";
395
+ filters.push(`${labels.join("")}concat=n=${labels.length}:v=0:a=1[${output}]`);
396
+ return output;
397
+ }
398
+ function addTitleOverlays(filters, input, model, fontFile) {
399
+ let current = input;
400
+ for (const [index, title] of (model.timeline.sequence.titles ?? []).entries()) {
401
+ const start = rationalSeconds(title.startTime, `${title.id}.startTime`);
402
+ const end = start + rationalSeconds(title.durationTime, `${title.id}.durationTime`);
403
+ const style = title.style;
404
+ const color = escapeFilterValue(style?.color ?? "white");
405
+ const size = formatNumber(style?.fontSize ?? 32);
406
+ const x = title.position
407
+ ? `${formatNumber(model.width / 2 + title.position.x * model.width / 2)}-(text_w/2)`
408
+ : style?.alignment === "left" ? "20" : style?.alignment === "right" ? "w-text_w-20" : "(w-text_w)/2";
409
+ const y = title.position
410
+ ? `${formatNumber(model.height / 2 - title.position.y * model.height / 2)}-(text_h/2)`
411
+ : "(h-text_h)/2";
412
+ const output = `title-${index}`;
413
+ const font = fontFile ? `fontfile='${escapeFilterValue(fontFile)}':` : "";
414
+ filters.push(`[${current}]drawtext=${font}text='${escapeFilterValue(title.text)}':fontcolor=${color}:fontsize=${size}:x=${x}:y=${y}:enable='between(t,${formatNumber(start)},${formatNumber(end)})'[${output}]`);
415
+ current = output;
416
+ }
417
+ return current;
418
+ }
419
+ function transformFilter(occurrence, resource, width, height) {
420
+ const transform = occurrence.transform;
421
+ if (!transform)
422
+ return `scale=${width}:${height}:force_original_aspect_ratio=disable`;
423
+ const sourceVideo = resource.metadata.streams.find((stream) => stream.kind === "video");
424
+ const scaledWidth = evenDimension(sourceVideo.width * transform.scaleX);
425
+ const scaledHeight = evenDimension(sourceVideo.height * transform.scaleY);
426
+ const positionX = transform.positionX ?? 0;
427
+ const positionY = -(transform.positionY ?? 0);
428
+ if (scaledWidth >= width && scaledHeight >= height) {
429
+ const cropX = clamp((scaledWidth - width) / 2 - positionX, 0, scaledWidth - width);
430
+ const cropY = clamp((scaledHeight - height) / 2 - positionY, 0, scaledHeight - height);
431
+ return `scale=${scaledWidth}:${scaledHeight},crop=${width}:${height}:${formatNumber(cropX)}:${formatNumber(cropY)}`;
432
+ }
433
+ return `scale=${scaledWidth}:${scaledHeight},pad=${width}:${height}:${formatNumber((width - scaledWidth) / 2 + positionX)}:${formatNumber((height - scaledHeight) / 2 + positionY)}:color=black`;
434
+ }
435
+ function supportsLocalMedia(timeline) {
436
+ const resources = new Map(timeline.resources.map((resource) => [resource.id, resource]));
437
+ return timeline.sequence.occurrences.every((occurrence) => {
438
+ const resource = occurrence.mediaId ? resources.get(occurrence.mediaId) : undefined;
439
+ return resource?.sourceKind === "local-file" && Boolean(resource.source) && Boolean(resource.sourceDigest);
440
+ });
441
+ }
442
+ function supportsStructuralEdits(timeline) {
443
+ const primaryOccurrences = timeline.sequence.occurrences.filter(isPrimaryVideoOccurrence);
444
+ return timeline.sequence.storyElements.length === 0
445
+ && timeline.sequence.markers.length === 0
446
+ && timeline.sequence.captions.length === 0
447
+ && primaryOccurrences.length > 0
448
+ && primaryOccurrences.length + timeline.sequence.occurrences.filter(isIndependentAudioOccurrence).length === timeline.sequence.occurrences.length
449
+ && timeline.sequence.occurrences.every((occurrence) => (isPrimaryVideoOccurrence(occurrence) || isIndependentAudioOccurrence(occurrence))
450
+ && occurrence.enabled !== false
451
+ && !occurrence.attachedTo
452
+ && occurrence.fadeIn === undefined
453
+ && occurrence.fadeOut === undefined);
454
+ }
455
+ function supportsAudioMixing(timeline) {
456
+ return timeline.sequence.occurrences
457
+ .filter((occurrence) => occurrence.role === "audio" || occurrence.role === "music")
458
+ .every(isIndependentAudioOccurrence);
459
+ }
460
+ function isPrimaryVideoOccurrence(occurrence) {
461
+ return occurrence.track === 0 && (occurrence.role === undefined || occurrence.role === "video");
462
+ }
463
+ function isIndependentAudioOccurrence(occurrence) {
464
+ return occurrence.track > 0 && (occurrence.role === "audio" || occurrence.role === "music");
465
+ }
466
+ function supportsAudioGain(timeline) {
467
+ return timeline.sequence.occurrences.every((occurrence) => occurrence.gainDb === undefined || Number.isFinite(occurrence.gainDb));
468
+ }
469
+ function supportsTransforms(timeline) {
470
+ return timeline.sequence.occurrences.every((occurrence) => !occurrence.transform || (Number.isFinite(occurrence.transform.scaleX)
471
+ && Number.isFinite(occurrence.transform.scaleY)
472
+ && occurrence.transform.scaleX > 0
473
+ && occurrence.transform.scaleY > 0
474
+ && [occurrence.transform.positionX, occurrence.transform.positionY, occurrence.transform.rotationDegrees].every((value) => value === undefined || Number.isFinite(value))
475
+ && occurrence.transform.rotationDegrees === undefined));
476
+ }
477
+ function supportsTitles(timeline) {
478
+ return (timeline.sequence.titles ?? []).every((title) => !title.style || (title.style.fontFamily === undefined
479
+ && (title.style.fontSize === undefined || (Number.isFinite(title.style.fontSize) && title.style.fontSize > 0))));
480
+ }
481
+ function supportsCrossDissolve(timeline) {
482
+ const transitions = timeline.sequence.transitions ?? [];
483
+ const primaryCount = timeline.sequence.occurrences.filter(isPrimaryVideoOccurrence).length;
484
+ return transitions.length <= 1
485
+ && transitions.every((transition) => transition.kind === "cross-dissolve")
486
+ && (transitions.length === 0 || primaryCount === 2);
487
+ }
488
+ async function assertOutputPath(outputPath, overwrite, sourcePaths) {
489
+ if (sourcePaths.includes(outputPath)) {
490
+ throw new FfmpegRenderError("HEADLESS_RENDER_OUTPUT_INVALID", "render output must not overwrite a source media file");
491
+ }
492
+ if (!overwrite && await isRegularFile(outputPath)) {
493
+ throw new FfmpegRenderError("HEADLESS_RENDER_OUTPUT_EXISTS", `render output already exists: ${outputPath}`);
494
+ }
495
+ }
496
+ async function isRegularFile(path) {
497
+ try {
498
+ const details = await lstat(path);
499
+ return details.isFile() && !details.isSymbolicLink();
500
+ }
501
+ catch (error) {
502
+ if (error.code === "ENOENT")
503
+ return false;
504
+ throw error;
505
+ }
506
+ }
507
+ async function digestFile(path) {
508
+ const digest = createHash("sha256");
509
+ await new Promise((resolvePromise, reject) => {
510
+ const stream = createReadStream(path);
511
+ stream.on("data", (chunk) => digest.update(chunk));
512
+ stream.once("error", reject);
513
+ stream.once("end", resolvePromise);
514
+ });
515
+ return digest.digest("hex");
516
+ }
517
+ function rationalSeconds(time, field) {
518
+ if (!/^-?\d+$/.test(time.value) || !/^\d+$/.test(time.timescale) || BigInt(time.timescale) <= 0n) {
519
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `invalid rational time: ${field}`);
520
+ }
521
+ const result = Number(BigInt(time.value)) / Number(BigInt(time.timescale));
522
+ if (!Number.isFinite(result))
523
+ throw new FfmpegRenderError("HEADLESS_RENDER_UNSUPPORTED", `rational time is too large: ${field}`);
524
+ return result;
525
+ }
526
+ function compareSeconds(left, right) {
527
+ const leftValue = BigInt(left.value) * BigInt(right.timescale);
528
+ const rightValue = BigInt(right.value) * BigInt(left.timescale);
529
+ return leftValue < rightValue ? -1 : leftValue > rightValue ? 1 : 0;
530
+ }
531
+ function evenDimension(value) {
532
+ return Math.max(2, Math.round(value / 2) * 2);
533
+ }
534
+ function clamp(value, minimum, maximum) {
535
+ return Math.min(maximum, Math.max(minimum, value));
536
+ }
537
+ function formatNumber(value) {
538
+ return Number(value.toFixed(6)).toString();
539
+ }
540
+ function escapeFilterValue(value) {
541
+ return value.replaceAll("\\", "\\\\").replaceAll("'", "\\'").replaceAll(":", "\\:").replaceAll(",", "\\,").replaceAll("[", "\\[").replaceAll("]", "\\]").replaceAll("\n", "\\n");
542
+ }
543
+ function resolveFontFile(preferred) {
544
+ const candidates = [
545
+ preferred,
546
+ "/System/Library/Fonts/HelveticaNeue.ttc",
547
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
548
+ "/usr/share/fonts/truetype/liberation2/LiberationSans-Regular.ttf",
549
+ ].filter((candidate) => Boolean(candidate));
550
+ return candidates.find((candidate) => existsSync(candidate));
551
+ }