Skip to content

🎥 Make videos programmatically with React

move types around and start testing

Jonny Burger702d9d57fa9d
diff --git a/packages/studio-server/src/test/discriminated-union-update.test.ts b/packages/studio-server/src/test/discriminated-union-update.test.ts
new file mode 100644
index 0000000000..c36005cf35
--- /dev/null
+++ b/packages/studio-server/src/test/discriminated-union-update.test.ts
@@ -0,0 +1,92 @@
+import {test} from 'bun:test';
+import assert from 'node:assert';
+import {readFileSync} from 'node:fs';
+import path from 'node:path';
+import type {SchemaFieldInfo} from '@remotion/studio-shared';
+import {parseAst} from '../codemods/parse-ast';
+import {updateSequenceProps} from '../codemods/update-sequence-props';
+import {lineColumnToNodePath} from '../preview-server/routes/can-update-sequence-props';
+
+test('Should be able to update a discriminated union', async () => {
+ const file = readFileSync(
+ path.join(__dirname, 'snapshots', 'discriminated-union.txt'),
+ 'utf-8',
+ );
+
+ const ast = parseAst(file);
+
+ const nodePath = lineColumnToNodePath(ast, 177);
+ assert(nodePath, 'No node path found');
+
+ const field: SchemaFieldInfo = {
+ key: 'layout',
+ description: 'Layout',
+ typeName: 'enum',
+ supported: true,
+ rowHeight: 22,
+ currentValue: 'none',
+ fieldSchema: {
+ type: 'enum',
+ default: 'absolute-fill',
+ description: 'Layout',
+ variants: {
+ 'absolute-fill': {
+ 'style.translate': {
+ type: 'translate',
+ step: 1,
+ default: '0px 0px',
+ description: 'Position',
+ },
+ 'style.scale': {
+ type: 'number',
+ min: 0.05,
+ max: 100,
+ step: 0.01,
+ default: 1,
+ description: 'Scale',
+ },
+ 'style.rotate': {
+ type: 'rotation',
+ step: 1,
+ default: '0deg',
+ description: 'Rotation',
+ },
+ 'style.opacity': {
+ type: 'number',
+ min: 0,
+ max: 1,
+ step: 0.01,
+ default: 1,
+ description: 'Opacity',
+ },
+ },
+ none: {},
+ },
+ },
+ };
+
+ const update = await updateSequenceProps({
+ input: file,
+ nodePath,
+ key: 'layout',
+ value: 'none',
+ defaultValue: 'absolute-fill',
+ });
+
+ const expected = readFileSync(
+ path.join(__dirname, 'snapshots', 'discriminated-union-expected.txt'),
+ 'utf-8',
+ );
+ const actualLines = update.output.split('\n');
+ const expectedLines = expected.split('\n');
+ const maxLines = Math.max(actualLines.length, expectedLines.length);
+ for (let i = 0; i < maxLines; i++) {
+ if (actualLines[i] !== expectedLines[i]) {
+ console.log(update.output);
+ console.log(actualLines[i], expectedLines[i]);
+ throw new Error(
+ `Line ${i + 1} differs ${actualLines[i]} ${expectedLines[i]}`,
+ );
+ }
+ }
+});
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union-expected.txt b/packages/studio-server/src/test/snapshots/discriminated-union-expected.txt
new file mode 100644
index 0000000000..26e2ac89a4
--- /dev/null
+++ b/packages/studio-server/src/test/snapshots/discriminated-union-expected.txt
@@ -0,0 +1,190 @@
+import {Gif} from '@remotion/gif';
+import {LightLeak} from '@remotion/light-leaks';
+import {Audio, Video} from '@remotion/media';
+import {Starburst} from '@remotion/starburst';
+import React from 'react';
+import {AbsoluteFill, HtmlInCanvas, Img, Sequence, staticFile} from 'remotion';
+
+const Tile: React.FC<{
+ readonly title: string;
+ readonly children: React.ReactNode;
+}> = ({title, children}) => {
+ return (
+ <div
+ style={{
+ flex: 1,
+ display: 'flex',
+ flexDirection: 'column',
+ border: '2px solid #333',
+ borderRadius: 8,
+ overflow: 'hidden',
+ backgroundColor: '#111',
+ minWidth: 0,
+ minHeight: 0,
+ }}
+ >
+ <div
+ style={{
+ padding: '8px 12px',
+ fontFamily: 'monospace',
+ fontSize: 16,
+ color: '#fff',
+ backgroundColor: '#222',
+ }}
+ >
+ {title}
+ </div>
+ <div
+ style={{
+ flex: 1,
+ position: 'relative',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ overflow: 'hidden',
+ }}
+ >
+ {children}
+ </div>
+ </div>
+ );
+};
+
+export const ExperimentalControlsShowcase: React.FC = () => {
+ return (
+ <AbsoluteFill style={{backgroundColor: 'black', padding: 16, gap: 16}}>
+ <div style={{flex: 1, display: 'flex', flexDirection: 'row', gap: 16}}>
+ <Tile title="Img">
+ <Img
+ src={staticFile('1.jpg')}
+ style={{
+ maxWidth: '100%',
+ maxHeight: '100%',
+ translate: '0px 93px',
+ scale: 1.98,
+ }}
+ />
+ </Tile>
+ <Tile title="Sequence">
+ <Sequence
+ durationInFrames={120}
+ name="inner-sequence"
+ style={{
+ translate: '0px 66px',
+ scale: 2.05,
+ }}
+ >
+ <AbsoluteFill
+ style={{
+ backgroundColor: '#3344ff',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 40,
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ Sequence
+ </AbsoluteFill>
+ </Sequence>
+ </Tile>
+ <Tile title="HtmlInCanvas">
+ <HtmlInCanvas
+ width={400}
+ height={300}
+ style={{
+ translate: '0px -124px',
+ scale: 2.06,
+ rotate: '48deg',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 300,
+ backgroundColor: '#101728',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 32,
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ HtmlInCanvas
+ </div>
+ </HtmlInCanvas>
+ </Tile>
+ <Tile title="LightLeak">
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <LightLeak
+ durationInFrames={120}
+ seed={3}
+ hueShift={30}
+ style={{
+ translate: '0px 81px',
+ scale: 1.1,
+ }}
+ />
+ </AbsoluteFill>
+ </Tile>
+ </div>
+ <div style={{flex: 1, display: 'flex', flexDirection: 'row', gap: 16}}>
+ <Tile title="Starburst">
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <Starburst
+ rays={16}
+ colors={['#ffdd00', '#ff8800']}
+ vignette={0.5}
+ />
+ </AbsoluteFill>
+ </Tile>
+ <Tile title="Gif">
+ <Gif
+ src={staticFile('giphy.gif')}
+ fit="contain"
+ width={400}
+ height={300}
+ style={{
+ translate: '0px 59px',
+ }}
+ />
+ </Tile>
+ <Tile title="Video">
+ <Video
+ src={staticFile('bigbuckbunny.mp4')}
+ style={{
+ width: '100%',
+ height: '100%',
+ translate: '0px -27px',
+ }}
+ objectFit="contain"
+ />
+ </Tile>
+ <Tile title="Audio">
+ <div
+ style={{
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 24,
+ textAlign: 'center',
+ }}
+ >
+ Audio (no visual)
+ <Audio src={staticFile('music.mp3')} volume={1.29} />
+ </div>
+ </Tile>
+ <Tile title="Sequence layout='none'">
+ <Sequence
+ name="layout='none'"
+ style={{
+ scale: 1.74,
+ }}
+ layout={'none'}
+ >
+ hi
+ </Sequence>
+ </Tile>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/studio-server/src/test/snapshots/discriminated-union.txt b/packages/studio-server/src/test/snapshots/discriminated-union.txt
new file mode 100644
index 0000000000..b62743b84a
--- /dev/null
+++ b/packages/studio-server/src/test/snapshots/discriminated-union.txt
@@ -0,0 +1,189 @@
+import {Gif} from '@remotion/gif';
+import {LightLeak} from '@remotion/light-leaks';
+import {Audio, Video} from '@remotion/media';
+import {Starburst} from '@remotion/starburst';
+import React from 'react';
+import {AbsoluteFill, HtmlInCanvas, Img, Sequence, staticFile} from 'remotion';
+
+const Tile: React.FC<{
+ readonly title: string;
+ readonly children: React.ReactNode;
+}> = ({title, children}) => {
+ return (
+ <div
+ style={{
+ flex: 1,
+ display: 'flex',
+ flexDirection: 'column',
+ border: '2px solid #333',
+ borderRadius: 8,
+ overflow: 'hidden',
+ backgroundColor: '#111',
+ minWidth: 0,
+ minHeight: 0,
+ }}
+ >
+ <div
+ style={{
+ padding: '8px 12px',
+ fontFamily: 'monospace',
+ fontSize: 16,
+ color: '#fff',
+ backgroundColor: '#222',
+ }}
+ >
+ {title}
+ </div>
+ <div
+ style={{
+ flex: 1,
+ position: 'relative',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ overflow: 'hidden',
+ }}
+ >
+ {children}
+ </div>
+ </div>
+ );
+};
+
+export const ExperimentalControlsShowcase: React.FC = () => {
+ return (
+ <AbsoluteFill style={{backgroundColor: 'black', padding: 16, gap: 16}}>
+ <div style={{flex: 1, display: 'flex', flexDirection: 'row', gap: 16}}>
+ <Tile title="Img">
+ <Img
+ src={staticFile('1.jpg')}
+ style={{
+ maxWidth: '100%',
+ maxHeight: '100%',
+ translate: '0px 93px',
+ scale: 1.98,
+ }}
+ />
+ </Tile>
+ <Tile title="Sequence">
+ <Sequence
+ durationInFrames={120}
+ name="inner-sequence"
+ style={{
+ translate: '0px 66px',
+ scale: 2.05,
+ }}
+ >
+ <AbsoluteFill
+ style={{
+ backgroundColor: '#3344ff',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 40,
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ Sequence
+ </AbsoluteFill>
+ </Sequence>
+ </Tile>
+ <Tile title="HtmlInCanvas">
+ <HtmlInCanvas
+ width={400}
+ height={300}
+ style={{
+ translate: '0px -124px',
+ scale: 2.06,
+ rotate: '48deg',
+ }}
+ >
+ <div
+ style={{
+ width: 400,
+ height: 300,
+ backgroundColor: '#101728',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 32,
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ HtmlInCanvas
+ </div>
+ </HtmlInCanvas>
+ </Tile>
+ <Tile title="LightLeak">
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <LightLeak
+ durationInFrames={120}
+ seed={3}
+ hueShift={30}
+ style={{
+ translate: '0px 81px',
+ scale: 1.1,
+ }}
+ />
+ </AbsoluteFill>
+ </Tile>
+ </div>
+ <div style={{flex: 1, display: 'flex', flexDirection: 'row', gap: 16}}>
+ <Tile title="Starburst">
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <Starburst
+ rays={16}
+ colors={['#ffdd00', '#ff8800']}
+ vignette={0.5}
+ />
+ </AbsoluteFill>
+ </Tile>
+ <Tile title="Gif">
+ <Gif
+ src={staticFile('giphy.gif')}
+ fit="contain"
+ width={400}
+ height={300}
+ style={{
+ translate: '0px 59px',
+ }}
+ />
+ </Tile>
+ <Tile title="Video">
+ <Video
+ src={staticFile('bigbuckbunny.mp4')}
+ style={{
+ width: '100%',
+ height: '100%',
+ translate: '0px -27px',
+ }}
+ objectFit="contain"
+ />
+ </Tile>
+ <Tile title="Audio">
+ <div
+ style={{
+ color: 'white',
+ fontFamily: 'sans-serif',
+ fontSize: 24,
+ textAlign: 'center',
+ }}
+ >
+ Audio (no visual)
+ <Audio src={staticFile('music.mp3')} volume={1.29} />
+ </div>
+ </Tile>
+ <Tile title="Sequence layout='none'">
+ <Sequence
+ name="layout='none'"
+ style={{
+ scale: 1.74,
+ }}
+ >
+ hi
+ </Sequence>
+ </Tile>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.ts
index 198b680d3e..cb573bf30e 100644
--- a/packages/studio-shared/src/index.ts
+++ b/packages/studio-shared/src/index.ts
@@ -95,6 +95,7 @@ export {
UiOpenGlOptions,
} from './render-job';
export type {CompletedClientRender} from './render-job';
+export type {SchemaFieldInfo} from './schema-field-info';
export {SOURCE_MAP_ENDPOINT} from './source-map-endpoint';
export {
ScriptLine,
diff --git a/packages/studio-shared/src/schema-field-info.ts b/packages/studio-shared/src/schema-field-info.ts
new file mode 100644
index 0000000000..e783689d47
--- /dev/null
+++ b/packages/studio-shared/src/schema-field-info.ts
@@ -0,0 +1,11 @@
+import type {SequenceFieldSchema} from 'remotion';
+
+export type SchemaFieldInfo = {
+ key: string;
+ description: string | undefined;
+ typeName: string;
+ supported: boolean;
+ rowHeight: number;
+ currentValue: unknown;
+ fieldSchema: SequenceFieldSchema;
+};
diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.ts
index d9af235c33..3efdcdc491 100644
--- a/packages/studio/src/helpers/timeline-layout.ts
+++ b/packages/studio/src/helpers/timeline-layout.ts
@@ -1,10 +1,12 @@
+import type {SchemaFieldInfo} from '@remotion/studio-shared';
import type {
EffectDefinitionAndStack,
SequenceControls,
- SequenceFieldSchema,
TSequence,
} from 'remotion';
+export type {SchemaFieldInfo};
+
export const TIMELINE_PADDING = 16;
export const TIMELINE_BORDER = 1;
export const TIMELINE_ITEM_BORDER_BOTTOM = 1;
@@ -26,16 +28,6 @@ const SUPPORTED_SCHEMA_TYPES = new Set([
'enum',
]);
-export type SchemaFieldInfo = {
- key: string;
- description: string | undefined;
- typeName: string;
- supported: boolean;
- rowHeight: number;
- currentValue: unknown;
- fieldSchema: SequenceFieldSchema;
-};
-
export const getSchemaFields = (
controls: SequenceControls | null,
): SchemaFieldInfo[] | null => {