Skip to content

🎥 Make videos programmatically with React

advancements

Jonny Burger49f396a46f8f
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index e4e0a6d94e..d6496a4dc5 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -18,7 +18,7 @@ import {
} from '../state/rich-timeline';
import {EditorContent} from './EditorContent';
import {FramePersistor} from './FramePersistor';
-import {NewSequence} from './NewComposition';
+import {NewSequence} from './NewComposition/NewComposition';
import {UpdateCheck} from './UpdateCheck';
const Background = styled.div`
diff --git a/packages/cli/src/editor/components/NewComposition.tsx b/packages/cli/src/editor/components/NewComposition.tsx
deleted file mode 100644
index 4ce0b7644c..0000000000
--- a/packages/cli/src/editor/components/NewComposition.tsx
+++ /dev/null
@@ -1,118 +0,0 @@
-import React, {ChangeEventHandler, useCallback, useState} from 'react';
-import {BACKGROUND} from '../helpers/colors';
-import {FONT_FAMILY} from '../helpers/font';
-
-const backgroundOverlay: React.CSSProperties = {
- backgroundColor: 'rgba(255, 255, 255, 0.05)',
- position: 'fixed',
- height: '100%',
- width: '100%',
- display: 'flex',
- justifyContent: 'center',
- alignItems: 'center',
-};
-
-const panel: React.CSSProperties = {
- backgroundColor: BACKGROUND,
- boxShadow: '0 0 4px black',
- padding: 20,
- color: 'white',
- fontFamily: FONT_FAMILY,
-};
-
-type CompositionType = 'composition' | 'still';
-
-export const NewSequence: React.FC = () => {
- const [type, setType] = useState<CompositionType>('composition');
- const [width, setWidth] = useState('1280');
- const [height, setHeight] = useState('720');
-
- const onStillChanged: React.ChangeEventHandler<HTMLInputElement> = useCallback(
- (e) => {
- if (e.target.checked) {
- setType('still');
- }
- },
- []
- );
-
- const onCompositionChanged: React.ChangeEventHandler<HTMLInputElement> = useCallback(
- (e) => {
- if (e.target.checked) {
- setType('composition');
- }
- },
- []
- );
-
- const onWidthChanged: ChangeEventHandler<HTMLInputElement> = useCallback(
- (e) => {
- setWidth(e.target.value);
- },
- []
- );
-
- const onHeightChanged: ChangeEventHandler<HTMLInputElement> = useCallback(
- (e) => {
- setHeight(e.target.value);
- },
- []
- );
-
- return (
- <div style={backgroundOverlay}>
- <div style={panel}>
- <form>
- <input type="text" placeholder="Composition name" />
- <br />
- <br />
- <label>
- <input
- onChange={onCompositionChanged}
- checked={type === 'composition'}
- name="type"
- type="radio"
- />
- Composition
- </label>
- <label>
- <input
- onChange={onStillChanged}
- checked={type === 'still'}
- name="type"
- type="radio"
- />
- Still
- </label>
- <br />
- <br />
- <input
- type="number"
- value={width}
- placeholder="Width (px)"
- onChange={onWidthChanged}
- name="width"
- />
- <br />
- <input
- type="number"
- value={height}
- onChange={onHeightChanged}
- placeholder="Height (px)"
- name="height"
- />
- <br />
- <select>
- <option>24fps</option>
- <option>25fps</option>
- <option>29.97fps</option>
- <option>30fps</option>
- <option>48fps</option>
- <option>50fps</option>
- <option>60fps</option>
- </select>
- </form>
- </div>
- </div>
- );
-};
diff --git a/packages/cli/src/editor/components/NewComposition/NewCompCode.tsx b/packages/cli/src/editor/components/NewComposition/NewCompCode.tsx
new file mode 100644
index 0000000000..41289f60b9
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/NewCompCode.tsx
@@ -0,0 +1,72 @@
+const strColor = '#9ECBFF';
+const consColor = '#79B8FF';
+const propColor = '#B392F0';
+const keywordColor = '#F97583';
+
+const makeProperty = (
+ key: string,
+ val: number | string,
+ type: 'const' | 'string'
+) => {
+ return [
+ ` `,
+ <span key={key + 'key'} style={{color: propColor}}>
+ {key}
+ </span>,
+ <span key={key + 'equal'} style={{color: keywordColor}}>
+ {'='}
+ </span>,
+ type === 'string'
+ ? [
+ <span key={key + 'value'} style={{color: strColor}}>
+ {'"'}
+ {val}
+ {'"'}
+ </span>,
+ ]
+ : [
+ '{',
+ <span
+ key={key + 'value'}
+ style={{color: typeof val === 'number' ? consColor : strColor}}
+ >
+ {val}
+ </span>,
+ '}',
+ ],
+ <br key={key + 'br'} />,
+ ];
+};
+
+export const getNewCompositionCode = ({
+ type,
+ height,
+ width,
+ fps,
+ durationInFrames,
+ name,
+}: {
+ type: 'still' | 'composition';
+ height: number;
+ width: number;
+ fps: number;
+ durationInFrames: number;
+ name: string;
+}) => {
+ return [
+ `<`,
+ <span key="compname" style={{color: '#79B8FF'}}>
+ {type === 'still' ? 'Still' : 'Composition'}
+ </span>,
+ <br key="linebr1" />,
+ ...makeProperty('id', name, 'string'),
+ ...makeProperty('component', name, 'const'),
+ ...(type === 'composition'
+ ? makeProperty('durationInFrames', durationInFrames, 'const')
+ : []),
+ ...makeProperty('height', height, 'const'),
+ ...makeProperty('width', width, 'const'),
+ ...(type === 'composition' ? makeProperty('fps', fps, 'const') : []),
+ '/>',
+ ];
+};
diff --git a/packages/cli/src/editor/components/NewComposition/NewComposition.tsx b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
new file mode 100644
index 0000000000..08334046a4
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
@@ -0,0 +1,231 @@
+import React, {ChangeEventHandler, useCallback, useState} from 'react';
+import {BACKGROUND} from '../../helpers/colors';
+import {FONT_FAMILY} from '../../helpers/font';
+import {getNewCompositionCode} from './NewCompCode';
+import {aspectRatio} from './render-aspect-ratio';
+
+const backgroundOverlay: React.CSSProperties = {
+ backgroundColor: 'rgba(255, 255, 255, 0.2)',
+ backdropFilter: `blur(1px)`,
+ position: 'fixed',
+ height: '100%',
+ width: '100%',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+};
+
+const panel: React.CSSProperties = {
+ backgroundColor: BACKGROUND,
+ boxShadow: '0 0 4px black',
+ color: 'white',
+ fontFamily: FONT_FAMILY,
+ flexDirection: 'row',
+ display: 'flex',
+};
+
+const left: React.CSSProperties = {
+ padding: 20,
+ paddingTop: 80,
+ paddingBottom: 80,
+ paddingRight: 40,
+};
+
+const panelRight: React.CSSProperties = {
+ width: 400,
+ backgroundColor: 'black',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+};
+
+const leftLabel: React.CSSProperties = {
+ width: 160,
+ display: 'inline-block',
+ textAlign: 'right',
+ paddingRight: 30,
+ fontSize: 14,
+};
+
+const pre: React.CSSProperties = {
+ fontSize: 17,
+};
+
+type CompositionType = 'composition' | 'still';
+
+export const NewSequence: React.FC = () => {
+ const [type, setType] = useState<CompositionType>('composition');
+ const [name, setName] = useState('MyComp');
+ const [width, setWidth] = useState('1280');
+ const [height, setHeight] = useState('720');
+ const [fps, setFps] = useState(24);
+ const [durationInFrames, setDurationInFrames] = useState('150');
+
+ const onStillChanged: React.ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ if (e.target.checked) {
+ setType('still');
+ }
+ },
+ []
+ );
+
+ const onCompositionChanged: React.ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ if (e.target.checked) {
+ setType('composition');
+ }
+ },
+ []
+ );
+
+ const onWidthChanged: ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ setWidth(e.target.value);
+ },
+ []
+ );
+
+ const onHeightChanged: ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ setHeight(e.target.value);
+ },
+ []
+ );
+ const onDurationInFramesChanged: ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ setDurationInFrames(e.target.value);
+ },
+ []
+ );
+ const onNameChange: ChangeEventHandler<HTMLInputElement> = useCallback(
+ (e) => {
+ setName(e.target.value);
+ },
+ []
+ );
+ const onFpsChange: ChangeEventHandler<HTMLSelectElement> = useCallback(
+ (e) => {
+ setFps(Number(e.target.value));
+ },
+ []
+ );
+
+ return (
+ <div style={backgroundOverlay}>
+ <div style={panel}>
+ <div style={left}>
+ <form>
+ <label>
+ <div style={leftLabel}>Name</div>
+ <input
+ value={name}
+ onChange={onNameChange}
+ type="text"
+ placeholder="Composition name"
+ />
+ </label>
+ <br />
+ <br />
+ <div>
+ <div style={leftLabel}>Type</div>
+ <label>
+ <input
+ onChange={onCompositionChanged}
+ checked={type === 'composition'}
+ name="type"
+ type="radio"
+ />
+ Composition
+ </label>
+ <label>
+ <input
+ onChange={onStillChanged}
+ checked={type === 'still'}
+ name="type"
+ type="radio"
+ />
+ Still
+ </label>
+ </div>
+ <br />
+ <label>
+ <div style={leftLabel}>Width</div>
+ <input
+ type="number"
+ value={width}
+ placeholder="Width (px)"
+ onChange={onWidthChanged}
+ name="width"
+ />
+ </label>
+ <br />
+ <br />
+ <label>
+ <div style={leftLabel}>Height</div>
+ <input
+ type="number"
+ value={height}
+ onChange={onHeightChanged}
+ placeholder="Height (px)"
+ name="height"
+ />
+ </label>
+ <br />
+ <br />
+ <div>
+ <div style={leftLabel}>Aspect ratio</div>
+ {aspectRatio(Number(width) / Number(height))}
+ </div>
+ <br />
+ <label>
+ <div style={leftLabel}> Duration in frames</div>
+ <input
+ type="number"
+ value={durationInFrames}
+ onChange={onDurationInFramesChanged}
+ placeholder="Duration (frames)"
+ name="height"
+ />
+ </label>
+ <br />
+ <br />
+ <div>
+ <div style={leftLabel} />
+ <span>
+ {(Number(durationInFrames) / Number(fps)).toFixed(2)}sec
+ </span>
+ </div>
+ <br />
+ <br />
+ <label>
+ <div style={leftLabel}>Framerate</div>
+ <select value={fps} onChange={onFpsChange}>
+ <option value="24">24fps</option>
+ <option value="25">25fps</option>
+ <option value="29.97">29.97fps</option>
+ <option value="30">30fps</option>
+ <option value="48">48fps</option>
+ <option value="50">50fps</option>
+ <option value="60">60fps</option>
+ </select>
+ </label>
+ </form>
+ </div>
+
+ <div style={panelRight}>
+ <pre style={pre}>
+ {getNewCompositionCode({
+ type,
+ durationInFrames: Number(durationInFrames),
+ fps: Number(fps),
+ height: Number(height),
+ width: Number(width),
+ name,
+ })}
+ </pre>
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx b/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx
new file mode 100644
index 0000000000..f8493d0de3
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx
@@ -0,0 +1,11 @@
+export const aspectRatio = (ratio: number) => {
+ if (ratio === 16 / 9) {
+ return '16:9';
+ }
+
+ if (ratio === 5 / 4) {
+ return '5:4';
+ }
+
+ return ratio.toFixed(2);
+};