Skip to content

🎥 Make videos programmatically with React

makes sense

Jonny Burger7adca7cf643b
diff --git a/packages/cli/src/editor/components/Canvas.tsx b/packages/cli/src/editor/components/Canvas.tsx
index 7a18362227..a312184ad2 100644
--- a/packages/cli/src/editor/components/Canvas.tsx
+++ b/packages/cli/src/editor/components/Canvas.tsx
@@ -58,7 +58,7 @@ export const Canvas: React.FC<{
>(null);
const contentDimensions = useMemo(() => {
- if (canvasContent.type === 'asset') {
+ if (canvasContent.type === 'asset' || canvasContent.type === 'output') {
return assetResolution;
}
@@ -292,15 +292,21 @@ export const Canvas: React.FC<{
}, [keybindings, onReset, onZoomIn, onZoomOut]);
const fetchMetadata = useCallback(async () => {
- if (canvasContent.type !== 'asset') {
+ if (canvasContent.type !== 'asset' && canvasContent.type !== 'output') {
return;
}
+ const src =
+ canvasContent.type === 'asset'
+ ? staticFile(canvasContent.asset)
+ : window.remotion_staticBase.replace('static', 'outputs') +
+ canvasContent.path;
+
setAssetResolution(null);
- const assetSrc = staticFile(canvasContent.asset);
+ const assetSrc = src;
- const fileType = getPreviewFileType(canvasContent.asset);
+ const fileType = getPreviewFileType(src);
if (fileType === 'video') {
try {
diff --git a/packages/cli/src/editor/components/CanvasOrLoading.tsx b/packages/cli/src/editor/components/CanvasOrLoading.tsx
index 4ad370d0ec..9c9cea886f 100644
--- a/packages/cli/src/editor/components/CanvasOrLoading.tsx
+++ b/packages/cli/src/editor/components/CanvasOrLoading.tsx
@@ -71,7 +71,7 @@ export const CanvasOrLoading: React.FC = () => {
<Canvas canvasContent={canvasContent} />
</>
);
- if (canvasContent.type === 'asset') {
+ if (canvasContent.type === 'asset' || canvasContent.type === 'output') {
return content;
}
diff --git a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
index 172db9b672..390861a047 100644
--- a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
+++ b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
@@ -26,6 +26,11 @@ export const TitleUpdater: React.FC = () => {
return;
}
+ if (canvasContent.type === 'output') {
+ setCurrentCanvasContentId(canvasContent.path);
+ return;
+ }
+
setCurrentCanvasContentId(canvasContent.asset);
}, [canvasContent]);
diff --git a/packages/cli/src/editor/components/FilePreview.tsx b/packages/cli/src/editor/components/FilePreview.tsx
new file mode 100644
index 0000000000..1ba26da112
--- /dev/null
+++ b/packages/cli/src/editor/components/FilePreview.tsx
@@ -0,0 +1,70 @@
+import React from 'react';
+import {LIGHT_TEXT} from '../helpers/colors';
+import {JSONViewer} from './JSONViewer';
+import {Spacing} from './layout';
+import type {AssetFileType} from './Preview';
+import {TextViewer} from './TextViewer';
+
+const msgStyle: React.CSSProperties = {
+ fontSize: 13,
+ color: 'white',
+ fontFamily: 'sans-serif',
+ display: 'flex',
+ justifyContent: 'center',
+};
+
+const errMsgStyle: React.CSSProperties = {
+ ...msgStyle,
+ color: LIGHT_TEXT,
+};
+
+export const FilePreview: React.FC<{
+ staticFileSrc: string;
+ fileType: AssetFileType;
+ currentAsset: string;
+ fileSize: string;
+}> = ({fileType, staticFileSrc, currentAsset, fileSize}) => {
+ if (fileType === 'audio') {
+ try {
+ return (
+ <div>
+ <audio src={staticFileSrc} controls />
+ </div>
+ );
+ } catch (err) {
+ return <div style={errMsgStyle}>The audio could not be loaded</div>;
+ }
+ }
+
+ if (fileType === 'video') {
+ try {
+ return <video src={staticFileSrc} controls />;
+ } catch (err) {
+ return <div style={errMsgStyle}>The video could not be loaded</div>;
+ }
+ }
+
+ if (fileType === 'image') {
+ try {
+ return <img src={staticFileSrc} />;
+ } catch (err) {
+ return <div style={errMsgStyle}>The image could not be loaded</div>;
+ }
+ }
+
+ if (fileType === 'json') {
+ return <JSONViewer src={staticFileSrc} />;
+ }
+
+ if (fileType === 'txt') {
+ return <TextViewer src={staticFileSrc} />;
+ }
+
+ return (
+ <>
+ <div style={msgStyle}>{currentAsset}</div>
+ <Spacing y={1} />
+ <div style={msgStyle}>Size: {fileSize} </div>
+ </>
+ );
+};
diff --git a/packages/cli/src/editor/components/InitialCompositionLoader.tsx b/packages/cli/src/editor/components/InitialCompositionLoader.tsx
index 54a42b967d..4cee8e376e 100644
--- a/packages/cli/src/editor/components/InitialCompositionLoader.tsx
+++ b/packages/cli/src/editor/components/InitialCompositionLoader.tsx
@@ -89,6 +89,11 @@ export const InitialCompositionLoader: React.FC = () => {
return;
}
+ if (canvasContentFromUrl && canvasContentFromUrl.type === 'output') {
+ setCanvasContent(canvasContentFromUrl);
+ return;
+ }
+
if (compositions.length > 0) {
selectComposition(compositions[0], true);
}
@@ -103,13 +108,17 @@ export const InitialCompositionLoader: React.FC = () => {
useEffect(() => {
const onchange = () => {
const newCanvas = deriveCanvasContentFromUrl();
- if (newCanvas && newCanvas?.type === 'composition') {
+ if (newCanvas && newCanvas.type === 'composition') {
const newComp = window.location.pathname.substring(1);
const exists = compositions.find((c) => c.id === newComp);
if (exists) {
selectComposition(exists, false);
}
- } else if (newCanvas && newCanvas.type === 'asset') {
+
+ return;
+ }
+
+ if (newCanvas && newCanvas.type === 'asset') {
const staticFiles = getStaticFiles();
const exists = staticFiles.find((file) => {
return file.name === newCanvas.asset;
@@ -118,7 +127,11 @@ export const InitialCompositionLoader: React.FC = () => {
if (exists) {
setCanvasContent(newCanvas);
}
+
+ return;
}
+
+ setCanvasContent(newCanvas);
};
window.addEventListener('popstate', onchange);
diff --git a/packages/cli/src/editor/components/Preview.tsx b/packages/cli/src/editor/components/Preview.tsx
index aeb0ac13f1..543bd7d113 100644
--- a/packages/cli/src/editor/components/Preview.tsx
+++ b/packages/cli/src/editor/components/Preview.tsx
@@ -15,10 +15,8 @@ import {LIGHT_TEXT} from '../helpers/colors';
import type {Dimensions} from '../helpers/is-current-selected-still';
import {CheckerboardContext} from '../state/checkerboard';
import {PreviewSizeContext} from '../state/preview-size';
-import {JSONViewer} from './JSONViewer';
-import {Spacing} from './layout';
+import {FilePreview} from './FilePreview';
import {Spinner} from './Spinner';
-import {TextViewer} from './TextViewer';
const spinnerContainer: React.CSSProperties = {
display: 'flex',
@@ -40,7 +38,13 @@ const errMsgStyle: React.CSSProperties = {
color: LIGHT_TEXT,
};
-type AssetFileType = 'audio' | 'video' | 'image' | 'json' | 'txt' | 'other';
+export type AssetFileType =
+ | 'audio'
+ | 'video'
+ | 'image'
+ | 'json'
+ | 'txt'
+ | 'other';
export const getPreviewFileType = (fileName: string | null): AssetFileType => {
if (!fileName) {
return 'other';
@@ -127,61 +131,29 @@ const AssetComponent: React.FC<{currentAsset: string}> = ({currentAsset}) => {
);
}
- const fileSize = () => {
+ const fileSize = (() => {
const fileFromStaticFiles = staticFiles.find(
(file) => file.name === currentAsset,
);
+
if (fileFromStaticFiles) {
return formatBytes(fileFromStaticFiles?.sizeInBytes);
}
- };
+
+ return '';
+ })();
if (!currentAsset) {
return null;
}
- if (fileType === 'audio') {
- try {
- return (
- <div>
- <audio src={staticFileSrc} controls />
- </div>
- );
- } catch (err) {
- return <div style={errMsgStyle}>The audio could not be loaded</div>;
- }
- }
-
- if (fileType === 'video') {
- try {
- return <video src={staticFileSrc} controls />;
- } catch (err) {
- return <div style={errMsgStyle}>The video could not be loaded</div>;
- }
- }
-
- if (fileType === 'image') {
- try {
- return <img src={staticFileSrc} />;
- } catch (err) {
- return <div style={errMsgStyle}>The image could not be loaded</div>;
- }
- }
-
- if (fileType === 'json') {
- return <JSONViewer src={staticFileSrc} />;
- }
-
- if (fileType === 'txt') {
- return <TextViewer src={staticFileSrc} />;
- }
-
return (
- <>
- <div style={msgStyle}>{currentAsset}</div>
- <Spacing y={1} />
- <div style={msgStyle}>Size: {fileSize()} </div>
- </>
+ <FilePreview
+ currentAsset={currentAsset}
+ fileSize={fileSize}
+ fileType={fileType}
+ staticFileSrc={staticFileSrc}
+ />
);
};
@@ -268,6 +240,8 @@ const CompWhenItHasDimensions: React.FC<{
<div style={outer}>
{canvasContent.type === 'asset' ? (
<AssetComponent currentAsset={canvasContent.asset} />
+ ) : canvasContent.type === 'output' ? (
+ <div>hi there</div>
) : (
<PortalContainer
contentDimensions={contentDimensions as Dimensions}
diff --git a/packages/cli/src/editor/components/ZoomPersistor.tsx b/packages/cli/src/editor/components/ZoomPersistor.tsx
index dd1261513e..eedc15a221 100644
--- a/packages/cli/src/editor/components/ZoomPersistor.tsx
+++ b/packages/cli/src/editor/components/ZoomPersistor.tsx
@@ -23,14 +23,25 @@ export const getZoomForComposition = (composition: string) => {
};
export const deriveCanvasContentFromUrl = (): CanvasContent | null => {
- const substrings = window.location.pathname.split('/');
+ const substrings = window.location.pathname.split('/').filter(Boolean);
const lastPart = substrings[substrings.length - 1];
- if (substrings.includes('assets')) {
+ if (substrings[0] === 'assets') {
return {
type: 'asset',
- asset: decodeURIComponent(window.location.pathname.substring(8)),
+ asset: decodeURIComponent(
+ window.location.pathname.substring('/assets/'.length),
+ ),
+ };
+ }
+
+ if (substrings[0] === 'outputs') {
+ return {
+ type: 'output',
+ path: decodeURIComponent(
+ window.location.pathname.substring('/outputs/'.length),
+ ),
};
}
diff --git a/packages/core/src/CompositionManagerContext.tsx b/packages/core/src/CompositionManagerContext.tsx
index 3cf64ba534..07891bf04a 100644
--- a/packages/core/src/CompositionManagerContext.tsx
+++ b/packages/core/src/CompositionManagerContext.tsx
@@ -17,6 +17,10 @@ export type CanvasContent =
| {
type: 'asset';
asset: string;
+ }
+ | {
+ type: 'output';
+ path: string;
};
export type CompositionManagerContext = {