agentcut 0.1.0 → 0.1.1
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.
- package/package.json +1 -1
- package/template/runtime/composition.tsx +920 -918
|
@@ -1,918 +1,920 @@
|
|
|
1
|
-
// StudioComposition: draws a project's layers.json with Remotion.
|
|
2
|
-
// Tracks are drawn bottom → top (tracks[0] is the top layer) so stacking never
|
|
3
|
-
// depends on z-index, which the browser export ignores.
|
|
4
|
-
import { useEffect, useMemo, useState, type ComponentType, type CSSProperties, type ReactNode } from "react";
|
|
5
|
-
import {
|
|
6
|
-
AbsoluteFill,
|
|
7
|
-
Img,
|
|
8
|
-
Sequence,
|
|
9
|
-
continueRender,
|
|
10
|
-
delayRender,
|
|
11
|
-
interpolate,
|
|
12
|
-
useCurrentFrame,
|
|
13
|
-
useRemotionEnvironment,
|
|
14
|
-
useVideoConfig,
|
|
15
|
-
} from "remotion";
|
|
16
|
-
import { Audio, Video } from "@remotion/media";
|
|
17
|
-
// The Agentcut component library, provided by the studio page (and by a copy in
|
|
18
|
-
// .agentcut/components for local renders).
|
|
19
|
-
import * as ComponentLibrary from "agentcut/components";
|
|
20
|
-
import { MOTION_COMPONENTS, resolveComponentProps } from "agentcut/components";
|
|
21
|
-
|
|
22
|
-
// Project styles (project.json "style") come from the library too. Pages from before styles don't
|
|
23
|
-
// provide them: the project then renders as if it had none.
|
|
24
|
-
type LibraryStyle = { captions: CaptionsClip["style"]; text: TextClip["style"] };
|
|
25
|
-
function projectStyle(id: string | undefined): LibraryStyle | null {
|
|
26
|
-
const get = (ComponentLibrary as { getMotionStyle?: (id?: string) => LibraryStyle | null }).getMotionStyle;
|
|
27
|
-
return id && typeof get === "function" ? get(id) : null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type StyleLayer = {
|
|
31
|
-
styleCamera?: (
|
|
32
|
-
id: string | undefined,
|
|
33
|
-
at: { frame: number; durationInFrames: number; width: number; height: number },
|
|
34
|
-
) => CSSProperties | null;
|
|
35
|
-
StyleOverlay?: ComponentType<{ styleId: string | undefined }>;
|
|
36
|
-
StyledCaptions?: ComponentType<{
|
|
37
|
-
styleId: string | undefined;
|
|
38
|
-
words: CaptionsClip["words"];
|
|
39
|
-
position?: string;
|
|
40
|
-
wordsPerLine?: number;
|
|
41
|
-
fontSize?: number;
|
|
42
|
-
}>;
|
|
43
|
-
};
|
|
44
|
-
const styleLayer = ComponentLibrary as StyleLayer;
|
|
45
|
-
|
|
46
|
-
/** Loads the library's fonts (the style's caption and text fonts) and holds the render until they're in. */
|
|
47
|
-
function StyleFonts() {
|
|
48
|
-
const use = (ComponentLibrary as { useMotionFonts?: () => void }).useMotionFonts;
|
|
49
|
-
use?.();
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
import {
|
|
53
|
-
StudioRuntimeContext,
|
|
54
|
-
asset,
|
|
55
|
-
clamp,
|
|
56
|
-
setRuntimeState,
|
|
57
|
-
type StudioRuntimeContextValue,
|
|
58
|
-
} from "./helpers";
|
|
59
|
-
import type {
|
|
60
|
-
AudioClip,
|
|
61
|
-
Box,
|
|
62
|
-
CaptionsClip,
|
|
63
|
-
ClipAnim,
|
|
64
|
-
ImageClip,
|
|
65
|
-
SceneClip,
|
|
66
|
-
SceneProps,
|
|
67
|
-
StudioClip,
|
|
68
|
-
StudioCompositionProps,
|
|
69
|
-
StudioFont,
|
|
70
|
-
StudioTrack,
|
|
71
|
-
TextClip,
|
|
72
|
-
CursorTrack,
|
|
73
|
-
VideoClip,
|
|
74
|
-
VideoCursor,
|
|
75
|
-
} from "./types";
|
|
76
|
-
|
|
77
|
-
export type SceneRegistry = Record<string, ComponentType<SceneProps & Record<string, unknown>>>;
|
|
78
|
-
|
|
79
|
-
// ---------------------------------------------------------------------------
|
|
80
|
-
// Fonts
|
|
81
|
-
|
|
82
|
-
const loadedFontSpecs = new Map<string, Promise<void>>();
|
|
83
|
-
|
|
84
|
-
function fontSpecToCss(spec: string) {
|
|
85
|
-
// "Inter:400;700;900" → family=Inter:wght@400;700;900 ; "Inter" → family=Inter
|
|
86
|
-
const [family, weights] = spec.split(":");
|
|
87
|
-
const name = family.trim().replace(/\s+/g, "+");
|
|
88
|
-
return weights ? `family=${name}:wght@${weights.trim()}` : `family=${name}`;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function loadLocalFont(font: Exclude<StudioFont, string>): Promise<void> {
|
|
92
|
-
const key = `${font.family}|${font.src}|${font.weight ?? ""}|${font.style ?? ""}`;
|
|
93
|
-
const existing = loadedFontSpecs.get(key);
|
|
94
|
-
if (existing) return existing;
|
|
95
|
-
const promise = (async () => {
|
|
96
|
-
if (typeof document === "undefined") return;
|
|
97
|
-
const face = new FontFace(font.family, `url(${JSON.stringify(asset(font.src))})`, {
|
|
98
|
-
weight: font.weight ?? "400",
|
|
99
|
-
style: font.style ?? "normal",
|
|
100
|
-
});
|
|
101
|
-
try {
|
|
102
|
-
document.fonts.add(await face.load());
|
|
103
|
-
} catch {
|
|
104
|
-
// Missing font file: keep rendering with the fallback font.
|
|
105
|
-
}
|
|
106
|
-
})();
|
|
107
|
-
loadedFontSpecs.set(key, promise);
|
|
108
|
-
return promise;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function loadFont(font: StudioFont): Promise<void> {
|
|
112
|
-
return typeof font === "string" ? loadFontSpec(font) : loadLocalFont(font);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function loadFontSpec(spec: string): Promise<void> {
|
|
116
|
-
const existing = loadedFontSpecs.get(spec);
|
|
117
|
-
if (existing) return existing;
|
|
118
|
-
const promise = (async () => {
|
|
119
|
-
if (typeof document === "undefined") return;
|
|
120
|
-
const href = `https://fonts.googleapis.com/css2?${fontSpecToCss(spec)}&display=block`;
|
|
121
|
-
if (!document.querySelector(`link[data-studio-font="${CSS.escape(spec)}"]`)) {
|
|
122
|
-
const link = document.createElement("link");
|
|
123
|
-
link.rel = "stylesheet";
|
|
124
|
-
link.href = href;
|
|
125
|
-
link.crossOrigin = "anonymous";
|
|
126
|
-
link.dataset.studioFont = spec;
|
|
127
|
-
await new Promise<void>((resolve) => {
|
|
128
|
-
link.onload = () => resolve();
|
|
129
|
-
link.onerror = () => resolve();
|
|
130
|
-
document.head.appendChild(link);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
const [family, weights] = spec.split(":");
|
|
134
|
-
const list = weights ? weights.split(";").map((w) => w.trim()) : ["400"];
|
|
135
|
-
await Promise.all(
|
|
136
|
-
list.map((weight) =>
|
|
137
|
-
document.fonts.load(`${weight} 32px "${family.trim()}"`).catch(() => undefined),
|
|
138
|
-
),
|
|
139
|
-
);
|
|
140
|
-
})();
|
|
141
|
-
loadedFontSpecs.set(spec, promise);
|
|
142
|
-
return promise;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function useProjectFonts(fonts: StudioFont[]) {
|
|
146
|
-
const key = fonts.map((font) => (typeof font === "string" ? font : `${font.family}|${font.src}|${font.weight ?? ""}`)).join("||");
|
|
147
|
-
const [handle] = useState(() =>
|
|
148
|
-
fonts.length > 0 ? delayRender(`Loading fonts ${key}`, { timeoutInMilliseconds: 20_000 }) : null,
|
|
149
|
-
);
|
|
150
|
-
useEffect(() => {
|
|
151
|
-
if (handle === null) return;
|
|
152
|
-
let cancelled = false;
|
|
153
|
-
Promise.all(fonts.map(loadFont)).finally(() => {
|
|
154
|
-
if (!cancelled) continueRender(handle);
|
|
155
|
-
});
|
|
156
|
-
return () => {
|
|
157
|
-
cancelled = true;
|
|
158
|
-
};
|
|
159
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
|
-
}, [handle, key]);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// ---------------------------------------------------------------------------
|
|
164
|
-
// Clip helpers
|
|
165
|
-
|
|
166
|
-
export function resolveClip<T extends StudioClip>(clip: T, variant: string): T {
|
|
167
|
-
const override = (clip as { variants?: Record<string, Partial<T>> }).variants?.[variant];
|
|
168
|
-
return override ? ({ ...clip, ...override } as T) : clip;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function animState(anim: ClipAnim | undefined, t: number) {
|
|
172
|
-
// t: 0 = fully hidden, 1 = fully shown
|
|
173
|
-
const preset = anim?.preset ?? "none";
|
|
174
|
-
const out: { opacity: number; transform: string; filter?: string } = { opacity: 1, transform: "" };
|
|
175
|
-
if (preset === "none" || t >= 1) return out;
|
|
176
|
-
const inv = 1 - t;
|
|
177
|
-
switch (preset) {
|
|
178
|
-
case "fade":
|
|
179
|
-
out.opacity = t;
|
|
180
|
-
break;
|
|
181
|
-
case "slide-up":
|
|
182
|
-
out.opacity = t;
|
|
183
|
-
out.transform = `translateY(${inv * 8}%)`;
|
|
184
|
-
break;
|
|
185
|
-
case "slide-down":
|
|
186
|
-
out.opacity = t;
|
|
187
|
-
out.transform = `translateY(${-inv * 8}%)`;
|
|
188
|
-
break;
|
|
189
|
-
case "slide-left":
|
|
190
|
-
out.opacity = t;
|
|
191
|
-
out.transform = `translateX(${inv * 8}%)`;
|
|
192
|
-
break;
|
|
193
|
-
case "slide-right":
|
|
194
|
-
out.opacity = t;
|
|
195
|
-
out.transform = `translateX(${-inv * 8}%)`;
|
|
196
|
-
break;
|
|
197
|
-
case "pop": {
|
|
198
|
-
// overshoot: 0 → 1.08 → 1
|
|
199
|
-
const s = t < 0.7 ? interpolate(t, [0, 0.7], [0.6, 1.08]) : interpolate(t, [0.7, 1], [1.08, 1]);
|
|
200
|
-
out.opacity = Math.min(1, t * 2);
|
|
201
|
-
out.transform = `scale(${s})`;
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
case "scale":
|
|
205
|
-
out.opacity = t;
|
|
206
|
-
out.transform = `scale(${0.85 + 0.15 * t})`;
|
|
207
|
-
break;
|
|
208
|
-
case "blur":
|
|
209
|
-
out.opacity = t;
|
|
210
|
-
out.filter = `blur(${inv * 16}px)`;
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
return out;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const easeOutCubic = (t: number) => 1 - Math.pow(1 - t, 3);
|
|
217
|
-
|
|
218
|
-
/** Enter/exit + opacity wrapper for one visual clip; frame is local to the clip. */
|
|
219
|
-
function useClipStyle(clip: StudioClip): CSSProperties {
|
|
220
|
-
const frame = useCurrentFrame();
|
|
221
|
-
const enterFrames = clip.enter && clip.enter.preset !== "none" ? Math.max(1, clip.enter.frames) : 0;
|
|
222
|
-
const exitFrames = clip.exit && clip.exit.preset !== "none" ? Math.max(1, clip.exit.frames) : 0;
|
|
223
|
-
const tIn = enterFrames ? easeOutCubic(interpolate(frame, [0, enterFrames], [0, 1], clamp)) : 1;
|
|
224
|
-
const tOut = exitFrames
|
|
225
|
-
? easeOutCubic(interpolate(frame, [clip.duration - exitFrames, clip.duration], [1, 0], clamp))
|
|
226
|
-
: 1;
|
|
227
|
-
const a = animState(clip.enter, tIn);
|
|
228
|
-
const b = animState(clip.exit, tOut);
|
|
229
|
-
const transform = [a.transform, b.transform].filter(Boolean).join(" ");
|
|
230
|
-
const filter = [a.filter, b.filter].filter(Boolean).join(" ");
|
|
231
|
-
return {
|
|
232
|
-
opacity: a.opacity * b.opacity * (clip.opacity ?? 1),
|
|
233
|
-
transform: transform || undefined,
|
|
234
|
-
filter: filter || undefined,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function boxStyle(box: Box | undefined): CSSProperties {
|
|
239
|
-
if (!box) return { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" };
|
|
240
|
-
return {
|
|
241
|
-
position: "absolute",
|
|
242
|
-
left: `${box.x * 100}%`,
|
|
243
|
-
top: `${box.y * 100}%`,
|
|
244
|
-
width: `${box.width * 100}%`,
|
|
245
|
-
height: `${box.height * 100}%`,
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// ---------------------------------------------------------------------------
|
|
250
|
-
// Clip renderers (all frames local to the clip's <Sequence>)
|
|
251
|
-
|
|
252
|
-
function SceneClipView({
|
|
253
|
-
clip,
|
|
254
|
-
scenes,
|
|
255
|
-
ctx,
|
|
256
|
-
}: {
|
|
257
|
-
clip: SceneClip;
|
|
258
|
-
scenes: SceneRegistry;
|
|
259
|
-
ctx: StudioRuntimeContextValue;
|
|
260
|
-
}) {
|
|
261
|
-
const style = useClipStyle(clip);
|
|
262
|
-
if (clip.component) {
|
|
263
|
-
const entry = MOTION_COMPONENTS[clip.component];
|
|
264
|
-
if (!entry) {
|
|
265
|
-
return (
|
|
266
|
-
<AbsoluteFill
|
|
267
|
-
style={{
|
|
268
|
-
alignItems: "center",
|
|
269
|
-
justifyContent: "center",
|
|
270
|
-
background: "rgba(220,38,38,0.25)",
|
|
271
|
-
color: "#fff",
|
|
272
|
-
fontFamily: "sans-serif",
|
|
273
|
-
fontSize: Math.round(ctx.height * 0.03),
|
|
274
|
-
}}
|
|
275
|
-
>
|
|
276
|
-
Unknown component “{clip.component}”
|
|
277
|
-
</AbsoluteFill>
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
const Component = entry.Component;
|
|
281
|
-
return (
|
|
282
|
-
<AbsoluteFill style={style}>
|
|
283
|
-
<Component
|
|
284
|
-
{...resolveComponentProps(clip.component, clip.props, ctx.project.style)}
|
|
285
|
-
width={ctx.width}
|
|
286
|
-
height={ctx.height}
|
|
287
|
-
fps={ctx.fps}
|
|
288
|
-
durationInFrames={clip.duration}
|
|
289
|
-
variant={ctx.variant}
|
|
290
|
-
/>
|
|
291
|
-
</AbsoluteFill>
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
const Scene = clip.scene ? scenes[clip.scene] : undefined;
|
|
295
|
-
if (!Scene) {
|
|
296
|
-
return (
|
|
297
|
-
<AbsoluteFill
|
|
298
|
-
style={{
|
|
299
|
-
alignItems: "center",
|
|
300
|
-
justifyContent: "center",
|
|
301
|
-
background: "rgba(220,38,38,0.25)",
|
|
302
|
-
color: "#fff",
|
|
303
|
-
fontFamily: "sans-serif",
|
|
304
|
-
fontSize: Math.round(ctx.height * 0.03),
|
|
305
|
-
}}
|
|
306
|
-
>
|
|
307
|
-
Missing scene “{clip.scene}” (scenes/{clip.scene}.tsx)
|
|
308
|
-
</AbsoluteFill>
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
return (
|
|
312
|
-
<AbsoluteFill style={style}>
|
|
313
|
-
<Scene
|
|
314
|
-
{...(clip.props ?? {})}
|
|
315
|
-
clip={{ id: clip.id, from: clip.from, duration: clip.duration }}
|
|
316
|
-
variant={ctx.variant}
|
|
317
|
-
width={ctx.width}
|
|
318
|
-
height={ctx.height}
|
|
319
|
-
fps={ctx.fps}
|
|
320
|
-
/>
|
|
321
|
-
</AbsoluteFill>
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function ImageClipView({ clip }: { clip: ImageClip }) {
|
|
326
|
-
const style = useClipStyle(clip);
|
|
327
|
-
return (
|
|
328
|
-
<div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
|
|
329
|
-
<Img
|
|
330
|
-
src={asset(clip.src)}
|
|
331
|
-
style={{ width: "100%", height: "100%", objectFit: clip.fit ?? "cover", display: "block" }}
|
|
332
|
-
/>
|
|
333
|
-
</div>
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/** CSS filter for a color grade (supported by the browser export). */
|
|
338
|
-
function gradeFilter(grade: VideoClip["grade"]): string | undefined {
|
|
339
|
-
if (!grade) return undefined;
|
|
340
|
-
const parts: string[] = [];
|
|
341
|
-
if (grade.brightness !== undefined && grade.brightness !== 1) parts.push(`brightness(${grade.brightness})`);
|
|
342
|
-
if (grade.contrast !== undefined && grade.contrast !== 1) parts.push(`contrast(${grade.contrast})`);
|
|
343
|
-
if (grade.saturation !== undefined && grade.saturation !== 1) parts.push(`saturate(${grade.saturation})`);
|
|
344
|
-
const warmth = grade.warmth ?? 0;
|
|
345
|
-
if (warmth > 0) parts.push(`sepia(${(warmth * 0.35).toFixed(3)})`, `saturate(${(1 + warmth * 0.25).toFixed(3)})`);
|
|
346
|
-
const hue = (grade.hue ?? 0) + (warmth < 0 ? warmth * -12 : warmth * -4);
|
|
347
|
-
if (hue) parts.push(`hue-rotate(${hue.toFixed(1)}deg)`);
|
|
348
|
-
return parts.length ? parts.join(" ") : undefined;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function VideoClipView({
|
|
352
|
-
clip,
|
|
353
|
-
trackMuted,
|
|
354
|
-
gain,
|
|
355
|
-
ctx,
|
|
356
|
-
}: {
|
|
357
|
-
clip: VideoClip;
|
|
358
|
-
trackMuted: boolean;
|
|
359
|
-
gain: number;
|
|
360
|
-
ctx: StudioRuntimeContextValue;
|
|
361
|
-
}) {
|
|
362
|
-
const style = useClipStyle(clip);
|
|
363
|
-
const frame = useCurrentFrame();
|
|
364
|
-
// The project style's camera (punch-ins, drift, …) moves the footage inside its box.
|
|
365
|
-
const camera =
|
|
366
|
-
ctx.project.style && clip.camera !== "none" && styleLayer.styleCamera
|
|
367
|
-
? styleLayer.styleCamera(ctx.project.style, { frame, durationInFrames: clip.duration, width: ctx.width, height: ctx.height })
|
|
368
|
-
: null;
|
|
369
|
-
return (
|
|
370
|
-
<div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
|
|
371
|
-
<div style={{ position: "absolute", inset: 0, ...camera }}>
|
|
372
|
-
<Video
|
|
373
|
-
src={asset(clip.src)}
|
|
374
|
-
trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
|
|
375
|
-
volume={(clip.volume ?? 1) * gain}
|
|
376
|
-
muted={trackMuted || clip.muted}
|
|
377
|
-
playbackRate={clip.playbackRate}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
//
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
.then((
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
)
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
let
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
if (
|
|
446
|
-
const
|
|
447
|
-
if (
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
const
|
|
472
|
-
const
|
|
473
|
-
const
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
let
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
const
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
const
|
|
668
|
-
const
|
|
669
|
-
const
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
const
|
|
680
|
-
|
|
681
|
-
const
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
const
|
|
687
|
-
const
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
const
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
const
|
|
747
|
-
const
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
const
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
//
|
|
818
|
-
//
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
{
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
//
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
{
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
//
|
|
855
|
-
//
|
|
856
|
-
//
|
|
857
|
-
//
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
.studio-composition svg
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
style
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
)
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
}
|
|
1
|
+
// StudioComposition: draws a project's layers.json with Remotion.
|
|
2
|
+
// Tracks are drawn bottom → top (tracks[0] is the top layer) so stacking never
|
|
3
|
+
// depends on z-index, which the browser export ignores.
|
|
4
|
+
import { useEffect, useMemo, useState, type ComponentType, type CSSProperties, type ReactNode } from "react";
|
|
5
|
+
import {
|
|
6
|
+
AbsoluteFill,
|
|
7
|
+
Img,
|
|
8
|
+
Sequence,
|
|
9
|
+
continueRender,
|
|
10
|
+
delayRender,
|
|
11
|
+
interpolate,
|
|
12
|
+
useCurrentFrame,
|
|
13
|
+
useRemotionEnvironment,
|
|
14
|
+
useVideoConfig,
|
|
15
|
+
} from "remotion";
|
|
16
|
+
import { Audio, Video } from "@remotion/media";
|
|
17
|
+
// The Agentcut component library, provided by the studio page (and by a copy in
|
|
18
|
+
// .agentcut/components for local renders).
|
|
19
|
+
import * as ComponentLibrary from "agentcut/components";
|
|
20
|
+
import { MOTION_COMPONENTS, resolveComponentProps } from "agentcut/components";
|
|
21
|
+
|
|
22
|
+
// Project styles (project.json "style") come from the library too. Pages from before styles don't
|
|
23
|
+
// provide them: the project then renders as if it had none.
|
|
24
|
+
type LibraryStyle = { captions: CaptionsClip["style"]; text: TextClip["style"] };
|
|
25
|
+
function projectStyle(id: string | undefined): LibraryStyle | null {
|
|
26
|
+
const get = (ComponentLibrary as { getMotionStyle?: (id?: string) => LibraryStyle | null }).getMotionStyle;
|
|
27
|
+
return id && typeof get === "function" ? get(id) : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type StyleLayer = {
|
|
31
|
+
styleCamera?: (
|
|
32
|
+
id: string | undefined,
|
|
33
|
+
at: { frame: number; durationInFrames: number; width: number; height: number },
|
|
34
|
+
) => CSSProperties | null;
|
|
35
|
+
StyleOverlay?: ComponentType<{ styleId: string | undefined }>;
|
|
36
|
+
StyledCaptions?: ComponentType<{
|
|
37
|
+
styleId: string | undefined;
|
|
38
|
+
words: CaptionsClip["words"];
|
|
39
|
+
position?: string;
|
|
40
|
+
wordsPerLine?: number;
|
|
41
|
+
fontSize?: number;
|
|
42
|
+
}>;
|
|
43
|
+
};
|
|
44
|
+
const styleLayer = ComponentLibrary as StyleLayer;
|
|
45
|
+
|
|
46
|
+
/** Loads the library's fonts (the style's caption and text fonts) and holds the render until they're in. */
|
|
47
|
+
function StyleFonts() {
|
|
48
|
+
const use = (ComponentLibrary as { useMotionFonts?: () => void }).useMotionFonts;
|
|
49
|
+
use?.();
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
import {
|
|
53
|
+
StudioRuntimeContext,
|
|
54
|
+
asset,
|
|
55
|
+
clamp,
|
|
56
|
+
setRuntimeState,
|
|
57
|
+
type StudioRuntimeContextValue,
|
|
58
|
+
} from "./helpers";
|
|
59
|
+
import type {
|
|
60
|
+
AudioClip,
|
|
61
|
+
Box,
|
|
62
|
+
CaptionsClip,
|
|
63
|
+
ClipAnim,
|
|
64
|
+
ImageClip,
|
|
65
|
+
SceneClip,
|
|
66
|
+
SceneProps,
|
|
67
|
+
StudioClip,
|
|
68
|
+
StudioCompositionProps,
|
|
69
|
+
StudioFont,
|
|
70
|
+
StudioTrack,
|
|
71
|
+
TextClip,
|
|
72
|
+
CursorTrack,
|
|
73
|
+
VideoClip,
|
|
74
|
+
VideoCursor,
|
|
75
|
+
} from "./types";
|
|
76
|
+
|
|
77
|
+
export type SceneRegistry = Record<string, ComponentType<SceneProps & Record<string, unknown>>>;
|
|
78
|
+
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
// Fonts
|
|
81
|
+
|
|
82
|
+
const loadedFontSpecs = new Map<string, Promise<void>>();
|
|
83
|
+
|
|
84
|
+
function fontSpecToCss(spec: string) {
|
|
85
|
+
// "Inter:400;700;900" → family=Inter:wght@400;700;900 ; "Inter" → family=Inter
|
|
86
|
+
const [family, weights] = spec.split(":");
|
|
87
|
+
const name = family.trim().replace(/\s+/g, "+");
|
|
88
|
+
return weights ? `family=${name}:wght@${weights.trim()}` : `family=${name}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function loadLocalFont(font: Exclude<StudioFont, string>): Promise<void> {
|
|
92
|
+
const key = `${font.family}|${font.src}|${font.weight ?? ""}|${font.style ?? ""}`;
|
|
93
|
+
const existing = loadedFontSpecs.get(key);
|
|
94
|
+
if (existing) return existing;
|
|
95
|
+
const promise = (async () => {
|
|
96
|
+
if (typeof document === "undefined") return;
|
|
97
|
+
const face = new FontFace(font.family, `url(${JSON.stringify(asset(font.src))})`, {
|
|
98
|
+
weight: font.weight ?? "400",
|
|
99
|
+
style: font.style ?? "normal",
|
|
100
|
+
});
|
|
101
|
+
try {
|
|
102
|
+
document.fonts.add(await face.load());
|
|
103
|
+
} catch {
|
|
104
|
+
// Missing font file: keep rendering with the fallback font.
|
|
105
|
+
}
|
|
106
|
+
})();
|
|
107
|
+
loadedFontSpecs.set(key, promise);
|
|
108
|
+
return promise;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function loadFont(font: StudioFont): Promise<void> {
|
|
112
|
+
return typeof font === "string" ? loadFontSpec(font) : loadLocalFont(font);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function loadFontSpec(spec: string): Promise<void> {
|
|
116
|
+
const existing = loadedFontSpecs.get(spec);
|
|
117
|
+
if (existing) return existing;
|
|
118
|
+
const promise = (async () => {
|
|
119
|
+
if (typeof document === "undefined") return;
|
|
120
|
+
const href = `https://fonts.googleapis.com/css2?${fontSpecToCss(spec)}&display=block`;
|
|
121
|
+
if (!document.querySelector(`link[data-studio-font="${CSS.escape(spec)}"]`)) {
|
|
122
|
+
const link = document.createElement("link");
|
|
123
|
+
link.rel = "stylesheet";
|
|
124
|
+
link.href = href;
|
|
125
|
+
link.crossOrigin = "anonymous";
|
|
126
|
+
link.dataset.studioFont = spec;
|
|
127
|
+
await new Promise<void>((resolve) => {
|
|
128
|
+
link.onload = () => resolve();
|
|
129
|
+
link.onerror = () => resolve();
|
|
130
|
+
document.head.appendChild(link);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const [family, weights] = spec.split(":");
|
|
134
|
+
const list = weights ? weights.split(";").map((w) => w.trim()) : ["400"];
|
|
135
|
+
await Promise.all(
|
|
136
|
+
list.map((weight) =>
|
|
137
|
+
document.fonts.load(`${weight} 32px "${family.trim()}"`).catch(() => undefined),
|
|
138
|
+
),
|
|
139
|
+
);
|
|
140
|
+
})();
|
|
141
|
+
loadedFontSpecs.set(spec, promise);
|
|
142
|
+
return promise;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function useProjectFonts(fonts: StudioFont[]) {
|
|
146
|
+
const key = fonts.map((font) => (typeof font === "string" ? font : `${font.family}|${font.src}|${font.weight ?? ""}`)).join("||");
|
|
147
|
+
const [handle] = useState(() =>
|
|
148
|
+
fonts.length > 0 ? delayRender(`Loading fonts ${key}`, { timeoutInMilliseconds: 20_000 }) : null,
|
|
149
|
+
);
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
if (handle === null) return;
|
|
152
|
+
let cancelled = false;
|
|
153
|
+
Promise.all(fonts.map(loadFont)).finally(() => {
|
|
154
|
+
if (!cancelled) continueRender(handle);
|
|
155
|
+
});
|
|
156
|
+
return () => {
|
|
157
|
+
cancelled = true;
|
|
158
|
+
};
|
|
159
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
|
+
}, [handle, key]);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
// Clip helpers
|
|
165
|
+
|
|
166
|
+
export function resolveClip<T extends StudioClip>(clip: T, variant: string): T {
|
|
167
|
+
const override = (clip as { variants?: Record<string, Partial<T>> }).variants?.[variant];
|
|
168
|
+
return override ? ({ ...clip, ...override } as T) : clip;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function animState(anim: ClipAnim | undefined, t: number) {
|
|
172
|
+
// t: 0 = fully hidden, 1 = fully shown
|
|
173
|
+
const preset = anim?.preset ?? "none";
|
|
174
|
+
const out: { opacity: number; transform: string; filter?: string } = { opacity: 1, transform: "" };
|
|
175
|
+
if (preset === "none" || t >= 1) return out;
|
|
176
|
+
const inv = 1 - t;
|
|
177
|
+
switch (preset) {
|
|
178
|
+
case "fade":
|
|
179
|
+
out.opacity = t;
|
|
180
|
+
break;
|
|
181
|
+
case "slide-up":
|
|
182
|
+
out.opacity = t;
|
|
183
|
+
out.transform = `translateY(${inv * 8}%)`;
|
|
184
|
+
break;
|
|
185
|
+
case "slide-down":
|
|
186
|
+
out.opacity = t;
|
|
187
|
+
out.transform = `translateY(${-inv * 8}%)`;
|
|
188
|
+
break;
|
|
189
|
+
case "slide-left":
|
|
190
|
+
out.opacity = t;
|
|
191
|
+
out.transform = `translateX(${inv * 8}%)`;
|
|
192
|
+
break;
|
|
193
|
+
case "slide-right":
|
|
194
|
+
out.opacity = t;
|
|
195
|
+
out.transform = `translateX(${-inv * 8}%)`;
|
|
196
|
+
break;
|
|
197
|
+
case "pop": {
|
|
198
|
+
// overshoot: 0 → 1.08 → 1
|
|
199
|
+
const s = t < 0.7 ? interpolate(t, [0, 0.7], [0.6, 1.08]) : interpolate(t, [0.7, 1], [1.08, 1]);
|
|
200
|
+
out.opacity = Math.min(1, t * 2);
|
|
201
|
+
out.transform = `scale(${s})`;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "scale":
|
|
205
|
+
out.opacity = t;
|
|
206
|
+
out.transform = `scale(${0.85 + 0.15 * t})`;
|
|
207
|
+
break;
|
|
208
|
+
case "blur":
|
|
209
|
+
out.opacity = t;
|
|
210
|
+
out.filter = `blur(${inv * 16}px)`;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const easeOutCubic = (t: number) => 1 - Math.pow(1 - t, 3);
|
|
217
|
+
|
|
218
|
+
/** Enter/exit + opacity wrapper for one visual clip; frame is local to the clip. */
|
|
219
|
+
function useClipStyle(clip: StudioClip): CSSProperties {
|
|
220
|
+
const frame = useCurrentFrame();
|
|
221
|
+
const enterFrames = clip.enter && clip.enter.preset !== "none" ? Math.max(1, clip.enter.frames) : 0;
|
|
222
|
+
const exitFrames = clip.exit && clip.exit.preset !== "none" ? Math.max(1, clip.exit.frames) : 0;
|
|
223
|
+
const tIn = enterFrames ? easeOutCubic(interpolate(frame, [0, enterFrames], [0, 1], clamp)) : 1;
|
|
224
|
+
const tOut = exitFrames
|
|
225
|
+
? easeOutCubic(interpolate(frame, [clip.duration - exitFrames, clip.duration], [1, 0], clamp))
|
|
226
|
+
: 1;
|
|
227
|
+
const a = animState(clip.enter, tIn);
|
|
228
|
+
const b = animState(clip.exit, tOut);
|
|
229
|
+
const transform = [a.transform, b.transform].filter(Boolean).join(" ");
|
|
230
|
+
const filter = [a.filter, b.filter].filter(Boolean).join(" ");
|
|
231
|
+
return {
|
|
232
|
+
opacity: a.opacity * b.opacity * (clip.opacity ?? 1),
|
|
233
|
+
transform: transform || undefined,
|
|
234
|
+
filter: filter || undefined,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function boxStyle(box: Box | undefined): CSSProperties {
|
|
239
|
+
if (!box) return { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" };
|
|
240
|
+
return {
|
|
241
|
+
position: "absolute",
|
|
242
|
+
left: `${box.x * 100}%`,
|
|
243
|
+
top: `${box.y * 100}%`,
|
|
244
|
+
width: `${box.width * 100}%`,
|
|
245
|
+
height: `${box.height * 100}%`,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Clip renderers (all frames local to the clip's <Sequence>)
|
|
251
|
+
|
|
252
|
+
function SceneClipView({
|
|
253
|
+
clip,
|
|
254
|
+
scenes,
|
|
255
|
+
ctx,
|
|
256
|
+
}: {
|
|
257
|
+
clip: SceneClip;
|
|
258
|
+
scenes: SceneRegistry;
|
|
259
|
+
ctx: StudioRuntimeContextValue;
|
|
260
|
+
}) {
|
|
261
|
+
const style = useClipStyle(clip);
|
|
262
|
+
if (clip.component) {
|
|
263
|
+
const entry = MOTION_COMPONENTS[clip.component];
|
|
264
|
+
if (!entry) {
|
|
265
|
+
return (
|
|
266
|
+
<AbsoluteFill
|
|
267
|
+
style={{
|
|
268
|
+
alignItems: "center",
|
|
269
|
+
justifyContent: "center",
|
|
270
|
+
background: "rgba(220,38,38,0.25)",
|
|
271
|
+
color: "#fff",
|
|
272
|
+
fontFamily: "sans-serif",
|
|
273
|
+
fontSize: Math.round(ctx.height * 0.03),
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
Unknown component “{clip.component}”
|
|
277
|
+
</AbsoluteFill>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const Component = entry.Component;
|
|
281
|
+
return (
|
|
282
|
+
<AbsoluteFill style={style}>
|
|
283
|
+
<Component
|
|
284
|
+
{...resolveComponentProps(clip.component, clip.props, ctx.project.style)}
|
|
285
|
+
width={ctx.width}
|
|
286
|
+
height={ctx.height}
|
|
287
|
+
fps={ctx.fps}
|
|
288
|
+
durationInFrames={clip.duration}
|
|
289
|
+
variant={ctx.variant}
|
|
290
|
+
/>
|
|
291
|
+
</AbsoluteFill>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const Scene = clip.scene ? scenes[clip.scene] : undefined;
|
|
295
|
+
if (!Scene) {
|
|
296
|
+
return (
|
|
297
|
+
<AbsoluteFill
|
|
298
|
+
style={{
|
|
299
|
+
alignItems: "center",
|
|
300
|
+
justifyContent: "center",
|
|
301
|
+
background: "rgba(220,38,38,0.25)",
|
|
302
|
+
color: "#fff",
|
|
303
|
+
fontFamily: "sans-serif",
|
|
304
|
+
fontSize: Math.round(ctx.height * 0.03),
|
|
305
|
+
}}
|
|
306
|
+
>
|
|
307
|
+
Missing scene “{clip.scene}” (scenes/{clip.scene}.tsx)
|
|
308
|
+
</AbsoluteFill>
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
return (
|
|
312
|
+
<AbsoluteFill style={style}>
|
|
313
|
+
<Scene
|
|
314
|
+
{...(clip.props ?? {})}
|
|
315
|
+
clip={{ id: clip.id, from: clip.from, duration: clip.duration }}
|
|
316
|
+
variant={ctx.variant}
|
|
317
|
+
width={ctx.width}
|
|
318
|
+
height={ctx.height}
|
|
319
|
+
fps={ctx.fps}
|
|
320
|
+
/>
|
|
321
|
+
</AbsoluteFill>
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function ImageClipView({ clip }: { clip: ImageClip }) {
|
|
326
|
+
const style = useClipStyle(clip);
|
|
327
|
+
return (
|
|
328
|
+
<div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
|
|
329
|
+
<Img
|
|
330
|
+
src={asset(clip.src)}
|
|
331
|
+
style={{ width: "100%", height: "100%", objectFit: clip.fit ?? "cover", display: "block" }}
|
|
332
|
+
/>
|
|
333
|
+
</div>
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** CSS filter for a color grade (supported by the browser export). */
|
|
338
|
+
function gradeFilter(grade: VideoClip["grade"]): string | undefined {
|
|
339
|
+
if (!grade) return undefined;
|
|
340
|
+
const parts: string[] = [];
|
|
341
|
+
if (grade.brightness !== undefined && grade.brightness !== 1) parts.push(`brightness(${grade.brightness})`);
|
|
342
|
+
if (grade.contrast !== undefined && grade.contrast !== 1) parts.push(`contrast(${grade.contrast})`);
|
|
343
|
+
if (grade.saturation !== undefined && grade.saturation !== 1) parts.push(`saturate(${grade.saturation})`);
|
|
344
|
+
const warmth = grade.warmth ?? 0;
|
|
345
|
+
if (warmth > 0) parts.push(`sepia(${(warmth * 0.35).toFixed(3)})`, `saturate(${(1 + warmth * 0.25).toFixed(3)})`);
|
|
346
|
+
const hue = (grade.hue ?? 0) + (warmth < 0 ? warmth * -12 : warmth * -4);
|
|
347
|
+
if (hue) parts.push(`hue-rotate(${hue.toFixed(1)}deg)`);
|
|
348
|
+
return parts.length ? parts.join(" ") : undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function VideoClipView({
|
|
352
|
+
clip,
|
|
353
|
+
trackMuted,
|
|
354
|
+
gain,
|
|
355
|
+
ctx,
|
|
356
|
+
}: {
|
|
357
|
+
clip: VideoClip;
|
|
358
|
+
trackMuted: boolean;
|
|
359
|
+
gain: number;
|
|
360
|
+
ctx: StudioRuntimeContextValue;
|
|
361
|
+
}) {
|
|
362
|
+
const style = useClipStyle(clip);
|
|
363
|
+
const frame = useCurrentFrame();
|
|
364
|
+
// The project style's camera (punch-ins, drift, …) moves the footage inside its box.
|
|
365
|
+
const camera =
|
|
366
|
+
ctx.project.style && clip.camera !== "none" && styleLayer.styleCamera
|
|
367
|
+
? styleLayer.styleCamera(ctx.project.style, { frame, durationInFrames: clip.duration, width: ctx.width, height: ctx.height })
|
|
368
|
+
: null;
|
|
369
|
+
return (
|
|
370
|
+
<div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
|
|
371
|
+
<div style={{ position: "absolute", inset: 0, ...camera }}>
|
|
372
|
+
<Video
|
|
373
|
+
src={asset(clip.src)}
|
|
374
|
+
trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
|
|
375
|
+
volume={(clip.volume ?? 1) * gain}
|
|
376
|
+
muted={trackMuted || clip.muted}
|
|
377
|
+
playbackRate={clip.playbackRate}
|
|
378
|
+
// @remotion/media draws the frame itself: fit has to be its prop (CSS object-fit is ignored).
|
|
379
|
+
objectFit={clip.fit ?? "cover"}
|
|
380
|
+
style={{ width: "100%", height: "100%", filter: gradeFilter(clip.grade) }}
|
|
381
|
+
/>
|
|
382
|
+
{clip.cursor ? <CursorOverlay cursor={clip.cursor} clip={clip} /> : null}
|
|
383
|
+
</div>
|
|
384
|
+
</div>
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ---------------------------------------------------------------------------
|
|
389
|
+
// Recorded cursor (`cursor` on a video clip): the pointer and click ripples from a
|
|
390
|
+
// `<video>.cursor.json`, drawn inside the clip so it follows its trim, speed and box.
|
|
391
|
+
|
|
392
|
+
const loadedCursorTracks = new Map<string, CursorTrack | null>();
|
|
393
|
+
const pendingCursorTracks = new Map<string, Promise<CursorTrack | null>>();
|
|
394
|
+
|
|
395
|
+
function loadCursorTrack(url: string) {
|
|
396
|
+
let pending = pendingCursorTracks.get(url);
|
|
397
|
+
if (!pending) {
|
|
398
|
+
pending = fetch(url)
|
|
399
|
+
.then((response) => (response.ok ? (response.json() as Promise<CursorTrack>) : null))
|
|
400
|
+
.catch(() => null)
|
|
401
|
+
.then((track) => {
|
|
402
|
+
loadedCursorTracks.set(url, track);
|
|
403
|
+
return track;
|
|
404
|
+
});
|
|
405
|
+
pendingCursorTracks.set(url, pending);
|
|
406
|
+
}
|
|
407
|
+
return pending;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function useCursorTrack(src: string) {
|
|
411
|
+
const url = asset(src);
|
|
412
|
+
const [track, setTrack] = useState(() => loadedCursorTracks.get(url) ?? null);
|
|
413
|
+
const [handle] = useState(() =>
|
|
414
|
+
loadedCursorTracks.has(url) ? null : delayRender(`Loading cursor ${src}`, { timeoutInMilliseconds: 20_000 }),
|
|
415
|
+
);
|
|
416
|
+
useEffect(() => {
|
|
417
|
+
let cancelled = false;
|
|
418
|
+
loadCursorTrack(url)
|
|
419
|
+
.then((loaded) => {
|
|
420
|
+
if (!cancelled) setTrack(loaded);
|
|
421
|
+
})
|
|
422
|
+
.finally(() => {
|
|
423
|
+
if (handle !== null) continueRender(handle);
|
|
424
|
+
});
|
|
425
|
+
return () => {
|
|
426
|
+
cancelled = true;
|
|
427
|
+
};
|
|
428
|
+
}, [url, handle]);
|
|
429
|
+
return track;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function cursorPointAt(path: CursorTrack["path"], frame: number) {
|
|
433
|
+
let low = 0;
|
|
434
|
+
let high = path.length - 1;
|
|
435
|
+
let index = -1;
|
|
436
|
+
while (low <= high) {
|
|
437
|
+
const mid = (low + high) >> 1;
|
|
438
|
+
if (path[mid][0] <= frame) {
|
|
439
|
+
index = mid;
|
|
440
|
+
low = mid + 1;
|
|
441
|
+
} else {
|
|
442
|
+
high = mid - 1;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (index < 0) return null;
|
|
446
|
+
const [from, x, y] = path[index];
|
|
447
|
+
if (x === null || y === null) return null;
|
|
448
|
+
const next = path[index + 1];
|
|
449
|
+
if (!next || next[1] === null || next[2] === null || next[0] <= from) return { x, y };
|
|
450
|
+
const t = Math.min(1, (frame - from) / (next[0] - from));
|
|
451
|
+
return { x: x + (next[1] - x) * t, y: y + (next[2] - y) * t };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const CURSOR_POINTER_PATH =
|
|
455
|
+
"M3.55 1.75Q2.65 1.4 2.65 2.45L2.65 19.95Q2.65 21.45 3.6 21.75Q4.2 21.95 4.85 21.3L8.25 17.85Q8.65 17.45 8.9 17.98L11.65 23.4Q12.15 24.35 13.1 23.88L15.55 22.7Q16.55 22.22 16.05 21.25L13.3 15.8Q13.02 15.25 13.65 15.25L19.1 15.25Q20.55 15.25 20.78 14.2Q20.95 13.42 20.05 12.78L4.45 1.95Q3.98 1.62 3.55 1.75Z";
|
|
456
|
+
const CURSOR_GREEN = "#2dd47a";
|
|
457
|
+
const HAND_EMOJI = "\u{1F446}";
|
|
458
|
+
|
|
459
|
+
function CursorOverlay({ cursor, clip }: { cursor: VideoCursor; clip: VideoClip }) {
|
|
460
|
+
const frame = useCurrentFrame();
|
|
461
|
+
const { width: compWidth, height: compHeight } = useVideoConfig();
|
|
462
|
+
const track = useCursorTrack(cursor.src);
|
|
463
|
+
// Edits ("source" space) carry clicks for timing only: their positions don't match the picture.
|
|
464
|
+
if (!track || track.space !== "video") return null;
|
|
465
|
+
|
|
466
|
+
const style = cursor.style ?? "classic";
|
|
467
|
+
const fps = track.fps;
|
|
468
|
+
const sourceFrame = (clip.trimStart ?? 0) + frame * (clip.playbackRate ?? 1);
|
|
469
|
+
|
|
470
|
+
// Where the video's picture sits inside the clip box (contain letterboxes, cover crops).
|
|
471
|
+
const box = clip.box ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
472
|
+
const boxWidth = box.width * compWidth;
|
|
473
|
+
const boxHeight = box.height * compHeight;
|
|
474
|
+
const videoWidth = track.width ?? boxWidth;
|
|
475
|
+
const videoHeight = track.height ?? boxHeight;
|
|
476
|
+
const scale =
|
|
477
|
+
(clip.fit ?? "cover") === "contain"
|
|
478
|
+
? Math.min(boxWidth / videoWidth, boxHeight / videoHeight)
|
|
479
|
+
: Math.max(boxWidth / videoWidth, boxHeight / videoHeight);
|
|
480
|
+
const offsetX = (boxWidth - videoWidth * scale) / 2;
|
|
481
|
+
const offsetY = (boxHeight - videoHeight * scale) / 2;
|
|
482
|
+
const toBox = (x: number, y: number) => ({
|
|
483
|
+
left: offsetX + x * videoWidth * scale,
|
|
484
|
+
top: offsetY + y * videoHeight * scale,
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Sized to the frame, not the box, so a punch-in doesn't blow the pointer up.
|
|
488
|
+
const size =
|
|
489
|
+
Math.max(18, Math.min(44, Math.min(compWidth, compHeight) * 0.032)) * ((cursor.size ?? 100) / 100);
|
|
490
|
+
|
|
491
|
+
// Smoothing averages the path over a short window around this frame.
|
|
492
|
+
const reach = Math.round((cursor.smoothing ?? 0.3) * 0.2 * fps);
|
|
493
|
+
let sumX = 0;
|
|
494
|
+
let sumY = 0;
|
|
495
|
+
let count = 0;
|
|
496
|
+
for (let offset = -reach; offset <= reach; offset += 1) {
|
|
497
|
+
const sample = cursorPointAt(track.path, sourceFrame + offset);
|
|
498
|
+
if (!sample) continue;
|
|
499
|
+
sumX += sample.x;
|
|
500
|
+
sumY += sample.y;
|
|
501
|
+
count += 1;
|
|
502
|
+
}
|
|
503
|
+
const point = cursorPointAt(track.path, sourceFrame) && count ? toBox(sumX / count, sumY / count) : null;
|
|
504
|
+
|
|
505
|
+
const rippleFrames = 0.6 * fps;
|
|
506
|
+
const ripples =
|
|
507
|
+
cursor.clicks === false
|
|
508
|
+
? []
|
|
509
|
+
: track.clicks.filter((click) => sourceFrame >= click.frame && sourceFrame - click.frame < rippleFrames);
|
|
510
|
+
let lastPressFrame: number | null = null;
|
|
511
|
+
for (const click of track.clicks) {
|
|
512
|
+
if (click.frame > sourceFrame) break;
|
|
513
|
+
lastPressFrame = click.frame;
|
|
514
|
+
}
|
|
515
|
+
const press = lastPressFrame === null ? 0 : Math.max(0, 1 - (sourceFrame - lastPressFrame) / (0.3 * fps));
|
|
516
|
+
const rippleColor = style === "accent" || style === "spotlight" ? CURSOR_GREEN : "#ffffff";
|
|
517
|
+
|
|
518
|
+
return (
|
|
519
|
+
<div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
|
|
520
|
+
{ripples.map((click) => {
|
|
521
|
+
const progress = (sourceFrame - click.frame) / rippleFrames;
|
|
522
|
+
const diameter = size * (0.6 + 2.2 * progress);
|
|
523
|
+
const at = toBox(click.x, click.y);
|
|
524
|
+
return (
|
|
525
|
+
<div
|
|
526
|
+
key={click.frame}
|
|
527
|
+
style={{
|
|
528
|
+
position: "absolute",
|
|
529
|
+
left: at.left - diameter / 2,
|
|
530
|
+
top: at.top - diameter / 2,
|
|
531
|
+
width: diameter,
|
|
532
|
+
height: diameter,
|
|
533
|
+
borderRadius: "50%",
|
|
534
|
+
border: `${Math.max(2, size * 0.09)}px solid ${rippleColor}`,
|
|
535
|
+
boxShadow: "0 0 6px rgba(0,0,0,0.45)",
|
|
536
|
+
boxSizing: "border-box",
|
|
537
|
+
opacity: 0.9 * (1 - progress),
|
|
538
|
+
}}
|
|
539
|
+
/>
|
|
540
|
+
);
|
|
541
|
+
})}
|
|
542
|
+
{point && style === "spotlight" ? (
|
|
543
|
+
<div
|
|
544
|
+
style={{
|
|
545
|
+
position: "absolute",
|
|
546
|
+
left: point.left - size * 0.42,
|
|
547
|
+
top: point.top - size * 0.42,
|
|
548
|
+
width: size * 0.84,
|
|
549
|
+
height: size * 0.84,
|
|
550
|
+
borderRadius: "50%",
|
|
551
|
+
border: `${Math.max(2, size * 0.08)}px solid ${CURSOR_GREEN}`,
|
|
552
|
+
background: "rgba(45, 212, 122, 0.16)",
|
|
553
|
+
boxShadow: "0 1px 4px rgba(0,0,0,0.45)",
|
|
554
|
+
boxSizing: "border-box",
|
|
555
|
+
transform: `scale(${1 - press * 0.15})`,
|
|
556
|
+
}}
|
|
557
|
+
/>
|
|
558
|
+
) : null}
|
|
559
|
+
{point && style === "hand" ? (
|
|
560
|
+
<div
|
|
561
|
+
style={{
|
|
562
|
+
position: "absolute",
|
|
563
|
+
left: point.left - size * 0.42,
|
|
564
|
+
top: point.top - size * 0.16,
|
|
565
|
+
fontSize: size,
|
|
566
|
+
lineHeight: 1,
|
|
567
|
+
textShadow: "0 1px 3px rgba(0,0,0,0.55)",
|
|
568
|
+
transform: `rotate(${press * 9}deg) scale(${1 - press * 0.05})`,
|
|
569
|
+
transformOrigin: "42% 16%",
|
|
570
|
+
}}
|
|
571
|
+
>
|
|
572
|
+
{HAND_EMOJI}
|
|
573
|
+
</div>
|
|
574
|
+
) : null}
|
|
575
|
+
{point && (style === "classic" || style === "accent") ? (
|
|
576
|
+
<svg
|
|
577
|
+
width={size}
|
|
578
|
+
height={size * 1.1}
|
|
579
|
+
viewBox="0 0 24 26.4"
|
|
580
|
+
style={{
|
|
581
|
+
position: "absolute",
|
|
582
|
+
left: point.left - (3.55 / 24) * size,
|
|
583
|
+
top: point.top - (1.75 / 24) * size,
|
|
584
|
+
overflow: "visible",
|
|
585
|
+
transform: `scale(${1 - press * 0.08})`,
|
|
586
|
+
transformOrigin: `${(3.55 / 24) * 100}% ${(1.75 / 26.4) * 100}%`,
|
|
587
|
+
}}
|
|
588
|
+
>
|
|
589
|
+
<defs>
|
|
590
|
+
<filter id="studio-cursor-shadow" x="-50%" y="-50%" width="200%" height="200%">
|
|
591
|
+
<feDropShadow dx="0" dy="1.5" stdDeviation="1.1" floodColor="#000000" floodOpacity="0.65" />
|
|
592
|
+
</filter>
|
|
593
|
+
</defs>
|
|
594
|
+
<path
|
|
595
|
+
d={CURSOR_POINTER_PATH}
|
|
596
|
+
fill={style === "accent" ? CURSOR_GREEN : "#ffffff"}
|
|
597
|
+
stroke="#000000"
|
|
598
|
+
strokeWidth={2.4}
|
|
599
|
+
strokeLinejoin="round"
|
|
600
|
+
filter="url(#studio-cursor-shadow)"
|
|
601
|
+
/>
|
|
602
|
+
</svg>
|
|
603
|
+
) : null}
|
|
604
|
+
</div>
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function TextClipView({ clip, ctx }: { clip: TextClip; ctx: StudioRuntimeContextValue }) {
|
|
609
|
+
const style = useClipStyle(clip);
|
|
610
|
+
const s = { ...projectStyle(ctx.project.style)?.text, ...clip.style };
|
|
611
|
+
const align = s.align ?? "center";
|
|
612
|
+
return (
|
|
613
|
+
<div
|
|
614
|
+
style={{
|
|
615
|
+
...boxStyle(clip.box ?? { x: 0.05, y: 0.4, width: 0.9, height: 0.2 }),
|
|
616
|
+
...style,
|
|
617
|
+
display: "flex",
|
|
618
|
+
alignItems: "center",
|
|
619
|
+
justifyContent: align === "left" ? "flex-start" : align === "right" ? "flex-end" : "center",
|
|
620
|
+
}}
|
|
621
|
+
>
|
|
622
|
+
<div
|
|
623
|
+
style={{
|
|
624
|
+
fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
|
|
625
|
+
fontSize: s.fontSize ?? Math.round(ctx.height * 0.07),
|
|
626
|
+
fontWeight: s.fontWeight ?? 800,
|
|
627
|
+
color: s.color ?? "#ffffff",
|
|
628
|
+
textAlign: align,
|
|
629
|
+
lineHeight: s.lineHeight ?? 1.1,
|
|
630
|
+
letterSpacing: s.letterSpacing,
|
|
631
|
+
textTransform: s.textTransform,
|
|
632
|
+
textShadow: s.shadow,
|
|
633
|
+
whiteSpace: "pre-wrap",
|
|
634
|
+
width: "100%",
|
|
635
|
+
}}
|
|
636
|
+
>
|
|
637
|
+
{clip.text}
|
|
638
|
+
</div>
|
|
639
|
+
</div>
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function CaptionsClipView({ clip, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
|
|
644
|
+
const StyledCaptions = styleLayer.StyledCaptions;
|
|
645
|
+
if (ctx.project.style && !clip.style?.preset && StyledCaptions && projectStyle(ctx.project.style)) {
|
|
646
|
+
return (
|
|
647
|
+
<StyledCaptionsClip clip={clip} ctx={ctx}>
|
|
648
|
+
<StyledCaptions
|
|
649
|
+
styleId={ctx.project.style}
|
|
650
|
+
words={clip.words}
|
|
651
|
+
position={clip.style?.position}
|
|
652
|
+
wordsPerLine={clip.style?.maxWordsPerLine}
|
|
653
|
+
fontSize={clip.style?.fontSize}
|
|
654
|
+
/>
|
|
655
|
+
</StyledCaptionsClip>
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
return <PlainCaptionsClipView clip={clip} ctx={ctx} />;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function StyledCaptionsClip({ clip, children }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue; children: ReactNode }) {
|
|
662
|
+
const style = useClipStyle(clip);
|
|
663
|
+
return <AbsoluteFill style={{ ...style, pointerEvents: "none" }}>{children}</AbsoluteFill>;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function PlainCaptionsClipView({ clip, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
|
|
667
|
+
const frame = useCurrentFrame();
|
|
668
|
+
const style = useClipStyle(clip);
|
|
669
|
+
const s = { ...projectStyle(ctx.project.style)?.captions, ...clip.style };
|
|
670
|
+
const perLine = Math.max(1, s.maxWordsPerLine ?? 4);
|
|
671
|
+
const words = clip.words;
|
|
672
|
+
// Group words into lines of N; show the line containing the current (or last spoken) word.
|
|
673
|
+
let current = -1;
|
|
674
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
675
|
+
if (words[i].from <= frame) current = i;
|
|
676
|
+
else break;
|
|
677
|
+
}
|
|
678
|
+
if (current < 0) return null;
|
|
679
|
+
const lineStart = Math.floor(current / perLine) * perLine;
|
|
680
|
+
const line = words.slice(lineStart, lineStart + perLine);
|
|
681
|
+
const lastWord = line[line.length - 1];
|
|
682
|
+
// Hide the line once its last word ended and the next line hasn't started (pause > 12 frames).
|
|
683
|
+
const next = words[lineStart + perLine];
|
|
684
|
+
if (frame > lastWord.to + 12 && (!next || next.from > frame)) return null;
|
|
685
|
+
|
|
686
|
+
const fontSize = s.fontSize ?? Math.round(ctx.height * (ctx.height > ctx.width ? 0.045 : 0.06));
|
|
687
|
+
const top = s.position === "top" ? "8%" : s.position === "center" ? "50%" : undefined;
|
|
688
|
+
const bottom = s.position === "top" || s.position === "center" ? undefined : "10%";
|
|
689
|
+
const preset = s.preset ?? "pop";
|
|
690
|
+
return (
|
|
691
|
+
<AbsoluteFill style={{ ...style, pointerEvents: "none" }}>
|
|
692
|
+
<div
|
|
693
|
+
style={{
|
|
694
|
+
position: "absolute",
|
|
695
|
+
left: "6%",
|
|
696
|
+
right: "6%",
|
|
697
|
+
top,
|
|
698
|
+
bottom,
|
|
699
|
+
transform: s.position === "center" ? "translateY(-50%)" : undefined,
|
|
700
|
+
display: "flex",
|
|
701
|
+
flexWrap: "wrap",
|
|
702
|
+
justifyContent: "center",
|
|
703
|
+
gap: `0 ${Math.round(fontSize * 0.28)}px`,
|
|
704
|
+
fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
|
|
705
|
+
fontWeight: s.fontWeight ?? 900,
|
|
706
|
+
fontSize,
|
|
707
|
+
lineHeight: 1.15,
|
|
708
|
+
textAlign: "center",
|
|
709
|
+
}}
|
|
710
|
+
>
|
|
711
|
+
{line.map((word, i) => {
|
|
712
|
+
const index = lineStart + i;
|
|
713
|
+
const active = index === current && frame <= word.to + 2;
|
|
714
|
+
const spoken = index <= current;
|
|
715
|
+
let scale = 1;
|
|
716
|
+
if (preset === "pop" && index === current) {
|
|
717
|
+
scale = interpolate(frame, [word.from, word.from + 4, word.from + 8], [0.85, 1.12, 1], clamp);
|
|
718
|
+
}
|
|
719
|
+
const color =
|
|
720
|
+
preset === "plain"
|
|
721
|
+
? (s.color ?? "#fff")
|
|
722
|
+
: active || (preset === "karaoke" && spoken)
|
|
723
|
+
? (s.highlight ?? "#facc15")
|
|
724
|
+
: (s.color ?? "#fff");
|
|
725
|
+
return (
|
|
726
|
+
<span
|
|
727
|
+
key={`${index}-${word.text}`}
|
|
728
|
+
style={{
|
|
729
|
+
display: "inline-block",
|
|
730
|
+
color,
|
|
731
|
+
opacity: preset === "pop" && !spoken ? 0 : 1,
|
|
732
|
+
transform: `scale(${scale})`,
|
|
733
|
+
textShadow: "0 4px 18px rgba(0,0,0,0.55)",
|
|
734
|
+
}}
|
|
735
|
+
>
|
|
736
|
+
{word.text}
|
|
737
|
+
</span>
|
|
738
|
+
);
|
|
739
|
+
})}
|
|
740
|
+
</div>
|
|
741
|
+
</AbsoluteFill>
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function AudioClipView({ clip, trackMuted, gain }: { clip: AudioClip; trackMuted: boolean; gain: number }) {
|
|
746
|
+
const base = (clip.volume ?? 1) * gain;
|
|
747
|
+
const fadeIn = Math.max(0, clip.fadeIn ?? 0);
|
|
748
|
+
const fadeOut = Math.max(0, clip.fadeOut ?? 0);
|
|
749
|
+
const duration = clip.duration;
|
|
750
|
+
return (
|
|
751
|
+
<Audio
|
|
752
|
+
src={asset(clip.src)}
|
|
753
|
+
trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
|
|
754
|
+
muted={trackMuted}
|
|
755
|
+
loop={clip.loop}
|
|
756
|
+
playbackRate={clip.playbackRate}
|
|
757
|
+
volume={
|
|
758
|
+
fadeIn || fadeOut
|
|
759
|
+
? (f: number) => {
|
|
760
|
+
const a = fadeIn ? interpolate(f, [0, fadeIn], [0, 1], clamp) : 1;
|
|
761
|
+
const b = fadeOut ? interpolate(f, [duration - fadeOut, duration], [1, 0], clamp) : 1;
|
|
762
|
+
return base * a * b;
|
|
763
|
+
}
|
|
764
|
+
: base
|
|
765
|
+
}
|
|
766
|
+
/>
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const PREMOUNT_SECONDS = 1;
|
|
771
|
+
|
|
772
|
+
function TrackView({
|
|
773
|
+
track,
|
|
774
|
+
scenes,
|
|
775
|
+
ctx,
|
|
776
|
+
masterGain,
|
|
777
|
+
}: {
|
|
778
|
+
track: StudioTrack;
|
|
779
|
+
scenes: SceneRegistry;
|
|
780
|
+
ctx: StudioRuntimeContextValue;
|
|
781
|
+
masterGain: number;
|
|
782
|
+
}) {
|
|
783
|
+
const visual = track.kind !== "audio";
|
|
784
|
+
const { isRendering } = useRemotionEnvironment();
|
|
785
|
+
if (visual && track.hidden) return null;
|
|
786
|
+
const gain = (track.volume ?? 1) * masterGain;
|
|
787
|
+
const trackMuted = Boolean(track.muted);
|
|
788
|
+
return (
|
|
789
|
+
<>
|
|
790
|
+
{(track.clips as StudioClip[]).map((raw) => {
|
|
791
|
+
const clip = resolveClip(raw, ctx.variant);
|
|
792
|
+
if (clip.duration <= 0) return null;
|
|
793
|
+
let body: ReactNode = null;
|
|
794
|
+
switch (track.kind) {
|
|
795
|
+
case "scene":
|
|
796
|
+
body = <SceneClipView clip={clip as SceneClip} scenes={scenes} ctx={ctx} />;
|
|
797
|
+
break;
|
|
798
|
+
case "image":
|
|
799
|
+
body = <ImageClipView clip={clip as ImageClip} />;
|
|
800
|
+
break;
|
|
801
|
+
case "video":
|
|
802
|
+
body = <VideoClipView clip={clip as VideoClip} trackMuted={trackMuted} gain={gain} ctx={ctx} />;
|
|
803
|
+
break;
|
|
804
|
+
case "text":
|
|
805
|
+
body = <TextClipView clip={clip as TextClip} ctx={ctx} />;
|
|
806
|
+
break;
|
|
807
|
+
case "captions":
|
|
808
|
+
body = <CaptionsClipView clip={clip as CaptionsClip} ctx={ctx} />;
|
|
809
|
+
break;
|
|
810
|
+
case "audio":
|
|
811
|
+
body = <AudioClipView clip={clip as AudioClip} trackMuted={trackMuted} gain={gain} />;
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
const premountFor = clip.from > 0 ? Math.round(ctx.fps * PREMOUNT_SECONDS) : undefined;
|
|
815
|
+
const name = `${track.name} · ${clip.name ?? clip.id}`;
|
|
816
|
+
if (!visual && !isRendering) {
|
|
817
|
+
// Audio in the preview. Remotion only premounts a <Sequence> that has a layout box
|
|
818
|
+
// (layout="none" silently skips premountFor), and an <Audio> that mounts on its start frame
|
|
819
|
+
// pauses the player while it fetches and decodes the file, which is the hitch you feel on
|
|
820
|
+
// every voice line, SFX and music cut. So give it an invisible box purely to enable premount.
|
|
821
|
+
return (
|
|
822
|
+
<Sequence
|
|
823
|
+
key={clip.id}
|
|
824
|
+
from={clip.from}
|
|
825
|
+
durationInFrames={clip.duration}
|
|
826
|
+
premountFor={premountFor}
|
|
827
|
+
name={name}
|
|
828
|
+
style={{ display: "none" }}
|
|
829
|
+
>
|
|
830
|
+
{body}
|
|
831
|
+
</Sequence>
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
return (
|
|
835
|
+
<Sequence
|
|
836
|
+
key={clip.id}
|
|
837
|
+
from={clip.from}
|
|
838
|
+
durationInFrames={clip.duration}
|
|
839
|
+
// Mount each clip a moment early in the preview (hidden), so heavy scene setup
|
|
840
|
+
// (WebGL, big DOM) and media buffering happen before the cut instead of on it.
|
|
841
|
+
// No effect on captures or the export.
|
|
842
|
+
premountFor={premountFor}
|
|
843
|
+
name={name}
|
|
844
|
+
layout={visual ? "absolute-fill" : "none"}
|
|
845
|
+
>
|
|
846
|
+
{body}
|
|
847
|
+
</Sequence>
|
|
848
|
+
);
|
|
849
|
+
})}
|
|
850
|
+
</>
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// Compositions render inside the Agentcut page (Player, captures, browser export). Its global CSS
|
|
855
|
+
// (Tailwind preflight: border-box everywhere, line-height 1.5, image rules…) must not leak in, or a
|
|
856
|
+
// project would lay out differently than in a plain Remotion render. Revert author styles inside the
|
|
857
|
+
// composition to browser defaults; inline styles (everything scenes set) still win.
|
|
858
|
+
// SVG content is excluded: its presentation attributes (fill, stroke…) lose to any CSS rule.
|
|
859
|
+
// Then Remotion's own page defaults (remotion/dist default-css: * { box-sizing: border-box }).
|
|
860
|
+
const ISOLATION_CSS = `.studio-composition, .studio-composition :not(svg, svg *) { all: revert; }
|
|
861
|
+
.studio-composition, .studio-composition :not(svg, svg *), .studio-composition svg, .studio-composition svg * { box-sizing: border-box; }
|
|
862
|
+
.studio-composition svg { display: inline; vertical-align: baseline; }`;
|
|
863
|
+
|
|
864
|
+
function useStyleIsolation() {
|
|
865
|
+
if (typeof document === "undefined") return;
|
|
866
|
+
if (document.getElementById("studio-composition-isolation")) return;
|
|
867
|
+
const style = document.createElement("style");
|
|
868
|
+
style.id = "studio-composition-isolation";
|
|
869
|
+
style.textContent = ISOLATION_CSS;
|
|
870
|
+
document.head.appendChild(style);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
export function createStudioComposition(scenes: SceneRegistry) {
|
|
874
|
+
function StudioComposition(props: StudioCompositionProps) {
|
|
875
|
+
const { project, layers, variant, assetBase, libraryBase } = props;
|
|
876
|
+
const size = project.variants[variant] ?? project.variants[project.defaultVariant] ?? {
|
|
877
|
+
width: 1920,
|
|
878
|
+
height: 1080,
|
|
879
|
+
};
|
|
880
|
+
setRuntimeState({ fps: project.fps, assetBase, libraryBase });
|
|
881
|
+
useStyleIsolation();
|
|
882
|
+
useProjectFonts(project.fonts ?? []);
|
|
883
|
+
const ctx = useMemo<StudioRuntimeContextValue>(
|
|
884
|
+
() => ({
|
|
885
|
+
project,
|
|
886
|
+
variant,
|
|
887
|
+
width: size.width,
|
|
888
|
+
height: size.height,
|
|
889
|
+
fps: project.fps,
|
|
890
|
+
assetBase,
|
|
891
|
+
libraryBase,
|
|
892
|
+
}),
|
|
893
|
+
[project, variant, size.width, size.height, assetBase, libraryBase],
|
|
894
|
+
);
|
|
895
|
+
const masterGain = props.masterGain ?? 1;
|
|
896
|
+
const ordered = [...layers.tracks].reverse();
|
|
897
|
+
return (
|
|
898
|
+
<StudioRuntimeContext.Provider value={ctx}>
|
|
899
|
+
<AbsoluteFill
|
|
900
|
+
className="studio-composition"
|
|
901
|
+
style={{
|
|
902
|
+
background: project.background || "#000",
|
|
903
|
+
overflow: "hidden",
|
|
904
|
+
// Same starting point as a bare Remotion page: browser default font, size, color, line height.
|
|
905
|
+
font: "initial",
|
|
906
|
+
color: "black",
|
|
907
|
+
letterSpacing: "normal",
|
|
908
|
+
}}
|
|
909
|
+
>
|
|
910
|
+
{project.style && projectStyle(project.style) ? <StyleFonts /> : null}
|
|
911
|
+
{ordered.map((track, index) => (
|
|
912
|
+
<TrackView key={`${track.id}:${index}`} track={track} scenes={scenes} ctx={ctx} masterGain={masterGain} />
|
|
913
|
+
))}
|
|
914
|
+
{project.style && styleLayer.StyleOverlay ? <styleLayer.StyleOverlay styleId={project.style} /> : null}
|
|
915
|
+
</AbsoluteFill>
|
|
916
|
+
</StudioRuntimeContext.Provider>
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
return StudioComposition;
|
|
920
|
+
}
|