Skip to content

🎥 Make videos programmatically with React

commit

Jonny Burger0006fff0f823
diff --git a/packages/cli/src/editor/components/Canvas.tsx b/packages/cli/src/editor/components/Canvas.tsx
index a312184ad2..d44510a4ac 100644
--- a/packages/cli/src/editor/components/Canvas.tsx
+++ b/packages/cli/src/editor/components/Canvas.tsx
@@ -304,13 +304,11 @@ export const Canvas: React.FC<{
setAssetResolution(null);
- const assetSrc = src;
-
const fileType = getPreviewFileType(src);
if (fileType === 'video') {
try {
- await getVideoMetadata(assetSrc).then((data) => {
+ await getVideoMetadata(src).then((data) => {
setAssetResolution({width: data.width, height: data.height});
});
} catch {
@@ -322,7 +320,7 @@ export const Canvas: React.FC<{
setAssetResolution({width: img.width, height: img.height});
};
- img.src = assetSrc;
+ img.src = src;
} else {
setAssetResolution('none');
}
diff --git a/packages/cli/src/editor/helpers/get-asset-metadata.ts b/packages/cli/src/editor/helpers/get-asset-metadata.ts
new file mode 100644
index 0000000000..2d8c628c35
--- /dev/null
+++ b/packages/cli/src/editor/helpers/get-asset-metadata.ts
@@ -0,0 +1,53 @@
+import {getVideoMetadata} from '@remotion/media-utils';
+import type {CanvasContent} from 'remotion';
+import {staticFile} from 'remotion';
+import {getPreviewFileType} from '../components/Preview';
+import type {Dimensions} from './is-current-selected-still';
+
+const getSrcFromCanvasContent = (canvasContent: CanvasContent) => {
+ if (canvasContent.type === 'asset') {
+ return staticFile(canvasContent.asset);
+ }
+
+ if (canvasContent.type === 'composition') {
+ throw new Error('cannot get dimensions for composition');
+ }
+
+ return (
+ window.remotion_staticBase.replace('static', 'outputs') + canvasContent.path
+ );
+};
+
+export const getAssetMetadata = async (
+ canvasContent: CanvasContent,
+): Promise<Dimensions | 'none' | null> => {
+ const src = getSrcFromCanvasContent(canvasContent);
+
+ const fileType = getPreviewFileType(src);
+
+ if (fileType === 'video') {
+ const resolution = await getVideoMetadata(src);
+ return {width: resolution.width, height: resolution.height};
+ }
+
+ if (fileType === 'image') {
+ const resolution = await new Promise<{
+ width: number;
+ height: number;
+ }>((resolve, reject) => {
+ const img = new Image();
+ img.onload = () => {
+ resolve({width: img.width, height: img.height});
+ };
+
+ img.onerror = () => {
+ reject(new Error('Failed to load image'));
+ };
+
+ img.src = src;
+ });
+ return {width: resolution.width, height: resolution.height};
+ }
+
+ return null;
+};