🎥 Make videos programmatically with React
@remotion/studio-codemods: Split video and audio without Prettier
Jonny Burger1b79db97efe8
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.tsindex 3b8499de77..65e448ed7b 100644--- a/packages/browser-studio/src/browser-studio-operations.ts+++ b/packages/browser-studio/src/browser-studio-operations.ts@@ -1894,7 +1894,6 @@ export const createBrowserStudioOperations = ({const result = await splitVideoFromAudioCodemod({input: project.files[absolutePath],nodePath,- formatFile: formatCodemodFile,});const nodePathMutation = controller.applyMutation({undoRedoNavigation: null,diff --git a/packages/studio-codemods/src/split-video-from-audio.ts b/packages/studio-codemods/src/split-video-from-audio.tsindex a03117dc0d..fb2d887e9a 100644--- a/packages/studio-codemods/src/split-video-from-audio.ts+++ b/packages/studio-codemods/src/split-video-from-audio.ts@@ -19,8 +19,22 @@ import {captureJsxNodePaths,getNodePathRemappings,} from './get-node-path-remappings';+import {indentInsertedJsx, printInsertedJsx} from './print-jsx';+import {recastLocToOffset} from './recast-loc-to-offset';import {ensureNamedImport} from './sequence-props/imports';-import {parseAst, serializeAst} from './sequence-props/parse-ast';+import {parseAst} from './sequence-props/parse-ast';+import {+ applySourceEdits,+ captureImportSnapshots,+ getInsertImportSourceEdits,+ type SourceEdit,+} from './source-edits';+import {+ getEndOfLine,+ getIndentationUnit,+ getLineIndent,+ indentContinuationLines,+} from './source-style';const {namedTypes} = recast.types;@@ -153,6 +167,186 @@ const makeFragment = (first: JSXElement, second: JSXElement): JSXFragment => ({children: [first, second],});+const getMutedSourceEdit = ({+ element,+ input,+ prettierConfigOverride,+}: {+ element: JSXElement;+ input: string;+ prettierConfigOverride: Record<string, unknown> | null;+}): SourceEdit => {+ const existingMuted = element.openingElement.attributes.find(+ (attribute) => getAttributeName(attribute) === 'muted',+ );+ if (existingMuted) {+ if (!existingMuted.loc) {+ throw new Error('Could not locate the muted attribute');+ }++ return {+ start: recastLocToOffset(input, existingMuted.loc.start),+ end: recastLocToOffset(input, existingMuted.loc.end),+ replacement: 'muted',+ };+ }++ const {openingElement} = element;+ if (!openingElement.loc) {+ throw new Error('Could not locate the video opening element');+ }++ const openingStart = recastLocToOffset(input, openingElement.loc.start);+ const openingEnd = recastLocToOffset(input, openingElement.loc.end);+ const closingStart = openingEnd - (openingElement.selfClosing ? 2 : 1);+ const lineStart = input.lastIndexOf('\n', closingStart - 1) + 1;+ const beforeClosing = input.slice(lineStart, closingStart);++ if (lineStart > openingStart && /^[\t ]*$/.test(beforeClosing)) {+ const lastAttribute = openingElement.attributes.at(-1);+ const attributeIndent = lastAttribute?.loc+ ? getLineIndent({+ input,+ offset: recastLocToOffset(input, lastAttribute.loc.start),+ })+ : `${getLineIndent({input, offset: openingStart})}${getIndentationUnit(+ input,+ prettierConfigOverride,+ )}`;++ return {+ start: lineStart,+ end: lineStart,+ replacement: `${attributeIndent}muted${getEndOfLine(input)}`,+ };+ }++ const trailingWhitespace = beforeClosing.match(/[\t ]*$/)?.[0] ?? '';+ return {+ start: closingStart - trailingWhitespace.length,+ end: closingStart - trailingWhitespace.length,+ replacement: ' muted',+ };+};++const applyEditToElementSource = ({+ edit,+ element,+ input,+}: {+ edit: SourceEdit;+ element: JSXElement;+ input: string;+}) => {+ if (!element.loc) {+ throw new Error('Could not locate the video element');+ }++ const start = recastLocToOffset(input, element.loc.start);+ const end = recastLocToOffset(input, element.loc.end);+ if (edit.start < start || edit.end > end) {+ throw new Error('Muted attribute edit is outside the video element');+ }++ const source =+ input.slice(start, edit.start) ++ edit.replacement ++ input.slice(edit.end, end);+ const originalIndent = getLineIndent({input, offset: start});+ return source+ .split(/\r?\n/)+ .map((line, index) => {+ if (index === 0) {+ return line;+ }++ return line.startsWith(originalIndent)+ ? line.slice(originalIndent.length)+ : line.trimStart();+ })+ .join(getEndOfLine(input));+};++const getAudioSiblingSourceEdit = ({+ audioSource,+ element,+ input,+}: {+ audioSource: string;+ element: JSXElement;+ input: string;+}): SourceEdit => {+ if (!element.loc) {+ throw new Error('Could not locate the video element');+ }++ const start = recastLocToOffset(input, element.loc.start);+ const end = recastLocToOffset(input, element.loc.end);+ const indent = getLineIndent({input, offset: start});+ const lineEnd = input.indexOf('\n', end);+ const sourceAfterElement = input.slice(+ end,+ lineEnd === -1 ? input.length : lineEnd,+ );+ const isStandalone = sourceAfterElement.trim() === '';+ const lineStart = input.lastIndexOf('\n', start - 1) + 1;+ const whitespaceBeforeElement =+ input.slice(lineStart, start).match(/[\t ]*$/)?.[0] ?? '';++ return {+ start: end,+ end,+ replacement: isStandalone+ ? `${getEndOfLine(input)}${indentInsertedJsx({+ indent,+ insertion: audioSource,+ })}`+ : `${whitespaceBeforeElement}${indentContinuationLines({+ indent,+ input,+ printed: audioSource,+ })}`,+ };+};++const getFragmentSourceEdit = ({+ audioSource,+ element,+ input,+ mutedEdit,+ prettierConfigOverride,+}: {+ audioSource: string;+ element: JSXElement;+ input: string;+ mutedEdit: SourceEdit;+ prettierConfigOverride: Record<string, unknown> | null;+}): SourceEdit => {+ if (!element.loc) {+ throw new Error('Could not locate the video element');+ }++ const start = recastLocToOffset(input, element.loc.start);+ const end = recastLocToOffset(input, element.loc.end);+ const indent = getLineIndent({input, offset: start});+ const unit = getIndentationUnit(input, prettierConfigOverride);+ const fragment = [+ '<>',+ indentInsertedJsx({+ indent: unit,+ insertion: applyEditToElementSource({edit: mutedEdit, element, input}),+ }),+ indentInsertedJsx({indent: unit, insertion: audioSource}),+ '</>',+ ].join(getEndOfLine(input));++ return {+ start,+ end,+ replacement: indentContinuationLines({indent, input, printed: fragment}),+ };+};+const insertAfter = (parentNode: Node,node: JSXElement,@@ -191,15 +385,16 @@ const insertAfter = (return false;};-export const splitVideoFromAudio = async ({+export const splitVideoFromAudio = ({input,nodePath,- formatFile,prettierConfigOverride,}: {input: string;nodePath: SequenceNodePath;- formatFile: (input: {+ // Kept optional for compatibility with callers from before source edits+ // replaced the full-file formatting pass.+ formatFile?: (input: {contents: string;prettierConfigOverride: Record<string, unknown> | null;}) => Promise<{output: string; formatted: boolean}>;@@ -213,6 +408,7 @@ export const splitVideoFromAudio = async ({}> => {const ast = parseAst(input);const capturedNodePaths = captureJsxNodePaths(ast);+ const importSnapshots = captureImportSnapshots(ast);const jsxPath = findJsxElementPathForDeletion(ast, nodePath);if (!jsxPath) {throw new Error(@@ -251,12 +447,19 @@ export const splitVideoFromAudio = async ({);}+ const formattingConfig = prettierConfigOverride ?? null;+ const mutedEdit = getMutedSourceEdit({+ element: jsxElement,+ input,+ prettierConfigOverride: formattingConfig,+ });const audioLocalName = ensureNamedImport({ast,importedName: 'Audio',sourcePath: importSource,localName: 'Audio',});+ const originalAttributeSources = new Map<object, string>();const audioElement: JSXElement = {type: 'JSXElement',openingElement: {@@ -267,12 +470,22 @@ export const splitVideoFromAudio = async ({const name = getAttributeName(attribute);return name !== null && audioProps.includes(name);})- .map((attribute) => cloneAstValue(attribute)),+ .map((attribute) => {+ const clone = cloneAstValue(attribute);+ originalAttributeSources.set(clone, recast.print(clone).code);+ return clone;+ }),selfClosing: true,},closingElement: null,children: [],};+ const audioSource = printInsertedJsx({+ element: audioElement as never,+ input,+ originalAttributeSources,+ prettierConfigOverride: formattingConfig,+ });setBareMutedAttribute(jsxElement);@@ -281,14 +494,43 @@ export const splitVideoFromAudio = async ({throw new Error('Cannot split audio from a JSX element with no parent');}- if (!insertAfter(parentPath.node, jsxElement, audioElement)) {+ const insertedAfter = insertAfter(parentPath.node, jsxElement, audioElement);+ if (!insertedAfter) {jsxPath.replace(makeFragment(jsxElement, audioElement));}- const finalFile = serializeAst(ast);- const {output, formatted} = await formatFile({- contents: finalFile,- prettierConfigOverride: prettierConfigOverride ?? null,+ const insertedAsJsxSibling =+ insertedAfter &&+ (parentPath.node.type === 'JSXElement' ||+ parentPath.node.type === 'JSXFragment');+ const output = applySourceEdits({+ input,+ edits: [+ ...getInsertImportSourceEdits({+ ast,+ input,+ prettierConfigOverride: formattingConfig,+ snapshots: importSnapshots,+ }),+ ...(insertedAsJsxSibling+ ? [+ mutedEdit,+ getAudioSiblingSourceEdit({+ audioSource,+ element: jsxElement,+ input,+ }),+ ]+ : [+ getFragmentSourceEdit({+ audioSource,+ element: jsxElement,+ input,+ mutedEdit,+ prettierConfigOverride: formattingConfig,+ }),+ ]),+ ],});const {nodePathRemappings} = getNodePathRemappings({ast,@@ -296,14 +538,14 @@ export const splitVideoFromAudio = async ({output,});- return {+ return Promise.resolve({output,- formatted,+ formatted: true,nodeLabel,logLine:jsxElement.openingElement.loc?.start.line ??jsxElement.loc?.start.line ??1,nodePathRemappings,- };+ });};diff --git a/packages/studio-server/src/codemods/split-video-from-audio.ts b/packages/studio-server/src/codemods/split-video-from-audio.tsindex f359c2ea15..a0b0085c03 100644--- a/packages/studio-server/src/codemods/split-video-from-audio.ts+++ b/packages/studio-server/src/codemods/split-video-from-audio.ts@@ -1,23 +1 @@-import {splitVideoFromAudio as splitVideoFromAudioCodemod} from '@remotion/studio-codemods';-import type {SequenceNodePath} from 'remotion';-import {formatFileContent} from './format-file-content';--export const splitVideoFromAudio = ({- input,- nodePath,- prettierConfigOverride,-}: {- input: string;- nodePath: SequenceNodePath;- prettierConfigOverride?: Record<string, unknown> | null;-}) =>- splitVideoFromAudioCodemod({- input,- nodePath,- formatFile: ({contents, prettierConfigOverride: override}) =>- formatFileContent({- input: contents,- prettierConfigOverride: override,- }),- prettierConfigOverride,- });+export {splitVideoFromAudio} from '@remotion/studio-codemods';diff --git a/packages/studio-server/src/test/split-video-from-audio.test.ts b/packages/studio-server/src/test/split-video-from-audio.test.tsindex 4901cbc079..96650210db 100644--- a/packages/studio-server/src/test/split-video-from-audio.test.ts+++ b/packages/studio-server/src/test/split-video-from-audio.test.ts@@ -54,6 +54,65 @@ test('splitVideoFromAudio mutes the video and adds an Audio with the same timing);});+test('splitVideoFromAudio preserves unrelated source formatting without calling the formatter', async () => {+ const input = `import{Video}from"@remotion/media"++const untouched = {keep : "spacing"}+export const Comp=()=>(+ <>+ <Video+ src = {"video.mp4"}+ from = {10}+ style = {{opacity:.5}}+ />+ <div data-value = "unchanged"/>+ </>+)+`;+ let formatCalls = 0;+ const {formatted, nodePathRemappings, output} = await splitVideoFromAudio({+ input,+ nodePath: lineContainingToNodePath(input, '<Video'),+ formatFile: () => {+ formatCalls++;+ throw new Error('Prettier must not be called');+ },+ });++ expect(formatCalls).toBe(0);+ expect(formatted).toBe(true);+ expect(output).toBe(`import{Video, Audio}from"@remotion/media"++const untouched = {keep : "spacing"}+export const Comp=()=>(+ <>+ <Video+ src = {"video.mp4"}+ from = {10}+ style = {{opacity:.5}}+ muted+ />+ <Audio src = {"video.mp4"} from = {10} />+ <div data-value = "unchanged"/>+ </>+)+`);+ expect(nodePathRemappings).toEqual([+ {+ oldNodePath: lineContainingToNodePath(input, '<Video'),+ newNodePath: lineContainingToNodePath(output, '<Video'),+ },+ {+ oldNodePath: lineContainingToNodePath(input, '<div'),+ newNodePath: lineContainingToNodePath(output, '<div'),+ },+ {+ oldNodePath: null,+ newNodePath: lineContainingToNodePath(output, '<Audio'),+ },+ ]);+});+test('splitVideoFromAudio does not copy non-audio props', async () => {const output = await split('<Video src="video.mp4" name="my video" freeze={20} crop={{x: 0, y: 0, width: 100, height: 100}} />',@@ -62,6 +121,67 @@ test('splitVideoFromAudio does not copy non-audio props', async () => {expect(output).toContain('<Audio src="video.mp4" />');});+test('splitVideoFromAudio wraps a root video in a fragment', async () => {+ const input = `import {Video} from '@remotion/media';++export const Comp = () => <Video src="video.mp4" />;+`;+ const {output} = await splitVideoFromAudio({+ input,+ nodePath: lineContainingToNodePath(input, '<Video'),+ });++ expect(output).toBe(`import {Video, Audio} from '@remotion/media';++export const Comp = () => <>+ <Video src="video.mp4" muted />+ <Audio src="video.mp4" />+</>;+`);+});++test('splitVideoFromAudio preserves CRLF and tab indentation', async () => {+ const input = [+ `import {Video} from '@remotion/media';`,+ '',+ 'export const Comp = () => {',+ '\treturn (',+ '\t\t<>',+ '\t\t\t<Video',+ '\t\t\t\tsrc="video.mp4"',+ '\t\t\t\ttrimBefore={10}',+ '\t\t\t/>',+ '\t\t</>',+ '\t);',+ '};',+ '',+ ].join('\r\n');+ const {output} = await splitVideoFromAudio({+ input,+ nodePath: lineContainingToNodePath(input, '<Video'),+ });++ expect(output).toBe(+ [+ `import {Video, Audio} from '@remotion/media';`,+ '',+ 'export const Comp = () => {',+ '\treturn (',+ '\t\t<>',+ '\t\t\t<Video',+ '\t\t\t\tsrc="video.mp4"',+ '\t\t\t\ttrimBefore={10}',+ '\t\t\t\tmuted',+ '\t\t\t/>',+ '\t\t\t<Audio src="video.mp4" trimBefore={10} />',+ '\t\t</>',+ '\t);',+ '};',+ '',+ ].join('\r\n'),+ );+});+test('splitVideoFromAudio replaces an existing muted prop', async () => {const output = await split('<Video src="video.mp4" muted={false} />');@@ -79,6 +199,18 @@ test('splitVideoFromAudio reuses an existing Audio import', async () => {expect(output.match(/from '@remotion\/media'/g)?.length).toBe(1);});+test('splitVideoFromAudio uses an aliased Audio import', async () => {+ const output = await split(+ '<Video src="video.mp4" />',+ `import {Audio as Sound, Video} from '@remotion/media';`,+ );++ expect(output).toContain(+ `import {Audio as Sound, Video} from '@remotion/media';`,+ );+ expect(output).toContain('<Sound src="video.mp4" />');+});+test('splitVideoFromAudio imports Audio from the same module as the tag', async () => {const output = await split('<OffthreadVideo src="video.mp4" from={5} />',@@ -170,14 +302,19 @@ test('splitVideoFromAudioHandler writes success and failure responses', async ()try {clearUndoStack();const entryPoint = path.join(remotionRoot, 'Root.tsx');- const input = wrap('<Video src="video.mp4" from={0} trimBefore={10} />');+ const input = wrap(+ '<Video src="video.mp4" from={0} trimBefore={10} />',+ ).replace(+ '\n\nexport const Comp',+ '\n\nconst untouched = { value : "keep" };\n\nexport const Comp',+ );writeFileSync(entryPoint, input);const success = await splitVideoFromAudioHandler(getHandlerOptions({input: {fileName: entryPoint,- nodePath: lineColumnToNodePath(input, elementLine),+ nodePath: lineContainingToNodePath(input, '<Video'),},entryPoint,remotionRoot,@@ -192,6 +329,7 @@ test('splitVideoFromAudioHandler writes success and failure responses', async ()expect(written).toContain('<Audio src="video.mp4" from={0} trimBefore={10} />',);+ expect(written).toContain('const untouched = { value : "keep" };');expect(getUndoStack().length).toBe(1);const failureInput = wrap('<Video from={0} />');