Skip to content

🎥 Make videos programmatically with React

Fix left edge trimming of sequences clipped by ancestors

Jonny Burgerfd9ca5cae58e
diff --git a/packages/example/e2e/caption-left-edge-trim.test.mts b/packages/example/e2e/caption-left-edge-trim.test.mts
new file mode 100644
index 0000000000..ef471551a6
--- /dev/null
+++ b/packages/example/e2e/caption-left-edge-trim.test.mts
@@ -0,0 +1,102 @@
+import fs from 'fs';
+import path from 'path';
+import {expect, test} from '@playwright/test';
+import {EXPANDED_SIDEBAR_STATE, STUDIO_URL, exampleDir} from './constants.mts';
+import {startStudio, stopStudio} from './studio-server.mts';
+
+test.use({storageState: EXPANDED_SIDEBAR_STATE});
+
+test('trimming captions clipped by a parent moves their visible start and persists across reload', async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ const fixture = path.join(exampleDir, 'src', 'CaptionLeftEdgeTrimRepro.tsx');
+ const original = fs.readFileSync(fixture, 'utf-8');
+ try {
+ await startStudio();
+ await page.goto(`${STUDIO_URL}/caption-left-edge-trim-repro`);
+ const trim = page.getByRole('separator', {
+ name: 'Drag to trim start',
+ exact: true,
+ });
+ const row = page
+ .getByRole('group', {
+ name: 'Presenter introduction (2) captions',
+ exact: true,
+ })
+ .filter({has: trim});
+ await expect(row).toBeVisible({timeout: 30_000});
+ await page.keyboard.press('g');
+ await page.locator('input:focus').fill('90');
+ await page.locator('input:focus').press('Enter');
+ await expect(async () => {
+ await row.click();
+ await expect(page.getByRole('textbox', {name: 'Caption 1'})).toBeVisible({
+ timeout: 1_000,
+ });
+ }).toPass({timeout: 30_000});
+
+ const left = row.getByRole('separator', {
+ name: 'Drag to trim start',
+ exact: true,
+ });
+ const right = row.getByRole('separator', {
+ name: 'Drag to change duration',
+ exact: true,
+ });
+ const bar = await row.boundingBox();
+ const initialLeft = await left.boundingBox();
+ const initialRight = await right.boundingBox();
+ if (!bar || !initialLeft || !initialRight) {
+ throw new Error('Expected caption bar and trim handles');
+ }
+ const pixelsPerFrame = bar.width / 129;
+ let trimmedFrames = 0;
+ for (const delta of [30, 10]) {
+ const handle = await left.boundingBox();
+ if (!handle) throw new Error('Expected caption left edge');
+ const x = handle.x + handle.width / 2;
+ const y = handle.y + handle.height / 2;
+ await page.mouse.move(x, y);
+ await page.mouse.down();
+ await page.mouse.move(x + pixelsPerFrame * delta, y, {steps: 5});
+ await page.mouse.up();
+ trimmedFrames += delta;
+ await expect
+ .poll(() => fs.readFileSync(fixture, 'utf-8'))
+ .toMatch(
+ new RegExp(
+ `from=\\{${369 + trimmedFrames}\\}[\\s\\S]*durationInFrames=\\{${129 - trimmedFrames}\\}[\\s\\S]*trimBefore=\\{${369 + trimmedFrames}\\}`,
+ ),
+ );
+ await expect
+ .poll(async () => (await left.boundingBox())?.x)
+ .toBeCloseTo(initialLeft.x + pixelsPerFrame * trimmedFrames, 0);
+ await expect
+ .poll(async () => (await right.boundingBox())?.x)
+ .toBeCloseTo(initialRight.x, 0);
+ }
+
+ await page.reload();
+ await expect(row).toBeVisible({timeout: 30_000});
+ for (const frame of [69, 70]) {
+ await page.keyboard.press('g');
+ await page.locator('input:focus').fill(String(frame));
+ await page.locator('input:focus').press('Enter');
+ const caption = page.getByText(
+ 'Drag the left edge of this caption track',
+ {
+ exact: true,
+ },
+ );
+ if (frame === 69) {
+ await expect(caption).toBeHidden();
+ } else {
+ await expect(caption).toBeVisible();
+ }
+ }
+ } finally {
+ await stopStudio();
+ fs.writeFileSync(fixture, original);
+ }
+});
diff --git a/packages/example/src/CaptionLeftEdgeTrimRepro.tsx b/packages/example/src/CaptionLeftEdgeTrimRepro.tsx
index c5f5a57bcd..117da17c4c 100644
--- a/packages/example/src/CaptionLeftEdgeTrimRepro.tsx
+++ b/packages/example/src/CaptionLeftEdgeTrimRepro.tsx
@@ -42,7 +42,7 @@ const BasicCaptions = Interactive.withSchema({
// https://github.com/remotion-dev/remotion/issues/11891
// In Visual Mode, expand "Presenter introduction (2)" and drag the left edge
// of its captions to the right. The captions' visible start should move.
-// Currently, the source props change but the visible start stays at frame 30.
+// The captions' timing and right edge should stay unchanged.
export const CaptionLeftEdgeTrimRepro: React.FC = () => {
return (
<AbsoluteFill
@@ -79,6 +79,9 @@ export const CaptionLeftEdgeTrimRepro: React.FC = () => {
},
]}
style={{position: 'absolute', left: 100, bottom: 100}}
+ from={33}
+ durationInFrames={465}
+ trimBefore={33}
/>
</Series.Sequence>
<Series.Sequence name="Tail" durationInFrames={30} />
diff --git a/packages/example/src/E2eTestRoot.tsx b/packages/example/src/E2eTestRoot.tsx
index 453ac02ab7..9a40828da8 100644
--- a/packages/example/src/E2eTestRoot.tsx
+++ b/packages/example/src/E2eTestRoot.tsx
@@ -1,6 +1,7 @@
import React from 'react';
import {Composition, Folder} from 'remotion';
import {BarChart} from './BarChart';
+import {CaptionLeftEdgeTrimRepro} from './CaptionLeftEdgeTrimRepro';
import {
CAPTIONS_DURATION_IN_FRAMES,
CAPTIONS_HEIGHT,
@@ -32,6 +33,14 @@ import {SequenceShiftRepro} from './VisualModeTests/SequenceShiftRepro';
export const E2eTestRoot: React.FC = () => {
return (
<>
+ <Composition
+ id="caption-left-edge-trim-repro"
+ component={CaptionLeftEdgeTrimRepro}
+ width={1920}
+ height={1080}
+ fps={30}
+ durationInFrames={189}
+ />
<Composition
id="sequence-playback-rate"
component={SequencePlaybackRateE2e}
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index be41d7e24e..a5784de0ef 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -1288,29 +1288,43 @@ export const getTimelineSequenceLeftEdgeDragTargets = ({
typeof durationStatus.codeValue === 'number'
? durationStatus.codeValue
: runtimeValues.durationInFrames;
+ // The handle is at the visible start, which can be later than the
+ // child's own start when an ancestor clips it. Include those hidden
+ // frames in the trim so the visible edge follows the pointer.
+ const hiddenStartInParentFrames =
+ !trimBeforeOnly && positionField === 'from'
+ ? (track.sequence.from - track.cascadedStart) *
+ track.keyframePlaybackRate
+ : 0;
targets.set(key, {
parentPlaybackRate: getParentSequencePlaybackRate(
originalSequence,
sequences,
),
fileName: nodePath.absolutePath,
- initialDuration: originalSequence.autoDuration
- ? (track.sequence.from +
- track.sequence.duration -
- track.cascadedStart) *
- track.keyframePlaybackRate
- : isSeriesSequence(originalSequence) ||
- typeof runtimeDuration !== 'number' ||
- !Number.isFinite(runtimeDuration)
- ? originalSequence.duration
- : runtimeDuration / playbackRate,
- initialFrom: positionField === 'from' ? originalSequence.from : 0,
- initialTrimBefore: trimBeforeOnly
- ? ownTrimBefore
- : trimsMedia
- ? (originalSequence.trimBefore ??
- Math.max(0, originalSequence.startMediaFrom))
- : (originalSequence.trimBefore ?? 0),
+ initialDuration:
+ (originalSequence.autoDuration
+ ? (track.sequence.from +
+ track.sequence.duration -
+ track.cascadedStart) *
+ track.keyframePlaybackRate
+ : isSeriesSequence(originalSequence) ||
+ typeof runtimeDuration !== 'number' ||
+ !Number.isFinite(runtimeDuration)
+ ? originalSequence.duration
+ : runtimeDuration / playbackRate) - hiddenStartInParentFrames,
+ initialFrom:
+ positionField === 'from'
+ ? originalSequence.from + hiddenStartInParentFrames
+ : 0,
+ initialTrimBefore:
+ (trimBeforeOnly
+ ? ownTrimBefore
+ : trimsMedia
+ ? (originalSequence.trimBefore ??
+ Math.max(0, originalSequence.startMediaFrom))
+ : (originalSequence.trimBefore ?? 0)) +
+ hiddenStartInParentFrames * playbackRate,
minimumDuration: getMinimumSequenceDuration({
sequence: originalSequence,
sequences,
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index 5d50147e45..909bc33657 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -150,6 +150,7 @@ import {
getKeyframesForTimelineEasingDrag,
getTimelineSelectionsAfterEasingKeyframeDrag,
} from '../components/Timeline/use-timeline-keyframe-drag';
+import {calculateTimeline} from '../helpers/calculate-timeline';
import {
getConnectedCompositionFrame,
getSequenceDoubleClickAction,
@@ -2730,6 +2731,88 @@ test('Timeline left edge drag adjusts from, duration and trimBefore for selected
]);
});
+test.each([
+ {durationInFrames: null, from: 0, parentRate: 1, childRate: 1},
+ {durationInFrames: 465, from: 33, parentRate: 1, childRate: 1},
+ {durationInFrames: 232.5, from: 33, parentRate: 2, childRate: 0.5},
+])(
+ 'left edge trimming uses the visible start of a clipped child: %j',
+ ({durationInFrames, from, parentRate, childRate}) => {
+ const schema = {} satisfies InteractivitySchema;
+ const nodePathInfo = makeNodePathInfo(['body', 0, 'children', 0], []);
+ const child = makeTimelineSequence({
+ schema,
+ id: 'captions',
+ parentId: 'parent',
+ from,
+ duration: (durationInFrames ?? 498 * childRate) / childRate,
+ trimBefore: from * childRate,
+ sequencePlaybackRate: childRate,
+ });
+ const sequences = [
+ makeTimelineSequence({
+ schema,
+ id: 'parent',
+ overrideId: 'parent',
+ from: 30,
+ duration: 129 / parentRate,
+ trimBefore: 369,
+ sequencePlaybackRate: parentRate,
+ }),
+ child,
+ ];
+ const overrideIdsToNodePaths = {
+ override: nodePathInfo.sequenceSubscriptionKey,
+ };
+ const targets = getTimelineSequenceLeftEdgeDragTargets({
+ draggedNodePathInfo: nodePathInfo,
+ selectedItems: [{type: 'sequence', nodePathInfo}],
+ sequences,
+ overrideIdsToNodePaths,
+ propStatuses: makeLeftEdgePropStatuses(
+ [nodePathInfo.sequenceSubscriptionKey],
+ true,
+ true,
+ [durationInFrames ?? undefined],
+ ),
+ });
+ const changes = getTimelineSequenceLeftEdgeDragChanges({
+ targets: targets ?? [],
+ deltaFrames: 6,
+ });
+ const values = Object.fromEntries(
+ changes.map((change) => {
+ if (typeof change.value !== 'number') {
+ throw new Error('Expected numeric trim values');
+ }
+
+ return [change.fieldKey, change.value] as const;
+ }),
+ );
+ expect(values).toEqual({
+ from: 369 + 6 * parentRate,
+ durationInFrames: (129 - 6 * parentRate) * childRate,
+ trimBefore: (369 + 6 * parentRate) * childRate,
+ });
+ const tracks = calculateTimeline({
+ sequences: [
+ sequences[0],
+ {
+ ...child,
+ from: values.from,
+ duration: values.durationInFrames / childRate,
+ trimBefore: values.trimBefore,
+ autoDuration: false,
+ } as TSequence,
+ ],
+ overrideIdsToNodePaths,
+ });
+ expect(
+ tracks.find((track) => track.sequence.id === 'captions')?.sequence,
+ ).toMatchObject({from: 36, duration: 129 / parentRate - 6});
+ },
+);
+
test('TransitionSeries.Sequence self-trim changes its duration and trimBefore', () => {
const schema = {} satisfies InteractivitySchema;
const nodePathInfo = makeNodePathInfo(['body', 0], []);