🎥 Make videos programmatically with React
`@remotion/studio`: Reuse edited Basic captions source
Jonny Burger75fcab61b4ae
diff --git a/packages/browser-studio/src/test/browser-studio-operations.test.ts b/packages/browser-studio/src/test/browser-studio-operations.test.tsindex 3144128930..62de85b0d0 100644--- a/packages/browser-studio/src/test/browser-studio-operations.test.ts+++ b/packages/browser-studio/src/test/browser-studio-operations.test.ts@@ -437,6 +437,68 @@ registerRoot(Root);`,type: 'sequence-node-paths-remapped',mutation: captionsResult.nodePathMutation,});+ const localElementPath = '/project/src/basic-captions.element.tsx';+ const customizedSource = currentProject.files[localElementPath].replace(+ 'bottom: 120',+ 'bottom: 90',+ );+ currentProject = {+ ...currentProject,+ files: {...currentProject.files, [localElementPath]: customizedSource},+ };+ const videoOffset = currentProject.files[fileName].indexOf('<Video');+ const beforeVideo = currentProject.files[fileName].slice(0, videoOffset);+ const currentSubscription = await operations.subscribeToSequenceProps({+ fileName: 'src/Composition.tsx',+ line: beforeVideo.split('\n').length,+ column: beforeVideo.length - beforeVideo.lastIndexOf('\n') - 1,+ nodePath: null,+ componentIdentity: 'dev.remotion.media.Video',+ keys: ['from', 'durationInFrames'],+ assetKeys: [],+ effects: [],+ clientId: 'browser-studio',+ videoConfigValues: {+ durationInFrames: 60,+ fps: 30,+ height: 720,+ width: 1280,+ },+ });+ if (!currentSubscription.success) {+ throw new Error('Expected the updated Video node path');+ }++ const repeatedCaptionsResult = await operations.insertBasicCaptions({+ fileName: 'src/Composition.tsx',+ nodePath: currentSubscription.nodePath.nodePath,+ durationInFrames: 20,+ captions: [+ {+ text: ' Again',+ startMs: 100,+ endMs: 500,+ timestampMs: 300,+ confidence: null,+ },+ ],+ });+ if (!repeatedCaptionsResult.success) {+ throw new Error(repeatedCaptionsResult.reason);+ }++ expect(currentProject.files[localElementPath]).toBe(customizedSource);+ expect(+ currentProject.files['/project/src/basic-captions-2.element.tsx'],+ ).toBeUndefined();+ expect(+ currentProject.files[fileName].match(/<BasicCaptions captions/g),+ ).toHaveLength(2);+ expect((await operations.undo()).success).toBe(true);+ expect(currentProject.files[localElementPath]).toBe(customizedSource);+ expect(+ currentProject.files[fileName].match(/<BasicCaptions captions/g),+ ).toHaveLength(1);expect((await operations.undo()).success).toBe(true);expect(currentProject.files[fileName]).toBe(initialSource);expect(diff --git a/packages/studio-codemods/src/basic-captions-element-source.ts b/packages/studio-codemods/src/basic-captions-element-source.tsindex 93d0adccbe..625003eeff 100644--- a/packages/studio-codemods/src/basic-captions-element-source.ts+++ b/packages/studio-codemods/src/basic-captions-element-source.ts@@ -1,3 +1,5 @@+import {parseAst} from './sequence-props/parse-ast';+export const basicCaptionsElementSource = `import React, {forwardRef, useImperativeHandle, useMemo, useRef} from 'react';import {Interactive,@@ -228,7 +230,47 @@ export const getBasicCaptionsElementFile = ({const baseName = `basic-captions${index === 0 ? '' : `-${index + 1}`}.element`;const candidate = `${directory}${baseName}.tsx`;const existing = readFileContents(candidate);- if (existing === null || existing === basicCaptionsElementSource) {+ let exportsBasicCaptions = false;+ if (existing !== null) {+ try {+ const ast = parseAst(existing);+ exportsBasicCaptions = ast.program.body.some((statement) => {+ if (statement.type !== 'ExportNamedDeclaration') {+ return false;+ }++ const {declaration} = statement;+ if (declaration?.type === 'VariableDeclaration') {+ return declaration.declarations.some(+ (item) =>+ item.id.type === 'Identifier' &&+ item.id.name === 'BasicCaptions',+ );+ }++ if (+ declaration?.type === 'FunctionDeclaration' ||+ declaration?.type === 'ClassDeclaration'+ ) {+ return declaration.id?.name === 'BasicCaptions';+ }++ return statement.specifiers.some(+ (specifier) =>+ (specifier.type === 'ExportSpecifier' &&+ specifier.exported.type === 'Identifier' &&+ specifier.exported.name === 'BasicCaptions') ||+ (specifier.type === 'ExportSpecifier' &&+ specifier.exported.type === 'StringLiteral' &&+ specifier.exported.value === 'BasicCaptions'),+ );+ });+ } catch {+ // Keep a malformed or unrelated file untouched and use another name.+ }+ }++ if (existing === null || exportsBasicCaptions) {return {fileName: candidate,importPath: `./${baseName}`,diff --git a/packages/studio-server/src/test/insert-basic-captions.test.ts b/packages/studio-server/src/test/insert-basic-captions.test.tsindex f3ce2ef8ed..0ac3542fac 100644--- a/packages/studio-server/src/test/insert-basic-captions.test.ts+++ b/packages/studio-server/src/test/insert-basic-captions.test.ts@@ -8,7 +8,10 @@ import {} from 'node:fs';import {tmpdir} from 'node:os';import path from 'node:path';-import {basicCaptionsElementSource} from '@remotion/studio-codemods';+import {+ basicCaptionsElementSource,+ getBasicCaptionsElementFile,+} from '@remotion/studio-codemods';import {insertBasicCaptions} from '../codemods/insert-basic-captions';import {createFileWatcherRegistry,@@ -141,3 +144,50 @@ export const Comp = () => <Audio src="voice.mp3" />;`;expect(output).toContain('<BasicCaptions captions={[');expect(output).toContain('durationInFrames={30}');});++test('keeps edited Basic captions source and adds another instance for the same clip', () => {+ const customizedSource = basicCaptionsElementSource.replace(+ 'bottom: 120',+ 'bottom: 90',+ );+ const localElement = getBasicCaptionsElementFile({+ fileName: '/project/Comp.tsx',+ readFileContents: (candidate) =>+ candidate === '/project/basic-captions.element.tsx'+ ? customizedSource+ : null,+ });+ expect(localElement).toEqual({+ fileName: '/project/basic-captions.element.tsx',+ importPath: './basic-captions.element',+ shouldWrite: false,+ });++ const input = `import {Audio} from 'remotion';+export const Comp = () => <Audio src="voice.mp3" />;`;+ const captions = [+ {+ text: ' Hello',+ startMs: 0,+ endMs: 500,+ timestampMs: null,+ confidence: null,+ },+ ];+ const first = insertBasicCaptions({+ input,+ nodePath: lineContainingToNodePath(input, '<Audio'),+ captions,+ durationInFrames: 30,+ importPath: localElement.importPath,+ });+ const second = insertBasicCaptions({+ input: first.output,+ nodePath: lineContainingToNodePath(first.output, '<Audio'),+ captions,+ durationInFrames: 30,+ importPath: localElement.importPath,+ });+ expect(second.output.match(/<BasicCaptions captions/g)).toHaveLength(2);+ expect(second.output.match(/import \{BasicCaptions\}/g)).toHaveLength(1);+});