Skip to content

🎥 Make videos programmatically with React

Elements: Preserve Element duration in Browser Studio

Igor Samokhovets9e6a511bf6bd
diff --git a/packages/browser-studio/e2e/app.tsx b/packages/browser-studio/e2e/app.tsx
index 6cef3cbca1..7c29391385 100644
--- a/packages/browser-studio/e2e/app.tsx
+++ b/packages/browser-studio/e2e/app.tsx
@@ -19,7 +19,9 @@ const render = async () => {
? await loadGitHubRepository({
repoUrl: 'https://github.com/remotion-dev/opfs-fixture',
})
- : createBlankTemplateProject();
+ : createBlankTemplateProject({
+ durationInFrames: initialElementPayload?.durationInFrames ?? null,
+ });
(
window as typeof window & {
__browserStudioProject: VirtualProject;
diff --git a/packages/browser-studio/e2e/browser-studio.test.ts b/packages/browser-studio/e2e/browser-studio.test.ts
index e4dd251d97..c1f89b36af 100644
--- a/packages/browser-studio/e2e/browser-studio.test.ts
+++ b/packages/browser-studio/e2e/browser-studio.test.ts
@@ -1007,7 +1007,7 @@ test('confirms and imports an Element payload from the URL fragment', async ({
dependencies: [{name: '@remotion/shapes', version: null}],
dimensions: {height: 180, width: 320},
displayName: 'Linked Element',
- durationInFrames: 60,
+ durationInFrames: 150,
installationMode: 'wrapped',
slug: 'linked-element',
sourceCode: `import {Rect} from '@remotion/shapes';
@@ -1073,6 +1073,20 @@ export const LinkedElement = () => <Rect width={320} height={180} fill="red" />;
composition: expect.stringContaining('<LinkedElement />'),
element: expect.stringContaining("import {Rect} from '@remotion/shapes'"),
});
+ const composition = await page.evaluate(() => {
+ const browserWindow = window as typeof window & {
+ __browserStudioProject: {files: Record<string, string>};
+ };
+ return browserWindow.__browserStudioProject.files[
+ '/project/src/Composition.tsx'
+ ];
+ });
+ expect(composition).toMatch(
+ /<Composition\b(?=[^>]*\bdurationInFrames=\{150\})[^>]*\/>/,
+ );
+ expect(composition).toMatch(
+ /<Sequence\b(?=[^>]*\bdurationInFrames=\{150\})[^>]*>/,
+ );
expect(
await studio.locator('body').evaluate(
() =>
diff --git a/packages/browser-studio/src/templates/blank.ts b/packages/browser-studio/src/templates/blank.ts
index 8b959abac3..f367a5005f 100644
--- a/packages/browser-studio/src/templates/blank.ts
+++ b/packages/browser-studio/src/templates/blank.ts
@@ -1,22 +1,7 @@
import type {VirtualProject} from '../types';
-export const blankTemplateFiles = {
- '/project/src/index.ts': `import { registerRoot } from "remotion";
-import { RemotionRoot } from "./Root";
-
-registerRoot(RemotionRoot);
-`,
- '/project/src/Root.tsx': `import { MyComposition } from "./Composition";
-
-export const RemotionRoot: React.FC = () => {
- return (
- <>
- <MyComposition />
- </>
- );
-};
-`,
- '/project/src/Composition.tsx': `import { CalculateMetadataFunction, Composition } from "remotion";
+const makeBlankCompositionFile = (durationInFrames: number) =>
+ `import { CalculateMetadataFunction, Composition } from "remotion";
type Props = {};
@@ -29,7 +14,7 @@ export const MyComposition = () => {
<Composition
id="MyComp"
component={MyComponent}
- durationInFrames={60}
+ durationInFrames={${durationInFrames}}
fps={30}
width={1280}
height={720}
@@ -41,7 +26,25 @@ export const MyComposition = () => {
export const MyComponent: React.FC<Props> = () => {
return null;
};
+`;
+
+export const blankTemplateFiles = {
+ '/project/src/index.ts': `import { registerRoot } from "remotion";
+import { RemotionRoot } from "./Root";
+
+registerRoot(RemotionRoot);
+`,
+ '/project/src/Root.tsx': `import { MyComposition } from "./Composition";
+
+export const RemotionRoot: React.FC = () => {
+ return (
+ <>
+ <MyComposition />
+ </>
+ );
+};
`,
+ '/project/src/Composition.tsx': makeBlankCompositionFile(60),
'/project/package.json': `{
"name": "template-empty",
"version": "1.0.0",
@@ -103,8 +106,27 @@ Config.setOverwriteOutput(true);
`,
} as const;
-export const createBlankTemplateProject = (): VirtualProject => ({
- rootDir: '/project',
- entryPoint: '/project/src/index.ts',
- files: {...blankTemplateFiles},
-});
+export const createBlankTemplateProject = (
+ {durationInFrames}: {readonly durationInFrames: number | null} = {
+ durationInFrames: null,
+ },
+): VirtualProject => {
+ const resolvedDurationInFrames = durationInFrames ?? 60;
+ if (
+ !Number.isInteger(resolvedDurationInFrames) ||
+ resolvedDurationInFrames <= 0
+ ) {
+ throw new TypeError('durationInFrames must be a positive integer');
+ }
+
+ return {
+ rootDir: '/project',
+ entryPoint: '/project/src/index.ts',
+ files: {
+ ...blankTemplateFiles,
+ '/project/src/Composition.tsx': makeBlankCompositionFile(
+ resolvedDurationInFrames,
+ ),
+ },
+ };
+};
diff --git a/packages/browser-studio/src/test/template-blank.test.ts b/packages/browser-studio/src/test/template-blank.test.ts
index 7bfa82cbee..91be6ed6e7 100644
--- a/packages/browser-studio/src/test/template-blank.test.ts
+++ b/packages/browser-studio/src/test/template-blank.test.ts
@@ -37,10 +37,15 @@ test('blank template virtual files match the source template', () => {
);
});
-test('createBlankTemplateProject points to the virtual entry point', () => {
+test('createBlankTemplateProject uses the requested composition duration', () => {
expect(createBlankTemplateProject()).toEqual({
rootDir: '/project',
entryPoint: '/project/src/index.ts',
files: {...blankTemplateFiles},
});
+
+ const project = createBlankTemplateProject({durationInFrames: 150});
+ expect(project.files['/project/src/Composition.tsx']).toContain(
+ 'durationInFrames={150}',
+ );
});
diff --git a/packages/docs/standalone/experimental-new/index.tsx b/packages/docs/standalone/experimental-new/index.tsx
index 7e3ef66c36..ee7bcff167 100644
--- a/packages/docs/standalone/experimental-new/index.tsx
+++ b/packages/docs/standalone/experimental-new/index.tsx
@@ -76,10 +76,22 @@ type ProjectState =
}
| {type: 'error'; repoUrl: string; message: string};
-const getInitialProjectState = (): ProjectState => {
+const createBlankProject = (
+ initialElementState: InitialElementState,
+): VirtualProject =>
+ createBlankTemplateProject({
+ durationInFrames:
+ initialElementState.type === 'payload'
+ ? initialElementState.payload.payload.durationInFrames
+ : null,
+ });
+
+const getInitialProjectState = (
+ initialElementState: InitialElementState,
+): ProjectState => {
const repoUrl = new URLSearchParams(window.location.search).get('repo');
if (!repoUrl) {
- return {type: 'ready', project: createBlankTemplateProject()};
+ return {type: 'ready', project: createBlankProject(initialElementState)};
}
return {
@@ -116,7 +128,9 @@ const getInitialElementState = (): InitialElementState => {
const BrowserStudioContent: React.FC = () => {
const [initialElementState] = useState(getInitialElementState);
- const [projectState, setProjectState] = useState(getInitialProjectState);
+ const [projectState, setProjectState] = useState(() =>
+ getInitialProjectState(initialElementState),
+ );
const loadingRepoUrl =
projectState.type === 'loading' ? projectState.repoUrl : null;
@@ -233,7 +247,7 @@ const BrowserStudioContent: React.FC = () => {
<button
onClick={() =>
setProjectState({
- project: createBlankTemplateProject(),
+ project: createBlankProject(initialElementState),
type: 'ready',
})
}