Skip to content

🎥 Make videos programmatically with React

:eyes:

Jonny Burgerc96cc0479f42
diff --git a/packages/bundler/src/renderEntry.tsx b/packages/bundler/src/renderEntry.tsx
index 65f5f34dcb..d86864a755 100644
--- a/packages/bundler/src/renderEntry.tsx
+++ b/packages/bundler/src/renderEntry.tsx
@@ -16,6 +16,7 @@ import {
} from 'remotion';
Internals.CSSUtils.injectCSS(Internals.CSSUtils.makeDefaultCSS(null));
+Internals.setupEnvVariables();
const Root = Internals.getRoot();
diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.ts
index 62c22a5849..91d238f8b3 100644
--- a/packages/bundler/src/webpack-config.ts
+++ b/packages/bundler/src/webpack-config.ts
@@ -1,7 +1,6 @@
import path from 'path';
import {Internals, WebpackConfiguration, WebpackOverrideFn} from 'remotion';
import webpack, {ProgressPlugin} from 'webpack';
-import {convertEnvToProcessEnv} from './convert-env-to-process-env';
import {getWebpackCacheName} from './webpack-cache';
const ErrorOverlayPlugin = require('@webhotelier/webpack-fast-refresh/error-overlay');
@@ -31,7 +30,7 @@ export const webpackConfig = ({
onProgressUpdate?: (f: number) => void;
enableCaching?: boolean;
inputProps?: object;
- env?: {[key: string]: string};
+ env?: Record<string, string>;
}): WebpackConfiguration => {
return webpackOverride({
optimization: {
@@ -72,7 +71,9 @@ export const webpackConfig = ({
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
'process.env.INPUT_PROPS': JSON.stringify(inputProps ?? {}),
- ...convertEnvToProcessEnv(env ?? {}),
+ [`process.env.${Internals.ENV_VARIABLES_ENV_NAME}`]: JSON.stringify(
+ env ?? {}
+ ),
}),
]
: [
diff --git a/packages/cli/src/previewEntry.tsx b/packages/cli/src/previewEntry.tsx
index 3586f4b0d3..f458ba251a 100644
--- a/packages/cli/src/previewEntry.tsx
+++ b/packages/cli/src/previewEntry.tsx
@@ -5,6 +5,7 @@ import '../styles/styles.css';
import {Editor} from './editor/components/Editor';
Internals.CSSUtils.injectCSS(Internals.CSSUtils.makeDefaultCSS(null));
+Internals.setupEnvVariables();
render(
<Internals.RemotionRoot>
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 92bbe7f833..ce08307903 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -183,7 +183,7 @@ export const render = async () => {
totalFrames = fc;
},
inputProps,
- env,
+ envVariables: env,
webpackBundle: bundled,
imageFormat,
quality,
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index f3c785dddb..4a1b418d59 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -60,6 +60,11 @@ import {
} from './register-root';
import {RemotionRoot} from './RemotionRoot';
import {SequenceContext} from './sequencing';
+import {
+ ENV_VARIABLES_ENV_NAME,
+ ENV_VARIABLES_LOCAL_STORAGE_KEY,
+ setupEnvVariables,
+} from './setup-env-variables';
import * as Timeline from './timeline-position-state';
import {
SetTimelineContextValue,
@@ -121,6 +126,9 @@ export const Internals = {
RemotionContextProvider,
isPlainIndex,
CSSUtils,
+ setupEnvVariables,
+ ENV_VARIABLES_ENV_NAME,
+ ENV_VARIABLES_LOCAL_STORAGE_KEY,
};
export type {
diff --git a/packages/core/src/setup-env-variables.ts b/packages/core/src/setup-env-variables.ts
new file mode 100644
index 0000000000..358a2d4a76
--- /dev/null
+++ b/packages/core/src/setup-env-variables.ts
@@ -0,0 +1,30 @@
+export const ENV_VARIABLES_LOCAL_STORAGE_KEY = 'remotion.envVariables';
+export const ENV_VARIABLES_ENV_NAME = 'ENV_VARIABLES';
+
+const getEnvVariables = (): Record<string, string> => {
+ if (process.env.NODE_ENV === 'production') {
+ const param = localStorage.getItem(ENV_VARIABLES_LOCAL_STORAGE_KEY);
+ if (!param) {
+ return {};
+ }
+
+ return JSON.parse(param);
+ }
+ return (process.env[ENV_VARIABLES_ENV_NAME] as unknown) as Record<
+ string,
+ string
+ >;
+};
+
+export const setupEnvVariables = () => {
+ const env = getEnvVariables();
+ if (!window.process) {
+ window.process = {} as NodeJS.Process;
+ }
+ if (!window.process.env) {
+ window.process.env = {};
+ }
+ Object.keys(env).map((key) => {
+ window.process.env[key] = env[key];
+ });
+};
diff --git a/packages/renderer/src/get-compositions.ts b/packages/renderer/src/get-compositions.ts
index 1a703de23d..3b0a1eb955 100644
--- a/packages/renderer/src/get-compositions.ts
+++ b/packages/renderer/src/get-compositions.ts
@@ -2,12 +2,14 @@ import {Browser as PuppeteerBrowser} from 'puppeteer-core';
import {Browser, Internals, TCompMetadata} from 'remotion';
import {openBrowser} from './open-browser';
import {serveStatic} from './serve-static';
+import {setPropsAndEnv} from './set-props-and-env';
export const getCompositions = async (
webpackBundle: string,
config?: {
browser?: Browser;
inputProps?: object | null;
+ envVariables?: Record<string, string>;
browserInstance?: PuppeteerBrowser;
}
): Promise<TCompMetadata[]> => {
@@ -20,16 +22,12 @@ export const getCompositions = async (
page.on('error', console.error);
page.on('pageerror', console.error);
- if (config?.inputProps) {
- await page.goto(`http://localhost:${port}/index.html`);
- await page.evaluate(
- (key, input) => {
- window.localStorage.setItem(key, input);
- },
- Internals.INPUT_PROPS_KEY,
- JSON.stringify(config.inputProps)
- );
- }
+ await setPropsAndEnv({
+ inputProps: config?.inputProps,
+ envVariables: config?.envVariables,
+ page,
+ port,
+ });
await page.goto(`http://localhost:${port}/index.html?evaluation=true`);
await page.waitForFunction('window.ready === true');
diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.ts
index 9dd921ce14..e7f3daa78a 100644
--- a/packages/renderer/src/render.ts
+++ b/packages/renderer/src/render.ts
@@ -17,6 +17,7 @@ import {Pool} from './pool';
import {provideScreenshot} from './provide-screenshot';
import {seekToFrame} from './seek-to-frame';
import {serveStatic} from './serve-static';
+import {setPropsAndEnv} from './set-props-and-env';
export type RenderFramesOutput = {
frameCount: number;
@@ -37,7 +38,7 @@ export const renderFrames = async ({
outputDir,
onStart,
inputProps,
- env = {},
+ envVariables: envVariables = {},
webpackBundle,
quality,
imageFormat = DEFAULT_IMAGE_FORMAT,
@@ -53,7 +54,7 @@ export const renderFrames = async ({
onFrameUpdate: (f: number) => void;
outputDir: string;
inputProps: unknown;
- env?: object;
+ envVariables?: Record<string, string>;
webpackBundle: string;
imageFormat: ImageFormat;
parallelism?: number | null;
@@ -92,21 +93,7 @@ export const renderFrames = async ({
page.on('pageerror', errorCallback);
- await page.evaluateOnNewDocument((parsedEnv: object) => {
- window.remotion_env = parsedEnv;
- }, env || {});
-
- if (inputProps) {
- await page.goto(`http://localhost:${port}/index.html`);
-
- await page.evaluate(
- (key, input) => {
- window.localStorage.setItem(key, input);
- },
- Internals.INPUT_PROPS_KEY,
- JSON.stringify(inputProps)
- );
- }
+ await setPropsAndEnv({inputProps, envVariables, page, port});
const site = `http://localhost:${port}/index.html?composition=${compositionId}`;
await page.goto(site);
@@ -153,7 +140,7 @@ export const renderFrames = async ({
) {
errorCallback(
new Error(
- `The rendering timed out. See https://www.remotion.dev/docs/timeout/ for possible reasons.`
+ 'The rendering timed out. See https://www.remotion.dev/docs/timeout/ for possible reasons.'
)
);
} else {
diff --git a/packages/renderer/src/set-props-and-env.ts b/packages/renderer/src/set-props-and-env.ts
new file mode 100644
index 0000000000..1c5cda3b84
--- /dev/null
+++ b/packages/renderer/src/set-props-and-env.ts
@@ -0,0 +1,37 @@
+import {Page} from 'puppeteer-core';
+import {Internals} from 'remotion';
+
+export const setPropsAndEnv = async ({
+ inputProps,
+ envVariables,
+ page,
+ port,
+}: {
+ inputProps: unknown;
+ envVariables: Record<string, string> | undefined;
+ page: Page;
+ port: number;
+}) => {
+ if (inputProps || envVariables) {
+ await page.goto(`http://localhost:${port}/index.html`);
+
+ if (inputProps) {
+ await page.evaluate(
+ (key, input) => {
+ window.localStorage.setItem(key, input);
+ },
+ Internals.INPUT_PROPS_KEY,
+ JSON.stringify(inputProps)
+ );
+ }
+ if (envVariables) {
+ await page.evaluate(
+ (key, input) => {
+ window.localStorage.setItem(key, input);
+ },
+ Internals.ENV_VARIABLES_LOCAL_STORAGE_KEY,
+ JSON.stringify(envVariables)
+ );
+ }
+ }
+};