@motionhub/remotion 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -41,7 +41,7 @@ export const Film = () => {
41
41
 
42
42
  ## A film of scenes
43
43
 
44
- A film drawn as `@motionhub/runtime`'s `<Film>` of `<Scene>`s (0.6 or later; the Open Motion Studio's films are, and its Remotion export
44
+ A film drawn as `@motionhub/runtime`'s `<Film>` of `<Scene>`s (0.6 or 0.7; the Open Motion Studio's films are, and its Remotion export
45
45
  writes this for one) plays in Remotion with each scene in a `Series.Sequence` of its own, so the scenes are named clips on the Studio's
46
46
  timeline. `<FilmScene id>` draws one scene of the film on the sequence's clock; `sceneFrames` gives each its length in frames, and
47
47
  `filmMetadata` the composition's, checking that the film's scenes are the ones placed:
@@ -75,6 +75,40 @@ made in a loop, they would show as one row. A scene's seconds are put on whole f
75
75
  the film follows by itself; a scene added, taken out or moved is an error until the `Series` places it. The film's own motions are its
76
76
  `<MotionScope>`s, so the provider's tokens can be empty.
77
77
 
78
+ A film's `<Sound>`s (`@motionhub/runtime` 0.7) play with `<FilmAudio component={Main} film={film} />`, placed once inside the provider and
79
+ outside the `Series`, so its frames are the film's. Each sound is a `Sequence` from where the film places it with an `<Html5Audio>` of its
80
+ file (a URL, or a path in `public/`), trimmed, looped and faded as the film says, its level in dB a gain. Above 0 dB, Remotion amplifies
81
+ when it renders; its preview and Player hold a volume at 1 unless `useWebAudioApi` is set, which needs the file served with CORS, so the
82
+ binding leaves it off. A sound without `dur` that does not loop plays its file to its end or to the end of where it sits; its fade out is
83
+ heard only when it lasts that long, since the binding does not read the file. A sound's `duck` is not applied: it waits for a narration.
84
+ `filmSounds(Main, film)` gives the same in frames, for code of your own. It needs Remotion 4.0.530 or later, the first whose
85
+ `<Html5Audio>` takes `durationInFrames`.
86
+
87
+ ## A component's parameters in Remotion Studio
88
+
89
+ `paramsSchema(meta, z)` turns a component's `meta.params` into a composition's schema, so Remotion Studio's props editor edits
90
+ them; `paramDefaults(meta)` gives its `defaultProps`. Pass your project's `zod` (Remotion wants its own version):
91
+
92
+ ```tsx
93
+ import {z} from 'zod';
94
+ import {Composition} from 'remotion';
95
+ import {MotionProvider, paramDefaults, paramsSchema} from '@motionhub/remotion';
96
+ import tokens from '../motions/@iamsuosi/lab-notebook/tokens.json';
97
+ import {CurveArrow, meta} from '../motions/@iamsuosi/lab-notebook/react/parts/arrow.tsx';
98
+
99
+ const Arrow = (props) => <MotionProvider tokens={tokens}><CurveArrow {...props} /></MotionProvider>;
100
+
101
+ // paramDefaults gives what the meta has defaults for; a param with none (here from and to) is yours to give
102
+ <Composition id="arrow" component={Arrow} schema={paramsSchema(meta, z)} defaultProps={{...paramDefaults(meta), from: [400, 700], to: [1500, 380]}}
103
+ durationInFrames={90} fps={30} width={1920} height={1080} />
104
+ ```
105
+
106
+ A param whose `type` is in the vocabulary of SPEC.md §12 (`text`, `string`, `color`, `number`, `px`, `degrees`, `progress`, `boolean`,
107
+ `[x, y]`, `text[]`, or choices written `a | b | c`) is a field described by its `doc`: `progress` from 0 to 1, choices as a list.
108
+ One with no default is optional, one whose default is `null` is nullable. A param of any other type is not in the schema and passes
109
+ through untouched, for code to give. Remotion Studio edits the props live either way; to save them back into the source it needs
110
+ `defaultProps` written as an object literal, so write out what `paramDefaults(meta)` gives once you have it.
111
+
78
112
  ## Several motions in one film
79
113
 
80
114
  A film can change motion from scene to scene, or hold two in one frame ([a project's motions](../../SPEC.md#13-a-projects-motions)). Keep one `<MotionProvider>` outside every `<Sequence>`, and wrap each scene in `<MotionScope tokens>` from `@motionhub/runtime` (0.4 or later): the components under it read that motion's tokens and custom properties, and keep Remotion's frame and the film's time. Load every motion's faces before the first frame, each `<MotionFonts>` in its own scope with `root={false}`:
package/film.js CHANGED
@@ -13,7 +13,7 @@ function outlineOf(Component, {fps = 30, width = 1920, height = 1080, props = {}
13
13
  } catch (error) {
14
14
  throw new Error(`drawing the film once, off the page, at its start, to learn its scenes: ${error.message}. Its motions' tokens are its own <MotionScope>s: the provider here has none`, {cause: error});
15
15
  }
16
- if (!outline) throw new Error('the component draws no <Film> (@motionhub/runtime 0.6)');
16
+ if (!outline) throw new Error('the component draws no <Film> (@motionhub/runtime 0.6 or later)');
17
17
  return outline;
18
18
  }
19
19
 
@@ -48,3 +48,40 @@ export function filmMetadata(Component, film = {}, ids) {
48
48
  }
49
49
  return {durationInFrames: frames.reduce((sum, [, n]) => sum + n, 0), fps, width, height};
50
50
  }
51
+
52
+ /**
53
+ * A <Sound> of the film (@motionhub/runtime 0.7, in the film's seconds) as Remotion plays it at `fps`: a Sequence `from` its start for
54
+ * `durationInFrames` (to `until`, or its `dur` when it does not loop), and in it the file from `trimBefore` frames, `sourceFrames` of it
55
+ * (null: the rest of the file) played once or with `loop` again and again, at `volume(frame)`, the frame counted from the Sequence's
56
+ * start: its level in dB as a gain, through its fades, which shrink together when they would not fit in the Sequence. A file that ends
57
+ * before the Sequence does ends where it ends, and its fade out, which needs its length, is not heard then.
58
+ */
59
+ export function soundPlan(sound, fps) {
60
+ const from = Math.round(sound.at * fps);
61
+ const seconds = Math.max(0, sound.loop || sound.dur === null ? sound.until - sound.at : Math.min(sound.dur, sound.until - sound.at));
62
+ const durationInFrames = Math.max(1, Math.round(seconds * fps));
63
+ const span = durationInFrames / fps;
64
+ const fit = sound.fadeIn + sound.fadeOut > span ? span / (sound.fadeIn + sound.fadeOut) : 1;
65
+ const fadeIn = sound.fadeIn * fit, fadeOut = sound.fadeOut * fit;
66
+ const gain = 10 ** (sound.volume / 20);
67
+ return {
68
+ src: sound.src,
69
+ from,
70
+ durationInFrames,
71
+ trimBefore: Math.round(sound.trim * fps),
72
+ sourceFrames: sound.dur === null ? null : Math.max(1, Math.round(sound.dur * fps)),
73
+ loop: sound.loop,
74
+ volume: (frame) => {
75
+ const u = frame / fps;
76
+ const rise = fadeIn > 0 ? Math.min(1, Math.max(0, u / fadeIn)) : 1;
77
+ const fall = fadeOut > 0 ? Math.min(1, Math.max(0, (span - u) / fadeOut)) : 1;
78
+ return gain * Math.min(rise, fall);
79
+ },
80
+ };
81
+ }
82
+
83
+ /** Every <Sound> of the <Film> a component draws, the film's own and its scenes', as soundPlan gives it */
84
+ export function filmSounds(Component, film = {}) {
85
+ const outline = outlineOf(Component, film);
86
+ return [...(outline.sounds ?? []), ...outline.scenes.flatMap((scene) => scene.sounds ?? [])].map((sound) => soundPlan(sound, film.fps ?? 30));
87
+ }
package/index.d.ts CHANGED
@@ -2,11 +2,28 @@ import type {FC, ReactNode} from 'react';
2
2
  import type {Tokens} from '@motionhub/runtime';
3
3
 
4
4
  export declare const MotionProvider: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
5
+ /** A component's meta, as SPEC.md §12 has it */
6
+ type MetaOf = {params?: Record<string, {type: string; doc: string; default?: unknown}>};
7
+ /** The types of the vocabulary (SPEC.md §12, Parameters) a schema field is made for */
8
+ export declare const PARAM_TYPES: string[];
9
+ /** The choices of a type written `a | b | c`, or null */
10
+ export declare function paramChoices(type: string): string[] | null;
11
+ /** A composition's schema for a component, from its meta, built with the project's zod (`paramsSchema(meta, z)`) */
12
+ export declare function paramsSchema<Z>(meta: MetaOf, z: Z): any;
13
+ /** A composition's defaultProps for a component: every param's default, where it has one */
14
+ export declare function paramDefaults(meta: MetaOf): Record<string, unknown>;
5
15
  type FilmOf<P> = {fps?: number; width?: number; height?: number; props?: P};
6
16
  /** Each scene of the <Film> a component draws, by id, in whole frames that meet end to end */
7
17
  export declare function sceneFrames<P extends object>(Component: FC<P>, film?: FilmOf<P>): Record<string, number>;
8
18
  /** A composition's metadata for a component that draws a <Film>: its length in frames from the scenes; with `ids`, the scenes must be those, in order */
9
19
  export declare function filmMetadata<P extends object>(Component: FC<P>, film?: FilmOf<P>, ids?: string[]): {durationInFrames: number; fps: number; width: number; height: number};
20
+ /** A <Sound> of a film in Remotion's frames: a Sequence from `from` for `durationInFrames`, the file from `trimBefore` frames, `sourceFrames` of it (null: the rest), looped or once, at `volume(frame)` */
21
+ export type SoundPlan = {src: string; from: number; durationInFrames: number; trimBefore: number; sourceFrames: number | null; loop: boolean; volume(frame: number): number};
22
+ export declare function soundPlan(sound: import('@motionhub/runtime').FilmSound, fps: number): SoundPlan;
23
+ /** Every <Sound> of the <Film> a component draws, as soundPlan gives it */
24
+ export declare function filmSounds<P extends object>(Component: FC<P>, film?: FilmOf<P>): SoundPlan[];
25
+ /** The film's sounds on Remotion's: place it once, outside the Series of scenes */
26
+ export declare const FilmAudio: FC<{component: FC<any>; film?: FilmOf<any>}>;
10
27
  export declare const MotionFonts: FC<{han?: boolean}>;
11
28
  export declare function families(tokens: Tokens | undefined): string[];
12
29
  export declare function useMotion(): {
package/index.js CHANGED
@@ -1,12 +1,15 @@
1
1
  // @motionhub/remotion: a motion in Remotion. <MotionProvider> gives a motion's React components the runtime they draw with
2
2
  // (@motionhub/runtime) from Remotion's own frame, so they run unchanged; <MotionFonts> loads its faces; useMotion() turns its
3
3
  // tokens into Remotion values (frames, Easing) for anything you draw yourself. For a film of scenes (@motionhub/runtime's <Film>),
4
- // sceneFrames() and filmMetadata() give each scene and the composition their length in frames, from the scenes.
5
- import {createElement, useEffect, useMemo, useState} from 'react';
6
- import {Easing, OffthreadVideo, Sequence, continueRender, delayRender, interpolate, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
4
+ // sceneFrames() and filmMetadata() give each scene and the composition their length in frames, from the scenes, and <FilmAudio>
5
+ // plays its sounds (<Sound>, @motionhub/runtime 0.7).
6
+ import {Fragment, createElement, useEffect, useMemo, useState} from 'react';
7
+ import {Easing, Html5Audio, OffthreadVideo, Sequence, continueRender, delayRender, interpolate, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
7
8
  import {MotionRoot, MotionRuntimeProvider, resolveTokens, useTokens} from '@motionhub/runtime';
8
9
 
9
- export {filmMetadata, sceneFrames} from './film.js';
10
+ export {filmMetadata, filmSounds, sceneFrames, soundPlan} from './film.js';
11
+ import {filmSounds} from './film.js';
12
+ export {PARAM_TYPES, paramChoices, paramDefaults, paramsSchema} from './params.js';
10
13
 
11
14
  const sequenceSeconds = () => useCurrentFrame() / useVideoConfig().fps;
12
15
 
@@ -113,3 +116,25 @@ export function useMotion() {
113
116
  const progress = (frame, start, duration, easing) => interpolate(frame, [start, start + frames(duration)], [0, 1], {easing: easing ? ease(easing) : undefined, extrapolateLeft: 'clamp', extrapolateRight: 'clamp'});
114
117
  return {tokens, fps, frames, ease, progress};
115
118
  }
119
+
120
+ /** A film that says nothing of its frame, the same each render, so FilmAudio reads the film's sounds once */
121
+ const NO_FILM = Object.freeze({});
122
+
123
+ /**
124
+ * The sounds of the <Film> a component draws (@motionhub/runtime 0.7's <Sound>), on Remotion's: each in a Sequence from its start, an
125
+ * <Html5Audio> of its file (a URL, or a path in public/) trimmed, looped and faded as the film says. Place it once in the composition,
126
+ * outside the Series of scenes, so its frames are the film's: `<FilmAudio component={Main} film={film} />`.
127
+ */
128
+ export const FilmAudio = ({component, film = NO_FILM}) => {
129
+ const plans = useMemo(() => filmSounds(component, film), [component, film]);
130
+ return createElement(Fragment, null, plans.map((plan, i) =>
131
+ createElement(Sequence, {key: i, from: plan.from, durationInFrames: plan.durationInFrames, layout: 'none', name: plan.src},
132
+ createElement(Html5Audio, {
133
+ src: /^[a-z][a-z0-9+.-]*:/i.test(plan.src) ? plan.src : staticFile(plan.src),
134
+ trimBefore: plan.trimBefore,
135
+ ...(plan.sourceFrames !== null ? {durationInFrames: plan.sourceFrames} : {}),
136
+ loop: plan.loop,
137
+ loopVolumeCurveBehavior: 'extend',
138
+ volume: plan.volume,
139
+ }))));
140
+ };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@motionhub/remotion",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Use a motion from motionhub.dev in Remotion: its React components as they are, and its tokens as Remotion values.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/motionhub-dev/motions.git",
8
+ "url": "git+https://github.com/motionhub-dev/motionhub.git",
9
9
  "directory": "packages/remotion"
10
10
  },
11
11
  "homepage": "https://motionhub.dev",
@@ -21,14 +21,15 @@
21
21
  "files": [
22
22
  "index.js",
23
23
  "film.js",
24
+ "params.js",
24
25
  "index.d.ts",
25
26
  "README.md"
26
27
  ],
27
28
  "peerDependencies": {
28
- "@motionhub/runtime": "^0.6.0",
29
+ "@motionhub/runtime": "^0.6.0 || ^0.7.0",
29
30
  "react-dom": ">=18",
30
31
  "react": ">=18",
31
- "remotion": ">=4"
32
+ "remotion": ">=4.0.530"
32
33
  },
33
34
  "scripts": {
34
35
  "test": "node --test test.mjs"
@@ -36,6 +37,7 @@
36
37
  "devDependencies": {
37
38
  "@motionhub/runtime": "file:../runtime",
38
39
  "react": "19.1.1",
39
- "react-dom": "19.1.1"
40
+ "react-dom": "19.1.1",
41
+ "zod": "4.6.7"
40
42
  }
41
43
  }
package/params.js ADDED
@@ -0,0 +1,55 @@
1
+ // A component's params (SPEC.md §12, Parameters) as a Remotion composition's schema: each param whose type is in the vocabulary
2
+ // becomes a zod field, described by its doc, so Remotion Studio edits it; any other is left to code and kept as it is. zod is the
3
+ // project's own (pass `z`), since Remotion wants its version. No Remotion here, so it is tested on its own (test.mjs).
4
+
5
+ const TYPES = {
6
+ text: (z) => z.string(),
7
+ string: (z) => z.string(),
8
+ color: (z) => z.string(),
9
+ number: (z) => z.number(),
10
+ px: (z) => z.number(),
11
+ degrees: (z) => z.number(),
12
+ progress: (z) => z.number().min(0).max(1),
13
+ boolean: (z) => z.boolean(),
14
+ '[x, y]': (z) => z.tuple([z.number(), z.number()]),
15
+ 'text[]': (z) => z.array(z.string()),
16
+ };
17
+
18
+ /** The types of the vocabulary a field is made for */
19
+ export const PARAM_TYPES = Object.keys(TYPES);
20
+
21
+ /** The choices of a type written `a | b | c` (words or numbers, none of them a type of the vocabulary or null); null for any other type */
22
+ export function paramChoices(type) {
23
+ const words = type.split(' | ');
24
+ if (words.length < 2 || !words.every((w) => /^(-?\d+(\.\d+)?|[a-z0-9-]+)$/.test(w))) return null;
25
+ return words.some((w) => w in TYPES || w === 'null') ? null : words;
26
+ }
27
+
28
+ /** The zod field for one param, or null when its type is outside the vocabulary */
29
+ function field(param, z) {
30
+ let f = TYPES[param.type]?.(z) ?? null;
31
+ const choices = f ? null : paramChoices(param.type);
32
+ if (choices) f = choices.every((c) => /^-?\d+(\.\d+)?$/.test(c)) ? z.union(choices.map((c) => z.literal(Number(c)))) : z.enum(choices);
33
+ if (!f) return null;
34
+ if (param.default === null) f = f.nullable();
35
+ else if (param.default === undefined) f = f.optional();
36
+ return f.describe(param.doc);
37
+ }
38
+
39
+ /**
40
+ * A composition's `schema` for a component, from its `meta`: `schema={paramsSchema(meta, z)}`. A param of a type outside the
41
+ * vocabulary is not in it, and passes through untouched; one with no default is optional, one whose default is null is nullable.
42
+ */
43
+ export function paramsSchema(meta, z) {
44
+ const shape = {};
45
+ for (const [name, param] of Object.entries(meta?.params ?? {})) {
46
+ const f = field(param, z);
47
+ if (f) shape[name] = f;
48
+ }
49
+ return z.looseObject ? z.looseObject(shape) : z.object(shape).passthrough();
50
+ }
51
+
52
+ /** A composition's `defaultProps` for a component, from its `meta`: every param's default, where it has one */
53
+ export function paramDefaults(meta) {
54
+ return Object.fromEntries(Object.entries(meta?.params ?? {}).filter(([, p]) => p.default !== undefined).map(([name, p]) => [name, p.default]));
55
+ }