🎥 Make videos programmatically with React
Add trimLeft/trimRight props with validation and tests
copilot-swe-agent[bot]35b5552fb810
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsxindex 2eddb0674d..e7ae33e4a4 100644--- a/packages/core/src/audio/Audio.tsx+++ b/packages/core/src/audio/Audio.tsx@@ -10,7 +10,7 @@ import {Loop} from '../loop/index.js';import {usePreload} from '../prefetch.js';import {useVideoConfig} from '../use-video-config.js';import {validateMediaProps} from '../validate-media-props.js';-import {validateStartFromProps} from '../validate-start-from-props.js';+import {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';import {DurationsContext} from '../video/duration-state.js';import {AudioForPreview} from './AudioForPreview.js';import {AudioForRendering} from './AudioForRendering.js';@@ -31,6 +31,8 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<const {startFrom,endAt,+ trimLeft,+ trimRight,name,stack,pauseWhenBuffering,@@ -99,10 +101,10 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<<Looplayout="none"durationInFrames={calculateLoopDuration({- endAt,+ endAt: trimRight ?? endAt,mediaDuration: duration,playbackRate: props.playbackRate ?? 1,- startFrom,+ startFrom: trimLeft ?? startFrom,})}><Audio@@ -114,17 +116,16 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<);}- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined') {- validateStartFromProps(startFrom, endAt);+ if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {+ validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);- const startFromFrameNo = startFrom ?? 0;- const endAtFrameNo = endAt ?? Infinity;+ const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);return (<Sequencelayout="none"- from={0 - startFromFrameNo}+ from={0 - trimLeftValue}showInTimeline={false}- durationInFrames={endAtFrameNo}+ durationInFrames={trimRightValue}name={name}><Audiodiff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.tsindex e97482aed3..e36737b23c 100644--- a/packages/core/src/audio/props.ts+++ b/packages/core/src/audio/props.ts@@ -2,8 +2,22 @@ import type {VolumeProp} from '../volume-prop.js';import type {LoopVolumeCurveBehavior} from './use-audio-frame.js';export type RemotionMainAudioProps = {+ /**+ * @deprecated Use `trimLeft` instead+ */startFrom?: number;+ /**+ * @deprecated Use `trimRight` instead+ */endAt?: number;+ /**+ * Trim the audio from the left (start). Replaces the deprecated `startFrom` prop.+ */+ trimLeft?: number;+ /**+ * Trim the audio from the right (end). Replaces the deprecated `endAt` prop.+ */+ trimRight?: number;};export type NativeAudioProps = Omit<diff --git a/packages/core/src/test/audio.test.tsx b/packages/core/src/test/audio.test.tsxindex bbe9d78962..cc9fb850f3 100644--- a/packages/core/src/test/audio.test.tsx+++ b/packages/core/src/test/audio.test.tsx@@ -57,4 +57,64 @@ describe('Render correctly with props', () => {),).not.toThrow();});++ test('It should render Audio with trimLeft props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} trimLeft={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render Audio with trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} trimRight={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render Audio with trimLeft and trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} trimLeft={10} trimRight={20} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render Audio with loop, trimLeft and trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} trimLeft={10} trimRight={20} loop />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should throw when both startFrom and trimLeft are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} startFrom={10} trimLeft={5} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both startFrom and trimLeft props/);+ });++ test('It should throw when both endAt and trimRight are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Audio src="test" volume={1} endAt={15} trimRight={20} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both endAt and trimRight props/);+ });});diff --git a/packages/core/src/test/offthread-video.test.tsx b/packages/core/src/test/offthread-video.test.tsxnew file mode 100644index 0000000000..44d2a46453--- /dev/null+++ b/packages/core/src/test/offthread-video.test.tsx@@ -0,0 +1,100 @@+import {cleanup, render} from '@testing-library/react';+import {afterEach, describe, expect, test} from 'bun:test';+import {OffthreadVideo} from '../video/index.js';+import {WrapSequenceContext} from './wrap-sequence-context.js';++afterEach(() => {+ cleanup();+});++describe('OffthreadVideo render correctly with props', () => {+ test('It should render OffthreadVideo without startFrom / endAt props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with startFrom props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" startFrom={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with endAt props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" endAt={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with startFrom and endAt props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" startFrom={10} endAt={20} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with trimLeft props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" trimLeft={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" trimRight={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should render OffthreadVideo with trimLeft and trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" trimLeft={10} trimRight={20} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+ });++ test('It should throw when both startFrom and trimLeft are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" startFrom={10} trimLeft={5} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both startFrom and trimLeft props/);+ });++ test('It should throw when both endAt and trimRight are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <OffthreadVideo src="test" endAt={15} trimRight={20} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both endAt and trimRight props/);+ });+});\ No newline at end of filediff --git a/packages/core/src/test/validate-start-from-props.test.ts b/packages/core/src/test/validate-start-from-props.test.tsindex ca393fe405..e179d55bf1 100644--- a/packages/core/src/test/validate-start-from-props.test.ts+++ b/packages/core/src/test/validate-start-from-props.test.ts@@ -1,5 +1,5 @@import {describe, test} from 'bun:test';-import {validateStartFromProps} from '../validate-start-from-props.js';+import {validateStartFromProps, validateTrimProps, validateMediaTrimProps} from '../validate-start-from-props.js';import {expectToThrow} from './expect-to-throw.js';describe('ValidateStartFrom props should throw', () => {@@ -62,3 +62,87 @@ describe('ValidateStartFrom props should throw', () => {});});});++describe('ValidateTrim props should throw', () => {+ describe('Throw with invalid trimLeft prop', () => {+ test('It should throw if trimLeft prop is negative', () => {+ expectToThrow(+ () => validateTrimProps(-40, 1),+ /trimLeft must be greater than equal to 0 instead got -40./,+ );+ });+ test('It should throw if trimLeft is not a number', () => {+ expectToThrow(+ // @ts-expect-error+ () => validateTrimProps('10', 20),+ /type of trimLeft prop must be a number, instead got type string./,+ );+ });+ test('It should throw if trimLeft is NaN', () => {+ expectToThrow(+ () => validateTrimProps(NaN, 20),+ /trimLeft prop can not be NaN or Infinity./,+ );+ });+ test('It should throw if trimLeft is Infinity', () => {+ expectToThrow(+ () => validateTrimProps(Infinity, 20),+ /trimLeft prop can not be NaN or Infinity./,+ );+ });+ });++ describe('Throw with invalid trimRight prop', () => {+ test('It should throw if trimRight prop is negative', () => {+ expectToThrow(+ () => validateTrimProps(0, -40),+ /trimRight must be a positive number, instead got -40./,+ );+ });++ test('It should throw if trimRight is less than trimLeft', () => {+ expectToThrow(+ () => validateTrimProps(10, 1),+ /trimRight prop must be greater than trimLeft prop./,+ );+ });++ test('It should throw if trimRight is not a number', () => {+ expectToThrow(+ // @ts-expect-error+ () => validateTrimProps(10, '20'),+ /type of trimRight prop must be a number, instead got type string./,+ );+ });++ test('It should throw if trimRight is NaN', () => {+ expectToThrow(+ () => validateTrimProps(10, NaN),+ /trimRight prop can not be NaN./,+ );+ });+ });+});++describe('ValidateMediaTrim props should throw on conflicts', () => {+ test('It should throw if both startFrom and trimLeft are provided', () => {+ expectToThrow(+ () => validateMediaTrimProps(10, undefined, 5, undefined),+ /Cannot use both startFrom and trimLeft props. Use trimLeft instead as startFrom is deprecated./,+ );+ });++ test('It should throw if both endAt and trimRight are provided', () => {+ expectToThrow(+ () => validateMediaTrimProps(undefined, 20, undefined, 15),+ /Cannot use both endAt and trimRight props. Use trimRight instead as endAt is deprecated./,+ );+ });++ test('It should throw if all props are provided', () => {+ expectToThrow(+ () => validateMediaTrimProps(10, 20, 5, 15),+ /Cannot use both startFrom and trimLeft props. Use trimLeft instead as startFrom is deprecated./,+ );+ });+});diff --git a/packages/core/src/test/video.test.tsx b/packages/core/src/test/video.test.tsxindex fd96cb697d..f2829f9143 100644--- a/packages/core/src/test/video.test.tsx+++ b/packages/core/src/test/video.test.tsx@@ -43,3 +43,51 @@ test('It should render Video with startFrom and endAt props', () => {),).not.toThrow();});++test('It should render Video with trimLeft props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Video src="test" trimLeft={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+});+test('It should render Video with trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Video src="test" trimRight={10} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+});+test('It should render Video with trimLeft and trimRight props', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Video src="test" trimLeft={10} trimRight={15} />+ </WrapSequenceContext>,+ ),+ ).not.toThrow();+});++test('It should throw when both startFrom and trimLeft are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Video src="test" startFrom={10} trimLeft={5} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both startFrom and trimLeft props/);+});++test('It should throw when both endAt and trimRight are provided', () => {+ expect(() =>+ render(+ <WrapSequenceContext>+ <Video src="test" endAt={15} trimRight={20} />+ </WrapSequenceContext>,+ ),+ ).toThrow(/Cannot use both endAt and trimRight props/);+});diff --git a/packages/core/src/validate-start-from-props.ts b/packages/core/src/validate-start-from-props.tsindex bad4088374..b83e378dfa 100644--- a/packages/core/src/validate-start-from-props.ts+++ b/packages/core/src/validate-start-from-props.ts@@ -42,3 +42,91 @@ export const validateStartFromProps = (throw new TypeError('endAt prop must be greater than startFrom prop.');}};++export const validateTrimProps = (+ trimLeft: number | undefined,+ trimRight: number | undefined,+) => {+ if (typeof trimLeft !== 'undefined') {+ if (typeof trimLeft !== 'number') {+ throw new TypeError(+ `type of trimLeft prop must be a number, instead got type ${typeof trimLeft}.`,+ );+ }++ if (isNaN(trimLeft) || trimLeft === Infinity) {+ throw new TypeError('trimLeft prop can not be NaN or Infinity.');+ }++ if (trimLeft < 0) {+ throw new TypeError(+ `trimLeft must be greater than equal to 0 instead got ${trimLeft}.`,+ );+ }+ }++ if (typeof trimRight !== 'undefined') {+ if (typeof trimRight !== 'number') {+ throw new TypeError(+ `type of trimRight prop must be a number, instead got type ${typeof trimRight}.`,+ );+ }++ if (isNaN(trimRight)) {+ throw new TypeError('trimRight prop can not be NaN.');+ }++ if (trimRight <= 0) {+ throw new TypeError(+ `trimRight must be a positive number, instead got ${trimRight}.`,+ );+ }+ }++ if ((trimRight as number) < (trimLeft as number)) {+ throw new TypeError('trimRight prop must be greater than trimLeft prop.');+ }+};++export const validateMediaTrimProps = (+ startFrom: number | undefined,+ endAt: number | undefined,+ trimLeft: number | undefined,+ trimRight: number | undefined,+) => {+ // Check for conflicting props+ if (typeof startFrom !== 'undefined' && typeof trimLeft !== 'undefined') {+ throw new TypeError(+ 'Cannot use both startFrom and trimLeft props. Use trimLeft instead as startFrom is deprecated.',+ );+ }++ if (typeof endAt !== 'undefined' && typeof trimRight !== 'undefined') {+ throw new TypeError(+ 'Cannot use both endAt and trimRight props. Use trimRight instead as endAt is deprecated.',+ );+ }++ // Validate using the appropriate validation function+ const hasNewProps = typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined';+ const hasOldProps = typeof startFrom !== 'undefined' || typeof endAt !== 'undefined';++ if (hasNewProps) {+ validateTrimProps(trimLeft, trimRight);+ } else if (hasOldProps) {+ validateStartFromProps(startFrom, endAt);+ }+};++export const resolveTrimProps = (+ startFrom: number | undefined,+ endAt: number | undefined,+ trimLeft: number | undefined,+ trimRight: number | undefined,+): {trimLeftValue: number; trimRightValue: number} => {+ // Use new props if available, otherwise fall back to old props+ const trimLeftValue = trimLeft ?? startFrom ?? 0;+ const trimRightValue = trimRight ?? endAt ?? Infinity;++ return {trimLeftValue, trimRightValue};+};diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsxindex 1dc2c42f6b..60c09b00a3 100644--- a/packages/core/src/video/OffthreadVideo.tsx+++ b/packages/core/src/video/OffthreadVideo.tsx@@ -2,7 +2,7 @@ import React, {useCallback} from 'react';import {Sequence} from '../Sequence.js';import {getRemotionEnvironment} from '../get-remotion-environment.js';import {validateMediaProps} from '../validate-media-props.js';-import {validateStartFromProps} from '../validate-start-from-props.js';+import {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';import {OffthreadVideoForRendering} from './OffthreadVideoForRendering.js';import {VideoForPreview} from './VideoForPreview.js';import type {RemotionOffthreadVideoProps} from './props.js';@@ -19,6 +19,8 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = (const {startFrom,endAt,+ trimLeft,+ trimRight,name,pauseWhenBuffering,stack,@@ -43,17 +45,16 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = ();}- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined') {- validateStartFromProps(startFrom, endAt);+ if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {+ validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);- const startFromFrameNo = startFrom ?? 0;- const endAtFrameNo = endAt ?? Infinity;+ const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);return (<Sequencelayout="none"- from={0 - startFromFrameNo}+ from={0 - trimLeftValue}showInTimeline={false}- durationInFrames={endAtFrameNo}+ durationInFrames={trimRightValue}name={name}><OffthreadVideodiff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsxindex 8027ef8243..358b5c6366 100644--- a/packages/core/src/video/Video.tsx+++ b/packages/core/src/video/Video.tsx@@ -9,7 +9,7 @@ import {Loop} from '../loop/index.js';import {usePreload} from '../prefetch.js';import {useVideoConfig} from '../use-video-config.js';import {validateMediaProps} from '../validate-media-props.js';-import {validateStartFromProps} from '../validate-start-from-props.js';+import {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';import {VideoForPreview} from './VideoForPreview.js';import {VideoForRendering} from './VideoForRendering.js';import {DurationsContext} from './duration-state.js';@@ -28,6 +28,8 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<const {startFrom,endAt,+ trimLeft,+ trimRight,name,pauseWhenBuffering,stack,@@ -85,10 +87,10 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<return (<LoopdurationInFrames={calculateLoopDuration({- endAt,+ endAt: trimRight ?? endAt,mediaDuration,playbackRate: props.playbackRate ?? 1,- startFrom,+ startFrom: trimLeft ?? startFrom,})}layout="none"name={name}@@ -102,17 +104,16 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<);}- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined') {- validateStartFromProps(startFrom, endAt);+ if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {+ validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);- const startFromFrameNo = startFrom ?? 0;- const endAtFrameNo = endAt ?? Infinity;+ const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);return (<Sequencelayout="none"- from={0 - startFromFrameNo}+ from={0 - trimLeftValue}showInTimeline={false}- durationInFrames={endAtFrameNo}+ durationInFrames={trimRightValue}name={name}><Videodiff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.tsindex 5b0a5d7aeb..4adbbad5b1 100644--- a/packages/core/src/video/props.ts+++ b/packages/core/src/video/props.ts@@ -3,8 +3,22 @@ import type {LoopVolumeCurveBehavior} from '../audio/use-audio-frame.js';import type {VolumeProp} from '../volume-prop.js';export type RemotionMainVideoProps = {+ /**+ * @deprecated Use `trimLeft` instead+ */startFrom?: number;+ /**+ * @deprecated Use `trimRight` instead+ */endAt?: number;+ /**+ * Trim the video from the left (start). Replaces the deprecated `startFrom` prop.+ */+ trimLeft?: number;+ /**+ * Trim the video from the right (end). Replaces the deprecated `endAt` prop.+ */+ trimRight?: number;/*** @deprecated Only for internal `transparent` use*/