Skip to content

🎥 Make videos programmatically with React

this works nicely

Jonny Burger9382ec322fb9
diff --git a/packages/studio-server/src/preview-server/api-routes.ts b/packages/studio-server/src/preview-server/api-routes.ts
index 765ee25394..73894d00b5 100644
--- a/packages/studio-server/src/preview-server/api-routes.ts
+++ b/packages/studio-server/src/preview-server/api-routes.ts
@@ -6,6 +6,7 @@ import {canUpdateDefaultPropsHandler} from './routes/can-update-default-props';
import {handleCancelRender} from './routes/cancel-render';
import {handleCopyStillToClipboard} from './routes/copy-still-to-clipboard-handler';
import {handleOpenInFileExplorer} from './routes/open-in-file-explorer';
+import {projectInfoHandler} from './routes/project-info';
import {handleRemoveRender} from './routes/remove-render';
import {subscribeToFileExistence} from './routes/subscribe-to-file-existence';
import {unsubscribeFromFileExistence} from './routes/unsubscribe-from-file-existence';
@@ -29,4 +30,5 @@ export const allApiRoutes: {
'/api/apply-codemod': applyCodemodHandler,
'/api/can-update-default-props': canUpdateDefaultPropsHandler,
'/api/update-available': handleUpdate,
+ '/api/project-info': projectInfoHandler,
};
diff --git a/packages/studio-server/src/preview-server/routes/project-info.ts b/packages/studio-server/src/preview-server/routes/project-info.ts
new file mode 100644
index 0000000000..acee4f08f9
--- /dev/null
+++ b/packages/studio-server/src/preview-server/routes/project-info.ts
@@ -0,0 +1,14 @@
+import type {
+ ProjectInfoRequest,
+ ProjectInfoResponse,
+} from '@remotion/studio-shared';
+import type {ApiHandler} from '../api-types';
+import {getProjectInfo} from '../project-info';
+
+export const projectInfoHandler: ApiHandler<
+ ProjectInfoRequest,
+ ProjectInfoResponse
+> = async ({remotionRoot}) => {
+ const info = await getProjectInfo(remotionRoot);
+ return {projectInfo: info};
+};
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 227772e939..701ab66550 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -11,6 +11,7 @@ import type {
} from '@remotion/renderer';
import type {RecastCodemod} from './codemods';
import type {PackageManager} from './package-manager';
+import type {ProjectInfo} from './project-info';
import type {RequiredChromiumOptions} from './render-job';
import type {EnumPath} from './stringify-default-props';
@@ -165,6 +166,11 @@ export type UpdateAvailableResponse = {
packageManager: PackageManager | 'unknown';
};
+export type ProjectInfoRequest = {};
+export type ProjectInfoResponse = {
+ projectInfo: ProjectInfo;
+};
+
export type ApiRoutes = {
'/api/cancel': ReqAndRes<CancelRenderRequest, CancelRenderResponse>;
'/api/render': ReqAndRes<AddRenderRequest, undefined>;
@@ -192,4 +198,5 @@ export type ApiRoutes = {
UpdateAvailableResponse
>;
'/api/apply-codemod': ReqAndRes<ApplyCodemodRequest, ApplyCodemodResponse>;
+ '/api/project-info': ReqAndRes<ProjectInfoRequest, ProjectInfoResponse>;
};
diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.ts
index 7dcc2e586a..61bea42f4e 100644
--- a/packages/studio-shared/src/index.ts
+++ b/packages/studio-shared/src/index.ts
@@ -10,6 +10,8 @@ export {
CanUpdateDefaultPropsResponse,
CopyStillToClipboardRequest,
OpenInFileExplorerRequest,
+ ProjectInfoRequest,
+ ProjectInfoResponse,
RemoveRenderRequest,
SubscribeToFileExistenceRequest,
SubscribeToFileExistenceResponse,
diff --git a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
index c0d4e785b3..0213ac123d 100644
--- a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
+++ b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
@@ -1,3 +1,4 @@
+import type {ProjectInfo} from '@remotion/studio-shared';
import type {ChangeEventHandler} from 'react';
import React, {
useCallback,
@@ -23,10 +24,12 @@ import {Button} from '../Button';
import {Flex, Row, Spacing} from '../layout';
import {ModalContainer} from '../ModalContainer';
import {NewCompHeader} from '../ModalHeader';
+import {notificationCenter} from '../Notifications/NotificationCenter';
import {
ResolveCompositionBeforeModal,
ResolvedCompositionContext,
} from '../RenderModal/ResolveCompositionBeforeModal';
+import {getProjectInfo} from '../RenderQueue/actions';
import type {ComboboxValue} from './ComboBox';
import {Combobox} from './ComboBox';
import {InputDragger} from './InputDragger';
@@ -69,6 +72,7 @@ const DuplicateCompositionLoaded: React.FC<{
const [selectedFrameRate, setFrameRate] = useState<string>(
String(resolved.result.fps),
);
+ const [projectInfo, setProjectInfo] = useState<ProjectInfo | null>(null);
const {compositions} = useContext(Internals.CompositionManager);
const [type, setType] = useState<CompType>(initialCompType);
const [name, setName] = useState(() => {
@@ -91,11 +95,33 @@ const DuplicateCompositionLoaded: React.FC<{
return currentName;
});
+
const [size, setSize] = useState(() => ({
width: String(resolved.result.width),
height: String(resolved.result.height),
}));
+ useEffect(() => {
+ const controller = new AbortController();
+
+ getProjectInfo(controller.signal)
+ .then((info) => {
+ setProjectInfo(info.projectInfo);
+ })
+ .catch((err) => {
+ notificationCenter.current?.addNotification({
+ content: `Could not get project info: ${err.message}. Unable to duplicate composition`,
+ created: Date.now(),
+ duration: 3000,
+ id: String(Math.random()),
+ });
+ });
+
+ return () => {
+ controller.abort();
+ };
+ }, []);
+
const [lockedAspectRatio, setLockedAspectRatio] = useState(
loadAspectRatioOption() ? Number(size.width) / Number(size.height) : null,
);
@@ -233,7 +259,8 @@ const DuplicateCompositionLoaded: React.FC<{
const valid =
compNameErrMessage === null &&
compWidthErrMessage === null &&
- compHeightErrMessage === null;
+ compHeightErrMessage === null &&
+ projectInfo !== null;
const {registerKeybinding} = useKeybinding();
@@ -441,7 +468,7 @@ const DuplicateCompositionLoaded: React.FC<{
backgroundColor: valid ? BLUE : BLUE_DISABLED,
}}
>
- Add to Root.tsx
+ {projectInfo ? `Add to ${projectInfo.relativeRootFile}` : 'Add'}
<ShortcutHint keyToPress="↵" cmdOrCtrl />
</Button>
</Row>
diff --git a/packages/studio/src/components/RenderQueue/actions.ts b/packages/studio/src/components/RenderQueue/actions.ts
index 7fc5fbe3bd..09f4b70933 100644
--- a/packages/studio/src/components/RenderQueue/actions.ts
+++ b/packages/studio/src/components/RenderQueue/actions.ts
@@ -364,6 +364,10 @@ export const updateAvailable = (signal: AbortSignal) => {
return callApi('/api/update-available', {}, signal);
};
+export const getProjectInfo = (signal: AbortSignal) => {
+ return callApi('/api/project-info', {}, signal);
+};
+
export const updateDefaultProps = (
compositionId: string,
defaultProps: Record<string, unknown>,