Skip to content

🎥 Make videos programmatically with React

test(studio): cover inherited source-only trim behavior

JonnyBurgera525b7348736
diff --git a/packages/browser-studio/e2e/source-expressions.test.ts b/packages/browser-studio/e2e/source-expressions.test.ts
index d5ace04ab0..31da1f7727 100644
--- a/packages/browser-studio/e2e/source-expressions.test.ts
+++ b/packages/browser-studio/e2e/source-expressions.test.ts
@@ -48,11 +48,10 @@ test('evaluates shared source expressions while trimming without replacing Brows
const end = await parent
.getByRole('separator', {name: 'Drag to change duration', exact: true})
.boundingBox();
- const start = await parent
- .getByRole('separator', {name: 'Drag to trim start', exact: true})
- .boundingBox();
- if (!end || !start) throw new Error('Expected parent trim handles');
- const pixelsPerFrame = (end.x - start.x) / 300;
+ const parentBox = await parent.boundingBox();
+ if (!end || !parentBox) throw new Error('Expected parent and trim handle');
+ // Handles sit inside the clip; their distance excludes the right inset.
+ const pixelsPerFrame = parentBox.width / 300;
const x = end.x + end.width / 2;
const y = end.y + end.height / 2;
await page.mouse.move(x, y);
diff --git a/packages/example/e2e/source-subscription-churn.test.mts b/packages/example/e2e/source-subscription-churn.test.mts
index d104f6e766..81b09f3b74 100644
--- a/packages/example/e2e/source-subscription-churn.test.mts
+++ b/packages/example/e2e/source-subscription-churn.test.mts
@@ -29,22 +29,19 @@ test('trimming a parent then its expression child saves with the latest instance
.getByRole('separator', {name: 'Drag to change duration', exact: true})
.filter({visible: true})
.first();
- const start = row
- .getByRole('separator', {name: 'Drag to trim start', exact: true})
- .filter({visible: true})
- .first();
let endBox = await end.boundingBox();
- let startBox = await start.boundingBox();
+ let rowBox = await row.boundingBox();
await expect
.poll(async () => {
await end.scrollIntoViewIfNeeded();
endBox = await end.boundingBox();
- startBox = await start.boundingBox();
- return endBox !== null && startBox !== null;
+ rowBox = await row.boundingBox();
+ return endBox !== null && rowBox !== null;
})
.toBe(true);
- if (!endBox || !startBox) throw new Error('Expected trim handles');
- const pixelsPerFrame = (endBox.x - startBox.x) / duration;
+ if (!endBox || !rowBox) throw new Error('Expected clip and trim handle');
+ // Handles sit inside the clip; their distance excludes the right inset.
+ const pixelsPerFrame = rowBox.width / duration;
const x = endBox.x + endBox.width / 2;
const y = endBox.y + endBox.height / 2;
await page.mouse.move(x, y);
diff --git a/packages/studio/src/test/keyframe-trim-constraints.test.ts b/packages/studio/src/test/keyframe-trim-constraints.test.ts
index 1756bcb627..be6071a6e8 100644
--- a/packages/studio/src/test/keyframe-trim-constraints.test.ts
+++ b/packages/studio/src/test/keyframe-trim-constraints.test.ts
@@ -182,6 +182,302 @@ const getTargets = (
})!;
};
+const makeSourceOnlyFixture = ({
+ trimBefore,
+ explicitDuration,
+ playbackRate,
+ parentPlaybackRate,
+ range,
+}: {
+ trimBefore: number;
+ explicitDuration: boolean;
+ playbackRate: number;
+ parentPlaybackRate: number;
+ range: string;
+}) => {
+ const fixture = makeFixture(range, 'child', 120);
+ // A declared source out-point bounds these unit tests without fetching media.
+ const runtime = {
+ from: 0,
+ trimBefore,
+ trimAfter: 300,
+ playbackRate,
+ ...(explicitDuration ? {durationInFrames: 120 * playbackRate} : {}),
+ };
+ fixture.files['/trim-test/parent.tsx'] =
+ `import {Video} from '@remotion/media';
+export const App = () => <Video from={0} trimBefore={${trimBefore}} trimAfter={300} playbackRate={${playbackRate}} ${explicitDuration ? `durationInFrames={${120 * playbackRate}}` : ''} />;`;
+ fixture.sequences[0] = {
+ ...makeSequence({
+ id: 'parent',
+ parent: 'outer',
+ duration: 120,
+ contextDuration: 120,
+ runtime,
+ }),
+ type: 'video',
+ src: 'source-only-fixture.mp4',
+ muted: true,
+ mediaFrameAtSequenceZero: null,
+ frozenMediaFrame: null,
+ playbackRate,
+ startMediaFrom: trimBefore,
+ // Media seeks through its runtime trim; its enclosing Sequence is untrimmed.
+ trimBefore: 0,
+ autoDuration: !explicitDuration,
+ };
+ fixture.sequences[0].controls!.componentIdentity = 'dev.remotion.media.Video';
+ fixture.sequences[1].controls!.videoConfigValues = config(120 + trimBefore);
+ fixture.sequences.push({
+ ...makeSequence({
+ id: 'outer',
+ parent: null,
+ duration: 90 / parentPlaybackRate,
+ contextDuration: 1000,
+ runtime: {from: 60, durationInFrames: 90, trimBefore: 30},
+ }),
+ from: 60,
+ trimBefore: 30,
+ sequencePlaybackRate: parentPlaybackRate,
+ });
+ const node = getNodes({
+ project: {rootDir: '/trim-test', files: fixture.files},
+ filePath: '/trim-test/parent.tsx',
+ })[0];
+ const nodePath = {
+ ...fixture.mappings.parent,
+ nodePath: node.nodePath,
+ sequenceKeys: [
+ 'from',
+ 'durationInFrames',
+ 'trimBefore',
+ 'trimAfter',
+ 'playbackRate',
+ ],
+ };
+ fixture.mappings.parent = nodePath;
+ fixture.propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] =
+ getNodeProps({
+ project: {rootDir: '/trim-test', files: fixture.files},
+ node,
+ keys: nodePath.sequenceKeys,
+ effectKeys: [],
+ });
+ prepareSource(fixture);
+ const tracks = calculateTimeline({
+ sequences: fixture.sequences,
+ overrideIdsToNodePaths: fixture.mappings,
+ });
+ const mediaTrack = tracks.find((track) => track.sequence.id === 'parent')!;
+ return {
+ fixture,
+ mediaTrack,
+ targets: getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: mediaTrack.nodePathInfo!,
+ selectedItems: [],
+ sequences: fixture.sequences,
+ overrideIdsToNodePaths: fixture.mappings,
+ propStatuses: fixture.propStatuses,
+ timelineDurationInFrames: 1000,
+ trimBeforeOnly: true,
+ rippleEdit: false,
+ })!,
+ };
+};
+
+test.each([
+ {
+ trimBefore: 0,
+ explicitDuration: false,
+ playbackRate: 1,
+ parentPlaybackRate: 1,
+ midDelta: 15,
+ midFrom: 15,
+ maximumTrim: 180,
+ },
+ {
+ trimBefore: 20,
+ explicitDuration: false,
+ playbackRate: 1,
+ parentPlaybackRate: 1,
+ midDelta: 35,
+ midFrom: 15,
+ maximumTrim: 180,
+ },
+ {
+ trimBefore: 20,
+ explicitDuration: true,
+ playbackRate: 1,
+ parentPlaybackRate: 1,
+ midDelta: 35,
+ midFrom: 15,
+ maximumTrim: 180,
+ },
+ {
+ trimBefore: 20,
+ explicitDuration: true,
+ playbackRate: 0.5,
+ parentPlaybackRate: 2,
+ midDelta: 28,
+ midFrom: 16,
+ maximumTrim: 240,
+ },
+ {
+ trimBefore: 20,
+ explicitDuration: false,
+ playbackRate: 2,
+ parentPlaybackRate: 0.5,
+ midDelta: 50,
+ midFrom: 15,
+ maximumTrim: 60,
+ },
+])('source-only trims preserve the visible range: %j', (testCase) => {
+ const {fixture, targets, mediaTrack} = makeSourceOnlyFixture({
+ ...testCase,
+ range: '[0, 1]',
+ });
+ expect(targets).toHaveLength(1);
+ const initial: Record<'from' | 'trimBefore' | 'durationInFrames', number> = {
+ from: 0,
+ trimBefore: testCase.trimBefore,
+ durationInFrames: 120 * testCase.playbackRate,
+ };
+ for (const [deltaFrames, expected] of [
+ [0, initial],
+ [
+ testCase.trimBefore / testCase.playbackRate / testCase.parentPlaybackRate,
+ {...initial, trimBefore: 0},
+ ],
+ [
+ testCase.midDelta,
+ {
+ from: testCase.midFrom,
+ trimBefore: 0,
+ durationInFrames:
+ (testCase.explicitDuration ? 120 - testCase.midFrom : 120) *
+ testCase.playbackRate,
+ },
+ ],
+ [
+ 1000,
+ {
+ from: 30,
+ trimBefore: 0,
+ durationInFrames:
+ (testCase.explicitDuration ? 90 : 120) * testCase.playbackRate,
+ },
+ ],
+ [-1000, {...initial, trimBefore: testCase.maximumTrim}],
+ ] as const) {
+ const changes = getTimelineSequenceLeftEdgeDragChanges({
+ targets,
+ deltaFrames,
+ trimBeforeOnly: true,
+ });
+ const actual = {...initial};
+ for (const change of changes) {
+ expect(change.fileName).toBe(fixture.mappings.parent.absolutePath);
+ actual[change.fieldKey as keyof typeof actual] = change.value as number;
+ }
+
+ expect(actual).toEqual(expected);
+ if (deltaFrames === 0) expect(changes).toEqual([]);
+ const updatedTracks = calculateTimeline({
+ sequences: fixture.sequences.map((sequence) =>
+ sequence.id === 'parent'
+ ? {
+ ...sequence,
+ from: actual.from,
+ duration: actual.durationInFrames / testCase.playbackRate,
+ }
+ : sequence,
+ ),
+ overrideIdsToNodePaths: fixture.mappings,
+ });
+ const updated = updatedTracks.find(
+ (track) => track.sequence.id === 'parent',
+ )!;
+ expect(updated.sequence.from).toBe(mediaTrack.sequence.from);
+ expect(updated.sequence.duration).toBe(mediaTrack.sequence.duration);
+ }
+});
+
+test.each([
+ {
+ range: '[0, durationInFrames - 130]',
+ minimumTrim: 11,
+ reason: 'Keyframe constraint',
+ },
+ {
+ range: '[0, Math.max(1, durationInFrames - 50)]',
+ minimumTrim: 20,
+ reason: 'Computed keyframes',
+ },
+])(
+ 'source-only trims respect the keyframe floor: %j',
+ ({range, minimumTrim, reason}) => {
+ const {targets} = makeSourceOnlyFixture({
+ trimBefore: 20,
+ explicitDuration: true,
+ playbackRate: 1,
+ parentPlaybackRate: 1,
+ range,
+ });
+ const changes = getTimelineSequenceLeftEdgeDragChanges({
+ targets,
+ deltaFrames: 1000,
+ trimBeforeOnly: true,
+ });
+ expect(changes.map(({fieldKey, value}) => [fieldKey, value])).toEqual(
+ minimumTrim === 20 ? [] : [['trimBefore', minimumTrim]],
+ );
+ expect(targets[0].sourceOnlyLimits?.minimumReason).toBe(reason);
+ },
+);
+
+test('source-only edits do not replace an animated trimBefore', () => {
+ const {fixture} = makeSourceOnlyFixture({
+ trimBefore: 20,
+ explicitDuration: true,
+ playbackRate: 1,
+ parentPlaybackRate: 1,
+ range: '[0, 1]',
+ });
+ const filePath = fixture.mappings.parent.absolutePath;
+ fixture.files[filePath] = `import {Video} from '@remotion/media';
+import {interpolate, useCurrentFrame} from 'remotion';
+export const App = () => {
+ const frame = useCurrentFrame();
+ return <Video from={0} durationInFrames={120} trimBefore={interpolate(frame, [0, 120], [20, 40])} />;
+};`;
+ const project = {rootDir: '/trim-test', files: fixture.files};
+ const node = getNodes({project, filePath})[0];
+ const nodePath = {...fixture.mappings.parent, nodePath: node.nodePath};
+ fixture.mappings.parent = nodePath;
+ fixture.propStatuses[Internals.makeSequencePropsSubscriptionKey(nodePath)] =
+ getNodeProps({project, node, keys: nodePath.sequenceKeys, effectKeys: []});
+ expect(
+ Internals.getPropStatusesCtx(fixture.propStatuses, nodePath)?.trimBefore
+ .status,
+ ).toBe('keyframed');
+ const track = calculateTimeline({
+ sequences: fixture.sequences,
+ overrideIdsToNodePaths: fixture.mappings,
+ }).find((item) => item.sequence.id === 'parent')!;
+ expect(
+ getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: track.nodePathInfo!,
+ selectedItems: [],
+ sequences: fixture.sequences,
+ overrideIdsToNodePaths: fixture.mappings,
+ propStatuses: fixture.propStatuses,
+ timelineDurationInFrames: 1000,
+ trimBeforeOnly: true,
+ rippleEdit: false,
+ }),
+ ).toBeNull();
+});
+
test.each(['child', 'property', 'effect'] as const)(
'trimming respects %s keyframes',
(placement) => {