Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurger40fb102d59a6
diff --git a/packages/core/src/config/input-props.ts b/packages/core/src/config/input-props.ts
index 96cfbb98b9..74ec7c1b3a 100644
--- a/packages/core/src/config/input-props.ts
+++ b/packages/core/src/config/input-props.ts
@@ -34,17 +34,20 @@ export const getInputProps = <
);
}
- if (getRemotionEnvironment().isClientSideRendering) {
- throw new Error(
- 'You cannot call `getInputProps()` while client-side rendering.',
- );
- }
-
const override = getInputPropsOverride();
if (override) {
return override as T;
}
+ if (
+ typeof window === 'undefined' ||
+ typeof window.remotion_inputProps === 'undefined'
+ ) {
+ throw new Error(
+ 'Cannot call `getInputProps()` - window.remotion_inputProps is not set. This API is not available if you are in the Studio, or while you are rendering server-side.',
+ );
+ }
+
const param = window.remotion_inputProps;
if (!param) {
return {} as T;
diff --git a/packages/web-renderer/src/render-still-on-web.tsx b/packages/web-renderer/src/render-still-on-web.tsx
index 112b855961..b9e213f056 100644
--- a/packages/web-renderer/src/render-still-on-web.tsx
+++ b/packages/web-renderer/src/render-still-on-web.tsx
@@ -1,4 +1,5 @@
import {type ComponentType, type LazyExoticComponent} from 'react';
+import {flushSync} from 'react-dom';
import ReactDOM from 'react-dom/client';
import type {_InternalTypes, LogLevel} from 'remotion';
import {Internals} from 'remotion';
@@ -6,6 +7,7 @@ import {compose} from './compose';
import {findCanvasElements} from './find-canvas-elements';
import {findSvgElements} from './find-svg-elements';
import {waitForReady} from './wait-for-ready';
+import {withResolvers} from './with-resolvers';
type MandatoryRenderStillOnWebOptions<T extends Record<string, unknown>> = {
Component: LazyExoticComponent<ComponentType<T>> | ComponentType<T>;
@@ -67,7 +69,14 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
// TODO: delayRender()
// TODO: Video config
- const root = ReactDOM.createRoot(div);
+ const {promise, resolve, reject} = withResolvers<void>();
+
+ // TODO: This might not work in React 18
+ const root = ReactDOM.createRoot(div, {
+ onUncaughtError: (err) => {
+ reject(err);
+ },
+ });
const delayRenderScope: _InternalTypes['DelayRenderScope'] = {
remotion_renderReady: true,
@@ -76,74 +85,79 @@ async function internalRenderStillOnWeb<T extends Record<string, unknown>>({
remotion_attempt: 0,
};
- root.render(
- <Internals.RemotionEnvironmentContext
- value={{
- isStudio: false,
- isRendering: true,
- isPlayer: false,
- isReadOnlyStudio: false,
- isClientSideRendering: true,
- }}
- >
- <Internals.DelayRenderContextType.Provider value={delayRenderScope}>
- <Internals.CompositionManagerProvider
- initialCanvasContent={{
- type: 'composition',
- compositionId: COMP_ID,
- }}
- onlyRenderComposition={null}
- // TODO: Hardcoded
- currentCompositionMetadata={{
- // TODO: Empty
- props: {},
- durationInFrames,
- fps,
- height,
- width,
- defaultCodec: null,
- defaultOutName: null,
- defaultVideoImageFormat: null,
- defaultPixelFormat: null,
- defaultProResProfile: null,
- }}
- initialCompositions={[
- {
- id: COMP_ID,
- // @ts-expect-error
- component: Component,
- nonce: 0,
- // TODO: Do we need to allow to set this?
- defaultProps: undefined,
- folderName: null,
- parentFolderName: null,
- schema: null,
- calculateMetadata: null,
+ flushSync(() => {
+ root.render(
+ <Internals.RemotionEnvironmentContext
+ value={{
+ isStudio: false,
+ isRendering: true,
+ isPlayer: false,
+ isReadOnlyStudio: false,
+ isClientSideRendering: true,
+ }}
+ >
+ <Internals.DelayRenderContextType.Provider value={delayRenderScope}>
+ <Internals.CompositionManagerProvider
+ initialCanvasContent={{
+ type: 'composition',
+ compositionId: COMP_ID,
+ }}
+ onlyRenderComposition={null}
+ // TODO: Hardcoded
+ currentCompositionMetadata={{
+ // TODO: Empty
+ props: {},
durationInFrames,
fps,
height,
width,
- },
- ]}
- >
- <Internals.RemotionRoot
- audioEnabled={false}
- videoEnabled
- logLevel={logLevel}
- numberOfAudioTags={0}
- audioLatencyHint="interactive"
- frameState={{
- [COMP_ID]: frame,
+ defaultCodec: null,
+ defaultOutName: null,
+ defaultVideoImageFormat: null,
+ defaultPixelFormat: null,
+ defaultProResProfile: null,
}}
+ initialCompositions={[
+ {
+ id: COMP_ID,
+ // @ts-expect-error
+ component: Component,
+ nonce: 0,
+ // TODO: Do we need to allow to set this?
+ defaultProps: undefined,
+ folderName: null,
+ parentFolderName: null,
+ schema: null,
+ calculateMetadata: null,
+ durationInFrames,
+ fps,
+ height,
+ width,
+ },
+ ]}
>
- <Internals.CanUseRemotionHooks value>
- <Component {...inputProps} />
- </Internals.CanUseRemotionHooks>
- </Internals.RemotionRoot>
- </Internals.CompositionManagerProvider>
- </Internals.DelayRenderContextType.Provider>
- </Internals.RemotionEnvironmentContext>,
- );
+ <Internals.RemotionRoot
+ audioEnabled={false}
+ videoEnabled
+ logLevel={logLevel}
+ numberOfAudioTags={0}
+ audioLatencyHint="interactive"
+ frameState={{
+ [COMP_ID]: frame,
+ }}
+ >
+ <Internals.CanUseRemotionHooks value>
+ <Component {...inputProps} />
+ </Internals.CanUseRemotionHooks>
+ </Internals.RemotionRoot>
+ </Internals.CompositionManagerProvider>
+ </Internals.DelayRenderContextType.Provider>
+ </Internals.RemotionEnvironmentContext>,
+ );
+ });
+
+ resolve();
+ await promise;
await waitForReady(delayRenderTimeoutInMilliseconds, delayRenderScope);
const canvasElements = findCanvasElements(div);
diff --git a/packages/web-renderer/src/test/input-props.test.tsx b/packages/web-renderer/src/test/input-props.test.tsx
index 73b3e7f2e4..3d30ba4f72 100644
--- a/packages/web-renderer/src/test/input-props.test.tsx
+++ b/packages/web-renderer/src/test/input-props.test.tsx
@@ -1,7 +1,8 @@
-import {test} from 'vitest';
+import {getInputProps} from 'remotion';
+import {expect, test} from 'vitest';
import {renderStillOnWeb} from '../render-still-on-web';
-const MustNotBePlayer: React.FC<{
+const MustAcceptInputProps: React.FC<{
abc: string;
}> = ({abc}) => {
if (abc !== 'abc') {
@@ -11,9 +12,14 @@ const MustNotBePlayer: React.FC<{
return abc;
};
-test('should not give a false positive when the player is also mounted', async () => {
+const DisallowGetInputProps: React.FC<{}> = () => {
+ getInputProps();
+ return null;
+};
+
+test('cannot call getInputProps() while rendering client-side', async () => {
await renderStillOnWeb({
- Component: MustNotBePlayer,
+ Component: MustAcceptInputProps,
width: 100,
height: 100,
fps: 30,
@@ -22,3 +28,19 @@ test('should not give a false positive when the player is also mounted', async (
inputProps: {abc: 'abc'},
});
});
+
+test('cannot call getInputProps() while rendering client-side', async () => {
+ await expect(() => {
+ return renderStillOnWeb({
+ Component: DisallowGetInputProps,
+ width: 100,
+ height: 100,
+ fps: 30,
+ durationInFrames: 30,
+ frame: 20,
+ inputProps: {abc: 'abc'},
+ });
+ }).rejects.toThrow(
+ 'Cannot call `getInputProps()` - window.remotion_inputProps is not set. This API is not available if you are in the Studio, or while you are rendering server-side.',
+ );
+});
diff --git a/packages/web-renderer/src/with-resolvers.ts b/packages/web-renderer/src/with-resolvers.ts
new file mode 100644
index 0000000000..56095bc3b7
--- /dev/null
+++ b/packages/web-renderer/src/with-resolvers.ts
@@ -0,0 +1,16 @@
+export interface WithResolvers<T> {
+ promise: Promise<T>;
+ resolve: (value: T | PromiseLike<T>) => void;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ reject: (reason?: any) => void;
+}
+
+export const withResolvers = function <T>() {
+ let resolve: WithResolvers<T>['resolve'];
+ let reject: WithResolvers<T>['reject'];
+ const promise = new Promise<T>((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return {promise, resolve: resolve!, reject: reject!};
+};