🎥 Make videos programmatically with React
@remotion/studio: Set interpolate output types for keyframes
Jonny Burgerd582dc919542
diff --git a/packages/studio-codemods/src/sequence-props.ts b/packages/studio-codemods/src/sequence-props.tsindex 24d7e7f86c..024cdd461a 100644--- a/packages/studio-codemods/src/sequence-props.ts+++ b/packages/studio-codemods/src/sequence-props.ts@@ -21,6 +21,7 @@ import type {CanUpdateSequencePropStatus,ExtrapolateType,InterpolateOutputOption,+ InterpolateOutputType,JsxComponentIdentity,SequenceNodePath,VideoConfigNumericExpression,@@ -300,6 +301,30 @@ const getInterpolateOutputOption = (return null;};+const getInterpolateOutputType = (+ node: Expression,+): InterpolateOutputType | null => {+ if (node.type === 'StringLiteral') {+ if (+ node.value === 'font-weight' ||+ node.value === 'scale' ||+ node.value === 'translate' ||+ node.value === 'rotate' ||+ node.value === 'transform-origin'+ ) {+ return node.value;+ }++ return null;+ }++ if (node.type === 'TSAsExpression') {+ return getInterpolateOutputType(node.expression as Expression);+ }++ return null;+};+const getKeyframeEasing = (node: Expression): PropEasing[number] | null =>parseKeyframeEasingExpression(node);@@ -467,6 +492,17 @@ const getInterpolationMetadata = (continue;}+ if (key === 'outputType') {+ if (+ interpolationFunction === 'interpolateColors' ||+ getInterpolateOutputType(value) === null+ ) {+ return null;+ }++ continue;+ }+return null;}diff --git a/packages/studio-codemods/src/update-keyframes.ts b/packages/studio-codemods/src/update-keyframes.tsindex 5cef5fa10c..5c57ee5121 100644--- a/packages/studio-codemods/src/update-keyframes.ts+++ b/packages/studio-codemods/src/update-keyframes.ts@@ -17,6 +17,7 @@ import type {import {getKeyframeInterpolationFunction,getKeyframeInterpolationFunctionForSchemaField,+ getKeyframeOutputTypeForSchemaField,HOLD_KEYFRAME_EASING,isKeyframeInterpolationFunction,isSchemaFieldHoldOnly,@@ -32,6 +33,7 @@ import type {InteractivitySchema,InteractivitySchemaField,InterpolateOutputOption,+ InterpolateOutputType,SequenceNodePath,VideoConfigNumericExpression,VideoConfigValues,@@ -525,8 +527,10 @@ const createFrameExpression = (frame: number): ExpressionKind => {const createClampOptionsExpression = ({defaultOutput,+ outputType,}: {defaultOutput: InterpolateOutputOption | null;+ outputType: InterpolateOutputType | null;}): ExpressionKind => {const properties = [b.objectProperty(b.identifier('extrapolateLeft'), b.stringLiteral('clamp')),@@ -542,6 +546,12 @@ const createClampOptionsExpression = ({);}+ if (outputType !== null) {+ properties.push(+ b.objectProperty(b.identifier('outputType'), b.stringLiteral(outputType)),+ );+ }+return b.objectExpression(properties) as ExpressionKind;};@@ -805,6 +815,32 @@ const getInlineOptionsFromExtraArgs = (return existingOptions as ObjectExpression;};+const setOutputTypeInExtraArgs = ({+ extraArgs,+ outputType,+}: {+ extraArgs: (ExpressionKind | SpreadElementKind)[];+ outputType: InterpolateOutputType | null;+}): (ExpressionKind | SpreadElementKind)[] => {+ if (outputType === null) {+ return extraArgs;+ }++ const options =+ getInlineOptionsFromExtraArgs(extraArgs) ??+ (extraArgs.length === 0 ? createEmptyOptionsExpression() : null);+ if (options === null) {+ return extraArgs;+ }++ setOptionsProperty({+ options,+ propertyName: 'outputType',+ value: b.stringLiteral(outputType),+ });+ return getExtraArgsWithOptions({extraArgs, options});+};+const normalizeEasingAfterAddingKeyframe = ({extraArgs,previousSegmentCount,@@ -1278,12 +1314,16 @@ const addKeyframe = ({defaultEasing,}): {extraArgs: existing.extraArgs, needsEasingImport: false};+ const updatedExtraArgs = setOutputTypeInExtraArgs({+ extraArgs: normalizedEasing.extraArgs,+ outputType: getKeyframeOutputTypeForSchemaField({schema, key}),+ });return {expression: createInterpolateExpression({callee: b.identifier(nextCalleeName),input: existing.input,- extraArgs: normalizedEasing.extraArgs,+ extraArgs: updatedExtraArgs,keyframes: nextKeyframes,}),introduced: {@@ -1330,6 +1370,7 @@ const addKeyframe = ({: [createClampOptionsExpression({defaultOutput: getDefaultKeyframeOutput({schema, key}),+ outputType: getKeyframeOutputTypeForSchemaField({schema, key}),}),];diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.tsindex 3cb3b73625..3cc14496fb 100644--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts@@ -25,6 +25,7 @@ import type {CanUpdateSequencePropStatus,ExtrapolateType,InterpolateOutputOption,+ InterpolateOutputType,JsxComponentIdentity,LogLevel,SequenceNodePath,@@ -363,6 +364,30 @@ const getInterpolateOutputOption = (return null;};+const getInterpolateOutputType = (+ node: Expression,+): InterpolateOutputType | null => {+ if (node.type === 'StringLiteral') {+ if (+ node.value === 'font-weight' ||+ node.value === 'scale' ||+ node.value === 'translate' ||+ node.value === 'rotate' ||+ node.value === 'transform-origin'+ ) {+ return node.value;+ }++ return null;+ }++ if (node.type === 'TSAsExpression') {+ return getInterpolateOutputType(node.expression as Expression);+ }++ return null;+};+const getKeyframeEasing = (node: Expression): PropEasing[number] | null =>parseKeyframeEasingExpression(node);@@ -530,6 +555,17 @@ const getInterpolationMetadata = (continue;}+ if (key === 'outputType') {+ if (+ interpolationFunction === 'interpolateColors' ||+ getInterpolateOutputType(value) === null+ ) {+ return null;+ }++ continue;+ }+return null;}diff --git a/packages/studio-server/src/test/update-keyframes.test.ts b/packages/studio-server/src/test/update-keyframes.test.tsindex ef74dcd017..45e6cf1736 100644--- a/packages/studio-server/src/test/update-keyframes.test.ts+++ b/packages/studio-server/src/test/update-keyframes.test.ts@@ -269,6 +269,13 @@ const rotateSchema = {},} satisfies InteractivitySchema;+const fontWeightSchema = {+ 'style.fontWeight': {+ type: 'font-weight',+ default: 400,+ },+} satisfies InteractivitySchema;+const translateInput = `import React from 'react';import {AbsoluteFill} from 'remotion';@@ -1264,6 +1271,7 @@ test('updateSequenceKeyframes converts static translate to interpolate', async ();expect(output).toContain("extrapolateLeft: 'clamp'");expect(output).toContain("extrapolateRight: 'clamp'");+ expect(output).toContain("outputType: 'translate'");expect(output).toContain('interpolate');});@@ -1371,9 +1379,35 @@ test('updateSequenceKeyframes converts static rotate to interpolate', async () =expect(output).toContain("rotate: interpolate(frame, [55], ['19deg'], {");expect(output).toContain("extrapolateLeft: 'clamp'");expect(output).toContain("extrapolateRight: 'clamp'");+ expect(output).toContain("outputType: 'rotate'");expect(output).toContain('interpolate');});+test('updateSequenceKeyframes sets the font weight output type', async () => {+ const input = `import React from 'react';+import {AbsoluteFill} from 'remotion';++export const Example: React.FC = () => {+ return <AbsoluteFill style={{fontWeight: 400}} />;+};+`;+ const {output} = await updateSequenceKeyframes({+ videoConfigValues: null,+ input,+ nodePath: lineColumnToNodePath(input, getLine(input, 'fontWeight')),+ schema: fontWeightSchema,+ updates: [+ {+ key: 'style.fontWeight',+ operation: {type: 'add', frame: 55, value: 700},+ },+ ],+ });++ expect(output).toContain('fontWeight: interpolate(frame, [55], [700], {');+ expect(output).toContain("outputType: 'font-weight'");+});+test('updateSequenceKeyframes migrates rotate away from interpolateColors', async () => {const input = rotateInput.replace(diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.tsindex 8a6512621a..d530ddce62 100644--- a/packages/studio-shared/src/index.ts+++ b/packages/studio-shared/src/index.ts@@ -305,10 +305,11 @@ export {export {canEditEasingForInterpolationFunction,getKeyframeInterpolationFunction,- isSchemaFieldHoldOnly,getKeyframeInterpolationFunctionForSchemaField,+ getKeyframeOutputTypeForSchemaField,isInteractivitySchemaFieldKeyframable,isKeyframeInterpolationFunction,+ isSchemaFieldHoldOnly,isSchemaFieldKeyframable,keyframeInterpolationFunctions,type KeyframeInterpolationFunction,diff --git a/packages/studio-shared/src/keyframe-interpolation-function.ts b/packages/studio-shared/src/keyframe-interpolation-function.tsindex 5a574528d7..294ff68178 100644--- a/packages/studio-shared/src/keyframe-interpolation-function.ts+++ b/packages/studio-shared/src/keyframe-interpolation-function.ts@@ -1,4 +1,8 @@-import type {InteractivitySchema, InteractivitySchemaField} from 'remotion';+import type {+ InteractivitySchema,+ InteractivitySchemaField,+ InterpolateOutputType,+} from 'remotion';export const keyframeInterpolationFunctions = ['interpolate',@@ -58,6 +62,29 @@ const KEYFRAME_FIELD_TYPE_INTERPOLATION = {KeyframeInterpolationStrategy>;+const KEYFRAME_FIELD_TYPE_OUTPUT_TYPE = {+ array: null,+ asset: null,+ boolean: null,+ 'remotion-captions': null,+ color: null,+ enum: null,+ 'font-family': null,+ 'font-weight': 'font-weight',+ hidden: null,+ number: null,+ 'rotation-css': 'rotate',+ 'rotation-degrees': null,+ scale: 'scale',+ 'text-content': null,+ 'transform-origin': 'transform-origin',+ translate: 'translate',+ 'uv-coordinate': null,+} as const satisfies Record<+ InteractivitySchemaField['type'],+ InterpolateOutputType | null+>;+const KEYFRAME_INTERPOLATION_EASING_SUPPORT = {interpolate: true,interpolateColors: true,@@ -154,6 +181,17 @@ export const getKeyframeInterpolationFunctionForSchemaField = ({return strategy === 'infer' || strategy === 'unsupported' ? null : strategy;};+export const getKeyframeOutputTypeForSchemaField = ({+ schema,+ key,+}: {+ schema: InteractivitySchema | null;+ key: string;+}): InterpolateOutputType | null => {+ const field = schema ? findFieldInSchema(schema, key) : undefined;+ return field ? KEYFRAME_FIELD_TYPE_OUTPUT_TYPE[field.type] : null;+};+export const getKeyframeInterpolationFunction = ({schema,key,diff --git a/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts b/packages/studio-shared/src/test/keyframe-interpolation-function.test.tsindex 48ec766007..93340bfcba 100644--- a/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts+++ b/packages/studio-shared/src/test/keyframe-interpolation-function.test.ts@@ -4,6 +4,7 @@ import {Interactive} from 'remotion';import {canEditEasingForInterpolationFunction,getKeyframeInterpolationFunctionForSchemaField,+ getKeyframeOutputTypeForSchemaField,isInteractivitySchemaFieldKeyframable,isSchemaFieldHoldOnly,isSchemaFieldKeyframable,@@ -148,6 +149,12 @@ test('font-weight fields use interpolate keyframes', () => {key: 'style.fontWeight',}),).toBe('interpolate');+ expect(+ getKeyframeOutputTypeForSchemaField({+ schema,+ key: 'style.fontWeight',+ }),+ ).toBe('font-weight');});test('isSchemaFieldKeyframable rejects asset fields', () => {@@ -217,4 +224,37 @@ test('transform-origin fields use interpolate keyframes', () => {key: 'style.transformOrigin',}),).toBe('interpolate');+ expect(+ getKeyframeOutputTypeForSchemaField({+ schema,+ key: 'style.transformOrigin',+ }),+ ).toBe('transform-origin');+});++test('CSS transform schema fields expose their interpolation output types', () => {+ const schema = {+ 'style.scale': {+ type: 'scale',+ default: 1,+ },+ 'style.translate': {+ type: 'translate',+ default: '0px',+ },+ 'style.rotate': {+ type: 'rotation-css',+ default: '0deg',+ },+ } satisfies InteractivitySchema;++ expect(+ getKeyframeOutputTypeForSchemaField({schema, key: 'style.scale'}),+ ).toBe('scale');+ expect(+ getKeyframeOutputTypeForSchemaField({schema, key: 'style.translate'}),+ ).toBe('translate');+ expect(+ getKeyframeOutputTypeForSchemaField({schema, key: 'style.rotate'}),+ ).toBe('rotate');});