Skip to content

🎥 Make videos programmatically with React

start writing docs

Jonny Burgerc5381b95480d
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 14f39529fc..f50feafb35 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -17,7 +17,10 @@ import {Loading} from './loading-indicator.js';
import {NativeLayersContext} from './NativeLayers.js';
import {useNonce} from './nonce.js';
import {portalNode} from './portal-node.js';
-import type {PropsIfHasProps} from './props-if-has-props.js';
+import type {
+ PropsIfHasProps,
+ RenamePropsIfHasProps,
+} from './props-if-has-props.js';
import {useLazyComponent} from './use-lazy-component.js';
import {useVideo} from './use-video.js';
import {validateCompositionId} from './validation/validate-composition-id.js';
@@ -35,21 +38,23 @@ export type CompProps<Props> =
component: LooseComponentType<Props>;
};
-export type CalculateMetadataFunction = (options: {
- defaultProps: unknown;
+export type CalculateMetadataFunction<T = unknown> = (options: {
+ defaultProps: T;
}) => Promise<{
durationInFrames: number;
fps: number;
width: number;
height: number;
- props: unknown;
+ props: T;
}>;
export type StillProps<Schema extends z.ZodTypeAny, Props> = {
width: number;
height: number;
id: string;
- calculateMetadata?: CalculateMetadataFunction;
+ calculateMetadata?: CalculateMetadataFunction<
+ RenamePropsIfHasProps<Schema, Props>
+ >;
schema?: Schema;
} & CompProps<Props> &
PropsIfHasProps<Schema, Props>;
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index 71169cf267..a3f05d8c91 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -13,7 +13,10 @@ import type {z} from 'zod';
import {SharedAudioContextProvider} from './audio/shared-audio-tags.js';
import type {CalculateMetadataFunction} from './Composition.js';
import type {TFolder} from './Folder.js';
-import type {PropsIfHasProps} from './props-if-has-props.js';
+import type {
+ PropsIfHasProps,
+ RenamePropsIfHasProps,
+} from './props-if-has-props.js';
import {resolveVideoConfig} from './resolve-video-config.js';
import type {VideoConfig} from './video-config.js';
@@ -28,7 +31,9 @@ export type TComposition<Schema extends z.ZodTypeAny, Props> = {
component: LazyExoticComponent<ComponentType<Props>>;
nonce: number;
schema: Schema | null;
- calculateMetadata: CalculateMetadataFunction | null;
+ calculateMetadata: CalculateMetadataFunction<
+ RenamePropsIfHasProps<Schema, Props>
+ > | null;
} & PropsIfHasProps<Schema, Props>;
export type AnyComposition = TComposition<z.ZodTypeAny, unknown>;
@@ -211,7 +216,11 @@ export const CompositionManagerProvider: React.FC<{
);
}
- return [...comps, comp].slice().sort((a, b) => a.nonce - b.nonce);
+ const value = [...comps, comp]
+ .slice()
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ .sort((a, b) => a.nonce - b.nonce) as TComposition<any, any>[];
+ return value;
});
},
[updateCompositions]
diff --git a/packages/core/src/props-if-has-props.ts b/packages/core/src/props-if-has-props.ts
index af9a982fce..83cf17e35a 100644
--- a/packages/core/src/props-if-has-props.ts
+++ b/packages/core/src/props-if-has-props.ts
@@ -3,22 +3,25 @@ import type {z} from 'zod';
export type PropsIfHasProps<
Schema extends z.ZodTypeAny,
Props
-> = z.ZodTypeAny extends Schema
- ? {} extends Props
- ? {
- // Neither props nor schema specified
- defaultProps?: Props;
- }
- : {
- // Only props specified
- defaultProps: Props;
- }
- : {} extends Props
+> = unknown extends RenamePropsIfHasProps<Schema, Props>
? {
- // Only schema specified
- defaultProps: z.infer<Schema>;
+ defaultProps?: RenamePropsIfHasProps<Schema, Props>;
}
: {
- // Props and schema specified
- defaultProps: z.infer<Schema> & Props;
+ defaultProps: RenamePropsIfHasProps<Schema, Props>;
};
+
+export type RenamePropsIfHasProps<
+ Schema extends z.ZodTypeAny,
+ Props
+> = z.ZodTypeAny extends Schema
+ ? {} extends Props
+ ? // Neither props nor schema specified
+ unknown
+ : // Only props specified
+ Props
+ : {} extends Props
+ ? // Only schema specified
+ z.infer<Schema>
+ : // Props and schema specified
+ z.infer<Schema> & Props;
diff --git a/packages/docs/docs/composition.md b/packages/docs/docs/composition.md
index 3ada6a4fd3..d5e48579b8 100644
--- a/packages/docs/docs/composition.md
+++ b/packages/docs/docs/composition.md
@@ -79,9 +79,12 @@ Type your components using the `React.FC<{}>` type and the `defaultProps` prop w
:::note
Passing huge objects to `defaultProps` can be slow. [Learn how to avoid it.](/docs/troubleshooting/defaultprops-too-big)
-
:::
+### `calculateMetadata()`
+
+TODO
+
## Example using `component`
```tsx twoslash
diff --git a/packages/docs/docs/dynamic-metadata-v2.md b/packages/docs/docs/dynamic-metadata-v2.md
new file mode 100644
index 0000000000..1cc9c44234
--- /dev/null
+++ b/packages/docs/docs/dynamic-metadata-v2.md
@@ -0,0 +1,37 @@
+---
+id: dynamic-metadata-v2
+title: Dynamic duration, FPS & dimensions
+crumb: "How To"
+---
+
+To dynamically set the `width`, `height`, `durationInFrames` or `fps` of a video, you can use the `calculateMetadata()` option of a [`<Composition>`](/docs/composition).
+
+```tsx twoslash title="Root.tsx"
+import React from "react";
+const MyComp: React.FC = () => null;
+
+import { Composition } from "remotion";
+
+// ---cut---
+<Composition
+ id="my-comp"
+ component={MyComp}
+ durationInFrames={100}
+ fps={30}
+ width={1080}
+ height={1080}
+ calculateMetadata={async ({ defaultProps }) => {
+ return {
+ fps: 30,
+ width: 1080,
+ height: 1080,
+ durationInFrames: 100,
+ props: defaultProps,
+ };
+ }}
+/>;
+```
+
+## How props flow
+
+## In a `<Player>`
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index cd7a4bd15a..348351dadf 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -380,6 +380,7 @@ module.exports = {
"encoding",
"transparent-videos",
"dynamic-metadata",
+ "dynamic-metadata-v2",
"ssr",
"webpack",
"tailwind",
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 15b2359e7f..63cc203d03 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -818,6 +818,14 @@ export const Index: React.FC = () => {
filePath: staticFile('nested/logö.png'),
}}
durationInFrames={150}
+ calculateMetadata={async ({defaultProps}) => {
+ return {
+ durationInFrames: defaultProps.mynum * 10,
+ props: {
+ ...defaultProps,
+ },
+ };
+ }}
/>
</Folder>
<Folder name="Schema">