Skip to content

🎥 Make videos programmatically with React

13.9 KB
Raw Blame
import {expect, test} from 'bun:test';
import {CanvasInternals} from '@remotion/sdk';
import type {
	CanUpdateSequencePropStatusKeyframed,
	SequencePropsSubscriptionKey,
	TSequence,
} from 'remotion';
import {Internals, type PropStatuses} from 'remotion';
import {getInspectorKeyframeSourceFrame} from '../components/InspectorPanel/keyframe-inspector-frame';
import {findTrackForNodePathInfo} from '../components/Timeline/find-track-for-node-path-info';
import {getNodeKeyframes} from '../components/Timeline/get-node-keyframes';
import {
	getKeyframeDisplayOffset,
	getTimelineKeyframes,
} from '../components/Timeline/get-timeline-keyframes';
import {getTimelineKeyframeDragKey} from '../components/Timeline/TimelineKeyframeDragState';
import {
	getSchemaFieldGroup,
	type TimelineTreeNode,
} from '../helpers/timeline-layout';
import {makeRuntimeValueStore} from './make-runtime-value-store';

const getStack = () => null;

const makeNodePath = (id: string): SequencePropsSubscriptionKey => ({
	absolutePath: '/src/Composition.tsx',
	nodePath: [id],
	sequenceKeys: ['style.scale'],
	effectKeys: [],
	videoConfigValues: null,
});

const makeNodePathWithEffectKeys = (
	id: string,
	effectKeys: string[][],
): SequencePropsSubscriptionKey => ({
	...makeNodePath(id),
	effectKeys,
});

const makeControls = (
	overrideId: string,
): NonNullable<TSequence['controls']> => ({
	schema: {},
	runtimeValues: makeRuntimeValueStore({}),
	videoConfigValues: null,
	overrideId,
	supportsEffects: false,
	componentIdentity: null,
	componentName: '<Sequence>',
});

const makeSequence = ({
	id,
	from,
	trimBefore,
	parent = null,
	overrideId = null,
	timelineOrder,
}: {
	id: string;
	from: number;
	trimBefore: number | null;
	parent?: string | null;
	overrideId?: string | null;
	timelineOrder: number;
}): TSequence => ({
	type: 'sequence',
	sequencePlaybackRate: 1,
	from,
	trimBefore,
	duration: 120,
	id,
	displayName: id,
	documentationLink: null,
	parent,
	showInTimeline: true,
	timelineOrder,
	loopDisplay: undefined,
	getStack,
	refForOutline: null,
	isInsideSeries: false,
	premountDisplay: null,
	postmountDisplay: null,
	controls: overrideId ? makeControls(overrideId) : null,
	effects: [],
	effectRuntimeValues: null,
	frozenFrame: null,
});

const numberFieldSchema = {
	type: 'number',
	default: 1,
	hiddenFromList: false,
} as const;

const makeKeyframedStatus = (): CanUpdateSequencePropStatusKeyframed => ({
	status: 'keyframed',
	keyframeDisplayOffsetAdjustment: null,
	interpolationFunction: 'interpolate',
	keyframes: [
		{frame: 0, value: 2},
		{frame: 60, value: 4},
	],
	easing: [{type: 'linear'}],
	clamping: {left: 'extend', right: 'extend'},
	posterize: undefined,
	output: undefined,
});

const makeSequenceFieldNode = (key: string): TimelineTreeNode => ({
	kind: 'field',
	label: key,
	nodePathInfo: {
		sequenceSubscriptionKey: makeNodePath('sequence'),
		auxiliaryKeys: ['controls', key],
		index: 0,
		numberOfSequencesWithThisNodePath: 1,
		supportsEffects: true,
	},
	field: {
		kind: 'sequence-field',
		key,
		description: undefined,
		typeName: 'number',
		rowHeight: 22,
		fieldSchema: numberFieldSchema,
		group: getSchemaFieldGroup(key),
	},
	runtimeValueStore: null,
});

const makeEffectFieldNode = (
	key: string,
	effectIndex: number,
): TimelineTreeNode => ({
	kind: 'field',
	label: key,
	nodePathInfo: {
		sequenceSubscriptionKey: makeNodePath('sequence'),
		auxiliaryKeys: ['effects', String(effectIndex), key],
		index: 0,
		numberOfSequencesWithThisNodePath: 1,
		supportsEffects: true,
	},
	field: {
		kind: 'effect-field',
		key,
		description: undefined,
		typeName: 'number',
		rowHeight: 22,
		fieldSchema: numberFieldSchema,
		effectIndex,
		effectSchema: {
			[key]: numberFieldSchema,
		},
		group: getSchemaFieldGroup(key),
	},
	runtimeValueStore: null,
});

const makePropStatuses = (
	nodePath: SequencePropsSubscriptionKey,
): PropStatuses => ({
	[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
		durationDependentKeyframes: null,
		canUpdate: true,
		props: {
			'style.scale': makeKeyframedStatus(),
		},
		effects: [
			{
				canUpdate: true,
				effectIndex: 0,
				callee: 'blur',
				importPath: null,
				props: {
					amount: makeKeyframedStatus(),
				},
			},
		],
	},
});

test('keyframe display offsets follow the parent sequence context', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const timeline = calculate({
		sequences: [
			makeSequence({
				id: 'root-style',
				from: 30,
				trimBefore: null,
				overrideId: 'root-style',
				timelineOrder: 0,
			}),
			makeSequence({
				id: 'parent',
				from: 30,
				trimBefore: null,
				timelineOrder: 1,
			}),
			makeSequence({
				id: 'child',
				from: 0,
				trimBefore: null,
				parent: 'parent',
				overrideId: 'child',
				timelineOrder: 2,
			}),
			makeSequence({id: 'outer', from: 10, trimBefore: null, timelineOrder: 3}),
			makeSequence({
				id: 'own-from',
				from: 20,
				trimBefore: null,
				parent: 'outer',
				overrideId: 'own-from',
				timelineOrder: 4,
			}),
			makeSequence({
				id: 'grandchild',
				from: 0,
				trimBefore: null,
				parent: 'own-from',
				overrideId: 'grandchild',
				timelineOrder: 5,
			}),
		],
		overrideIdsToNodePaths: {
			'root-style': makeNodePath('root-style'),
			child: makeNodePath('child'),
			'own-from': makeNodePath('own-from'),
			grandchild: makeNodePath('grandchild'),
		},
	}).tracks;

	const getOffset = (id: string): number => {
		const track = timeline.find((t) => t.sequence.id === id);
		if (!track) {
			throw new Error(`Could not find track ${id}`);
		}

		return track.keyframeDisplayOffset;
	};

	expect(getOffset('root-style')).toBe(0);
	expect(getOffset('child')).toBe(30);
	expect(getOffset('own-from')).toBe(10);
	expect(getOffset('grandchild')).toBe(30);
	expect(
		timeline.find((t) => t.sequence.id === 'parent')?.keyframeDisplayOffset,
	).toBe(0);

	expect(
		getTimelineKeyframes(
			{
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 0, value: 2},
					{frame: 60, value: 4},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			},
			getOffset('child'),
		),
	).toEqual([
		{frame: 30, value: 2},
		{frame: 90, value: 4},
	]);
});

test('keyframes retain their source frames across nested rates and trimmed clocks', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const nodePath = makeNodePath('child');
	const timeline = calculate({
		sequences: [
			{
				...makeSequence({
					id: 'outer',
					from: 30,
					trimBefore: 10,
					timelineOrder: 0,
				}),
				sequencePlaybackRate: 2,
			},
			{
				...makeSequence({
					id: 'inner',
					from: 20,
					trimBefore: 3,
					parent: 'outer',
					timelineOrder: 1,
				}),
				sequencePlaybackRate: 0.75,
			},
			{
				...makeSequence({
					id: 'child',
					from: 5,
					trimBefore: null,
					parent: 'inner',
					overrideId: 'child',
					timelineOrder: 2,
				}),
				sequencePlaybackRate: 3,
			},
		],
		overrideIdsToNodePaths: {child: nodePath},
	}).tracks;
	const track = timeline.find(
		(candidate) => candidate.sequence.id === 'child',
	)!;
	const status = {
		...makeKeyframedStatus(),
		keyframeDisplayOffsetAdjustment: -6,
	};
	const propStatuses: PropStatuses = {
		[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
			durationDependentKeyframes: null,
			canUpdate: true,
			props: {'style.scale': status},
			effects: [],
		},
	};
	const keyframes = getNodeKeyframes({
		node: makeSequenceFieldNode('style.scale'),
		nodePath,
		propStatuses,
		keyframeDisplayOffset: track.keyframeDisplayOffset,
		keyframePlaybackRate: track.keyframePlaybackRate,
		getDragOverrides: () => ({}),
		getEffectDragOverrides: () => ({}),
		timelinePosition: 49,
	});

	expect(keyframes).toEqual([
		{frame: 29, value: 2},
		{frame: 69, value: 4},
	]);
	expect(
		getInspectorKeyframeSourceFrame({
			displayFrame: 49,
			keyframeDisplayOffset: getKeyframeDisplayOffset({
				propStatus: status,
				keyframeDisplayOffset: track.keyframeDisplayOffset,
				keyframePlaybackRate: track.keyframePlaybackRate,
			}),
			keyframePlaybackRate: track.keyframePlaybackRate,
		}),
	).toBe(30);
});

test('track lookup survives effect key changes', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const sequences = [
		makeSequence({
			id: 'sequence',
			from: 0,
			trimBefore: null,
			overrideId: 'sequence',
			timelineOrder: 0,
		}),
	];
	const currentNodePath = makeNodePathWithEffectKeys('sequence', [
		['blur'],
		['noise'],
	]);
	const staleNodePath = makeNodePathWithEffectKeys('sequence', [['blur']]);

	const track = findTrackForNodePathInfo({
		tracks: calculate({
			sequences,
			overrideIdsToNodePaths: {
				sequence: currentNodePath,
			},
		}).tracks,
		nodePathInfo: {
			sequenceSubscriptionKey: staleNodePath,
			auxiliaryKeys: [],
			index: 0,
			numberOfSequencesWithThisNodePath: 1,
			supportsEffects: true,
		},
	});

	expect(track?.nodePathInfo?.sequenceSubscriptionKey).toEqual(currentNodePath);
});

test('keyframe display offsets account for parent trimBefore', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const timeline = calculate({
		sequences: [
			makeSequence({
				id: 'parent',
				from: 0,
				trimBefore: 20,
				timelineOrder: 0,
			}),
			makeSequence({
				id: 'child',
				from: 0,
				trimBefore: null,
				parent: 'parent',
				overrideId: 'child',
				timelineOrder: 1,
			}),
		],
		overrideIdsToNodePaths: {
			child: makeNodePath('child'),
		},
	}).tracks;

	const child = timeline.find((t) => t.sequence.id === 'child');
	expect(child?.keyframeDisplayOffset).toBe(-20);
	expect(
		getTimelineKeyframes(
			{
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 20, value: 2},
					{frame: 120, value: 4},
				],
				easing: [{type: 'linear'}],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			},
			child?.keyframeDisplayOffset ?? 0,
		),
	).toEqual([
		{frame: 0, value: 2},
		{frame: 100, value: 4},
	]);
});

test('keyframe display offsets respect the useCurrentFrame coordinate space', () => {
	const calculate = CanvasInternals.createTimelineCalculator();
	const timeline = calculate({
		sequences: [
			makeSequence({
				id: 'timing-wrapper',
				from: -5,
				trimBefore: null,
				timelineOrder: 0,
			}),
			makeSequence({
				id: 'controlled-element',
				from: 0,
				trimBefore: null,
				parent: 'timing-wrapper',
				overrideId: 'controlled-element',
				timelineOrder: 1,
			}),
		],
		overrideIdsToNodePaths: {
			'controlled-element': makeNodePath('controlled-element'),
		},
	}).tracks;
	const controlledElement = timeline.find(
		(track) => track.sequence.id === 'controlled-element',
	);
	if (!controlledElement) {
		throw new Error('Could not find controlled element');
	}

	expect(controlledElement.keyframeDisplayOffset).toBe(-5);

	const status: CanUpdateSequencePropStatusKeyframed = {
		...makeKeyframedStatus(),
		keyframeDisplayOffsetAdjustment: 5,
		keyframes: [
			{frame: 10, value: '0px 0px'},
			{frame: 20, value: '500px 0px'},
		],
	};

	expect(
		getTimelineKeyframes(status, controlledElement.keyframeDisplayOffset),
	).toEqual([
		{frame: 10, value: '0px 0px'},
		{frame: 20, value: '500px 0px'},
	]);
});

test('getNodeKeyframes shows a temporary sequence keyframe from drag overrides', () => {
	const nodePath = makeNodePath('sequence');

	expect(
		getNodeKeyframes({
			node: makeSequenceFieldNode('style.scale'),
			nodePath,
			propStatuses: makePropStatuses(nodePath),
			keyframeDisplayOffset: 30,
			keyframePlaybackRate: 1,
			getDragOverrides: () => ({
				'style.scale': Internals.makeStaticDragOverride(3),
			}),
			getEffectDragOverrides: () => ({}),
			timelinePosition: 60,
		}),
	).toEqual([
		{frame: 30, value: 2},
		{frame: 60, value: 3},
		{frame: 90, value: 4},
	]);
});

test('getNodeKeyframes shows a temporary effect keyframe from drag overrides', () => {
	const nodePath = makeNodePath('sequence');

	expect(
		getNodeKeyframes({
			node: makeEffectFieldNode('amount', 0),
			nodePath,
			propStatuses: makePropStatuses(nodePath),
			keyframeDisplayOffset: 30,
			keyframePlaybackRate: 1,
			getDragOverrides: () => ({}),
			getEffectDragOverrides: () => ({
				amount: Internals.makeStaticDragOverride(5),
			}),
			timelinePosition: 90,
		}),
	).toEqual([
		{frame: 30, value: 2},
		{frame: 90, value: 5},
	]);
});

test('getNodeKeyframes shows sequence keyframes from keyframed drag overrides', () => {
	const nodePath = makeNodePath('sequence');

	expect(
		getNodeKeyframes({
			node: makeSequenceFieldNode('style.scale'),
			nodePath,
			propStatuses: makePropStatuses(nodePath),
			keyframeDisplayOffset: 30,
			keyframePlaybackRate: 1,
			getDragOverrides: () => ({
				'style.scale': Internals.makeKeyframedDragOverride({
					status: makeKeyframedStatus(),
					frame: 30,
					value: 3,
				}),
			}),
			getEffectDragOverrides: () => ({}),
			timelinePosition: 60,
		}),
	).toEqual([
		{frame: 30, value: 2},
		{frame: 60, value: 3},
		{frame: 90, value: 4},
	]);
});

test('getNodeKeyframes replaces existing keyframes from keyframed drag overrides', () => {
	const nodePath = makeNodePath('sequence');

	expect(
		getNodeKeyframes({
			node: makeEffectFieldNode('amount', 0),
			nodePath,
			propStatuses: makePropStatuses(nodePath),
			keyframeDisplayOffset: 30,
			keyframePlaybackRate: 1,
			getDragOverrides: () => ({}),
			getEffectDragOverrides: () => ({
				amount: Internals.makeKeyframedDragOverride({
					status: makeKeyframedStatus(),
					frame: 60,
					value: 5,
				}),
			}),
			timelinePosition: 90,
		}),
	).toEqual([
		{frame: 30, value: 2},
		{frame: 90, value: 5},
	]);
});

test('timeline keyframe drag keys follow the current display frame', () => {
	const {nodePathInfo} = makeSequenceFieldNode('style.scale');

	expect(
		getTimelineKeyframeDragKey({
			nodePathInfo,
			frame: 30,
		}),
	).not.toBe(
		getTimelineKeyframeDragKey({
			nodePathInfo,
			frame: 60,
		}),
	);
});