Skip to content

🎥 Make videos programmatically with React

`@remotion/web-renderer`: Correctly resolve props

JonnyBurger4e465df69c32
diff --git a/packages/core/src/ResolveCompositionConfig.tsx b/packages/core/src/ResolveCompositionConfig.tsx
index 012caf7fb3..a194a7b9ab 100644
--- a/packages/core/src/ResolveCompositionConfig.tsx
+++ b/packages/core/src/ResolveCompositionConfig.tsx
@@ -147,7 +147,7 @@ export const ResolveCompositionConfigInStudio: React.FC<
const result = resolveVideoConfigOrCatch({
compositionId,
calculateMetadata,
- originalProps: combinedProps,
+ inputProps: combinedProps,
signal,
defaultProps,
compositionDurationInFrames,
diff --git a/packages/core/src/resolve-video-config.ts b/packages/core/src/resolve-video-config.ts
index d5fc043c4f..e2fc474e71 100644
--- a/packages/core/src/resolve-video-config.ts
+++ b/packages/core/src/resolve-video-config.ts
@@ -94,14 +94,14 @@ type ResolveVideoConfigParams = {
> | null;
signal: AbortSignal;
defaultProps: Record<string, unknown>;
- originalProps: Record<string, unknown>;
+ inputProps: Record<string, unknown>;
};
export const resolveVideoConfig = ({
calculateMetadata,
signal,
defaultProps,
- originalProps,
+ inputProps: originalProps,
compositionId,
compositionDurationInFrames,
compositionFps,
diff --git a/packages/core/src/test/composition-validation.test.tsx b/packages/core/src/test/composition-validation.test.tsx
index 1bcad3c1e3..5ed37cb313 100644
--- a/packages/core/src/test/composition-validation.test.tsx
+++ b/packages/core/src/test/composition-validation.test.tsx
@@ -22,7 +22,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionHeight: 100,
compositionId: 'id',
compositionWidth: null,
- originalProps: {},
+ inputProps: {},
defaultProps: {},
signal: new AbortController().signal,
}),
@@ -42,7 +42,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
@@ -60,7 +60,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "height" prop of the "<Composition \/>" component with the id "id" must be positive, but got 0./,
@@ -95,7 +95,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "height" prop of the "<Composition \/>" component with the id "id" must be an integer, but is 100.01./,
@@ -114,7 +114,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: -100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "width" prop of the "<Composition \/>" component with the id "id" must be positive, but got -100./,
@@ -131,7 +131,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 0,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "width" prop of the "<Composition \/>" component with the id "id" must be positive, but got 0./,
@@ -168,7 +168,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "durationInFrames" prop of the "<Composition \/>" component with the id "id" must be positive, but got -100./,
@@ -185,7 +185,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "durationInFrames" prop of the "<Composition \/>" component with the id "id" must be positive, but got 0./,
@@ -202,7 +202,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/The "durationInFrames" prop of the "<Composition \/>" component with the id "id" must be an integer, but got 0.11./,
@@ -239,7 +239,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/"fps" must be positive, but got -30./,
@@ -256,7 +256,7 @@ describe('Composition-validation render should throw with invalid props', () =>
compositionId: 'id',
compositionWidth: 100,
defaultProps: {},
- originalProps: {},
+ inputProps: {},
signal: new AbortController().signal,
}),
/"fps" must be positive, but got 0./,
@@ -401,7 +401,7 @@ test('should resolve props correctly with no calculateMetadata()', async () => {
compositionHeight: 1080,
compositionId: 'test',
compositionWidth: 1920,
- originalProps: {
+ inputProps: {
a: 'b',
c: 'd',
},
diff --git a/packages/studio/src/components/RenderModal/WebRenderModal.tsx b/packages/studio/src/components/RenderModal/WebRenderModal.tsx
index af89358962..1cb006b477 100644
--- a/packages/studio/src/components/RenderModal/WebRenderModal.tsx
+++ b/packages/studio/src/components/RenderModal/WebRenderModal.tsx
@@ -334,6 +334,8 @@ const WebRenderModal: React.FC<WebRenderModalProps> = ({
height: resolvedComposition.height,
fps: resolvedComposition.fps,
durationInFrames: resolvedComposition.durationInFrames,
+ defaultProps: resolvedComposition.defaultProps,
+ calculateMetadata: unresolvedComposition.calculateMetadata,
},
frame,
imageFormat,
@@ -366,6 +368,8 @@ const WebRenderModal: React.FC<WebRenderModalProps> = ({
resolvedComposition.height,
resolvedComposition.fps,
outName,
+ resolvedComposition.defaultProps,
+ unresolvedComposition.calculateMetadata,
]);
const onRenderVideo = useCallback(async () => {
@@ -378,6 +382,7 @@ const WebRenderModal: React.FC<WebRenderModalProps> = ({
height: resolvedComposition.height,
fps: resolvedComposition.fps,
durationInFrames: resolvedComposition.durationInFrames,
+ defaultProps: resolvedComposition.defaultProps,
},
inputProps,
delayRenderTimeoutInMilliseconds: delayRenderTimeout,
@@ -435,6 +440,7 @@ const WebRenderModal: React.FC<WebRenderModalProps> = ({
resolvedComposition.fps,
outName,
transparent,
+ resolvedComposition.defaultProps,
]);
const onRender = useCallback(async () => {
diff --git a/packages/studio/src/renderEntry.tsx b/packages/studio/src/renderEntry.tsx
index 9785ea03ac..d86a6711f9 100644
--- a/packages/studio/src/renderEntry.tsx
+++ b/packages/studio/src/renderEntry.tsx
@@ -401,7 +401,7 @@ if (typeof window !== 'undefined') {
compositionHeight: c.height ?? null,
compositionWidth: c.width ?? null,
signal: new AbortController().signal,
- originalProps,
+ inputProps: originalProps,
defaultProps: c.defaultProps ?? {},
compositionId: c.id,
});
@@ -466,7 +466,7 @@ if (typeof window !== 'undefined') {
compositionFps: selectedComp.fps ?? null,
compositionHeight: selectedComp.height ?? null,
compositionWidth: selectedComp.width ?? null,
- originalProps,
+ inputProps: originalProps,
signal: abortController.signal,
defaultProps: selectedComp.defaultProps ?? {},
compositionId: selectedComp.id,
diff --git a/packages/web-renderer/src/create-scaffold.tsx b/packages/web-renderer/src/create-scaffold.tsx
index 0a39ac4682..6f85563d7c 100644
--- a/packages/web-renderer/src/create-scaffold.tsx
+++ b/packages/web-renderer/src/create-scaffold.tsx
@@ -13,7 +13,7 @@ export async function createScaffold<Props extends Record<string, unknown>>({
height,
delayRenderTimeoutInMilliseconds,
logLevel,
- inputProps,
+ resolvedProps,
id,
mediaCacheSizeInBytes,
durationInFrames,
@@ -30,7 +30,7 @@ export async function createScaffold<Props extends Record<string, unknown>>({
height: number;
delayRenderTimeoutInMilliseconds: number;
logLevel: LogLevel;
- inputProps: Record<string, unknown>;
+ resolvedProps: Record<string, unknown>;
id: string;
mediaCacheSizeInBytes: number | null;
initialFrame: number;
@@ -64,8 +64,6 @@ export async function createScaffold<Props extends Record<string, unknown>>({
document.body.appendChild(div);
- // TODO: calculateMetadata()
-
const {promise, resolve, reject} = withResolvers<void>();
// TODO: This might not work in React 18
@@ -82,8 +80,6 @@ export async function createScaffold<Props extends Record<string, unknown>>({
remotion_attempt: 0,
};
- const actualInputProps = inputProps ?? ({} as Props);
-
const timeUpdater = createRef<TimeUpdaterRef | null>();
flushSync(() => {
@@ -125,7 +121,7 @@ export async function createScaffold<Props extends Record<string, unknown>>({
compositionId: id,
},
currentCompositionMetadata: {
- props: inputProps ?? {},
+ props: resolvedProps,
durationInFrames,
fps,
height,
@@ -150,7 +146,7 @@ export async function createScaffold<Props extends Record<string, unknown>>({
<Internals.CanUseRemotionHooks value>
{/**
* @ts-expect-error */}
- <Component {...actualInputProps} />
+ <Component {...resolvedProps} />
</Internals.CanUseRemotionHooks>
</UpdateTime>
</Internals.CompositionManager.Provider>
@@ -160,6 +156,8 @@ export async function createScaffold<Props extends Record<string, unknown>>({
);
});
+ console.log({resolvedProps});
+
resolve();
await promise;
diff --git a/packages/web-renderer/src/props-if-has-props.ts b/packages/web-renderer/src/props-if-has-props.ts
index 6ac5f5985a..fa169c093d 100644
--- a/packages/web-renderer/src/props-if-has-props.ts
+++ b/packages/web-renderer/src/props-if-has-props.ts
@@ -17,27 +17,27 @@ export type InferProps<
: // Props and schema specified
z.input<Schema> & Props;
-export type PropsIfHasProps<
+export type DefaultPropsIfHasProps<
Schema extends AnyZodObject,
Props,
> = AnyZodObject extends Schema
? {} extends Props
? {
// Neither props nor schema specified
- inputProps?: z.input<Schema> & Props;
+ defaultProps?: z.input<Schema> & Props;
}
: {
// Only props specified
- inputProps: Props;
+ defaultProps: Props;
}
: {} extends Props
? {
// Only schema specified
- inputProps: z.input<Schema>;
+ defaultProps: z.input<Schema>;
}
: {
// Props and schema specified
- inputProps: z.input<Schema> & Props;
+ defaultProps: z.input<Schema> & Props;
};
type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
@@ -59,13 +59,15 @@ type MandatoryDimensions<
component: LooseComponentType<Props>;
width: number;
height: number;
- calculateMetadata?: CalculateMetadataFunction<InferProps<Schema, Props>>;
+ calculateMetadata?: CalculateMetadataFunction<
+ InferProps<Schema, Props>
+ > | null;
};
export type CompositionCalculateMetadataOrExplicit<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
-> =
+> = (
| (OptionalDimensions<Schema, Props> & {
fps?: number;
durationInFrames?: number;
@@ -73,4 +75,6 @@ export type CompositionCalculateMetadataOrExplicit<
| (MandatoryDimensions<Schema, Props> & {
fps: number;
durationInFrames: number;
- });
+ })
+) &
+ DefaultPropsIfHasProps<Schema, Props>;
diff --git a/packages/web-renderer/src/render-media-on-web.tsx b/packages/web-renderer/src/render-media-on-web.tsx
index b27c6c3c6d..097b3523d6 100644
--- a/packages/web-renderer/src/render-media-on-web.tsx
+++ b/packages/web-renderer/src/render-media-on-web.tsx
@@ -1,7 +1,7 @@
import {BufferTarget, Output, VideoSample, VideoSampleSource} from 'mediabunny';
import type {CalculateMetadataFunction} from 'remotion';
import {Internals, type LogLevel} from 'remotion';
-import type {AnyZodObject} from 'zod';
+import type {AnyZodObject, z} from 'zod';
import {createScaffold} from './create-scaffold';
import {getRealFrameRange, type FrameRange} from './frame-range';
import type {
@@ -18,17 +18,39 @@ import {
import type {
CompositionCalculateMetadataOrExplicit,
InferProps,
- PropsIfHasProps,
} from './props-if-has-props';
import {createFrame} from './take-screenshot';
import {waitForReady} from './wait-for-ready';
+export type InputPropsIfHasProps<
+ Schema extends AnyZodObject,
+ Props,
+> = AnyZodObject extends Schema
+ ? {} extends Props
+ ? {
+ // Neither props nor schema specified
+ inputProps?: z.input<Schema> & Props;
+ }
+ : {
+ // Only props specified
+ inputProps: Props;
+ }
+ : {} extends Props
+ ? {
+ // Only schema specified
+ inputProps: z.input<Schema>;
+ }
+ : {
+ // Props and schema specified
+ inputProps: z.input<Schema> & Props;
+ };
+
type MandatoryRenderMediaOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
composition: CompositionCalculateMetadataOrExplicit<Schema, Props>;
-} & PropsIfHasProps<Schema, Props>;
+};
export type RenderMediaOnWebProgress = {
renderedFrames: number;
@@ -61,13 +83,15 @@ export type RenderMediaOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = MandatoryRenderMediaOnWebOptions<Schema, Props> &
- Partial<OptionalRenderMediaOnWebOptions<Schema>>;
+ Partial<OptionalRenderMediaOnWebOptions<Schema>> &
+ InputPropsIfHasProps<Schema, Props>;
type InternalRenderMediaOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = MandatoryRenderMediaOnWebOptions<Schema, Props> &
- OptionalRenderMediaOnWebOptions<Schema>;
+ OptionalRenderMediaOnWebOptions<Schema> &
+ InputPropsIfHasProps<Schema, Props>;
// TODO: frameRange
// TODO: More containers
@@ -120,8 +144,8 @@ const internalRenderMediaOnWeb = async <
InferProps<AnyZodObject, Record<string, unknown>>
>) ?? null,
signal: signal ?? new AbortController().signal,
- defaultProps: {},
- originalProps: inputProps ?? {},
+ defaultProps: composition.defaultProps ?? {},
+ inputProps: inputProps ?? {},
compositionId: id ?? 'default',
compositionDurationInFrames: composition.durationInFrames ?? null,
compositionFps: composition.fps ?? null,
@@ -145,7 +169,7 @@ const internalRenderMediaOnWeb = async <
fps: resolved.fps,
durationInFrames: resolved.durationInFrames,
Component: composition.component,
- inputProps: inputProps ?? {},
+ resolvedProps: resolved.props,
id: id ?? 'default',
delayRenderTimeoutInMilliseconds,
logLevel,
diff --git a/packages/web-renderer/src/render-still-on-web.tsx b/packages/web-renderer/src/render-still-on-web.tsx
index 2d40c574c9..38703b894c 100644
--- a/packages/web-renderer/src/render-still-on-web.tsx
+++ b/packages/web-renderer/src/render-still-on-web.tsx
@@ -8,8 +8,8 @@ import {createScaffold} from './create-scaffold';
import type {
CompositionCalculateMetadataOrExplicit,
InferProps,
- PropsIfHasProps,
} from './props-if-has-props';
+import type {InputPropsIfHasProps} from './render-media-on-web';
import {takeScreenshot} from './take-screenshot';
import {waitForReady} from './wait-for-ready';
@@ -23,7 +23,7 @@ type MandatoryRenderStillOnWebOptions<
imageFormat: RenderStillOnWebImageFormat;
} & {
composition: CompositionCalculateMetadataOrExplicit<Schema, Props>;
-} & PropsIfHasProps<Schema, Props>;
+};
type OptionalRenderStillOnWebOptions<Schema extends AnyZodObject> = {
delayRenderTimeoutInMilliseconds: number;
@@ -38,13 +38,15 @@ type InternalRenderStillOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = MandatoryRenderStillOnWebOptions<Schema, Props> &
- OptionalRenderStillOnWebOptions<Schema>;
+ OptionalRenderStillOnWebOptions<Schema> &
+ InputPropsIfHasProps<Schema, Props>;
export type RenderStillOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = MandatoryRenderStillOnWebOptions<Schema, Props> &
- Partial<OptionalRenderStillOnWebOptions<Schema>>;
+ Partial<OptionalRenderStillOnWebOptions<Schema>> &
+ InputPropsIfHasProps<Schema, Props>;
async function internalRenderStillOnWeb<
Schema extends AnyZodObject,
@@ -67,8 +69,8 @@ async function internalRenderStillOnWeb<
InferProps<AnyZodObject, Record<string, unknown>>
>) ?? null,
signal: signal ?? new AbortController().signal,
- defaultProps: {},
- originalProps: inputProps ?? {},
+ defaultProps: composition.defaultProps ?? {},
+ inputProps: inputProps ?? {},
compositionId: id ?? 'default',
compositionDurationInFrames: composition.durationInFrames ?? null,
compositionFps: composition.fps ?? null,
@@ -85,7 +87,7 @@ async function internalRenderStillOnWeb<
height: resolved.height,
delayRenderTimeoutInMilliseconds,
logLevel,
- inputProps: inputProps ?? {},
+ resolvedProps: resolved.props,
id: id ?? 'default',
mediaCacheSizeInBytes,
audioEnabled: false,
diff --git a/packages/web-renderer/src/test/input-props.test.tsx b/packages/web-renderer/src/test/input-props.test.tsx
index de372aa058..b6aae64a3c 100644
--- a/packages/web-renderer/src/test/input-props.test.tsx
+++ b/packages/web-renderer/src/test/input-props.test.tsx
@@ -43,6 +43,7 @@ test('cannot call getInputProps() while rendering client-side', async () => {
height: 100,
fps: 30,
durationInFrames: 30,
+ defaultProps: {abc: 'abc'},
},
frame: 20,
inputProps: {abc: 'abc'},
@@ -59,6 +60,7 @@ test('cannot call getInputProps() while rendering client-side', async () => {
height: 100,
fps: 30,
durationInFrames: 30,
+ defaultProps: {abc: 'abc'},
},
frame: 20,
inputProps: {abc: 'abc'},
@@ -96,6 +98,7 @@ test('Should not be able to omit input props when component accepts props', () =
height: 100,
fps: 30,
durationInFrames: 30,
+ defaultProps: {abc: 'def'},
},
frame: 20,
imageFormat: 'png',
@@ -108,6 +111,7 @@ test('Should not be able to omit input props when component accepts props', () =
height: 100,
fps: 30,
durationInFrames: 30,
+ defaultProps: {abc: 'def'},
},
frame: 20,
inputProps: {
@@ -125,6 +129,7 @@ test('Should not be able to omit input props when component accepts props', () =
height: 100,
fps: 30,
durationInFrames: 30,
+ defaultProps: {abc: 'def'},
},
frame: 20,
inputProps: {