@motionhub/remotion 0.4.0 → 0.6.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,29 @@ 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. `filmSounds(Main, film)` gives the same in frames, for code
84
+ of your own.
85
+
86
+ A narrated film (`<Film voice>`, `@motionhub/runtime` 0.8) gives each spoken line's speech by id, `{seconds, words, src}`: the engine
87
+ that spoke it knows them (Open Motion Studio keeps them in a project's `voice/`). Pass it to the provider, which times the lines by it, and
88
+ in the `film` that `filmMetadata` and `<FilmAudio>` read, so the scenes' lengths and the sound agree:
89
+
90
+ ```tsx
91
+ import voice from './voice.json'; // {"intro-1": {"seconds": 1.8, "words": [...], "src": "voice/9f86d081884c7d65.mp3"}}
92
+ const film = {fps: 30, width: 1920, height: 1080, voice};
93
+ // <MotionProvider tokens={{}} voice={voice}> … <FilmAudio component={Main} film={film} /> … </MotionProvider>
94
+ ```
95
+
96
+ `<FilmAudio>` then plays each voiced line's clip (`src`, a path in `public/`) from where the line is said until it ends, and a sound with
97
+ `duck` sinks that many dB under it, down over 0.12 s before a line and back over 0.5 s after it (`DUCK`, `ducked(spans, t)`), as Open
98
+ Motion Studio mixes it. It needs Remotion 4.0.530 or later, the first whose
99
+ `<Html5Audio>` takes `durationInFrames`.
100
+
78
101
  ## A component's parameters in Remotion Studio
79
102
 
80
103
  `paramsSchema(meta, z)` turns a component's `meta.params` into a composition's schema, so Remotion Studio's props editor edits
package/film.js CHANGED
@@ -4,16 +4,16 @@ import {createElement} from 'react';
4
4
  import {renderToStaticMarkup} from 'react-dom/server';
5
5
  import {MotionRuntimeProvider} from '@motionhub/runtime';
6
6
 
7
- /** The structure of the <Film> a component draws */
8
- function outlineOf(Component, {fps = 30, width = 1920, height = 1080, props = {}}) {
7
+ /** The structure of the <Film> a component draws, its voiced lines timed by their speech */
8
+ function outlineOf(Component, {fps = 30, width = 1920, height = 1080, props = {}, voice}) {
9
9
  let outline = null;
10
- const value = {useTime: () => 0, useCompositionTime: () => 0, fps, width, height, tokens: {}, Video: () => null, onFilm: (o) => { outline = o; }};
10
+ const value = {useTime: () => 0, useCompositionTime: () => 0, fps, width, height, tokens: {}, Video: () => null, onFilm: (o) => { outline = o; }, ...(voice ? {voice} : {})};
11
11
  try {
12
12
  renderToStaticMarkup(createElement(MotionRuntimeProvider, {value}, createElement(Component, 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,69 @@ 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
+ /** How a sound with `duck` sinks under the narration: down over `attack` s before a line is said, back over `release` s after it */
84
+ export const DUCK = {attack: 0.12, release: 0.5};
85
+
86
+ /** How far a ducking sound is down at `t`, the film's seconds, from 0 to 1 (its whole `duck`), by the narration's spans */
87
+ export function ducked(spans, t) {
88
+ let level = 0;
89
+ for (const {at, until} of spans) {
90
+ const rise = Math.min(1, Math.max(0, (t - (at - DUCK.attack)) / DUCK.attack));
91
+ const fall = Math.min(1, Math.max(0, (until + DUCK.release - t) / DUCK.release));
92
+ level = Math.max(level, Math.min(rise, fall));
93
+ }
94
+ return level;
95
+ }
96
+
97
+ /**
98
+ * Every <Sound> of the <Film> a component draws, the film's own and its scenes', as soundPlan gives it; then, with `film.voice` (each line's
99
+ * speech by id, `{seconds, words, src}`), each voiced line's clip from where it is said until it ends, and every sound with `duck` sunk under
100
+ * them by that many dB.
101
+ */
102
+ export function filmSounds(Component, film = {}) {
103
+ const fps = film.fps ?? 30;
104
+ const outline = outlineOf(Component, film);
105
+ const spoken = outline.scenes.flatMap((scene) => scene.lines ?? []).filter((line) => line.voiced && film.voice?.[line.id]?.src);
106
+ const spans = spoken.map((line) => ({at: line.start, until: line.end}));
107
+ const sounds = [...(outline.sounds ?? []), ...outline.scenes.flatMap((scene) => scene.sounds ?? [])].map((sound) => {
108
+ const plan = soundPlan(sound, fps);
109
+ if (!sound.duck || !spans.length) return plan;
110
+ const floor = 10 ** (-sound.duck / 20);
111
+ const level = plan.volume;
112
+ return {...plan, volume: (frame) => level(frame) * (1 - (1 - floor) * ducked(spans, (plan.from + frame) / fps))};
113
+ });
114
+ const speech = spoken.map((line) => soundPlan({src: film.voice[line.id].src, at: line.start, dur: null, until: line.end, trim: 0, loop: false, volume: 0, fadeIn: 0, fadeOut: 0}, fps));
115
+ return [...sounds, ...speech];
116
+ }
package/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import type {FC, ReactNode} from 'react';
2
2
  import type {Tokens} from '@motionhub/runtime';
3
3
 
4
- export declare const MotionProvider: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
4
+ /** A voiced line's speech: its length, its words' times into it, and, for <FilmAudio>, its clip (a URL, or a path in public/) */
5
+ export type LineSpeech = {seconds: number; words: {text: string; start: number; end: number}[]; src?: string};
6
+ export declare const MotionProvider: FC<{tokens: unknown; root?: boolean; voice?: Record<string, LineSpeech>; children?: ReactNode}>;
5
7
  /** A component's meta, as SPEC.md §12 has it */
6
8
  type MetaOf = {params?: Record<string, {type: string; doc: string; default?: unknown}>};
7
9
  /** The types of the vocabulary (SPEC.md §12, Parameters) a schema field is made for */
@@ -12,11 +14,23 @@ export declare function paramChoices(type: string): string[] | null;
12
14
  export declare function paramsSchema<Z>(meta: MetaOf, z: Z): any;
13
15
  /** A composition's defaultProps for a component: every param's default, where it has one */
14
16
  export declare function paramDefaults(meta: MetaOf): Record<string, unknown>;
15
- type FilmOf<P> = {fps?: number; width?: number; height?: number; props?: P};
17
+ /** The film as drawn: its frame, the component's props, and each line's speech by id when it is narrated (<Film voice>) */
18
+ type FilmOf<P> = {fps?: number; width?: number; height?: number; props?: P; voice?: Record<string, LineSpeech>};
16
19
  /** Each scene of the <Film> a component draws, by id, in whole frames that meet end to end */
17
20
  export declare function sceneFrames<P extends object>(Component: FC<P>, film?: FilmOf<P>): Record<string, number>;
18
21
  /** 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 */
19
22
  export declare function filmMetadata<P extends object>(Component: FC<P>, film?: FilmOf<P>, ids?: string[]): {durationInFrames: number; fps: number; width: number; height: number};
23
+ /** 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)` */
24
+ export type SoundPlan = {src: string; from: number; durationInFrames: number; trimBefore: number; sourceFrames: number | null; loop: boolean; volume(frame: number): number};
25
+ export declare function soundPlan(sound: import('@motionhub/runtime').FilmSound, fps: number): SoundPlan;
26
+ /** How a sound with `duck` sinks under the narration, in seconds */
27
+ export declare const DUCK: {attack: number; release: number};
28
+ /** How far a ducking sound is down at `t` (the film's seconds), 0 to 1, by the narration's spans */
29
+ export declare function ducked(spans: {at: number; until: number}[], t: number): number;
30
+ /** Every <Sound> of the <Film> a component draws, as soundPlan gives it; with `film.voice`, the voiced lines' clips too, and `duck` applied */
31
+ export declare function filmSounds<P extends object>(Component: FC<P>, film?: FilmOf<P>): SoundPlan[];
32
+ /** The film's sounds on Remotion's: place it once, outside the Series of scenes */
33
+ export declare const FilmAudio: FC<{component: FC<any>; film?: FilmOf<any>}>;
20
34
  export declare const MotionFonts: FC<{han?: boolean}>;
21
35
  export declare function families(tokens: Tokens | undefined): string[];
22
36
  export declare function useMotion(): {
package/index.js CHANGED
@@ -1,12 +1,14 @@
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) and its narration (<Film voice>, 0.8).
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 {DUCK, ducked, filmMetadata, filmSounds, sceneFrames, soundPlan} from './film.js';
11
+ import {filmSounds} from './film.js';
10
12
  export {PARAM_TYPES, paramChoices, paramDefaults, paramsSchema} from './params.js';
11
13
 
12
14
  const sequenceSeconds = () => useCurrentFrame() / useVideoConfig().fps;
@@ -22,13 +24,14 @@ const RemotionVideo = ({src, startAt = 0, trimBefore = 0, playbackRate = 1, obje
22
24
  /**
23
25
  * Wrap the composition in it once, outside every <Sequence>, with the motion's tokens.json: `import tokens from '../motion/tokens.json'`. Its components
24
26
  * then draw by Remotion's frame: useTime() inside a <Sequence> counts from its start. `root={false}` leaves out the full-frame root that sets the
25
- * tokens' custom properties (--color-*, --font-*).
27
+ * tokens' custom properties (--color-*, --font-*). `voice`, each line's speech by id (`{seconds, words}`), times a <Film voice>'s lines by it, as
28
+ * the film given to filmMetadata and <FilmAudio> must.
26
29
  */
27
- export const MotionProvider = ({tokens, root = true, children}) => {
30
+ export const MotionProvider = ({tokens, root = true, voice, children}) => {
28
31
  const frame = useCurrentFrame();
29
32
  const {fps, width, height} = useVideoConfig();
30
33
  const resolved = useMemo(() => resolveTokens(tokens), [tokens]);
31
- const value = {useTime: sequenceSeconds, useCompositionTime: () => frame / fps, fps, width, height, tokens: resolved, Video: RemotionVideo};
34
+ const value = {useTime: sequenceSeconds, useCompositionTime: () => frame / fps, fps, width, height, tokens: resolved, Video: RemotionVideo, ...(voice ? {voice} : {})};
32
35
  return createElement(MotionRuntimeProvider, {value}, root ? createElement(MotionRoot, {tokens: resolved}, children) : children);
33
36
  };
34
37
 
@@ -114,3 +117,26 @@ export function useMotion() {
114
117
  const progress = (frame, start, duration, easing) => interpolate(frame, [start, start + frames(duration)], [0, 1], {easing: easing ? ease(easing) : undefined, extrapolateLeft: 'clamp', extrapolateRight: 'clamp'});
115
118
  return {tokens, fps, frames, ease, progress};
116
119
  }
120
+
121
+ /** A film that says nothing of its frame, the same each render, so FilmAudio reads the film's sounds once */
122
+ const NO_FILM = Object.freeze({});
123
+
124
+ /**
125
+ * 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
126
+ * <Html5Audio> of its file (a URL, or a path in public/) trimmed, looped and faded as the film says; with `film.voice`, its narration too,
127
+ * the sounds with `duck` sunk under it. Place it once in the composition, outside the Series of scenes, so its frames are the film's:
128
+ * `<FilmAudio component={Main} film={film} />`.
129
+ */
130
+ export const FilmAudio = ({component, film = NO_FILM}) => {
131
+ const plans = useMemo(() => filmSounds(component, film), [component, film]);
132
+ return createElement(Fragment, null, plans.map((plan, i) =>
133
+ createElement(Sequence, {key: i, from: plan.from, durationInFrames: plan.durationInFrames, layout: 'none', name: plan.src},
134
+ createElement(Html5Audio, {
135
+ src: /^[a-z][a-z0-9+.-]*:/i.test(plan.src) ? plan.src : staticFile(plan.src),
136
+ trimBefore: plan.trimBefore,
137
+ ...(plan.sourceFrames !== null ? {durationInFrames: plan.sourceFrames} : {}),
138
+ loop: plan.loop,
139
+ loopVolumeCurveBehavior: 'extend',
140
+ volume: plan.volume,
141
+ }))));
142
+ };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@motionhub/remotion",
3
- "version": "0.4.0",
3
+ "version": "0.6.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",
@@ -26,10 +26,10 @@
26
26
  "README.md"
27
27
  ],
28
28
  "peerDependencies": {
29
- "@motionhub/runtime": "^0.6.0",
29
+ "@motionhub/runtime": "^0.6.0 || ^0.7.0 || ^0.8.0",
30
30
  "react-dom": ">=18",
31
31
  "react": ">=18",
32
- "remotion": ">=4"
32
+ "remotion": ">=4.0.530"
33
33
  },
34
34
  "scripts": {
35
35
  "test": "node --test test.mjs"