agentcut 0.2.26 → 0.2.28

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 (97) hide show
  1. package/README.md +177 -177
  2. package/dist/studio/agents/claude.js +3 -2
  3. package/dist/studio/agents/claude.js.map +1 -1
  4. package/dist/studio/chat.js +1 -1
  5. package/dist/studio/chat.js.map +1 -1
  6. package/dist/studio/cli.js +24 -24
  7. package/dist/studio/download.d.ts +24 -0
  8. package/dist/studio/download.js +234 -0
  9. package/dist/studio/download.js.map +1 -0
  10. package/dist/studio/host.js +8 -0
  11. package/dist/studio/host.js.map +1 -1
  12. package/dist/studio/housekeeping.d.ts +2 -2
  13. package/dist/studio/housekeeping.js +18 -17
  14. package/dist/studio/housekeeping.js.map +1 -1
  15. package/dist/studio/release-cursor.js +21 -21
  16. package/dist/studio/release-video.js +10 -10
  17. package/dist/studio/schema.d.ts +102 -0
  18. package/dist/studio/schema.js +5 -0
  19. package/dist/studio/schema.js.map +1 -1
  20. package/dist/studio/tools.js +7 -0
  21. package/dist/studio/tools.js.map +1 -1
  22. package/package.json +56 -56
  23. package/template/AGENTS.md +14 -14
  24. package/template/components/README.md +70 -70
  25. package/template/components/before-after/BeforeAfter.tsx +89 -89
  26. package/template/components/catalog.ts +107 -107
  27. package/template/components/cinematic/Device3D.tsx +324 -324
  28. package/template/components/cinematic/devices3d/display-model.ts +184 -184
  29. package/template/components/cinematic/devices3d/iphone-model.ts +643 -643
  30. package/template/components/cinematic/devices3d/macbook-model.ts +608 -608
  31. package/template/components/cinematic/devices3d/models.ts +657 -657
  32. package/template/components/cinematic/devices3d/runtime.ts +267 -267
  33. package/template/components/cinematic/math.ts +100 -100
  34. package/template/components/color/grade-effect.ts +650 -650
  35. package/template/components/color/grade.ts +512 -512
  36. package/template/components/color/looks.ts +154 -154
  37. package/template/components/countdown/Countdown.tsx +166 -166
  38. package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -181
  39. package/template/components/dot-trace-title/meta.json +51 -51
  40. package/template/components/generate/GenerateReveal.tsx +381 -381
  41. package/template/components/hook-card/HookCard.tsx +554 -554
  42. package/template/components/index.ts +199 -199
  43. package/template/components/lib.ts +191 -191
  44. package/template/components/line-diagram/LineDiagram.tsx +205 -205
  45. package/template/components/line-diagram/meta.json +215 -215
  46. package/template/components/list-card/ListCard.tsx +104 -104
  47. package/template/components/live-clip.ts +75 -75
  48. package/template/components/looks.tsx +1447 -1447
  49. package/template/components/loops/LoopCanvas.tsx +132 -132
  50. package/template/components/loops/engine.ts +184 -184
  51. package/template/components/loops/library.ts +667 -667
  52. package/template/components/loops/library2.ts +777 -777
  53. package/template/components/motion-loop/MotionLoop.tsx +37 -37
  54. package/template/components/motion-loop/meta.json +311 -311
  55. package/template/components/props.ts +73 -73
  56. package/template/components/quote-card/QuoteCard.tsx +71 -71
  57. package/template/components/style-layer.tsx +233 -233
  58. package/template/components/styles.ts +113 -113
  59. package/template/components/types.ts +66 -66
  60. package/template/components/word-pop/WordPop.tsx +59 -59
  61. package/template/components/word-stack/WordStack.tsx +122 -122
  62. package/template/presets/charcoal-sketch/PROMPT.md +48 -48
  63. package/template/presets/charcoal-sketch/scripts/duck_music.py +34 -34
  64. package/template/presets/punk-cut-paper/scenes/Motif.tsx +376 -376
  65. package/template/presets/punk-cut-paper/scenes/Photo.tsx +73 -73
  66. package/template/presets/punk-cut-paper/scenes/Ransom.tsx +119 -119
  67. package/template/runtime/composition.tsx +1207 -1189
  68. package/template/runtime/helpers.tsx +230 -230
  69. package/template/runtime/index.tsx +26 -22
  70. package/template/runtime/types.ts +324 -315
  71. package/template/skill/SKILL.md +188 -187
  72. package/template/skill/references/broll.md +44 -44
  73. package/template/skill/references/browser-export.md +61 -61
  74. package/template/skill/references/cinematic.md +206 -206
  75. package/template/skill/references/color.md +79 -79
  76. package/template/skill/references/craft.md +97 -97
  77. package/template/skill/references/editing-styles.md +57 -57
  78. package/template/skill/references/feature-launch.md +34 -34
  79. package/template/skill/references/footage.md +72 -72
  80. package/template/skill/references/format.md +145 -142
  81. package/template/skill/references/how-to.md +146 -146
  82. package/template/skill/references/launch-direction.md +89 -89
  83. package/template/skill/references/loops.md +151 -151
  84. package/template/skill/references/mcp-external.md +19 -19
  85. package/template/skill/references/performance.md +93 -93
  86. package/template/skill/references/person-cutout.md +19 -19
  87. package/template/skill/references/presets.md +60 -60
  88. package/template/skill/references/product-launch.md +134 -134
  89. package/template/skill/references/release-capture.md +53 -53
  90. package/template/skill/references/sound.md +167 -166
  91. package/template/skill/references/style-spy.md +160 -160
  92. package/template/skill/references/website-howto.md +45 -45
  93. package/template/starter/.studio/chat.json +1 -1
  94. package/template/starter/layers.json +46 -46
  95. package/template/starter/notes.md +4 -4
  96. package/template/starter/project.json +13 -13
  97. package/template/starter/scenes/Title.tsx +38 -38
@@ -1,1189 +1,1207 @@
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 { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ComponentType, type CSSProperties, type ReactNode } from "react";
5
- import {
6
- AbsoluteFill,
7
- Img,
8
- Internals,
9
- Sequence,
10
- continueRender,
11
- delayRender,
12
- interpolate,
13
- useCurrentFrame,
14
- useRemotionEnvironment,
15
- useVideoConfig,
16
- } from "remotion";
17
- import { Audio, Video } from "@remotion/media";
18
- import type { EffectDescriptor } from "remotion";
19
- import { framingCropAt } from "./framing";
20
- // The Agentcut component library, provided by the studio page (and by a copy in
21
- // .agentcut/components for local renders).
22
- import * as ComponentLibrary from "agentcut/components";
23
- import { MOTION_COMPONENTS, resolveComponentProps } from "agentcut/components";
24
-
25
- // Project styles (project.json "style") come from the library too. Pages from before styles don't
26
- // provide them: the project then renders as if it had none.
27
- type LibraryStyle = { captions: CaptionsClip["style"]; text: TextClip["style"] };
28
- function projectStyle(id: string | undefined): LibraryStyle | null {
29
- const get = (ComponentLibrary as { getMotionStyle?: (id?: string) => LibraryStyle | null }).getMotionStyle;
30
- return id && typeof get === "function" ? get(id) : null;
31
- }
32
-
33
- type StyleLayer = {
34
- styleCamera?: (
35
- id: string | undefined,
36
- at: { frame: number; durationInFrames: number; width: number; height: number },
37
- ) => CSSProperties | null;
38
- StyleOverlay?: ComponentType<{ styleId: string | undefined }>;
39
- StyledCaptions?: ComponentType<{
40
- styleId: string | undefined;
41
- words: CaptionsClip["words"];
42
- position?: string;
43
- wordsPerLine?: number;
44
- fontSize?: number;
45
- }>;
46
- };
47
- const styleLayer = ComponentLibrary as StyleLayer;
48
-
49
- /** Loads the library's fonts (the style's caption and text fonts) and holds the render until they're in. */
50
- function StyleFonts() {
51
- const use = (ComponentLibrary as { useMotionFonts?: () => void }).useMotionFonts;
52
- use?.();
53
- return null;
54
- }
55
- import {
56
- StudioRuntimeContext,
57
- asset,
58
- clamp,
59
- setRuntimeState,
60
- type StudioRuntimeContextValue,
61
- } from "./helpers";
62
- import type {
63
- AudioClip,
64
- AdjustmentClip,
65
- Box,
66
- CaptionsClip,
67
- ClipAnim,
68
- ColorGrade,
69
- ImageClip,
70
- SceneClip,
71
- SceneProps,
72
- StudioClip,
73
- StudioCompositionProps,
74
- StudioFont,
75
- StudioTrack,
76
- TextClip,
77
- CursorTrack,
78
- VideoClip,
79
- VideoCursor,
80
- } from "./types";
81
-
82
- export type SceneRegistry = Record<string, ComponentType<SceneProps & Record<string, unknown>>>;
83
-
84
- // ---------------------------------------------------------------------------
85
- // Fonts
86
-
87
- const loadedFontSpecs = new Map<string, Promise<void>>();
88
-
89
- function fontSpecToCss(spec: string) {
90
- // "Inter:400;700;900" → family=Inter:wght@400;700;900 ; "Inter" → family=Inter
91
- const [family, weights] = spec.split(":");
92
- const name = family.trim().replace(/\s+/g, "+");
93
- return weights ? `family=${name}:wght@${weights.trim()}` : `family=${name}`;
94
- }
95
-
96
- function loadLocalFont(font: Exclude<StudioFont, string>): Promise<void> {
97
- const key = `${font.family}|${font.src}|${font.weight ?? ""}|${font.style ?? ""}`;
98
- const existing = loadedFontSpecs.get(key);
99
- if (existing) return existing;
100
- const promise = (async () => {
101
- if (typeof document === "undefined") return;
102
- const face = new FontFace(font.family, `url(${JSON.stringify(asset(font.src))})`, {
103
- weight: font.weight ?? "400",
104
- style: font.style ?? "normal",
105
- });
106
- try {
107
- document.fonts.add(await face.load());
108
- } catch {
109
- // Missing font file: keep rendering with the fallback font.
110
- }
111
- })();
112
- loadedFontSpecs.set(key, promise);
113
- return promise;
114
- }
115
-
116
- function loadFont(font: StudioFont): Promise<void> {
117
- return typeof font === "string" ? loadFontSpec(font) : loadLocalFont(font);
118
- }
119
-
120
- function loadFontSpec(spec: string): Promise<void> {
121
- const existing = loadedFontSpecs.get(spec);
122
- if (existing) return existing;
123
- const promise = (async () => {
124
- if (typeof document === "undefined") return;
125
- const href = `https://fonts.googleapis.com/css2?${fontSpecToCss(spec)}&display=block`;
126
- if (!document.querySelector(`link[data-studio-font="${CSS.escape(spec)}"]`)) {
127
- const link = document.createElement("link");
128
- link.rel = "stylesheet";
129
- link.href = href;
130
- link.crossOrigin = "anonymous";
131
- link.dataset.studioFont = spec;
132
- await new Promise<void>((resolve) => {
133
- link.onload = () => resolve();
134
- link.onerror = () => resolve();
135
- document.head.appendChild(link);
136
- });
137
- }
138
- const [family, weights] = spec.split(":");
139
- const list = weights ? weights.split(";").map((w) => w.trim()) : ["400"];
140
- await Promise.all(
141
- list.map((weight) =>
142
- document.fonts.load(`${weight} 32px "${family.trim()}"`).catch(() => undefined),
143
- ),
144
- );
145
- })();
146
- loadedFontSpecs.set(spec, promise);
147
- return promise;
148
- }
149
-
150
- function useProjectFonts(fonts: StudioFont[]) {
151
- const key = fonts.map((font) => (typeof font === "string" ? font : `${font.family}|${font.src}|${font.weight ?? ""}`)).join("||");
152
- const [handle] = useState(() =>
153
- fonts.length > 0 ? delayRender(`Loading fonts ${key}`, { timeoutInMilliseconds: 20_000 }) : null,
154
- );
155
- useEffect(() => {
156
- if (handle === null) return;
157
- let cancelled = false;
158
- Promise.all(fonts.map(loadFont)).finally(() => {
159
- if (!cancelled) continueRender(handle);
160
- });
161
- return () => {
162
- cancelled = true;
163
- };
164
- // eslint-disable-next-line react-hooks/exhaustive-deps
165
- }, [handle, key]);
166
- }
167
-
168
- // ---------------------------------------------------------------------------
169
- // Clip helpers
170
-
171
- export function resolveClip<T extends StudioClip>(clip: T, variant: string): T {
172
- const override = (clip as { variants?: Record<string, Partial<T>> }).variants?.[variant];
173
- return override ? ({ ...clip, ...override } as T) : clip;
174
- }
175
-
176
- function animState(anim: ClipAnim | undefined, t: number) {
177
- // t: 0 = fully hidden, 1 = fully shown
178
- const preset = anim?.preset ?? "none";
179
- const out: { opacity: number; transform: string; filter?: string } = { opacity: 1, transform: "" };
180
- if (preset === "none" || t >= 1) return out;
181
- const inv = 1 - t;
182
- switch (preset) {
183
- case "fade":
184
- out.opacity = t;
185
- break;
186
- case "slide-up":
187
- out.opacity = t;
188
- out.transform = `translateY(${inv * 8}%)`;
189
- break;
190
- case "slide-down":
191
- out.opacity = t;
192
- out.transform = `translateY(${-inv * 8}%)`;
193
- break;
194
- case "slide-left":
195
- out.opacity = t;
196
- out.transform = `translateX(${inv * 8}%)`;
197
- break;
198
- case "slide-right":
199
- out.opacity = t;
200
- out.transform = `translateX(${-inv * 8}%)`;
201
- break;
202
- case "pop": {
203
- // overshoot: 0 → 1.08 → 1
204
- const s = t < 0.7 ? interpolate(t, [0, 0.7], [0.6, 1.08]) : interpolate(t, [0.7, 1], [1.08, 1]);
205
- out.opacity = Math.min(1, t * 2);
206
- out.transform = `scale(${s})`;
207
- break;
208
- }
209
- case "scale":
210
- out.opacity = t;
211
- out.transform = `scale(${0.85 + 0.15 * t})`;
212
- break;
213
- case "blur":
214
- out.opacity = t;
215
- out.filter = `blur(${inv * 16}px)`;
216
- break;
217
- }
218
- return out;
219
- }
220
-
221
- const easeOutCubic = (t: number) => 1 - Math.pow(1 - t, 3);
222
-
223
- /** Enter/exit + opacity wrapper for one visual clip; frame is local to the clip. */
224
- function useClipStyle(clip: StudioClip): CSSProperties {
225
- const frame = useCurrentFrame();
226
- const enterFrames = clip.enter && clip.enter.preset !== "none" ? Math.max(1, clip.enter.frames) : 0;
227
- const exitFrames = clip.exit && clip.exit.preset !== "none" ? Math.max(1, clip.exit.frames) : 0;
228
- const tIn = enterFrames ? easeOutCubic(interpolate(frame, [0, enterFrames], [0, 1], clamp)) : 1;
229
- const tOut = exitFrames
230
- ? easeOutCubic(interpolate(frame, [clip.duration - exitFrames, clip.duration], [1, 0], clamp))
231
- : 1;
232
- const a = animState(clip.enter, tIn);
233
- const b = animState(clip.exit, tOut);
234
- const transform = [a.transform, b.transform].filter(Boolean).join(" ");
235
- const filter = [a.filter, b.filter].filter(Boolean).join(" ");
236
- return {
237
- opacity: a.opacity * b.opacity * (clip.opacity ?? 1),
238
- transform: transform || undefined,
239
- filter: filter || undefined,
240
- };
241
- }
242
-
243
- function boxStyle(box: Box | undefined): CSSProperties {
244
- if (!box) return { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" };
245
- return {
246
- position: "absolute",
247
- left: `${box.x * 100}%`,
248
- top: `${box.y * 100}%`,
249
- width: `${box.width * 100}%`,
250
- height: `${box.height * 100}%`,
251
- };
252
- }
253
-
254
- // ---------------------------------------------------------------------------
255
- // Clip renderers (all frames local to the clip's <Sequence>)
256
-
257
- function SceneClipView({
258
- clip: saved,
259
- scenes,
260
- ctx,
261
- }: {
262
- clip: SceneClip;
263
- scenes: SceneRegistry;
264
- ctx: StudioRuntimeContextValue;
265
- }) {
266
- const clip = useLive(saved);
267
- const style = useClipStyle(clip);
268
- if (clip.component) {
269
- const entry = MOTION_COMPONENTS[clip.component];
270
- if (!entry) {
271
- return (
272
- <AbsoluteFill
273
- style={{
274
- alignItems: "center",
275
- justifyContent: "center",
276
- background: "rgba(220,38,38,0.25)",
277
- color: "#fff",
278
- fontFamily: "sans-serif",
279
- fontSize: Math.round(ctx.height * 0.03),
280
- }}
281
- >
282
- Unknown component “{clip.component}”
283
- </AbsoluteFill>
284
- );
285
- }
286
- const Component = entry.Component;
287
- return (
288
- <AbsoluteFill style={style}>
289
- <Component
290
- {...resolveComponentProps(clip.component, clip.props, ctx.project.style)}
291
- width={ctx.width}
292
- height={ctx.height}
293
- fps={ctx.fps}
294
- durationInFrames={clip.duration}
295
- variant={ctx.variant}
296
- />
297
- </AbsoluteFill>
298
- );
299
- }
300
- const Scene = clip.scene ? scenes[clip.scene] : undefined;
301
- if (!Scene) {
302
- return (
303
- <AbsoluteFill
304
- style={{
305
- alignItems: "center",
306
- justifyContent: "center",
307
- background: "rgba(220,38,38,0.25)",
308
- color: "#fff",
309
- fontFamily: "sans-serif",
310
- fontSize: Math.round(ctx.height * 0.03),
311
- }}
312
- >
313
- Missing scene “{clip.scene}” (scenes/{clip.scene}.tsx)
314
- </AbsoluteFill>
315
- );
316
- }
317
- return (
318
- <AbsoluteFill style={style}>
319
- <Scene
320
- {...(clip.props ?? {})}
321
- clip={{ id: clip.id, from: clip.from, duration: clip.duration }}
322
- variant={ctx.variant}
323
- width={ctx.width}
324
- height={ctx.height}
325
- fps={ctx.fps}
326
- />
327
- </AbsoluteFill>
328
- );
329
- }
330
-
331
- function ImageClipView({ clip: saved, ctx }: { clip: ImageClip; ctx: TrackContext }) {
332
- const clip = useLive(saved);
333
- const style = useClipStyle(clip);
334
- const { filter, effects } = useClipGrade(clip, ctx, clip.inheritGrade === true);
335
- return (
336
- <div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
337
- {effects.length ? (
338
- // Effects draw images on a canvas, which needs its size in pixels.
339
- <Img
340
- src={asset(clip.src)}
341
- width={Math.max(1, Math.round((clip.box?.width ?? 1) * ctx.width))}
342
- height={Math.max(1, Math.round((clip.box?.height ?? 1) * ctx.height))}
343
- effects={effects}
344
- style={{ width: "100%", height: "100%", display: "block", objectFit: clip.fit ?? "cover" }}
345
- />
346
- ) : (
347
- <Img
348
- src={asset(clip.src)}
349
- style={{ width: "100%", height: "100%", objectFit: clip.fit ?? "cover", display: "block", filter }}
350
- />
351
- )}
352
- </div>
353
- );
354
- }
355
-
356
- /** The runtime context plus what a clip needs to know about the stack it sits in. */
357
- type TrackContext = StudioRuntimeContextValue & {
358
- adjustments: { clip: AdjustmentClip; order: number }[];
359
- /** Position in the stack, bottom to top. */
360
- order: number;
361
- };
362
-
363
- // The grade engine comes from the library too. Pages from before it don't provide it: those keep the
364
- // CSS filter below, which covers the original controls.
365
- const LIVE_PROJECT = "project";
366
- type GradeEntry = { grade: ColorGrade; frame: number; strength?: number };
367
- type GradeEngine = {
368
- gradeEffects?: (grades: GradeEntry[], options: { seed: number; luts?: ReadonlyMap<string, unknown> }) => EffectDescriptor<unknown>[];
369
- gradeLutPaths?: (grade: ColorGrade | undefined) => string[];
370
- isCssGrade?: (grade: ColorGrade | undefined) => boolean;
371
- canDrawGrade?: () => boolean;
372
- parseCube?: (text: string) => unknown;
373
- };
374
- const gradeEngine = ComponentLibrary as GradeEngine;
375
-
376
- // A moving slider's value, straight from the inspector. Older pages don't provide it.
377
- type LiveEdits = {
378
- useLiveClip?: <T extends { id: string }>(clip: T, isRendering?: boolean) => T;
379
- useLiveClips?: (ids: string[], isRendering?: boolean) => void;
380
- getLiveClip?: (id: string) => Record<string, unknown> | undefined;
381
- };
382
- const liveEdits = ComponentLibrary as LiveEdits;
383
- const useLiveClipEngine = liveEdits.useLiveClip;
384
- const useLiveClipsEngine = liveEdits.useLiveClips;
385
-
386
- function useLive<T extends { id: string }>(clip: T): T {
387
- const { isRendering } = useRemotionEnvironment();
388
- // The library is fixed for the page's lifetime, so this always calls the same hooks.
389
- return useLiveClipEngine ? useLiveClipEngine(clip, isRendering) : clip;
390
- }
391
-
392
- const loadedLuts = new Map<string, unknown>();
393
- const pendingLuts = new Map<string, Promise<void>>();
394
-
395
- /** Load a .cube file once. A missing or broken file warns and the grade draws without it. */
396
- function loadLut(path: string) {
397
- let pending = pendingLuts.get(path);
398
- if (!pending) {
399
- pending = fetch(asset(path))
400
- .then((response) => {
401
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
402
- return response.text();
403
- })
404
- .then((text) => {
405
- loadedLuts.set(path, gradeEngine.parseCube?.(text) ?? null);
406
- })
407
- .catch((error) => {
408
- loadedLuts.set(path, null);
409
- console.warn(`Could not load the LUT ${path}: ${error instanceof Error ? error.message : error}`);
410
- });
411
- pendingLuts.set(path, pending);
412
- }
413
- return pending;
414
- }
415
-
416
- /** Holds the render until these .cube files have loaded; later additions (preview edits) just redraw. */
417
- function useGradeLuts(paths: string[]) {
418
- const key = paths.join("\n");
419
- const [, setLoaded] = useState(0);
420
- const [handle] = useState(() =>
421
- paths.some((path) => !loadedLuts.has(path)) ? delayRender(`Loading LUT ${paths.join(", ")}`, { timeoutInMilliseconds: 20_000 }) : null,
422
- );
423
- useEffect(() => {
424
- let cancelled = false;
425
- const wanted = key ? key.split("\n") : [];
426
- Promise.all(wanted.map(loadLut))
427
- .then(() => {
428
- if (!cancelled) setLoaded((n) => n + 1);
429
- })
430
- .finally(() => {
431
- if (handle !== null) continueRender(handle);
432
- });
433
- return () => {
434
- cancelled = true;
435
- };
436
- }, [key, handle]);
437
- return loadedLuts;
438
- }
439
-
440
- /** How far an adjustment clip is faded in at `local`: its enter and exit lengths ramp the grade. */
441
- function adjustmentStrength(clip: AdjustmentClip, local: number) {
442
- let strength = clip.opacity ?? 1;
443
- if (clip.enter?.frames) strength *= Math.min(1, (local + 1) / clip.enter.frames);
444
- if (clip.exit?.frames) strength *= Math.min(1, (clip.duration - local) / clip.exit.frames);
445
- return strength;
446
- }
447
-
448
- /**
449
- * What draws a clip's colour: its own grade, then the adjustment layers above it and the project
450
- * grade. A lone clip grade using only the original controls stays a CSS filter, so existing projects
451
- * render exactly as they did.
452
- */
453
- function useClipGrade(clip: VideoClip | ImageClip, ctx: TrackContext, inherits: boolean) {
454
- const local = useCurrentFrame();
455
- const frame = local + clip.from;
456
- const { isRendering } = useRemotionEnvironment();
457
- const liveIds = inherits ? [...ctx.adjustments.map((layer) => layer.clip.id), LIVE_PROJECT] : [];
458
- useLiveClipsEngine?.(liveIds, isRendering);
459
- const liveGrade = (id: string, saved: ColorGrade | undefined) => {
460
- const fields = isRendering ? undefined : liveEdits.getLiveClip?.(id);
461
- return fields && "grade" in fields ? (fields.grade as ColorGrade | undefined) : saved;
462
- };
463
- const grades: GradeEntry[] = [];
464
- if (clip.grade) grades.push({ grade: clip.grade, frame: local });
465
- const inherited: GradeEntry[] = [];
466
- if (inherits) {
467
- for (const layer of ctx.adjustments) {
468
- if (layer.order <= ctx.order) continue;
469
- const at = frame - layer.clip.from;
470
- if (at < 0 || at >= layer.clip.duration) continue;
471
- const grade = liveGrade(layer.clip.id, layer.clip.grade);
472
- if (grade) inherited.push({ grade, frame: at, strength: adjustmentStrength(layer.clip, at) });
473
- }
474
- const projectGrade = liveGrade(LIVE_PROJECT, ctx.project.grade);
475
- if (projectGrade) inherited.push({ grade: projectGrade, frame });
476
- }
477
- const paths = [...new Set([...grades, ...inherited].flatMap((entry) => gradeEngine.gradeLutPaths?.(entry.grade) ?? []))];
478
- const luts = useGradeLuts(paths);
479
- const useCss =
480
- !gradeEngine.gradeEffects ||
481
- !gradeEngine.canDrawGrade?.() ||
482
- (inherited.length === 0 && (grades.length === 0 || gradeEngine.isCssGrade?.(grades[0].grade)));
483
- if (useCss) return { filter: gradeFilter(clip.grade), effects: [] as EffectDescriptor<unknown>[] };
484
- return {
485
- filter: undefined,
486
- effects: gradeEngine.gradeEffects!([...grades, ...inherited], { seed: frame, luts: luts as ReadonlyMap<string, unknown> }),
487
- };
488
- }
489
-
490
- /** CSS filter for a color grade (supported by the browser export). */
491
- function gradeFilter(grade: VideoClip["grade"]): string | undefined {
492
- if (!grade) return undefined;
493
- const parts: string[] = [];
494
- if (grade.brightness !== undefined && grade.brightness !== 1) parts.push(`brightness(${grade.brightness})`);
495
- if (grade.contrast !== undefined && grade.contrast !== 1) parts.push(`contrast(${grade.contrast})`);
496
- if (grade.saturation !== undefined && grade.saturation !== 1) parts.push(`saturate(${grade.saturation})`);
497
- const warmth = grade.warmth ?? 0;
498
- if (warmth > 0) parts.push(`sepia(${(warmth * 0.35).toFixed(3)})`, `saturate(${(1 + warmth * 0.25).toFixed(3)})`);
499
- const hue = (grade.hue ?? 0) + (warmth < 0 ? warmth * -12 : warmth * -4);
500
- if (hue) parts.push(`hue-rotate(${hue.toFixed(1)}deg)`);
501
- return parts.length ? parts.join(" ") : undefined;
502
- }
503
-
504
- // Frames this preview drew while paused, small and already graded. Coming back to one during a drag
505
- // (the usual back-and-forth when looking for a cut) puts it on screen at once, over the player, until
506
- // the decoder has redrawn it. Preview only; a wrong guess lasts only until that redraw.
507
- const HELD_FRAME_WIDTH = 960;
508
- /** About 200 MB at 16:9, across every clip. */
509
- const MAX_HELD_FRAMES = 96;
510
- const heldFrames = new Map<string, ImageBitmap>();
511
-
512
- function holdFrame(key: string, frame: ImageBitmap) {
513
- heldFrames.get(key)?.close();
514
- heldFrames.delete(key);
515
- heldFrames.set(key, frame);
516
- if (heldFrames.size > MAX_HELD_FRAMES) {
517
- const oldest = heldFrames.keys().next().value!;
518
- heldFrames.get(oldest)?.close();
519
- heldFrames.delete(oldest);
520
- }
521
- }
522
-
523
- function useHeldFrames(version: string, frame: number, enabled: boolean) {
524
- const boxRef = useRef<HTMLDivElement>(null);
525
- const overlayRef = useRef<HTMLCanvasElement>(null);
526
- const latest = useRef({ version, frame, enabled });
527
- /** The frame the decoder's picture shows: nothing needs to cover it. */
528
- const drawn = useRef<number | null>(null);
529
- useLayoutEffect(() => {
530
- latest.current = { version, frame, enabled };
531
- const overlay = overlayRef.current;
532
- if (!overlay) return;
533
- if (!enabled || drawn.current === frame) {
534
- overlay.style.visibility = "hidden";
535
- return;
536
- }
537
- const picture = heldFrames.get(`${version}:${frame}`);
538
- if (!picture) return;
539
- if (overlay.width !== picture.width) overlay.width = picture.width;
540
- if (overlay.height !== picture.height) overlay.height = picture.height;
541
- overlay.getContext("2d")?.drawImage(picture, 0, 0);
542
- overlay.style.visibility = "visible";
543
- }, [version, frame, enabled]);
544
- const onDrawn = useCallback(() => {
545
- const now = latest.current;
546
- drawn.current = now.frame;
547
- const overlay = overlayRef.current;
548
- if (overlay) overlay.style.visibility = "hidden";
549
- if (!now.enabled) return;
550
- // The player's own canvas, which has the grade on it (the callback's frame doesn't yet).
551
- const canvas = boxRef.current?.querySelector<HTMLCanvasElement>("canvas");
552
- if (!canvas || canvas === overlay || !canvas.width || !canvas.height) return;
553
- const width = Math.min(HELD_FRAME_WIDTH, canvas.width);
554
- const key = `${now.version}:${now.frame}`;
555
- void createImageBitmap(canvas, {
556
- resizeWidth: width,
557
- resizeHeight: Math.max(1, Math.round((canvas.height * width) / canvas.width)),
558
- resizeQuality: "low",
559
- }).then((picture) => holdFrame(key, picture), () => undefined);
560
- }, []);
561
- return { boxRef, overlayRef, onDrawn };
562
- }
563
-
564
- function VideoClipView({
565
- clip: saved,
566
- trackMuted,
567
- gain,
568
- ctx,
569
- }: {
570
- clip: VideoClip;
571
- trackMuted: boolean;
572
- gain: number;
573
- ctx: TrackContext;
574
- }) {
575
- const clip = useLive(saved);
576
- const style = useClipStyle(clip);
577
- const frame = useCurrentFrame();
578
- const grade = useClipGrade(clip, ctx, clip.inheritGrade !== false);
579
- const { isRendering } = useRemotionEnvironment();
580
- const playing = Internals.Timeline.usePlaying();
581
- const source = (!isRendering && ctx.previewProxies?.[clip.src]) || asset(clip.src);
582
- const held = useHeldFrames(
583
- `${clip.id}|${source}|${clip.trimStart ?? 0}|${clip.playbackRate ?? 1}|${clip.fit ?? ""}|${grade.filter ?? ""}|${JSON.stringify(clip.grade ?? null)}|${JSON.stringify(ctx.project.grade ?? null)}|${ctx.adjustments.length}`,
584
- frame,
585
- !isRendering && !playing,
586
- );
587
- // The project style's camera (punch-ins, drift, …) moves the footage inside its box.
588
- const crop = clip.framing ? framingCropAt(clip.framing, (clip.trimStart ?? 0) / ctx.project.fps + frame / ctx.project.fps * (clip.playbackRate ?? 1)) : null;
589
- const camera =
590
- ctx.project.style && clip.camera !== "none" && styleLayer.styleCamera
591
- ? styleLayer.styleCamera(ctx.project.style, { frame, durationInFrames: clip.duration, width: ctx.width, height: ctx.height })
592
- : null;
593
- return (
594
- <div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
595
- <div style={{ position: "absolute", inset: 0, ...(clip.framing ? { filter: camera?.filter } : camera) }}>
596
- <div ref={held.boxRef} style={crop ? {
597
- position: "absolute", left: `${-crop.x / crop.width * 100}%`,
598
- top: `${-crop.y / crop.height * 100}%`,
599
- width: `${100 / crop.width}%`, height: `${100 / crop.height}%`,
600
- } : { position: "absolute", inset: 0 }}>
601
- <Video
602
- src={source}
603
- trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
604
- volume={(clip.volume ?? 1) * gain}
605
- muted={trackMuted || clip.muted}
606
- playbackRate={clip.playbackRate}
607
- // @remotion/media draws the frame itself: fit has to be its prop (CSS object-fit is ignored).
608
- objectFit={clip.framing ? "fill" : clip.fit ?? "cover"}
609
- style={{ width: "100%", height: "100%", filter: grade.filter }}
610
- {...(grade.effects.length ? { effects: grade.effects } : {})}
611
- {...(isRendering ? {} : { onVideoFrame: held.onDrawn })}
612
- />
613
- {isRendering ? null : (
614
- <canvas
615
- ref={held.overlayRef}
616
- style={{ position: "absolute", inset: 0, width: "100%", height: "100%", filter: grade.filter, visibility: "hidden" }}
617
- />
618
- )}
619
- {clip.cursor ? <CursorOverlay cursor={clip.cursor} clip={clip} /> : null}
620
- </div>
621
- </div>
622
- </div>
623
- );
624
- }
625
-
626
- // ---------------------------------------------------------------------------
627
- // Recorded cursor (`cursor` on a video clip): the pointer and click ripples from a
628
- // `<video>.cursor.json`, drawn inside the clip so it follows its trim, speed and box.
629
-
630
- const loadedCursorTracks = new Map<string, CursorTrack | null>();
631
- const pendingCursorTracks = new Map<string, Promise<CursorTrack | null>>();
632
-
633
- function loadCursorTrack(url: string) {
634
- let pending = pendingCursorTracks.get(url);
635
- if (!pending) {
636
- pending = fetch(url)
637
- .then((response) => (response.ok ? (response.json() as Promise<CursorTrack>) : null))
638
- .catch(() => null)
639
- .then((track) => {
640
- loadedCursorTracks.set(url, track);
641
- return track;
642
- });
643
- pendingCursorTracks.set(url, pending);
644
- }
645
- return pending;
646
- }
647
-
648
- function useCursorTrack(src: string) {
649
- const url = asset(src);
650
- const [track, setTrack] = useState(() => loadedCursorTracks.get(url) ?? null);
651
- const [handle] = useState(() =>
652
- loadedCursorTracks.has(url) ? null : delayRender(`Loading cursor ${src}`, { timeoutInMilliseconds: 20_000 }),
653
- );
654
- useEffect(() => {
655
- let cancelled = false;
656
- loadCursorTrack(url)
657
- .then((loaded) => {
658
- if (!cancelled) setTrack(loaded);
659
- })
660
- .finally(() => {
661
- if (handle !== null) continueRender(handle);
662
- });
663
- return () => {
664
- cancelled = true;
665
- };
666
- }, [url, handle]);
667
- return track;
668
- }
669
-
670
- function cursorPointAt(path: CursorTrack["path"], frame: number) {
671
- let low = 0;
672
- let high = path.length - 1;
673
- let index = -1;
674
- while (low <= high) {
675
- const mid = (low + high) >> 1;
676
- if (path[mid][0] <= frame) {
677
- index = mid;
678
- low = mid + 1;
679
- } else {
680
- high = mid - 1;
681
- }
682
- }
683
- if (index < 0) return null;
684
- const [from, x, y] = path[index];
685
- if (x === null || y === null) return null;
686
- const next = path[index + 1];
687
- if (!next || next[1] === null || next[2] === null || next[0] <= from) return { x, y };
688
- const t = Math.min(1, (frame - from) / (next[0] - from));
689
- return { x: x + (next[1] - x) * t, y: y + (next[2] - y) * t };
690
- }
691
-
692
- const CURSOR_POINTER_PATH =
693
- "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";
694
- const CURSOR_GREEN = "#2dd47a";
695
- const HAND_EMOJI = "\u{1F446}";
696
-
697
- function CursorOverlay({ cursor, clip }: { cursor: VideoCursor; clip: VideoClip }) {
698
- const frame = useCurrentFrame();
699
- const { width: compWidth, height: compHeight } = useVideoConfig();
700
- const track = useCursorTrack(cursor.src);
701
- // Edits ("source" space) carry clicks for timing only: their positions don't match the picture.
702
- if (!track || track.space !== "video") return null;
703
-
704
- const style = cursor.style ?? "classic";
705
- const fps = track.fps;
706
- const sourceFrame = (clip.trimStart ?? 0) + frame * (clip.playbackRate ?? 1);
707
-
708
- // Where the video's picture sits inside the clip box (contain letterboxes, cover crops).
709
- const box = clip.box ?? { x: 0, y: 0, width: 1, height: 1 };
710
- const boxWidth = box.width * compWidth;
711
- const boxHeight = box.height * compHeight;
712
- const videoWidth = track.width ?? boxWidth;
713
- const videoHeight = track.height ?? boxHeight;
714
- const scale =
715
- (clip.fit ?? "cover") === "contain"
716
- ? Math.min(boxWidth / videoWidth, boxHeight / videoHeight)
717
- : Math.max(boxWidth / videoWidth, boxHeight / videoHeight);
718
- const offsetX = (boxWidth - videoWidth * scale) / 2;
719
- const offsetY = (boxHeight - videoHeight * scale) / 2;
720
- const toBox = (x: number, y: number) => ({
721
- left: offsetX + x * videoWidth * scale,
722
- top: offsetY + y * videoHeight * scale,
723
- });
724
-
725
- // Sized to the frame, not the box, so a punch-in doesn't blow the pointer up.
726
- const size =
727
- Math.max(18, Math.min(44, Math.min(compWidth, compHeight) * 0.032)) * ((cursor.size ?? 100) / 100);
728
-
729
- // Smoothing averages the path over a short window around this frame.
730
- const reach = Math.round((cursor.smoothing ?? 0.3) * 0.2 * fps);
731
- let sumX = 0;
732
- let sumY = 0;
733
- let count = 0;
734
- for (let offset = -reach; offset <= reach; offset += 1) {
735
- const sample = cursorPointAt(track.path, sourceFrame + offset);
736
- if (!sample) continue;
737
- sumX += sample.x;
738
- sumY += sample.y;
739
- count += 1;
740
- }
741
- const point = cursorPointAt(track.path, sourceFrame) && count ? toBox(sumX / count, sumY / count) : null;
742
-
743
- const rippleFrames = 0.6 * fps;
744
- const ripples =
745
- cursor.clicks === false
746
- ? []
747
- : track.clicks.filter((click) => sourceFrame >= click.frame && sourceFrame - click.frame < rippleFrames);
748
- let lastPressFrame: number | null = null;
749
- for (const click of track.clicks) {
750
- if (click.frame > sourceFrame) break;
751
- lastPressFrame = click.frame;
752
- }
753
- const press = lastPressFrame === null ? 0 : Math.max(0, 1 - (sourceFrame - lastPressFrame) / (0.3 * fps));
754
- const rippleColor = style === "accent" || style === "spotlight" ? CURSOR_GREEN : "#ffffff";
755
-
756
- return (
757
- <div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
758
- {ripples.map((click) => {
759
- const progress = (sourceFrame - click.frame) / rippleFrames;
760
- const diameter = size * (0.6 + 2.2 * progress);
761
- const at = toBox(click.x, click.y);
762
- return (
763
- <div
764
- key={click.frame}
765
- style={{
766
- position: "absolute",
767
- left: at.left - diameter / 2,
768
- top: at.top - diameter / 2,
769
- width: diameter,
770
- height: diameter,
771
- borderRadius: "50%",
772
- border: `${Math.max(2, size * 0.09)}px solid ${rippleColor}`,
773
- boxShadow: "0 0 6px rgba(0,0,0,0.45)",
774
- boxSizing: "border-box",
775
- opacity: 0.9 * (1 - progress),
776
- }}
777
- />
778
- );
779
- })}
780
- {point && style === "spotlight" ? (
781
- <div
782
- style={{
783
- position: "absolute",
784
- left: point.left - size * 0.42,
785
- top: point.top - size * 0.42,
786
- width: size * 0.84,
787
- height: size * 0.84,
788
- borderRadius: "50%",
789
- border: `${Math.max(2, size * 0.08)}px solid ${CURSOR_GREEN}`,
790
- background: "rgba(45, 212, 122, 0.16)",
791
- boxShadow: "0 1px 4px rgba(0,0,0,0.45)",
792
- boxSizing: "border-box",
793
- transform: `scale(${1 - press * 0.15})`,
794
- }}
795
- />
796
- ) : null}
797
- {point && style === "hand" ? (
798
- <div
799
- style={{
800
- position: "absolute",
801
- left: point.left - size * 0.42,
802
- top: point.top - size * 0.16,
803
- fontSize: size,
804
- lineHeight: 1,
805
- textShadow: "0 1px 3px rgba(0,0,0,0.55)",
806
- transform: `rotate(${press * 9}deg) scale(${1 - press * 0.05})`,
807
- transformOrigin: "42% 16%",
808
- }}
809
- >
810
- {HAND_EMOJI}
811
- </div>
812
- ) : null}
813
- {point && (style === "classic" || style === "accent") ? (
814
- <svg
815
- width={size}
816
- height={size * 1.1}
817
- viewBox="0 0 24 26.4"
818
- style={{
819
- position: "absolute",
820
- left: point.left - (3.55 / 24) * size,
821
- top: point.top - (1.75 / 24) * size,
822
- overflow: "visible",
823
- transform: `scale(${1 - press * 0.08})`,
824
- transformOrigin: `${(3.55 / 24) * 100}% ${(1.75 / 26.4) * 100}%`,
825
- }}
826
- >
827
- <defs>
828
- <filter id="studio-cursor-shadow" x="-50%" y="-50%" width="200%" height="200%">
829
- <feDropShadow dx="0" dy="1.5" stdDeviation="1.1" floodColor="#000000" floodOpacity="0.65" />
830
- </filter>
831
- </defs>
832
- <path
833
- d={CURSOR_POINTER_PATH}
834
- fill={style === "accent" ? CURSOR_GREEN : "#ffffff"}
835
- stroke="#000000"
836
- strokeWidth={2.4}
837
- strokeLinejoin="round"
838
- filter="url(#studio-cursor-shadow)"
839
- />
840
- </svg>
841
- ) : null}
842
- </div>
843
- );
844
- }
845
-
846
- function TextClipView({ clip: saved, ctx }: { clip: TextClip; ctx: StudioRuntimeContextValue }) {
847
- const clip = useLive(saved);
848
- const style = useClipStyle(clip);
849
- const s = { ...projectStyle(ctx.project.style)?.text, ...clip.style };
850
- const align = s.align ?? "center";
851
- return (
852
- <div
853
- style={{
854
- ...boxStyle(clip.box ?? { x: 0.05, y: 0.4, width: 0.9, height: 0.2 }),
855
- ...style,
856
- display: "flex",
857
- alignItems: "center",
858
- justifyContent: align === "left" ? "flex-start" : align === "right" ? "flex-end" : "center",
859
- }}
860
- >
861
- <div
862
- style={{
863
- fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
864
- fontSize: s.fontSize ?? Math.round(ctx.height * 0.07),
865
- fontWeight: s.fontWeight ?? 800,
866
- color: s.color ?? "#ffffff",
867
- textAlign: align,
868
- lineHeight: s.lineHeight ?? 1.1,
869
- letterSpacing: s.letterSpacing,
870
- textTransform: s.textTransform,
871
- textShadow: s.shadow,
872
- whiteSpace: "pre-wrap",
873
- width: "100%",
874
- }}
875
- >
876
- {clip.text}
877
- </div>
878
- </div>
879
- );
880
- }
881
-
882
- function CaptionsClipView({ clip: saved, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
883
- const clip = useLive(saved);
884
- const StyledCaptions = styleLayer.StyledCaptions;
885
- if (ctx.project.style && !clip.style?.preset && StyledCaptions && projectStyle(ctx.project.style)) {
886
- return (
887
- <StyledCaptionsClip clip={clip} ctx={ctx}>
888
- <StyledCaptions
889
- styleId={ctx.project.style}
890
- words={clip.words}
891
- position={clip.style?.position}
892
- wordsPerLine={clip.style?.maxWordsPerLine}
893
- fontSize={clip.style?.fontSize}
894
- />
895
- </StyledCaptionsClip>
896
- );
897
- }
898
- return <PlainCaptionsClipView clip={clip} ctx={ctx} />;
899
- }
900
-
901
- function StyledCaptionsClip({ clip, children }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue; children: ReactNode }) {
902
- const style = useClipStyle(clip);
903
- return <AbsoluteFill style={{ ...style, pointerEvents: "none" }}>{children}</AbsoluteFill>;
904
- }
905
-
906
- function PlainCaptionsClipView({ clip, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
907
- const frame = useCurrentFrame();
908
- const style = useClipStyle(clip);
909
- const s = { ...projectStyle(ctx.project.style)?.captions, ...clip.style };
910
- const perLine = Math.max(1, s.maxWordsPerLine ?? 4);
911
- const words = clip.words;
912
- // Group words into lines of N; show the line containing the current (or last spoken) word.
913
- let current = -1;
914
- for (let i = 0; i < words.length; i += 1) {
915
- if (words[i].from <= frame) current = i;
916
- else break;
917
- }
918
- if (current < 0) return null;
919
- const lineStart = Math.floor(current / perLine) * perLine;
920
- const line = words.slice(lineStart, lineStart + perLine);
921
- const lastWord = line[line.length - 1];
922
- // Hide the line once its last word ended and the next line hasn't started (pause > 12 frames).
923
- const next = words[lineStart + perLine];
924
- if (frame > lastWord.to + 12 && (!next || next.from > frame)) return null;
925
-
926
- const fontSize = s.fontSize ?? Math.round(ctx.height * (ctx.height > ctx.width ? 0.045 : 0.06));
927
- const top = s.position === "top" ? "8%" : s.position === "center" ? "50%" : undefined;
928
- const bottom = s.position === "top" || s.position === "center" ? undefined : "10%";
929
- const preset = s.preset ?? "pop";
930
- return (
931
- <AbsoluteFill style={{ ...style, pointerEvents: "none" }}>
932
- <div
933
- style={{
934
- position: "absolute",
935
- left: "6%",
936
- right: "6%",
937
- top,
938
- bottom,
939
- transform: s.position === "center" ? "translateY(-50%)" : undefined,
940
- display: "flex",
941
- flexWrap: "wrap",
942
- justifyContent: "center",
943
- gap: `0 ${Math.round(fontSize * 0.28)}px`,
944
- fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
945
- fontWeight: s.fontWeight ?? 900,
946
- fontSize,
947
- lineHeight: 1.15,
948
- textAlign: "center",
949
- }}
950
- >
951
- {line.map((word, i) => {
952
- const index = lineStart + i;
953
- const active = index === current && frame <= word.to + 2;
954
- const spoken = index <= current;
955
- let scale = 1;
956
- if (preset === "pop" && index === current) {
957
- scale = interpolate(frame, [word.from, word.from + 4, word.from + 8], [0.85, 1.12, 1], clamp);
958
- }
959
- const color =
960
- preset === "plain"
961
- ? (s.color ?? "#fff")
962
- : active || (preset === "karaoke" && spoken)
963
- ? (s.highlight ?? "#facc15")
964
- : (s.color ?? "#fff");
965
- return (
966
- <span
967
- key={`${index}-${word.text}`}
968
- style={{
969
- display: "inline-block",
970
- color,
971
- opacity: preset === "pop" && !spoken ? 0 : 1,
972
- transform: `scale(${scale})`,
973
- textShadow: "0 4px 18px rgba(0,0,0,0.55)",
974
- }}
975
- >
976
- {word.text}
977
- </span>
978
- );
979
- })}
980
- </div>
981
- </AbsoluteFill>
982
- );
983
- }
984
-
985
- function AudioClipView({ clip: saved, trackMuted, gain }: { clip: AudioClip; trackMuted: boolean; gain: number }) {
986
- const clip = useLive(saved);
987
- const base = (clip.volume ?? 1) * gain;
988
- const fadeIn = Math.max(0, clip.fadeIn ?? 0);
989
- const fadeOut = Math.max(0, clip.fadeOut ?? 0);
990
- const duration = clip.duration;
991
- return (
992
- <Audio
993
- src={asset(clip.src)}
994
- trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
995
- muted={trackMuted}
996
- loop={clip.loop}
997
- playbackRate={clip.playbackRate}
998
- volume={
999
- fadeIn || fadeOut
1000
- ? (f: number) => {
1001
- const a = fadeIn ? interpolate(f, [0, fadeIn], [0, 1], clamp) : 1;
1002
- const b = fadeOut ? interpolate(f, [duration - fadeOut, duration], [1, 0], clamp) : 1;
1003
- return base * a * b;
1004
- }
1005
- : base
1006
- }
1007
- />
1008
- );
1009
- }
1010
-
1011
- const PREMOUNT_SECONDS = 1;
1012
-
1013
- /** Keep in sync with `clipClass` in lib/studio/preview-perf.ts. */
1014
- function clipClass(id: string) {
1015
- return `ac-clip--${id.replace(/\s+/g, "_")}`;
1016
- }
1017
-
1018
- function TrackView({
1019
- track,
1020
- scenes,
1021
- ctx,
1022
- masterGain,
1023
- order,
1024
- }: {
1025
- track: StudioTrack;
1026
- scenes: SceneRegistry;
1027
- ctx: Omit<TrackContext, "order">;
1028
- masterGain: number;
1029
- order: number;
1030
- }) {
1031
- const visual = track.kind !== "audio";
1032
- const { isRendering } = useRemotionEnvironment();
1033
- const trackCtx = useMemo<TrackContext>(() => ({ ...ctx, order }), [ctx, order]);
1034
- const frame = useCurrentFrame();
1035
- if (visual && track.hidden) return null;
1036
- // Adjustment layers draw nothing themselves: the clips below them read them (useClipGrade).
1037
- if (track.kind === "adjustment") return null;
1038
- const gain = (track.volume ?? 1) * masterGain;
1039
- const trackMuted = Boolean(track.muted);
1040
- return (
1041
- <>
1042
- {(track.clips as StudioClip[]).map((raw) => {
1043
- const clip = resolveClip(raw, ctx.variant);
1044
- if (clip.duration <= 0) return null;
1045
- const premountFor = clip.from > 0 ? Math.round(ctx.fps * PREMOUNT_SECONDS) : undefined;
1046
- // Only clips on screen (or about to be) get a <Sequence>: one for every clip on every frame
1047
- // is what made long edits drop frames. A <Sequence> outside this range draws nothing anyway.
1048
- if (frame < clip.from - (premountFor ?? 0) || frame >= clip.from + clip.duration) return null;
1049
- let body: ReactNode = null;
1050
- switch (track.kind) {
1051
- case "scene":
1052
- body = <SceneClipView clip={clip as SceneClip} scenes={scenes} ctx={trackCtx} />;
1053
- break;
1054
- case "image":
1055
- body = <ImageClipView clip={clip as ImageClip} ctx={trackCtx} />;
1056
- break;
1057
- case "video":
1058
- body = <VideoClipView clip={clip as VideoClip} trackMuted={trackMuted} gain={gain} ctx={trackCtx} />;
1059
- break;
1060
- case "text":
1061
- body = <TextClipView clip={clip as TextClip} ctx={trackCtx} />;
1062
- break;
1063
- case "captions":
1064
- body = <CaptionsClipView clip={clip as CaptionsClip} ctx={trackCtx} />;
1065
- break;
1066
- case "audio":
1067
- body = <AudioClipView clip={clip as AudioClip} trackMuted={trackMuted} gain={gain} />;
1068
- break;
1069
- }
1070
- const name = `${track.name} · ${clip.name ?? clip.id}`;
1071
- if (!visual && !isRendering) {
1072
- // Audio in the preview. Remotion only premounts a <Sequence> that has a layout box
1073
- // (layout="none" silently skips premountFor), and an <Audio> that mounts on its start frame
1074
- // pauses the player while it fetches and decodes the file, which is the hitch you feel on
1075
- // every voice line, SFX and music cut. So give it an invisible box purely to enable premount.
1076
- return (
1077
- <Sequence
1078
- key={clip.id}
1079
- from={clip.from}
1080
- durationInFrames={clip.duration}
1081
- premountFor={premountFor}
1082
- name={name}
1083
- style={{ display: "none" }}
1084
- >
1085
- {body}
1086
- </Sequence>
1087
- );
1088
- }
1089
- return (
1090
- <Sequence
1091
- key={clip.id}
1092
- from={clip.from}
1093
- durationInFrames={clip.duration}
1094
- // Mount each clip a moment early in the preview (hidden), so heavy scene setup
1095
- // (WebGL, big DOM) and media buffering happen before the cut instead of on it.
1096
- // No effect on captures or the export.
1097
- premountFor={premountFor}
1098
- name={name}
1099
- layout={visual ? "absolute-fill" : "none"}
1100
- // Lets the studio count what each clip draws (lib/studio/preview-perf.ts). No style.
1101
- className={visual ? clipClass(clip.id) : undefined}
1102
- >
1103
- {body}
1104
- </Sequence>
1105
- );
1106
- })}
1107
- </>
1108
- );
1109
- }
1110
-
1111
- // Compositions render inside the Agentcut page (Player, captures, browser export). Its global CSS
1112
- // (Tailwind preflight: border-box everywhere, line-height 1.5, image rules…) must not leak in, or a
1113
- // project would lay out differently than in a plain Remotion render. Revert author styles inside the
1114
- // composition to browser defaults; inline styles (everything scenes set) still win.
1115
- // SVG content is excluded: its presentation attributes (fill, stroke…) lose to any CSS rule.
1116
- // Then Remotion's own page defaults (remotion/dist default-css: * { box-sizing: border-box }).
1117
- const ISOLATION_CSS = `.studio-composition, .studio-composition :not(svg, svg *) { all: revert; }
1118
- .studio-composition, .studio-composition :not(svg, svg *), .studio-composition svg, .studio-composition svg * { box-sizing: border-box; }
1119
- .studio-composition svg { display: inline; vertical-align: baseline; }`;
1120
-
1121
- function useStyleIsolation() {
1122
- if (typeof document === "undefined") return;
1123
- if (document.getElementById("studio-composition-isolation")) return;
1124
- const style = document.createElement("style");
1125
- style.id = "studio-composition-isolation";
1126
- style.textContent = ISOLATION_CSS;
1127
- document.head.appendChild(style);
1128
- }
1129
-
1130
- export function createStudioComposition(scenes: SceneRegistry) {
1131
- function StudioComposition(props: StudioCompositionProps) {
1132
- const { project, layers, variant, assetBase, libraryBase, previewProxies } = props;
1133
- const size = project.variants[variant] ?? project.variants[project.defaultVariant] ?? {
1134
- width: 1920,
1135
- height: 1080,
1136
- };
1137
- setRuntimeState({ fps: project.fps, assetBase, libraryBase });
1138
- useStyleIsolation();
1139
- useProjectFonts(project.fonts ?? []);
1140
- const ctx = useMemo<StudioRuntimeContextValue>(
1141
- () => ({
1142
- project,
1143
- variant,
1144
- width: size.width,
1145
- height: size.height,
1146
- fps: project.fps,
1147
- assetBase,
1148
- libraryBase,
1149
- previewProxies,
1150
- }),
1151
- [project, variant, size.width, size.height, assetBase, libraryBase, previewProxies],
1152
- );
1153
- const masterGain = props.masterGain ?? 1;
1154
- const ordered = [...layers.tracks].reverse();
1155
- const adjustments = useMemo(
1156
- () =>
1157
- ordered.flatMap((track, order) =>
1158
- track.kind === "adjustment" && !track.hidden
1159
- ? (track.clips as AdjustmentClip[]).map((clip) => ({ clip: resolveClip(clip, variant) as AdjustmentClip, order }))
1160
- : [],
1161
- ),
1162
- // eslint-disable-next-line react-hooks/exhaustive-deps -- `ordered` is rebuilt from layers each render
1163
- [layers, variant],
1164
- );
1165
- const stackCtx = useMemo(() => ({ ...ctx, adjustments }), [ctx, adjustments]);
1166
- return (
1167
- <StudioRuntimeContext.Provider value={ctx}>
1168
- <AbsoluteFill
1169
- className="studio-composition"
1170
- style={{
1171
- background: project.background || "#000",
1172
- overflow: "hidden",
1173
- // Same starting point as a bare Remotion page: browser default font, size, color, line height.
1174
- font: "initial",
1175
- color: "black",
1176
- letterSpacing: "normal",
1177
- }}
1178
- >
1179
- {project.style && projectStyle(project.style) ? <StyleFonts /> : null}
1180
- {ordered.map((track, index) => (
1181
- <TrackView key={`${track.id}:${index}`} track={track} scenes={scenes} ctx={stackCtx} masterGain={masterGain} order={index} />
1182
- ))}
1183
- {project.style && styleLayer.StyleOverlay ? <styleLayer.StyleOverlay styleId={project.style} /> : null}
1184
- </AbsoluteFill>
1185
- </StudioRuntimeContext.Provider>
1186
- );
1187
- }
1188
- return StudioComposition;
1189
- }
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 { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ComponentType, type CSSProperties, type ReactNode } from "react";
5
+ import {
6
+ AbsoluteFill,
7
+ Img,
8
+ Internals,
9
+ Sequence,
10
+ continueRender,
11
+ delayRender,
12
+ interpolate,
13
+ useCurrentFrame,
14
+ useRemotionEnvironment,
15
+ useVideoConfig,
16
+ } from "remotion";
17
+ import { Audio, Video } from "@remotion/media";
18
+ import type { EffectDescriptor } from "remotion";
19
+ import { framingCropAt } from "./framing";
20
+ // The Agentcut component library, provided by the studio page (and by a copy in
21
+ // .agentcut/components for local renders).
22
+ import * as ComponentLibrary from "agentcut/components";
23
+ import { MOTION_COMPONENTS, resolveComponentProps } from "agentcut/components";
24
+
25
+ // Project styles (project.json "style") come from the library too. Pages from before styles don't
26
+ // provide them: the project then renders as if it had none.
27
+ type LibraryStyle = { captions: CaptionsClip["style"]; text: TextClip["style"] };
28
+ function projectStyle(id: string | undefined): LibraryStyle | null {
29
+ const get = (ComponentLibrary as { getMotionStyle?: (id?: string) => LibraryStyle | null }).getMotionStyle;
30
+ return id && typeof get === "function" ? get(id) : null;
31
+ }
32
+
33
+ type StyleLayer = {
34
+ styleCamera?: (
35
+ id: string | undefined,
36
+ at: { frame: number; durationInFrames: number; width: number; height: number },
37
+ ) => CSSProperties | null;
38
+ StyleOverlay?: ComponentType<{ styleId: string | undefined }>;
39
+ StyledCaptions?: ComponentType<{
40
+ styleId: string | undefined;
41
+ words: CaptionsClip["words"];
42
+ position?: string;
43
+ wordsPerLine?: number;
44
+ fontSize?: number;
45
+ }>;
46
+ };
47
+ const styleLayer = ComponentLibrary as StyleLayer;
48
+
49
+ /** Loads the library's fonts (the style's caption and text fonts) and holds the render until they're in. */
50
+ function StyleFonts() {
51
+ const use = (ComponentLibrary as { useMotionFonts?: () => void }).useMotionFonts;
52
+ use?.();
53
+ return null;
54
+ }
55
+ import {
56
+ StudioRuntimeContext,
57
+ asset,
58
+ clamp,
59
+ setRuntimeState,
60
+ type StudioRuntimeContextValue,
61
+ } from "./helpers";
62
+ import type {
63
+ AudioClip,
64
+ AdjustmentClip,
65
+ Box,
66
+ ClipTransform,
67
+ CaptionsClip,
68
+ ClipAnim,
69
+ ColorGrade,
70
+ ImageClip,
71
+ SceneClip,
72
+ SceneProps,
73
+ StudioClip,
74
+ StudioCompositionProps,
75
+ StudioFont,
76
+ StudioTrack,
77
+ TextClip,
78
+ CursorTrack,
79
+ VideoClip,
80
+ VideoCursor,
81
+ } from "./types";
82
+
83
+ export type SceneRegistry = Record<string, ComponentType<SceneProps & Record<string, unknown>>>;
84
+
85
+ // ---------------------------------------------------------------------------
86
+ // Fonts
87
+
88
+ const loadedFontSpecs = new Map<string, Promise<void>>();
89
+
90
+ function fontSpecToCss(spec: string) {
91
+ // "Inter:400;700;900" → family=Inter:wght@400;700;900 ; "Inter" → family=Inter
92
+ const [family, weights] = spec.split(":");
93
+ const name = family.trim().replace(/\s+/g, "+");
94
+ return weights ? `family=${name}:wght@${weights.trim()}` : `family=${name}`;
95
+ }
96
+
97
+ function loadLocalFont(font: Exclude<StudioFont, string>): Promise<void> {
98
+ const key = `${font.family}|${font.src}|${font.weight ?? ""}|${font.style ?? ""}`;
99
+ const existing = loadedFontSpecs.get(key);
100
+ if (existing) return existing;
101
+ const promise = (async () => {
102
+ if (typeof document === "undefined") return;
103
+ const face = new FontFace(font.family, `url(${JSON.stringify(asset(font.src))})`, {
104
+ weight: font.weight ?? "400",
105
+ style: font.style ?? "normal",
106
+ });
107
+ try {
108
+ document.fonts.add(await face.load());
109
+ } catch {
110
+ // Missing font file: keep rendering with the fallback font.
111
+ }
112
+ })();
113
+ loadedFontSpecs.set(key, promise);
114
+ return promise;
115
+ }
116
+
117
+ function loadFont(font: StudioFont): Promise<void> {
118
+ return typeof font === "string" ? loadFontSpec(font) : loadLocalFont(font);
119
+ }
120
+
121
+ function loadFontSpec(spec: string): Promise<void> {
122
+ const existing = loadedFontSpecs.get(spec);
123
+ if (existing) return existing;
124
+ const promise = (async () => {
125
+ if (typeof document === "undefined") return;
126
+ const href = `https://fonts.googleapis.com/css2?${fontSpecToCss(spec)}&display=block`;
127
+ if (!document.querySelector(`link[data-studio-font="${CSS.escape(spec)}"]`)) {
128
+ const link = document.createElement("link");
129
+ link.rel = "stylesheet";
130
+ link.href = href;
131
+ link.crossOrigin = "anonymous";
132
+ link.dataset.studioFont = spec;
133
+ await new Promise<void>((resolve) => {
134
+ link.onload = () => resolve();
135
+ link.onerror = () => resolve();
136
+ document.head.appendChild(link);
137
+ });
138
+ }
139
+ const [family, weights] = spec.split(":");
140
+ const list = weights ? weights.split(";").map((w) => w.trim()) : ["400"];
141
+ await Promise.all(
142
+ list.map((weight) =>
143
+ document.fonts.load(`${weight} 32px "${family.trim()}"`).catch(() => undefined),
144
+ ),
145
+ );
146
+ })();
147
+ loadedFontSpecs.set(spec, promise);
148
+ return promise;
149
+ }
150
+
151
+ function useProjectFonts(fonts: StudioFont[]) {
152
+ const key = fonts.map((font) => (typeof font === "string" ? font : `${font.family}|${font.src}|${font.weight ?? ""}`)).join("||");
153
+ const [handle] = useState(() =>
154
+ fonts.length > 0 ? delayRender(`Loading fonts ${key}`, { timeoutInMilliseconds: 20_000 }) : null,
155
+ );
156
+ useEffect(() => {
157
+ if (handle === null) return;
158
+ let cancelled = false;
159
+ Promise.all(fonts.map(loadFont)).finally(() => {
160
+ if (!cancelled) continueRender(handle);
161
+ });
162
+ return () => {
163
+ cancelled = true;
164
+ };
165
+ // eslint-disable-next-line react-hooks/exhaustive-deps
166
+ }, [handle, key]);
167
+ }
168
+
169
+ // ---------------------------------------------------------------------------
170
+ // Clip helpers
171
+
172
+ export function resolveClip<T extends StudioClip>(clip: T, variant: string): T {
173
+ const override = (clip as { variants?: Record<string, Partial<T>> }).variants?.[variant];
174
+ return override ? ({ ...clip, ...override } as T) : clip;
175
+ }
176
+
177
+ function animState(anim: ClipAnim | undefined, t: number) {
178
+ // t: 0 = fully hidden, 1 = fully shown
179
+ const preset = anim?.preset ?? "none";
180
+ const out: { opacity: number; transform: string; filter?: string } = { opacity: 1, transform: "" };
181
+ if (preset === "none" || t >= 1) return out;
182
+ const inv = 1 - t;
183
+ switch (preset) {
184
+ case "fade":
185
+ out.opacity = t;
186
+ break;
187
+ case "slide-up":
188
+ out.opacity = t;
189
+ out.transform = `translateY(${inv * 8}%)`;
190
+ break;
191
+ case "slide-down":
192
+ out.opacity = t;
193
+ out.transform = `translateY(${-inv * 8}%)`;
194
+ break;
195
+ case "slide-left":
196
+ out.opacity = t;
197
+ out.transform = `translateX(${inv * 8}%)`;
198
+ break;
199
+ case "slide-right":
200
+ out.opacity = t;
201
+ out.transform = `translateX(${-inv * 8}%)`;
202
+ break;
203
+ case "pop": {
204
+ // overshoot: 0 → 1.08 → 1
205
+ const s = t < 0.7 ? interpolate(t, [0, 0.7], [0.6, 1.08]) : interpolate(t, [0.7, 1], [1.08, 1]);
206
+ out.opacity = Math.min(1, t * 2);
207
+ out.transform = `scale(${s})`;
208
+ break;
209
+ }
210
+ case "scale":
211
+ out.opacity = t;
212
+ out.transform = `scale(${0.85 + 0.15 * t})`;
213
+ break;
214
+ case "blur":
215
+ out.opacity = t;
216
+ out.filter = `blur(${inv * 16}px)`;
217
+ break;
218
+ }
219
+ return out;
220
+ }
221
+
222
+ const easeOutCubic = (t: number) => 1 - Math.pow(1 - t, 3);
223
+
224
+ /** Enter/exit + opacity wrapper for one visual clip; frame is local to the clip. */
225
+ function useClipStyle(clip: StudioClip): CSSProperties {
226
+ const frame = useCurrentFrame();
227
+ const enterFrames = clip.enter && clip.enter.preset !== "none" ? Math.max(1, clip.enter.frames) : 0;
228
+ const exitFrames = clip.exit && clip.exit.preset !== "none" ? Math.max(1, clip.exit.frames) : 0;
229
+ const tIn = enterFrames ? easeOutCubic(interpolate(frame, [0, enterFrames], [0, 1], clamp)) : 1;
230
+ const tOut = exitFrames
231
+ ? easeOutCubic(interpolate(frame, [clip.duration - exitFrames, clip.duration], [1, 0], clamp))
232
+ : 1;
233
+ const a = animState(clip.enter, tIn);
234
+ const b = animState(clip.exit, tOut);
235
+ const transform = [a.transform, b.transform].filter(Boolean).join(" ");
236
+ const filter = [a.filter, b.filter].filter(Boolean).join(" ");
237
+ return {
238
+ opacity: a.opacity * b.opacity * (clip.opacity ?? 1),
239
+ transform: transform || undefined,
240
+ filter: filter || undefined,
241
+ };
242
+ }
243
+
244
+ /** A scene the user moved or resized on the preview: everything it draws shifts and scales together. */
245
+ function Moved({ transform, children }: { transform: ClipTransform | undefined; children: ReactNode }) {
246
+ const x = transform?.x ?? 0;
247
+ const y = transform?.y ?? 0;
248
+ const scale = transform?.scale ?? 1;
249
+ if (!x && !y && scale === 1) return <>{children}</>;
250
+ return (
251
+ <AbsoluteFill style={{ transform: `translate(${x * 100}%, ${y * 100}%) scale(${scale})`, transformOrigin: "50% 50%" }}>
252
+ {children}
253
+ </AbsoluteFill>
254
+ );
255
+ }
256
+
257
+ function boxStyle(box: Box | undefined): CSSProperties {
258
+ if (!box) return { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" };
259
+ return {
260
+ position: "absolute",
261
+ left: `${box.x * 100}%`,
262
+ top: `${box.y * 100}%`,
263
+ width: `${box.width * 100}%`,
264
+ height: `${box.height * 100}%`,
265
+ };
266
+ }
267
+
268
+ // ---------------------------------------------------------------------------
269
+ // Clip renderers (all frames local to the clip's <Sequence>)
270
+
271
+ function SceneClipView({
272
+ clip: saved,
273
+ scenes,
274
+ ctx,
275
+ }: {
276
+ clip: SceneClip;
277
+ scenes: SceneRegistry;
278
+ ctx: StudioRuntimeContextValue;
279
+ }) {
280
+ const clip = useLive(saved);
281
+ const style = useClipStyle(clip);
282
+ if (clip.component) {
283
+ const entry = MOTION_COMPONENTS[clip.component];
284
+ if (!entry) {
285
+ return (
286
+ <AbsoluteFill
287
+ style={{
288
+ alignItems: "center",
289
+ justifyContent: "center",
290
+ background: "rgba(220,38,38,0.25)",
291
+ color: "#fff",
292
+ fontFamily: "sans-serif",
293
+ fontSize: Math.round(ctx.height * 0.03),
294
+ }}
295
+ >
296
+ Unknown component “{clip.component}”
297
+ </AbsoluteFill>
298
+ );
299
+ }
300
+ const Component = entry.Component;
301
+ return (
302
+ <AbsoluteFill style={style}>
303
+ <Moved transform={clip.transform}>
304
+ <Component
305
+ {...resolveComponentProps(clip.component, clip.props, ctx.project.style)}
306
+ width={ctx.width}
307
+ height={ctx.height}
308
+ fps={ctx.fps}
309
+ durationInFrames={clip.duration}
310
+ variant={ctx.variant}
311
+ />
312
+ </Moved>
313
+ </AbsoluteFill>
314
+ );
315
+ }
316
+ const Scene = clip.scene ? scenes[clip.scene] : undefined;
317
+ if (!Scene) {
318
+ return (
319
+ <AbsoluteFill
320
+ style={{
321
+ alignItems: "center",
322
+ justifyContent: "center",
323
+ background: "rgba(220,38,38,0.25)",
324
+ color: "#fff",
325
+ fontFamily: "sans-serif",
326
+ fontSize: Math.round(ctx.height * 0.03),
327
+ }}
328
+ >
329
+ Missing scene “{clip.scene}” (scenes/{clip.scene}.tsx)
330
+ </AbsoluteFill>
331
+ );
332
+ }
333
+ return (
334
+ <AbsoluteFill style={style}>
335
+ <Moved transform={clip.transform}>
336
+ <Scene
337
+ {...(clip.props ?? {})}
338
+ clip={{ id: clip.id, from: clip.from, duration: clip.duration }}
339
+ variant={ctx.variant}
340
+ width={ctx.width}
341
+ height={ctx.height}
342
+ fps={ctx.fps}
343
+ />
344
+ </Moved>
345
+ </AbsoluteFill>
346
+ );
347
+ }
348
+
349
+ function ImageClipView({ clip: saved, ctx }: { clip: ImageClip; ctx: TrackContext }) {
350
+ const clip = useLive(saved);
351
+ const style = useClipStyle(clip);
352
+ const { filter, effects } = useClipGrade(clip, ctx, clip.inheritGrade === true);
353
+ return (
354
+ <div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
355
+ {effects.length ? (
356
+ // Effects draw images on a canvas, which needs its size in pixels.
357
+ <Img
358
+ src={asset(clip.src)}
359
+ width={Math.max(1, Math.round((clip.box?.width ?? 1) * ctx.width))}
360
+ height={Math.max(1, Math.round((clip.box?.height ?? 1) * ctx.height))}
361
+ effects={effects}
362
+ style={{ width: "100%", height: "100%", display: "block", objectFit: clip.fit ?? "cover" }}
363
+ />
364
+ ) : (
365
+ <Img
366
+ src={asset(clip.src)}
367
+ style={{ width: "100%", height: "100%", objectFit: clip.fit ?? "cover", display: "block", filter }}
368
+ />
369
+ )}
370
+ </div>
371
+ );
372
+ }
373
+
374
+ /** The runtime context plus what a clip needs to know about the stack it sits in. */
375
+ type TrackContext = StudioRuntimeContextValue & {
376
+ adjustments: { clip: AdjustmentClip; order: number }[];
377
+ /** Position in the stack, bottom to top. */
378
+ order: number;
379
+ };
380
+
381
+ // The grade engine comes from the library too. Pages from before it don't provide it: those keep the
382
+ // CSS filter below, which covers the original controls.
383
+ const LIVE_PROJECT = "project";
384
+ type GradeEntry = { grade: ColorGrade; frame: number; strength?: number };
385
+ type GradeEngine = {
386
+ gradeEffects?: (grades: GradeEntry[], options: { seed: number; luts?: ReadonlyMap<string, unknown> }) => EffectDescriptor<unknown>[];
387
+ gradeLutPaths?: (grade: ColorGrade | undefined) => string[];
388
+ isCssGrade?: (grade: ColorGrade | undefined) => boolean;
389
+ canDrawGrade?: () => boolean;
390
+ parseCube?: (text: string) => unknown;
391
+ };
392
+ const gradeEngine = ComponentLibrary as GradeEngine;
393
+
394
+ // A moving slider's value, straight from the inspector. Older pages don't provide it.
395
+ type LiveEdits = {
396
+ useLiveClip?: <T extends { id: string }>(clip: T, isRendering?: boolean) => T;
397
+ useLiveClips?: (ids: string[], isRendering?: boolean) => void;
398
+ getLiveClip?: (id: string) => Record<string, unknown> | undefined;
399
+ };
400
+ const liveEdits = ComponentLibrary as LiveEdits;
401
+ const useLiveClipEngine = liveEdits.useLiveClip;
402
+ const useLiveClipsEngine = liveEdits.useLiveClips;
403
+
404
+ function useLive<T extends { id: string }>(clip: T): T {
405
+ const { isRendering } = useRemotionEnvironment();
406
+ // The library is fixed for the page's lifetime, so this always calls the same hooks.
407
+ return useLiveClipEngine ? useLiveClipEngine(clip, isRendering) : clip;
408
+ }
409
+
410
+ const loadedLuts = new Map<string, unknown>();
411
+ const pendingLuts = new Map<string, Promise<void>>();
412
+
413
+ /** Load a .cube file once. A missing or broken file warns and the grade draws without it. */
414
+ function loadLut(path: string) {
415
+ let pending = pendingLuts.get(path);
416
+ if (!pending) {
417
+ pending = fetch(asset(path))
418
+ .then((response) => {
419
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
420
+ return response.text();
421
+ })
422
+ .then((text) => {
423
+ loadedLuts.set(path, gradeEngine.parseCube?.(text) ?? null);
424
+ })
425
+ .catch((error) => {
426
+ loadedLuts.set(path, null);
427
+ console.warn(`Could not load the LUT ${path}: ${error instanceof Error ? error.message : error}`);
428
+ });
429
+ pendingLuts.set(path, pending);
430
+ }
431
+ return pending;
432
+ }
433
+
434
+ /** Holds the render until these .cube files have loaded; later additions (preview edits) just redraw. */
435
+ function useGradeLuts(paths: string[]) {
436
+ const key = paths.join("\n");
437
+ const [, setLoaded] = useState(0);
438
+ const [handle] = useState(() =>
439
+ paths.some((path) => !loadedLuts.has(path)) ? delayRender(`Loading LUT ${paths.join(", ")}`, { timeoutInMilliseconds: 20_000 }) : null,
440
+ );
441
+ useEffect(() => {
442
+ let cancelled = false;
443
+ const wanted = key ? key.split("\n") : [];
444
+ Promise.all(wanted.map(loadLut))
445
+ .then(() => {
446
+ if (!cancelled) setLoaded((n) => n + 1);
447
+ })
448
+ .finally(() => {
449
+ if (handle !== null) continueRender(handle);
450
+ });
451
+ return () => {
452
+ cancelled = true;
453
+ };
454
+ }, [key, handle]);
455
+ return loadedLuts;
456
+ }
457
+
458
+ /** How far an adjustment clip is faded in at `local`: its enter and exit lengths ramp the grade. */
459
+ function adjustmentStrength(clip: AdjustmentClip, local: number) {
460
+ let strength = clip.opacity ?? 1;
461
+ if (clip.enter?.frames) strength *= Math.min(1, (local + 1) / clip.enter.frames);
462
+ if (clip.exit?.frames) strength *= Math.min(1, (clip.duration - local) / clip.exit.frames);
463
+ return strength;
464
+ }
465
+
466
+ /**
467
+ * What draws a clip's colour: its own grade, then the adjustment layers above it and the project
468
+ * grade. A lone clip grade using only the original controls stays a CSS filter, so existing projects
469
+ * render exactly as they did.
470
+ */
471
+ function useClipGrade(clip: VideoClip | ImageClip, ctx: TrackContext, inherits: boolean) {
472
+ const local = useCurrentFrame();
473
+ const frame = local + clip.from;
474
+ const { isRendering } = useRemotionEnvironment();
475
+ const liveIds = inherits ? [...ctx.adjustments.map((layer) => layer.clip.id), LIVE_PROJECT] : [];
476
+ useLiveClipsEngine?.(liveIds, isRendering);
477
+ const liveGrade = (id: string, saved: ColorGrade | undefined) => {
478
+ const fields = isRendering ? undefined : liveEdits.getLiveClip?.(id);
479
+ return fields && "grade" in fields ? (fields.grade as ColorGrade | undefined) : saved;
480
+ };
481
+ const grades: GradeEntry[] = [];
482
+ if (clip.grade) grades.push({ grade: clip.grade, frame: local });
483
+ const inherited: GradeEntry[] = [];
484
+ if (inherits) {
485
+ for (const layer of ctx.adjustments) {
486
+ if (layer.order <= ctx.order) continue;
487
+ const at = frame - layer.clip.from;
488
+ if (at < 0 || at >= layer.clip.duration) continue;
489
+ const grade = liveGrade(layer.clip.id, layer.clip.grade);
490
+ if (grade) inherited.push({ grade, frame: at, strength: adjustmentStrength(layer.clip, at) });
491
+ }
492
+ const projectGrade = liveGrade(LIVE_PROJECT, ctx.project.grade);
493
+ if (projectGrade) inherited.push({ grade: projectGrade, frame });
494
+ }
495
+ const paths = [...new Set([...grades, ...inherited].flatMap((entry) => gradeEngine.gradeLutPaths?.(entry.grade) ?? []))];
496
+ const luts = useGradeLuts(paths);
497
+ const useCss =
498
+ !gradeEngine.gradeEffects ||
499
+ !gradeEngine.canDrawGrade?.() ||
500
+ (inherited.length === 0 && (grades.length === 0 || gradeEngine.isCssGrade?.(grades[0].grade)));
501
+ if (useCss) return { filter: gradeFilter(clip.grade), effects: [] as EffectDescriptor<unknown>[] };
502
+ return {
503
+ filter: undefined,
504
+ effects: gradeEngine.gradeEffects!([...grades, ...inherited], { seed: frame, luts: luts as ReadonlyMap<string, unknown> }),
505
+ };
506
+ }
507
+
508
+ /** CSS filter for a color grade (supported by the browser export). */
509
+ function gradeFilter(grade: VideoClip["grade"]): string | undefined {
510
+ if (!grade) return undefined;
511
+ const parts: string[] = [];
512
+ if (grade.brightness !== undefined && grade.brightness !== 1) parts.push(`brightness(${grade.brightness})`);
513
+ if (grade.contrast !== undefined && grade.contrast !== 1) parts.push(`contrast(${grade.contrast})`);
514
+ if (grade.saturation !== undefined && grade.saturation !== 1) parts.push(`saturate(${grade.saturation})`);
515
+ const warmth = grade.warmth ?? 0;
516
+ if (warmth > 0) parts.push(`sepia(${(warmth * 0.35).toFixed(3)})`, `saturate(${(1 + warmth * 0.25).toFixed(3)})`);
517
+ const hue = (grade.hue ?? 0) + (warmth < 0 ? warmth * -12 : warmth * -4);
518
+ if (hue) parts.push(`hue-rotate(${hue.toFixed(1)}deg)`);
519
+ return parts.length ? parts.join(" ") : undefined;
520
+ }
521
+
522
+ // Frames this preview drew while paused, small and already graded. Coming back to one during a drag
523
+ // (the usual back-and-forth when looking for a cut) puts it on screen at once, over the player, until
524
+ // the decoder has redrawn it. Preview only; a wrong guess lasts only until that redraw.
525
+ const HELD_FRAME_WIDTH = 960;
526
+ /** About 200 MB at 16:9, across every clip. */
527
+ const MAX_HELD_FRAMES = 96;
528
+ const heldFrames = new Map<string, ImageBitmap>();
529
+
530
+ function holdFrame(key: string, frame: ImageBitmap) {
531
+ heldFrames.get(key)?.close();
532
+ heldFrames.delete(key);
533
+ heldFrames.set(key, frame);
534
+ if (heldFrames.size > MAX_HELD_FRAMES) {
535
+ const oldest = heldFrames.keys().next().value!;
536
+ heldFrames.get(oldest)?.close();
537
+ heldFrames.delete(oldest);
538
+ }
539
+ }
540
+
541
+ function useHeldFrames(version: string, frame: number, enabled: boolean) {
542
+ const boxRef = useRef<HTMLDivElement>(null);
543
+ const overlayRef = useRef<HTMLCanvasElement>(null);
544
+ const latest = useRef({ version, frame, enabled });
545
+ /** The frame the decoder's picture shows: nothing needs to cover it. */
546
+ const drawn = useRef<number | null>(null);
547
+ useLayoutEffect(() => {
548
+ latest.current = { version, frame, enabled };
549
+ const overlay = overlayRef.current;
550
+ if (!overlay) return;
551
+ if (!enabled || drawn.current === frame) {
552
+ overlay.style.visibility = "hidden";
553
+ return;
554
+ }
555
+ const picture = heldFrames.get(`${version}:${frame}`);
556
+ if (!picture) return;
557
+ if (overlay.width !== picture.width) overlay.width = picture.width;
558
+ if (overlay.height !== picture.height) overlay.height = picture.height;
559
+ overlay.getContext("2d")?.drawImage(picture, 0, 0);
560
+ overlay.style.visibility = "visible";
561
+ }, [version, frame, enabled]);
562
+ const onDrawn = useCallback(() => {
563
+ const now = latest.current;
564
+ drawn.current = now.frame;
565
+ const overlay = overlayRef.current;
566
+ if (overlay) overlay.style.visibility = "hidden";
567
+ if (!now.enabled) return;
568
+ // The player's own canvas, which has the grade on it (the callback's frame doesn't yet).
569
+ const canvas = boxRef.current?.querySelector<HTMLCanvasElement>("canvas");
570
+ if (!canvas || canvas === overlay || !canvas.width || !canvas.height) return;
571
+ const width = Math.min(HELD_FRAME_WIDTH, canvas.width);
572
+ const key = `${now.version}:${now.frame}`;
573
+ void createImageBitmap(canvas, {
574
+ resizeWidth: width,
575
+ resizeHeight: Math.max(1, Math.round((canvas.height * width) / canvas.width)),
576
+ resizeQuality: "low",
577
+ }).then((picture) => holdFrame(key, picture), () => undefined);
578
+ }, []);
579
+ return { boxRef, overlayRef, onDrawn };
580
+ }
581
+
582
+ function VideoClipView({
583
+ clip: saved,
584
+ trackMuted,
585
+ gain,
586
+ ctx,
587
+ }: {
588
+ clip: VideoClip;
589
+ trackMuted: boolean;
590
+ gain: number;
591
+ ctx: TrackContext;
592
+ }) {
593
+ const clip = useLive(saved);
594
+ const style = useClipStyle(clip);
595
+ const frame = useCurrentFrame();
596
+ const grade = useClipGrade(clip, ctx, clip.inheritGrade !== false);
597
+ const { isRendering } = useRemotionEnvironment();
598
+ const playing = Internals.Timeline.usePlaying();
599
+ const source = (!isRendering && ctx.previewProxies?.[clip.src]) || asset(clip.src);
600
+ const held = useHeldFrames(
601
+ `${clip.id}|${source}|${clip.trimStart ?? 0}|${clip.playbackRate ?? 1}|${clip.fit ?? ""}|${grade.filter ?? ""}|${JSON.stringify(clip.grade ?? null)}|${JSON.stringify(ctx.project.grade ?? null)}|${ctx.adjustments.length}`,
602
+ frame,
603
+ !isRendering && !playing,
604
+ );
605
+ // The project style's camera (punch-ins, drift, …) moves the footage inside its box.
606
+ const crop = clip.framing ? framingCropAt(clip.framing, (clip.trimStart ?? 0) / ctx.project.fps + frame / ctx.project.fps * (clip.playbackRate ?? 1)) : null;
607
+ const camera =
608
+ ctx.project.style && clip.camera !== "none" && styleLayer.styleCamera
609
+ ? styleLayer.styleCamera(ctx.project.style, { frame, durationInFrames: clip.duration, width: ctx.width, height: ctx.height })
610
+ : null;
611
+ return (
612
+ <div style={{ ...boxStyle(clip.box), ...style, overflow: "hidden", borderRadius: clip.radius }}>
613
+ <div style={{ position: "absolute", inset: 0, ...(clip.framing ? { filter: camera?.filter } : camera) }}>
614
+ <div ref={held.boxRef} style={crop ? {
615
+ position: "absolute", left: `${-crop.x / crop.width * 100}%`,
616
+ top: `${-crop.y / crop.height * 100}%`,
617
+ width: `${100 / crop.width}%`, height: `${100 / crop.height}%`,
618
+ } : { position: "absolute", inset: 0 }}>
619
+ <Video
620
+ src={source}
621
+ trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
622
+ volume={(clip.volume ?? 1) * gain}
623
+ muted={trackMuted || clip.muted}
624
+ playbackRate={clip.playbackRate}
625
+ // @remotion/media draws the frame itself: fit has to be its prop (CSS object-fit is ignored).
626
+ objectFit={clip.framing ? "fill" : clip.fit ?? "cover"}
627
+ style={{ width: "100%", height: "100%", filter: grade.filter }}
628
+ {...(grade.effects.length ? { effects: grade.effects } : {})}
629
+ {...(isRendering ? {} : { onVideoFrame: held.onDrawn })}
630
+ />
631
+ {isRendering ? null : (
632
+ <canvas
633
+ ref={held.overlayRef}
634
+ style={{ position: "absolute", inset: 0, width: "100%", height: "100%", filter: grade.filter, visibility: "hidden" }}
635
+ />
636
+ )}
637
+ {clip.cursor ? <CursorOverlay cursor={clip.cursor} clip={clip} /> : null}
638
+ </div>
639
+ </div>
640
+ </div>
641
+ );
642
+ }
643
+
644
+ // ---------------------------------------------------------------------------
645
+ // Recorded cursor (`cursor` on a video clip): the pointer and click ripples from a
646
+ // `<video>.cursor.json`, drawn inside the clip so it follows its trim, speed and box.
647
+
648
+ const loadedCursorTracks = new Map<string, CursorTrack | null>();
649
+ const pendingCursorTracks = new Map<string, Promise<CursorTrack | null>>();
650
+
651
+ function loadCursorTrack(url: string) {
652
+ let pending = pendingCursorTracks.get(url);
653
+ if (!pending) {
654
+ pending = fetch(url)
655
+ .then((response) => (response.ok ? (response.json() as Promise<CursorTrack>) : null))
656
+ .catch(() => null)
657
+ .then((track) => {
658
+ loadedCursorTracks.set(url, track);
659
+ return track;
660
+ });
661
+ pendingCursorTracks.set(url, pending);
662
+ }
663
+ return pending;
664
+ }
665
+
666
+ function useCursorTrack(src: string) {
667
+ const url = asset(src);
668
+ const [track, setTrack] = useState(() => loadedCursorTracks.get(url) ?? null);
669
+ const [handle] = useState(() =>
670
+ loadedCursorTracks.has(url) ? null : delayRender(`Loading cursor ${src}`, { timeoutInMilliseconds: 20_000 }),
671
+ );
672
+ useEffect(() => {
673
+ let cancelled = false;
674
+ loadCursorTrack(url)
675
+ .then((loaded) => {
676
+ if (!cancelled) setTrack(loaded);
677
+ })
678
+ .finally(() => {
679
+ if (handle !== null) continueRender(handle);
680
+ });
681
+ return () => {
682
+ cancelled = true;
683
+ };
684
+ }, [url, handle]);
685
+ return track;
686
+ }
687
+
688
+ function cursorPointAt(path: CursorTrack["path"], frame: number) {
689
+ let low = 0;
690
+ let high = path.length - 1;
691
+ let index = -1;
692
+ while (low <= high) {
693
+ const mid = (low + high) >> 1;
694
+ if (path[mid][0] <= frame) {
695
+ index = mid;
696
+ low = mid + 1;
697
+ } else {
698
+ high = mid - 1;
699
+ }
700
+ }
701
+ if (index < 0) return null;
702
+ const [from, x, y] = path[index];
703
+ if (x === null || y === null) return null;
704
+ const next = path[index + 1];
705
+ if (!next || next[1] === null || next[2] === null || next[0] <= from) return { x, y };
706
+ const t = Math.min(1, (frame - from) / (next[0] - from));
707
+ return { x: x + (next[1] - x) * t, y: y + (next[2] - y) * t };
708
+ }
709
+
710
+ const CURSOR_POINTER_PATH =
711
+ "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";
712
+ const CURSOR_GREEN = "#2dd47a";
713
+ const HAND_EMOJI = "\u{1F446}";
714
+
715
+ function CursorOverlay({ cursor, clip }: { cursor: VideoCursor; clip: VideoClip }) {
716
+ const frame = useCurrentFrame();
717
+ const { width: compWidth, height: compHeight } = useVideoConfig();
718
+ const track = useCursorTrack(cursor.src);
719
+ // Edits ("source" space) carry clicks for timing only: their positions don't match the picture.
720
+ if (!track || track.space !== "video") return null;
721
+
722
+ const style = cursor.style ?? "classic";
723
+ const fps = track.fps;
724
+ const sourceFrame = (clip.trimStart ?? 0) + frame * (clip.playbackRate ?? 1);
725
+
726
+ // Where the video's picture sits inside the clip box (contain letterboxes, cover crops).
727
+ const box = clip.box ?? { x: 0, y: 0, width: 1, height: 1 };
728
+ const boxWidth = box.width * compWidth;
729
+ const boxHeight = box.height * compHeight;
730
+ const videoWidth = track.width ?? boxWidth;
731
+ const videoHeight = track.height ?? boxHeight;
732
+ const scale =
733
+ (clip.fit ?? "cover") === "contain"
734
+ ? Math.min(boxWidth / videoWidth, boxHeight / videoHeight)
735
+ : Math.max(boxWidth / videoWidth, boxHeight / videoHeight);
736
+ const offsetX = (boxWidth - videoWidth * scale) / 2;
737
+ const offsetY = (boxHeight - videoHeight * scale) / 2;
738
+ const toBox = (x: number, y: number) => ({
739
+ left: offsetX + x * videoWidth * scale,
740
+ top: offsetY + y * videoHeight * scale,
741
+ });
742
+
743
+ // Sized to the frame, not the box, so a punch-in doesn't blow the pointer up.
744
+ const size =
745
+ Math.max(18, Math.min(44, Math.min(compWidth, compHeight) * 0.032)) * ((cursor.size ?? 100) / 100);
746
+
747
+ // Smoothing averages the path over a short window around this frame.
748
+ const reach = Math.round((cursor.smoothing ?? 0.3) * 0.2 * fps);
749
+ let sumX = 0;
750
+ let sumY = 0;
751
+ let count = 0;
752
+ for (let offset = -reach; offset <= reach; offset += 1) {
753
+ const sample = cursorPointAt(track.path, sourceFrame + offset);
754
+ if (!sample) continue;
755
+ sumX += sample.x;
756
+ sumY += sample.y;
757
+ count += 1;
758
+ }
759
+ const point = cursorPointAt(track.path, sourceFrame) && count ? toBox(sumX / count, sumY / count) : null;
760
+
761
+ const rippleFrames = 0.6 * fps;
762
+ const ripples =
763
+ cursor.clicks === false
764
+ ? []
765
+ : track.clicks.filter((click) => sourceFrame >= click.frame && sourceFrame - click.frame < rippleFrames);
766
+ let lastPressFrame: number | null = null;
767
+ for (const click of track.clicks) {
768
+ if (click.frame > sourceFrame) break;
769
+ lastPressFrame = click.frame;
770
+ }
771
+ const press = lastPressFrame === null ? 0 : Math.max(0, 1 - (sourceFrame - lastPressFrame) / (0.3 * fps));
772
+ const rippleColor = style === "accent" || style === "spotlight" ? CURSOR_GREEN : "#ffffff";
773
+
774
+ return (
775
+ <div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
776
+ {ripples.map((click) => {
777
+ const progress = (sourceFrame - click.frame) / rippleFrames;
778
+ const diameter = size * (0.6 + 2.2 * progress);
779
+ const at = toBox(click.x, click.y);
780
+ return (
781
+ <div
782
+ key={click.frame}
783
+ style={{
784
+ position: "absolute",
785
+ left: at.left - diameter / 2,
786
+ top: at.top - diameter / 2,
787
+ width: diameter,
788
+ height: diameter,
789
+ borderRadius: "50%",
790
+ border: `${Math.max(2, size * 0.09)}px solid ${rippleColor}`,
791
+ boxShadow: "0 0 6px rgba(0,0,0,0.45)",
792
+ boxSizing: "border-box",
793
+ opacity: 0.9 * (1 - progress),
794
+ }}
795
+ />
796
+ );
797
+ })}
798
+ {point && style === "spotlight" ? (
799
+ <div
800
+ style={{
801
+ position: "absolute",
802
+ left: point.left - size * 0.42,
803
+ top: point.top - size * 0.42,
804
+ width: size * 0.84,
805
+ height: size * 0.84,
806
+ borderRadius: "50%",
807
+ border: `${Math.max(2, size * 0.08)}px solid ${CURSOR_GREEN}`,
808
+ background: "rgba(45, 212, 122, 0.16)",
809
+ boxShadow: "0 1px 4px rgba(0,0,0,0.45)",
810
+ boxSizing: "border-box",
811
+ transform: `scale(${1 - press * 0.15})`,
812
+ }}
813
+ />
814
+ ) : null}
815
+ {point && style === "hand" ? (
816
+ <div
817
+ style={{
818
+ position: "absolute",
819
+ left: point.left - size * 0.42,
820
+ top: point.top - size * 0.16,
821
+ fontSize: size,
822
+ lineHeight: 1,
823
+ textShadow: "0 1px 3px rgba(0,0,0,0.55)",
824
+ transform: `rotate(${press * 9}deg) scale(${1 - press * 0.05})`,
825
+ transformOrigin: "42% 16%",
826
+ }}
827
+ >
828
+ {HAND_EMOJI}
829
+ </div>
830
+ ) : null}
831
+ {point && (style === "classic" || style === "accent") ? (
832
+ <svg
833
+ width={size}
834
+ height={size * 1.1}
835
+ viewBox="0 0 24 26.4"
836
+ style={{
837
+ position: "absolute",
838
+ left: point.left - (3.55 / 24) * size,
839
+ top: point.top - (1.75 / 24) * size,
840
+ overflow: "visible",
841
+ transform: `scale(${1 - press * 0.08})`,
842
+ transformOrigin: `${(3.55 / 24) * 100}% ${(1.75 / 26.4) * 100}%`,
843
+ }}
844
+ >
845
+ <defs>
846
+ <filter id="studio-cursor-shadow" x="-50%" y="-50%" width="200%" height="200%">
847
+ <feDropShadow dx="0" dy="1.5" stdDeviation="1.1" floodColor="#000000" floodOpacity="0.65" />
848
+ </filter>
849
+ </defs>
850
+ <path
851
+ d={CURSOR_POINTER_PATH}
852
+ fill={style === "accent" ? CURSOR_GREEN : "#ffffff"}
853
+ stroke="#000000"
854
+ strokeWidth={2.4}
855
+ strokeLinejoin="round"
856
+ filter="url(#studio-cursor-shadow)"
857
+ />
858
+ </svg>
859
+ ) : null}
860
+ </div>
861
+ );
862
+ }
863
+
864
+ function TextClipView({ clip: saved, ctx }: { clip: TextClip; ctx: StudioRuntimeContextValue }) {
865
+ const clip = useLive(saved);
866
+ const style = useClipStyle(clip);
867
+ const s = { ...projectStyle(ctx.project.style)?.text, ...clip.style };
868
+ const align = s.align ?? "center";
869
+ return (
870
+ <div
871
+ style={{
872
+ ...boxStyle(clip.box ?? { x: 0.05, y: 0.4, width: 0.9, height: 0.2 }),
873
+ ...style,
874
+ display: "flex",
875
+ alignItems: "center",
876
+ justifyContent: align === "left" ? "flex-start" : align === "right" ? "flex-end" : "center",
877
+ }}
878
+ >
879
+ <div
880
+ style={{
881
+ fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
882
+ fontSize: s.fontSize ?? Math.round(ctx.height * 0.07),
883
+ fontWeight: s.fontWeight ?? 800,
884
+ color: s.color ?? "#ffffff",
885
+ textAlign: align,
886
+ lineHeight: s.lineHeight ?? 1.1,
887
+ letterSpacing: s.letterSpacing,
888
+ textTransform: s.textTransform,
889
+ textShadow: s.shadow,
890
+ whiteSpace: "pre-wrap",
891
+ width: "100%",
892
+ }}
893
+ >
894
+ {clip.text}
895
+ </div>
896
+ </div>
897
+ );
898
+ }
899
+
900
+ function CaptionsClipView({ clip: saved, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
901
+ const clip = useLive(saved);
902
+ const StyledCaptions = styleLayer.StyledCaptions;
903
+ if (ctx.project.style && !clip.style?.preset && StyledCaptions && projectStyle(ctx.project.style)) {
904
+ return (
905
+ <StyledCaptionsClip clip={clip} ctx={ctx}>
906
+ <StyledCaptions
907
+ styleId={ctx.project.style}
908
+ words={clip.words}
909
+ position={clip.style?.position}
910
+ wordsPerLine={clip.style?.maxWordsPerLine}
911
+ fontSize={clip.style?.fontSize}
912
+ />
913
+ </StyledCaptionsClip>
914
+ );
915
+ }
916
+ return <PlainCaptionsClipView clip={clip} ctx={ctx} />;
917
+ }
918
+
919
+ function StyledCaptionsClip({ clip, children }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue; children: ReactNode }) {
920
+ const style = useClipStyle(clip);
921
+ return <AbsoluteFill style={{ ...style, pointerEvents: "none" }}>{children}</AbsoluteFill>;
922
+ }
923
+
924
+ function PlainCaptionsClipView({ clip, ctx }: { clip: CaptionsClip; ctx: StudioRuntimeContextValue }) {
925
+ const frame = useCurrentFrame();
926
+ const style = useClipStyle(clip);
927
+ const s = { ...projectStyle(ctx.project.style)?.captions, ...clip.style };
928
+ const perLine = Math.max(1, s.maxWordsPerLine ?? 4);
929
+ const words = clip.words;
930
+ // Group words into lines of N; show the line containing the current (or last spoken) word.
931
+ let current = -1;
932
+ for (let i = 0; i < words.length; i += 1) {
933
+ if (words[i].from <= frame) current = i;
934
+ else break;
935
+ }
936
+ if (current < 0) return null;
937
+ const lineStart = Math.floor(current / perLine) * perLine;
938
+ const line = words.slice(lineStart, lineStart + perLine);
939
+ const lastWord = line[line.length - 1];
940
+ // Hide the line once its last word ended and the next line hasn't started (pause > 12 frames).
941
+ const next = words[lineStart + perLine];
942
+ if (frame > lastWord.to + 12 && (!next || next.from > frame)) return null;
943
+
944
+ const fontSize = s.fontSize ?? Math.round(ctx.height * (ctx.height > ctx.width ? 0.045 : 0.06));
945
+ const top = s.position === "top" ? "8%" : s.position === "center" ? "50%" : undefined;
946
+ const bottom = s.position === "top" || s.position === "center" ? undefined : "10%";
947
+ const preset = s.preset ?? "pop";
948
+ return (
949
+ <AbsoluteFill style={{ ...style, pointerEvents: "none" }}>
950
+ <div
951
+ style={{
952
+ position: "absolute",
953
+ left: "6%",
954
+ right: "6%",
955
+ top,
956
+ bottom,
957
+ transform: s.position === "center" ? "translateY(-50%)" : undefined,
958
+ display: "flex",
959
+ flexWrap: "wrap",
960
+ justifyContent: "center",
961
+ gap: `0 ${Math.round(fontSize * 0.28)}px`,
962
+ fontFamily: s.fontFamily ? `"${s.fontFamily}", sans-serif` : "sans-serif",
963
+ fontWeight: s.fontWeight ?? 900,
964
+ fontSize,
965
+ lineHeight: 1.15,
966
+ textAlign: "center",
967
+ }}
968
+ >
969
+ {line.map((word, i) => {
970
+ const index = lineStart + i;
971
+ const active = index === current && frame <= word.to + 2;
972
+ const spoken = index <= current;
973
+ let scale = 1;
974
+ if (preset === "pop" && index === current) {
975
+ scale = interpolate(frame, [word.from, word.from + 4, word.from + 8], [0.85, 1.12, 1], clamp);
976
+ }
977
+ const color =
978
+ preset === "plain"
979
+ ? (s.color ?? "#fff")
980
+ : active || (preset === "karaoke" && spoken)
981
+ ? (s.highlight ?? "#facc15")
982
+ : (s.color ?? "#fff");
983
+ return (
984
+ <span
985
+ key={`${index}-${word.text}`}
986
+ style={{
987
+ display: "inline-block",
988
+ color,
989
+ opacity: preset === "pop" && !spoken ? 0 : 1,
990
+ transform: `scale(${scale})`,
991
+ textShadow: "0 4px 18px rgba(0,0,0,0.55)",
992
+ }}
993
+ >
994
+ {word.text}
995
+ </span>
996
+ );
997
+ })}
998
+ </div>
999
+ </AbsoluteFill>
1000
+ );
1001
+ }
1002
+
1003
+ function AudioClipView({ clip: saved, trackMuted, gain }: { clip: AudioClip; trackMuted: boolean; gain: number }) {
1004
+ const clip = useLive(saved);
1005
+ const base = (clip.volume ?? 1) * gain;
1006
+ const fadeIn = Math.max(0, clip.fadeIn ?? 0);
1007
+ const fadeOut = Math.max(0, clip.fadeOut ?? 0);
1008
+ const duration = clip.duration;
1009
+ return (
1010
+ <Audio
1011
+ src={asset(clip.src)}
1012
+ trimBefore={clip.trimStart && clip.trimStart > 0 ? clip.trimStart : undefined}
1013
+ muted={trackMuted}
1014
+ loop={clip.loop}
1015
+ playbackRate={clip.playbackRate}
1016
+ volume={
1017
+ fadeIn || fadeOut
1018
+ ? (f: number) => {
1019
+ const a = fadeIn ? interpolate(f, [0, fadeIn], [0, 1], clamp) : 1;
1020
+ const b = fadeOut ? interpolate(f, [duration - fadeOut, duration], [1, 0], clamp) : 1;
1021
+ return base * a * b;
1022
+ }
1023
+ : base
1024
+ }
1025
+ />
1026
+ );
1027
+ }
1028
+
1029
+ const PREMOUNT_SECONDS = 1;
1030
+
1031
+ /** Keep in sync with `clipClass` in lib/studio/preview-perf.ts. */
1032
+ function clipClass(id: string) {
1033
+ return `ac-clip--${id.replace(/\s+/g, "_")}`;
1034
+ }
1035
+
1036
+ function TrackView({
1037
+ track,
1038
+ scenes,
1039
+ ctx,
1040
+ masterGain,
1041
+ order,
1042
+ }: {
1043
+ track: StudioTrack;
1044
+ scenes: SceneRegistry;
1045
+ ctx: Omit<TrackContext, "order">;
1046
+ masterGain: number;
1047
+ order: number;
1048
+ }) {
1049
+ const visual = track.kind !== "audio";
1050
+ const { isRendering } = useRemotionEnvironment();
1051
+ const trackCtx = useMemo<TrackContext>(() => ({ ...ctx, order }), [ctx, order]);
1052
+ const frame = useCurrentFrame();
1053
+ if (visual && track.hidden) return null;
1054
+ // Adjustment layers draw nothing themselves: the clips below them read them (useClipGrade).
1055
+ if (track.kind === "adjustment") return null;
1056
+ const gain = (track.volume ?? 1) * masterGain;
1057
+ const trackMuted = Boolean(track.muted);
1058
+ return (
1059
+ <>
1060
+ {(track.clips as StudioClip[]).map((raw) => {
1061
+ const clip = resolveClip(raw, ctx.variant);
1062
+ if (clip.duration <= 0) return null;
1063
+ const premountFor = clip.from > 0 ? Math.round(ctx.fps * PREMOUNT_SECONDS) : undefined;
1064
+ // Only clips on screen (or about to be) get a <Sequence>: one for every clip on every frame
1065
+ // is what made long edits drop frames. A <Sequence> outside this range draws nothing anyway.
1066
+ if (frame < clip.from - (premountFor ?? 0) || frame >= clip.from + clip.duration) return null;
1067
+ let body: ReactNode = null;
1068
+ switch (track.kind) {
1069
+ case "scene":
1070
+ body = <SceneClipView clip={clip as SceneClip} scenes={scenes} ctx={trackCtx} />;
1071
+ break;
1072
+ case "image":
1073
+ body = <ImageClipView clip={clip as ImageClip} ctx={trackCtx} />;
1074
+ break;
1075
+ case "video":
1076
+ body = <VideoClipView clip={clip as VideoClip} trackMuted={trackMuted} gain={gain} ctx={trackCtx} />;
1077
+ break;
1078
+ case "text":
1079
+ body = <TextClipView clip={clip as TextClip} ctx={trackCtx} />;
1080
+ break;
1081
+ case "captions":
1082
+ body = <CaptionsClipView clip={clip as CaptionsClip} ctx={trackCtx} />;
1083
+ break;
1084
+ case "audio":
1085
+ body = <AudioClipView clip={clip as AudioClip} trackMuted={trackMuted} gain={gain} />;
1086
+ break;
1087
+ }
1088
+ const name = `${track.name} · ${clip.name ?? clip.id}`;
1089
+ if (!visual && !isRendering) {
1090
+ // Audio in the preview. Remotion only premounts a <Sequence> that has a layout box
1091
+ // (layout="none" silently skips premountFor), and an <Audio> that mounts on its start frame
1092
+ // pauses the player while it fetches and decodes the file, which is the hitch you feel on
1093
+ // every voice line, SFX and music cut. So give it an invisible box purely to enable premount.
1094
+ return (
1095
+ <Sequence
1096
+ key={clip.id}
1097
+ from={clip.from}
1098
+ durationInFrames={clip.duration}
1099
+ premountFor={premountFor}
1100
+ name={name}
1101
+ style={{ display: "none" }}
1102
+ >
1103
+ {body}
1104
+ </Sequence>
1105
+ );
1106
+ }
1107
+ return (
1108
+ <Sequence
1109
+ key={clip.id}
1110
+ from={clip.from}
1111
+ durationInFrames={clip.duration}
1112
+ // Mount each clip a moment early in the preview (hidden), so heavy scene setup
1113
+ // (WebGL, big DOM) and media buffering happen before the cut instead of on it.
1114
+ // No effect on captures or the export.
1115
+ premountFor={premountFor}
1116
+ name={name}
1117
+ layout={visual ? "absolute-fill" : "none"}
1118
+ // Lets the studio count what each clip draws (lib/studio/preview-perf.ts). No style.
1119
+ className={visual ? clipClass(clip.id) : undefined}
1120
+ >
1121
+ {body}
1122
+ </Sequence>
1123
+ );
1124
+ })}
1125
+ </>
1126
+ );
1127
+ }
1128
+
1129
+ // Compositions render inside the Agentcut page (Player, captures, browser export). Its global CSS
1130
+ // (Tailwind preflight: border-box everywhere, line-height 1.5, image rules…) must not leak in, or a
1131
+ // project would lay out differently than in a plain Remotion render. Revert author styles inside the
1132
+ // composition to browser defaults; inline styles (everything scenes set) still win.
1133
+ // SVG content is excluded: its presentation attributes (fill, stroke…) lose to any CSS rule.
1134
+ // Then Remotion's own page defaults (remotion/dist default-css: * { box-sizing: border-box }).
1135
+ const ISOLATION_CSS = `.studio-composition, .studio-composition :not(svg, svg *) { all: revert; }
1136
+ .studio-composition, .studio-composition :not(svg, svg *), .studio-composition svg, .studio-composition svg * { box-sizing: border-box; }
1137
+ .studio-composition svg { display: inline; vertical-align: baseline; }`;
1138
+
1139
+ function useStyleIsolation() {
1140
+ if (typeof document === "undefined") return;
1141
+ if (document.getElementById("studio-composition-isolation")) return;
1142
+ const style = document.createElement("style");
1143
+ style.id = "studio-composition-isolation";
1144
+ style.textContent = ISOLATION_CSS;
1145
+ document.head.appendChild(style);
1146
+ }
1147
+
1148
+ export function createStudioComposition(scenes: SceneRegistry) {
1149
+ function StudioComposition(props: StudioCompositionProps) {
1150
+ const { project, layers, variant, assetBase, libraryBase, previewProxies } = props;
1151
+ const size = project.variants[variant] ?? project.variants[project.defaultVariant] ?? {
1152
+ width: 1920,
1153
+ height: 1080,
1154
+ };
1155
+ setRuntimeState({ fps: project.fps, assetBase, libraryBase });
1156
+ useStyleIsolation();
1157
+ useProjectFonts(project.fonts ?? []);
1158
+ const ctx = useMemo<StudioRuntimeContextValue>(
1159
+ () => ({
1160
+ project,
1161
+ variant,
1162
+ width: size.width,
1163
+ height: size.height,
1164
+ fps: project.fps,
1165
+ assetBase,
1166
+ libraryBase,
1167
+ previewProxies,
1168
+ }),
1169
+ [project, variant, size.width, size.height, assetBase, libraryBase, previewProxies],
1170
+ );
1171
+ const masterGain = props.masterGain ?? 1;
1172
+ const ordered = [...layers.tracks].reverse();
1173
+ const adjustments = useMemo(
1174
+ () =>
1175
+ ordered.flatMap((track, order) =>
1176
+ track.kind === "adjustment" && !track.hidden
1177
+ ? (track.clips as AdjustmentClip[]).map((clip) => ({ clip: resolveClip(clip, variant) as AdjustmentClip, order }))
1178
+ : [],
1179
+ ),
1180
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- `ordered` is rebuilt from layers each render
1181
+ [layers, variant],
1182
+ );
1183
+ const stackCtx = useMemo(() => ({ ...ctx, adjustments }), [ctx, adjustments]);
1184
+ return (
1185
+ <StudioRuntimeContext.Provider value={ctx}>
1186
+ <AbsoluteFill
1187
+ className="studio-composition"
1188
+ style={{
1189
+ background: project.background || "#000",
1190
+ overflow: "hidden",
1191
+ // Same starting point as a bare Remotion page: browser default font, size, color, line height.
1192
+ font: "initial",
1193
+ color: "black",
1194
+ letterSpacing: "normal",
1195
+ }}
1196
+ >
1197
+ {project.style && projectStyle(project.style) ? <StyleFonts /> : null}
1198
+ {ordered.map((track, index) => (
1199
+ <TrackView key={`${track.id}:${index}`} track={track} scenes={scenes} ctx={stackCtx} masterGain={masterGain} order={index} />
1200
+ ))}
1201
+ {project.style && styleLayer.StyleOverlay ? <styleLayer.StyleOverlay styleId={project.style} /> : null}
1202
+ </AbsoluteFill>
1203
+ </StudioRuntimeContext.Provider>
1204
+ );
1205
+ }
1206
+ return StudioComposition;
1207
+ }