Skip to content

🎥 Make videos programmatically with React

static compositions

Jonny Burger4934f574c1e7
diff --git a/packages/bundler/src/homepage/homepage.tsx b/packages/bundler/src/homepage/homepage.tsx
index 4361637ff0..a8b3a57ebf 100644
--- a/packages/bundler/src/homepage/homepage.tsx
+++ b/packages/bundler/src/homepage/homepage.tsx
@@ -34,8 +34,9 @@ const AvailableCompositions: React.FC = () => {
let timeout: NodeJS.Timeout | null = null;
const check = () => {
if (window.ready === true) {
- const newComps = window.getStaticCompositions();
- setComps(newComps);
+ window.getStaticCompositions().then((newComps) => {
+ setComps(newComps);
+ });
} else {
timeout = setTimeout(check, 250);
}
diff --git a/packages/bundler/src/renderEntry.tsx b/packages/bundler/src/renderEntry.tsx
index 05704746b8..16e053d7f0 100644
--- a/packages/bundler/src/renderEntry.tsx
+++ b/packages/bundler/src/renderEntry.tsx
@@ -226,7 +226,7 @@ export const setBundleModeAndUpdate = (state: BundleState) => {
};
if (typeof window !== 'undefined') {
- window.getStaticCompositions = (): AnyCompMetadata[] => {
+ window.getStaticCompositions = (): Promise<AnyCompMetadata[]> => {
if (!Internals.getRoot()) {
throw new Error(
'registerRoot() was never called. 1. Make sure you specified the correct entrypoint for your bundle. 2. If your registerRoot() call is deferred, use the delayRender/continueRender pattern to tell Remotion to wait.'
@@ -261,16 +261,11 @@ if (typeof window !== 'undefined') {
);
}
- return compositions.map((c): AnyCompMetadata => {
- return {
- defaultProps: c.defaultProps,
- durationInFrames: c.durationInFrames,
- fps: c.fps,
- height: c.height,
- id: c.id,
- width: c.width,
- };
- });
+ return Promise.all(
+ compositions.map((c): Promise<AnyCompMetadata> => {
+ return Internals.resolveVideoConfig(c);
+ })
+ );
};
window.calculateComposition = (compId: string) => {
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 8f7b3286fa..14f39529fc 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -35,18 +35,21 @@ export type CompProps<Props> =
component: LooseComponentType<Props>;
};
-export type CalculateMetadataFunction = (defaultProps: unknown) => Promise<{
+export type CalculateMetadataFunction = (options: {
+ defaultProps: unknown;
+}) => Promise<{
durationInFrames: number;
fps: number;
width: number;
height: number;
+ props: unknown;
}>;
export type StillProps<Schema extends z.ZodTypeAny, Props> = {
width: number;
height: number;
id: string;
- calculateStuff?: CalculateMetadataFunction;
+ calculateMetadata?: CalculateMetadataFunction;
schema?: Schema;
} & CompProps<Props> &
PropsIfHasProps<Schema, Props>;
@@ -137,7 +140,7 @@ export const Composition = <Schema extends z.ZodTypeAny, Props>({
nonce,
parentFolderName: parentName,
schema: schema ?? null,
- calculateMetadata: compProps.calculateStuff ?? null,
+ calculateMetadata: compProps.calculateMetadata ?? null,
});
return () => {
@@ -157,7 +160,7 @@ export const Composition = <Schema extends z.ZodTypeAny, Props>({
nonce,
parentName,
schema,
- compProps.calculateStuff,
+ compProps.calculateMetadata,
]);
const editorPropsOrUndefined = allEditorProps[id] ?? {};
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 73c5cc9d29..b793107ac2 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -12,7 +12,7 @@ declare global {
interface Window {
ready: boolean;
remotion_cancelledError: string | undefined;
- getStaticCompositions: () => AnyCompMetadata[];
+ getStaticCompositions: () => Promise<AnyCompMetadata[]>;
calculateComposition: (compId: string) => Promise<VideoConfig>;
setBundleMode: (bundleMode: BundleState) => void;
remotion_staticBase: string;
diff --git a/packages/core/src/resolve-video-config.ts b/packages/core/src/resolve-video-config.ts
index 45ce3c18a7..263a01086e 100644
--- a/packages/core/src/resolve-video-config.ts
+++ b/packages/core/src/resolve-video-config.ts
@@ -8,7 +8,7 @@ export const resolveVideoConfig = async (
comp: TCompMetadataWithCalcFunction<ZodTypeAny, unknown>
): Promise<VideoConfig> => {
const calculated = comp.calculateMetadata
- ? await comp.calculateMetadata(comp.defaultProps)
+ ? await comp.calculateMetadata({defaultProps: comp.defaultProps})
: null;
const width = calculated?.width ?? comp.width ?? null;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index aa8ed07fc4..15b2359e7f 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -1,7 +1,8 @@
+import {CalculateMetadataFunction, random} from 'remotion';
import {zColor} from '@remotion/zod-types';
import './style.css';
import {alias} from 'lib/alias';
-import React from 'react';
+import React, {useCallback} from 'react';
import {Composition, Folder, getInputProps, staticFile, Still} from 'remotion';
import {z} from 'zod';
import {TwentyTwoKHzAudio} from './22KhzAudio';
@@ -55,8 +56,36 @@ if (alias !== 'alias') {
export const Index: React.FC = () => {
const inputProps = getInputProps();
+
+ const calculateMetadata: CalculateMetadataFunction = useCallback(
+ async ({defaultProps}) => {
+ await new Promise((r) => {
+ setTimeout(r, 1000);
+ });
+ return {
+ durationInFrames: random(null) * 100,
+ fps: 30,
+ height: Math.round(random(null) * 2 * 500),
+ width: Math.round(random(null) * 2 * 500),
+ props: defaultProps,
+ };
+ },
+ []
+ );
+
return (
<>
+ <Folder name="dynamic-parameters">
+ <Composition
+ id="dynamic-length"
+ component={Tailwind}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={100}
+ calculateMetadata={calculateMetadata}
+ />
+ </Folder>
<Folder name="components">
<Composition
id="looped"
@@ -824,24 +853,6 @@ export const Index: React.FC = () => {
durationInFrames={60}
/>
</Folder>
- <Folder name="dynamic-parameters">
- <Composition
- id="dynamic-length"
- component={Tailwind}
- width={1080}
- height={1080}
- fps={30}
- durationInFrames={100}
- calculateStuff={async () => {
- return {
- durationInFrames: Math.round(Math.random() * 100),
- fps: 30,
- height: Math.round(Math.random() * 2 * 500),
- width: Math.round(Math.random() * 2 * 500),
- };
- }}
- />
- </Folder>
</>
);
};
diff --git a/packages/player/src/SharedPlayerContext.tsx b/packages/player/src/SharedPlayerContext.tsx
index 025da72951..f129c66325 100644
--- a/packages/player/src/SharedPlayerContext.tsx
+++ b/packages/player/src/SharedPlayerContext.tsx
@@ -53,6 +53,7 @@ export const SharedPlayerContexts: React.FC<{
defaultProps: undefined,
parentFolderName: null,
schema: null,
+ calculateMetadata: null,
},
],
folders: [],
@@ -71,6 +72,7 @@ export const SharedPlayerContexts: React.FC<{
setCurrentCompositionMetadata: () => undefined,
assets: [],
setClipRegion: () => undefined,
+ resolved: null,
};
}, [
component,
diff --git a/packages/renderer/src/puppeteer-evaluate.ts b/packages/renderer/src/puppeteer-evaluate.ts
index 107e0d355b..4d80426f62 100644
--- a/packages/renderer/src/puppeteer-evaluate.ts
+++ b/packages/renderer/src/puppeteer-evaluate.ts
@@ -155,7 +155,7 @@ export async function puppeteerEvaluateWithCatch<ReturnType>({
return valueFromRemoteObject(remoteObject);
} catch (error) {
if (
- (error as {originalMessage: string})?.originalMessage.startsWith(
+ (error as {originalMessage: string})?.originalMessage?.startsWith(
"Object couldn't be returned by value"
)
) {
diff --git a/packages/renderer/src/test/get-assets-for-markup.tsx b/packages/renderer/src/test/get-assets-for-markup.tsx
index de922fcba0..cfe6001265 100644
--- a/packages/renderer/src/test/get-assets-for-markup.tsx
+++ b/packages/renderer/src/test/get-assets-for-markup.tsx
@@ -90,6 +90,11 @@ export const getAssetsForMarkup = async (
folderName: null,
parentFolderName: null,
schema: null,
+ calculateMetadata: null,
+ durationInFrames: config.durationInFrames,
+ fps: config.fps,
+ height: config.height,
+ width: config.width,
},
],
currentComposition: 'markup',