Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurgerfabaf91375f3
diff --git a/packages/studio/src/components/RenderModal/WebRenderModal.tsx b/packages/studio/src/components/RenderModal/WebRenderModal.tsx
index d9e4abaecb..c8eeeeaed6 100644
--- a/packages/studio/src/components/RenderModal/WebRenderModal.tsx
+++ b/packages/studio/src/components/RenderModal/WebRenderModal.tsx
@@ -267,17 +267,19 @@ const WebRenderModal: React.FC<WebRenderModalProps> = ({
const onRender = useCallback(async () => {
const blob = await renderStillOnWeb({
- component: unresolvedComposition.component,
+ composition: {
+ component: unresolvedComposition.component,
+ width: resolvedComposition.width,
+ height: resolvedComposition.height,
+ fps: resolvedComposition.fps,
+ durationInFrames: resolvedComposition.durationInFrames,
+ },
frame,
imageFormat,
- logLevel,
inputProps,
delayRenderTimeoutInMilliseconds: delayRenderTimeout,
mediaCacheSizeInBytes,
- durationInFrames: resolvedComposition.durationInFrames,
- width: resolvedComposition.width,
- height: resolvedComposition.height,
- fps: resolvedComposition.fps,
+ logLevel,
});
const url = URL.createObjectURL(blob);
diff --git a/packages/web-renderer/src/props-if-has-props.ts b/packages/web-renderer/src/props-if-has-props.ts
index 58c171c566..6ac5f5985a 100644
--- a/packages/web-renderer/src/props-if-has-props.ts
+++ b/packages/web-renderer/src/props-if-has-props.ts
@@ -1,3 +1,4 @@
+import type {ComponentType} from 'react';
import type {CalculateMetadataFunction} from 'remotion';
import type {AnyZodObject, z} from 'zod';
@@ -39,10 +40,13 @@ export type PropsIfHasProps<
inputProps: z.input<Schema> & Props;
};
+type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
+
type OptionalDimensions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
+ component: LooseComponentType<Props>;
width?: number;
height?: number;
calculateMetadata: CalculateMetadataFunction<InferProps<Schema, Props>>;
@@ -52,6 +56,7 @@ type MandatoryDimensions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
+ component: LooseComponentType<Props>;
width: number;
height: number;
calculateMetadata?: CalculateMetadataFunction<InferProps<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 0761032f8e..c3d7c8cc03 100644
--- a/packages/web-renderer/src/render-media-on-web.tsx
+++ b/packages/web-renderer/src/render-media-on-web.tsx
@@ -6,7 +6,6 @@ import {
VideoSample,
VideoSampleSource,
} from 'mediabunny';
-import type {ComponentType} from 'react';
import type {CalculateMetadataFunction} from 'remotion';
import {Internals, type LogLevel} from 'remotion';
import type {AnyZodObject} from 'zod';
@@ -19,15 +18,12 @@ import type {
import {createFrame} from './take-screenshot';
import {waitForReady} from './wait-for-ready';
-type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
-
type MandatoryRenderMediaOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
- component: LooseComponentType<Props>;
-} & CompositionCalculateMetadataOrExplicit<Schema, Props> &
- PropsIfHasProps<Schema, Props>;
+ composition: CompositionCalculateMetadataOrExplicit<Schema, Props>;
+} & PropsIfHasProps<Schema, Props>;
type OptionalRenderMediaOnWebOptions<Schema extends AnyZodObject> = {
delayRenderTimeoutInMilliseconds: number;
@@ -53,34 +49,29 @@ const internalRenderMediaOnWeb = async <
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
>({
- width,
- height,
- fps,
- durationInFrames,
- component: Component,
+ composition,
inputProps,
id,
delayRenderTimeoutInMilliseconds,
logLevel,
mediaCacheSizeInBytes,
schema,
- calculateMetadata,
}: InternalRenderMediaOnWebOptions<Schema, Props>) => {
const cleanupFns: (() => void)[] = [];
const resolved = await Internals.resolveVideoConfig({
calculateMetadata:
- (calculateMetadata as CalculateMetadataFunction<
+ (composition.calculateMetadata as CalculateMetadataFunction<
InferProps<AnyZodObject, Record<string, unknown>>
>) ?? null,
signal: new AbortController().signal,
defaultProps: {},
originalProps: inputProps ?? {},
compositionId: id ?? 'default',
- compositionDurationInFrames: durationInFrames ?? null,
- compositionFps: fps ?? null,
- compositionHeight: height ?? null,
- compositionWidth: width ?? null,
+ compositionDurationInFrames: composition.durationInFrames ?? null,
+ compositionFps: composition.fps ?? null,
+ compositionHeight: composition.height ?? null,
+ compositionWidth: composition.width ?? null,
});
const {delayRenderScope, div, cleanupScaffold, timeUpdater} =
@@ -89,7 +80,7 @@ const internalRenderMediaOnWeb = async <
height: resolved.height,
fps: resolved.fps,
durationInFrames: resolved.durationInFrames,
- Component,
+ Component: composition.component,
inputProps: inputProps ?? {},
id: id ?? 'default',
delayRenderTimeoutInMilliseconds,
@@ -117,6 +108,10 @@ const internalRenderMediaOnWeb = async <
});
cleanupFns.push(() => {
+ if (output.state === 'finalized' || output.state === 'canceled') {
+ return;
+ }
+
output.cancel();
});
diff --git a/packages/web-renderer/src/render-still-on-web.tsx b/packages/web-renderer/src/render-still-on-web.tsx
index 42bb4b556d..43d42572ea 100644
--- a/packages/web-renderer/src/render-still-on-web.tsx
+++ b/packages/web-renderer/src/render-still-on-web.tsx
@@ -1,4 +1,3 @@
-import {type ComponentType} from 'react';
import {
Internals,
type CalculateMetadataFunction,
@@ -14,19 +13,17 @@ import type {
import {takeScreenshot} from './take-screenshot';
import {waitForReady} from './wait-for-ready';
-type LooseComponentType<T> = ComponentType<T> | ((props: T) => React.ReactNode);
-
export type RenderStillOnWebImageFormat = 'png' | 'jpeg' | 'webp';
type MandatoryRenderStillOnWebOptions<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
> = {
- component: LooseComponentType<Props>;
frame: number;
imageFormat: RenderStillOnWebImageFormat;
-} & CompositionCalculateMetadataOrExplicit<Schema, Props> &
- PropsIfHasProps<Schema, Props>;
+} & {
+ composition: CompositionCalculateMetadataOrExplicit<Schema, Props>;
+} & PropsIfHasProps<Schema, Props>;
type OptionalRenderStillOnWebOptions<Schema extends AnyZodObject> = {
delayRenderTimeoutInMilliseconds: number;
@@ -52,11 +49,6 @@ async function internalRenderStillOnWeb<
Schema extends AnyZodObject,
Props extends Record<string, unknown>,
>({
- component: Component,
- width,
- height,
- fps,
- durationInFrames,
frame,
delayRenderTimeoutInMilliseconds,
logLevel,
@@ -65,21 +57,21 @@ async function internalRenderStillOnWeb<
schema,
imageFormat,
mediaCacheSizeInBytes,
- calculateMetadata,
+ composition,
}: InternalRenderStillOnWebOptions<Schema, Props>) {
const resolved = await Internals.resolveVideoConfig({
calculateMetadata:
- (calculateMetadata as CalculateMetadataFunction<
+ (composition.calculateMetadata as CalculateMetadataFunction<
InferProps<AnyZodObject, Record<string, unknown>>
>) ?? null,
signal: new AbortController().signal,
defaultProps: {},
originalProps: inputProps ?? {},
compositionId: id ?? 'default',
- compositionDurationInFrames: durationInFrames ?? null,
- compositionFps: fps ?? null,
- compositionHeight: height ?? null,
- compositionWidth: width ?? null,
+ compositionDurationInFrames: composition.durationInFrames ?? null,
+ compositionFps: composition.fps ?? null,
+ compositionHeight: composition.height ?? null,
+ compositionWidth: composition.width ?? null,
});
const {delayRenderScope, div, cleanupScaffold} = await createScaffold({
@@ -91,7 +83,7 @@ async function internalRenderStillOnWeb<
id: id ?? 'default',
mediaCacheSizeInBytes,
audioEnabled: false,
- Component,
+ Component: composition.component,
videoEnabled: true,
durationInFrames: resolved.durationInFrames,
fps: resolved.fps,
diff --git a/packages/web-renderer/src/test/cancel-render.test.tsx b/packages/web-renderer/src/test/cancel-render.test.tsx
index 66a91ca18b..1a53eb0a71 100644
--- a/packages/web-renderer/src/test/cancel-render.test.tsx
+++ b/packages/web-renderer/src/test/cancel-render.test.tsx
@@ -24,11 +24,13 @@ test('should be able to cancel render', async (t) => {
try {
await renderStillOnWeb({
- component: Component,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/delay-render.test.tsx b/packages/web-renderer/src/test/delay-render.test.tsx
index 983a75f767..6efda10e1b 100644
--- a/packages/web-renderer/src/test/delay-render.test.tsx
+++ b/packages/web-renderer/src/test/delay-render.test.tsx
@@ -25,11 +25,13 @@ test('should be able to use delayRender()', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/images.test.tsx b/packages/web-renderer/src/test/images.test.tsx
index 4ec1316c74..162b70f257 100644
--- a/packages/web-renderer/src/test/images.test.tsx
+++ b/packages/web-renderer/src/test/images.test.tsx
@@ -13,11 +13,14 @@ test('can display an image', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 1,
- fps: 25,
- width: 550,
- height: 368,
+ composition: {
+ component: Component,
+ width: 550,
+ height: 368,
+ fps: 25,
+ durationInFrames: 1,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/input-props.test.tsx b/packages/web-renderer/src/test/input-props.test.tsx
index 50a0516971..de372aa058 100644
--- a/packages/web-renderer/src/test/input-props.test.tsx
+++ b/packages/web-renderer/src/test/input-props.test.tsx
@@ -37,11 +37,13 @@ const HasProps: React.FC<{abc: 'def'}> = () => {
test('cannot call getInputProps() while rendering client-side', async () => {
await renderStillOnWeb({
- component: MustAcceptInputProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: MustAcceptInputProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {abc: 'abc'},
imageFormat: 'png',
@@ -51,11 +53,13 @@ test('cannot call getInputProps() while rendering client-side', async () => {
test('cannot call getInputProps() while rendering client-side', async () => {
await expect(() => {
return renderStillOnWeb({
- component: DisallowGetInputProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: DisallowGetInputProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {abc: 'abc'},
imageFormat: 'png',
@@ -70,11 +74,14 @@ const mockFn: MockSignature = () => Promise.resolve(new Blob());
test('Should be able to omit input props when component accepts no props', () => {
mockFn({
- component: HasNoProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: HasNoProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 20,
imageFormat: 'png',
});
@@ -83,21 +90,25 @@ test('Should be able to omit input props when component accepts no props', () =>
test('Should not be able to omit input props when component accepts props', () => {
// @ts-expect-error - inputProps is required
mockFn({
- component: HasProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
imageFormat: 'png',
});
mockFn({
- component: HasProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {
// @ts-expect-error - must match signature
@@ -108,11 +119,13 @@ test('Should not be able to omit input props when component accepts props', () =
// No error, how it should be :)
mockFn({
- component: HasProps,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: HasProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {
abc: 'def',
diff --git a/packages/web-renderer/src/test/player-false-positive.test.tsx b/packages/web-renderer/src/test/player-false-positive.test.tsx
index a611e24a59..e1608820e8 100644
--- a/packages/web-renderer/src/test/player-false-positive.test.tsx
+++ b/packages/web-renderer/src/test/player-false-positive.test.tsx
@@ -44,11 +44,14 @@ test('should not give a false positive when the player is also mounted', async (
});
await renderStillOnWeb({
- component: MustNotBePlayer,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: MustNotBePlayer,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 20,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/render-media.test.tsx b/packages/web-renderer/src/test/render-media.test.tsx
index 86a3cd17b4..e7719586d3 100644
--- a/packages/web-renderer/src/test/render-media.test.tsx
+++ b/packages/web-renderer/src/test/render-media.test.tsx
@@ -17,14 +17,17 @@ test('should render media on web', async () => {
);
};
- const file = await renderMediaOnWeb({
- component: Component,
- width: 400,
- height: 400,
- fps: 30,
- durationInFrames: 100,
+ await renderMediaOnWeb({
+ composition: {
+ component: Component,
+ width: 400,
+ height: 400,
+ fps: 30,
+ durationInFrames: 100,
+ },
inputProps: {},
});
+ /*
const url = URL.createObjectURL(
new File([file], 'test.mp4', {
type: 'video/mp4',
@@ -36,4 +39,5 @@ test('should render media on web', async () => {
videoTag.width = 400;
videoTag.height = 400;
document.body.appendChild(videoTag);
+*/
});
diff --git a/packages/web-renderer/src/test/render-still.test.tsx b/packages/web-renderer/src/test/render-still.test.tsx
index dad41d38b3..e512baceef 100644
--- a/packages/web-renderer/src/test/render-still.test.tsx
+++ b/packages/web-renderer/src/test/render-still.test.tsx
@@ -14,11 +14,14 @@ test('should render still on web', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -40,11 +43,13 @@ test('should be able to read frame number', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- width: 100,
- height: 100,
- fps: 30,
- durationInFrames: 30,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ },
frame: 20,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/transforms.test.tsx b/packages/web-renderer/src/test/transforms.test.tsx
index 5393edf896..481f3d56fb 100644
--- a/packages/web-renderer/src/test/transforms.test.tsx
+++ b/packages/web-renderer/src/test/transforms.test.tsx
@@ -21,11 +21,13 @@ test('should be able to deal with a simple transform directly on the element', a
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -46,11 +48,13 @@ test('should be able to deal with a simple transform on the parent', async () =>
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -76,11 +80,13 @@ test('should be able to deal with a transform-origin on itself', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -103,11 +109,14 @@ test('should be able to deal with a transform-origin on parent', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -132,11 +141,14 @@ test('accumulated transforms', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -178,11 +190,13 @@ test('transformed canvases', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -224,11 +238,13 @@ test('multi-level nested transforms with distinct transform-origins', async () =
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -268,11 +284,13 @@ test('three-level nested transforms with varying origins', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
@@ -306,11 +324,13 @@ test('nested transforms with pixel-based transform-origins', async () => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ },
frame: 0,
inputProps: {},
imageFormat: 'png',
diff --git a/packages/web-renderer/src/test/video.test.tsx b/packages/web-renderer/src/test/video.test.tsx
index 15276f4b7f..da15ac5a39 100644
--- a/packages/web-renderer/src/test/video.test.tsx
+++ b/packages/web-renderer/src/test/video.test.tsx
@@ -20,11 +20,14 @@ test('can extract a video frame', async (t) => {
};
const blob = await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 25,
- width: 1920 / 3,
- height: 1080 / 3,
+ composition: {
+ component: Component,
+ width: 1920 / 3,
+ height: 1080 / 3,
+ fps: 25,
+ durationInFrames: 100,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 20,
inputProps: {},
imageFormat: 'png',
@@ -55,11 +58,14 @@ test('cannot render inside an svg tag', async () => {
try {
await renderStillOnWeb({
- component: Component,
- durationInFrames: 100,
- fps: 30,
- width: 100,
- height: 100,
+ composition: {
+ component: Component,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 100,
+ calculateMetadata: () => Promise.resolve({}),
+ },
frame: 20,
inputProps: {},
imageFormat: 'png',