🎥 Make videos programmatically with React
set input props using localStorage
Jonny Burger00232a2c036c
diff --git a/packages/cli/src/get-input-props.ts b/packages/cli/src/get-input-props.tsindex f6782a3141..1c0e13a4bb 100644--- a/packages/cli/src/get-input-props.ts+++ b/packages/cli/src/get-input-props.ts@@ -18,7 +18,7 @@ export const getInputProps = (): object => {'You passed --props but it was neither valid JSON nor a file path to a valid JSON file.');console.log(- 'Check that your input is parseable using `JSON-parse` and try again.'+ 'Check that your input is parseable using `JSON.parse` and try again.');process.exit(1);}diff --git a/packages/core/src/config/input-props.ts b/packages/core/src/config/input-props.tsindex a8ad11a538..6e1949641d 100644--- a/packages/core/src/config/input-props.ts+++ b/packages/core/src/config/input-props.ts@@ -1,10 +1,12 @@+export const INPUT_PROPS_KEY = 'remotion.inputProps';+export const getInputProps = () => {if (process.env.NODE_ENV === 'production') {- const param = new URLSearchParams(window.location.search).get('props');+ const param = localStorage.getItem(INPUT_PROPS_KEY);if (!param) {return {};}- const parsed = JSON.parse(decodeURIComponent(param));+ const parsed = JSON.parse(param);return parsed;}return (process.env.INPUT_PROPS as unknown) as object | null;diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex fc4589a3ad..a545542b95 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -27,6 +27,7 @@ import {validateSelectedPixelFormatAndImageFormatCombination,} from './config/image-format';import {getShouldOutputImageSequence} from './config/image-sequence';+import {INPUT_PROPS_KEY} from './config/input-props';import {getWebpackOverrideFn,WebpackOverrideFn,@@ -94,6 +95,7 @@ export const Internals = {validateSelectedPixelFormatAndCodecCombination,validateFrameRange,getWebpackCaching,+ INPUT_PROPS_KEY,};export type {diff --git a/packages/core/src/register-root.tsx b/packages/core/src/register-root.tsxindex 16fecc74d1..4556147124 100644--- a/packages/core/src/register-root.tsx+++ b/packages/core/src/register-root.tsx@@ -33,7 +33,7 @@ export const getCompositionName = () => {if (param !== null) {return String(param);}- throw new Error('No comp name specified in URL');+ return null;};export const getIsEvaluation = () => {diff --git a/packages/example/out.mp4 b/packages/example/out.mp4new file mode 100644index 0000000000..c9ddd46f9bBinary files /dev/null and b/packages/example/out.mp4 differdiff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex 340e382c34..4374dc0a58 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -7,6 +7,7 @@ import {VideoTesting} from './VideoTesting';export const Index: React.FC = () => {const inputProps = getInputProps();+ console.log('props', JSON.stringify(inputProps));return (<><Compositiondiff --git a/packages/renderer/src/get-compositions.ts b/packages/renderer/src/get-compositions.tsindex 48f40dcbfc..8c40826395 100644--- a/packages/renderer/src/get-compositions.ts+++ b/packages/renderer/src/get-compositions.ts@@ -13,6 +13,17 @@ export const getCompositions = async (const {port, close} = await serveStatic(webpackBundle);+ 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 page.goto(`http://localhost:${port}/index.html?evaluation=true&props=${encodeURIComponent(JSON.stringify(config?.inputProps ?? null)diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.tsindex e67a5394d8..b157944863 100644--- a/packages/renderer/src/render.ts+++ b/packages/renderer/src/render.ts@@ -64,9 +64,18 @@ export const renderFrames = async ({page.on('error', console.error);page.on('pageerror', console.error);- const site = `http://localhost:${port}/index.html?composition=${compositionId}&props=${encodeURIComponent(- JSON.stringify(inputProps)- )}`;+ 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)+ );+ }+ const site = `http://localhost:${port}/index.html?composition=${compositionId}`;await page.goto(site);return page;});diff --git a/tests/rendering.test.ts b/tests/rendering.test.tsindex bd2d378f25..5122b7e4ea 100644--- a/tests/rendering.test.ts+++ b/tests/rendering.test.ts@@ -178,3 +178,34 @@ test("Should render a video with GIFs", async () => {recursive: true,});});++test("Dynamic duration should work", async () => {+ const task = await execa(+ "npx",+ [+ "remotion",+ "render",+ "src/index.tsx",+ "dynamic-duration",+ `--props`,+ `{"duration": 5}`,+ outputPath,+ ],+ {+ cwd: "packages/example",+ reject: false,+ }+ );++ expect(task.exitCode).toBe(0);+ expect(fs.existsSync(outputPath)).toBe(true);++ const info = await execa("ffprobe", [outputPath]);+ const data = info.stderr;+ expect(data).toContain("Video: h264");+ const expectedDuration = (5 / 30).toFixed(2);+ expect(data).toContain(`Duration: 00:00:0${expectedDuration}`);+ fs.rmdirSync(outputPath, {+ recursive: true,+ });+});