Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger31e67e6f5a79
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index e7ae33e4a4..2c2ae2056e 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -10,7 +10,10 @@ 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 {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';
+import {
+ resolveTrimProps,
+ validateMediaTrimProps,
+} from '../validate-start-from-props.js';
import {DurationsContext} from '../video/duration-state.js';
import {AudioForPreview} from './AudioForPreview.js';
import {AudioForRendering} from './AudioForRendering.js';
@@ -84,6 +87,15 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
durations[getAbsoluteSrc(preloadedSrc)] ??
durations[getAbsoluteSrc(props.src)];
+ validateMediaTrimProps({startFrom, endAt, trimLeft, trimRight});
+
+ const {trimLeftValue, trimRightValue} = resolveTrimProps({
+ startFrom,
+ endAt,
+ trimLeft,
+ trimRight,
+ });
+
if (loop && durationFetched !== undefined) {
if (!Number.isFinite(durationFetched)) {
return (
@@ -101,10 +113,10 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
<Loop
layout="none"
durationInFrames={calculateLoopDuration({
- endAt: trimRight ?? endAt,
+ endAt: trimRightValue ?? endAt,
mediaDuration: duration,
playbackRate: props.playbackRate ?? 1,
- startFrom: trimLeft ?? startFrom,
+ startFrom: trimLeftValue ?? startFrom,
})}
>
<Audio
@@ -116,14 +128,14 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
);
}
- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {
- validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);
-
- const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);
+ if (
+ typeof trimLeftValue !== 'undefined' ||
+ typeof trimRightValue !== 'undefined'
+ ) {
return (
<Sequence
layout="none"
- from={0 - trimLeftValue}
+ from={0 - (trimLeftValue ?? 0)}
showInTimeline={false}
durationInFrames={trimRightValue}
name={name}
diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.ts
index e36737b23c..1185c4b737 100644
--- a/packages/core/src/audio/props.ts
+++ b/packages/core/src/audio/props.ts
@@ -3,19 +3,19 @@ import type {LoopVolumeCurveBehavior} from './use-audio-frame.js';
export type RemotionMainAudioProps = {
/**
- * @deprecated Use `trimLeft` instead
+ * @deprecated `startFrom` was renamed to `trimLeft`
*/
startFrom?: number;
/**
- * @deprecated Use `trimRight` instead
+ * @deprecated `endAt` was renamed to `trimRight`
*/
endAt?: number;
/**
- * Trim the audio from the left (start). Replaces the deprecated `startFrom` prop.
+ * Trim of the audio from the left (start) in frames.
*/
trimLeft?: number;
/**
- * Trim the audio from the right (end). Replaces the deprecated `endAt` prop.
+ * Trim of the audio from the right (end) in frames.
*/
trimRight?: number;
};
diff --git a/packages/core/src/test/audio.test.tsx b/packages/core/src/test/audio.test.tsx
index cc9fb850f3..51205e441b 100644
--- a/packages/core/src/test/audio.test.tsx
+++ b/packages/core/src/test/audio.test.tsx
@@ -22,7 +22,7 @@ describe('Render correctly with props', () => {
expect(() =>
render(
<WrapSequenceContext>
- <Audio src="test" volume={1} startFrom={10} />
+ <Audio src="test" volume={1} trimLeft={10} />
</WrapSequenceContext>,
),
).not.toThrow();
@@ -32,7 +32,7 @@ describe('Render correctly with props', () => {
expect(() =>
render(
<WrapSequenceContext>
- <Audio src="test" volume={1} endAt={10} />
+ <Audio src="test" volume={1} trimRight={10} />
</WrapSequenceContext>,
),
).not.toThrow();
@@ -42,7 +42,7 @@ describe('Render correctly with props', () => {
expect(() =>
render(
<WrapSequenceContext>
- <Audio src="test" volume={1} startFrom={10} endAt={20} />
+ <Audio src="test" volume={1} trimLeft={10} trimRight={20} />
</WrapSequenceContext>,
),
).not.toThrow();
@@ -52,7 +52,7 @@ describe('Render correctly with props', () => {
expect(() =>
render(
<WrapSequenceContext>
- <Audio src="test" volume={1} startFrom={10} endAt={20} loop />
+ <Audio src="test" volume={1} trimLeft={10} trimRight={20} loop />
</WrapSequenceContext>,
),
).not.toThrow();
diff --git a/packages/core/src/test/validate-start-from-props.test.ts b/packages/core/src/test/validate-start-from-props.test.ts
index e179d55bf1..e2be0cc465 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,9 @@
import {describe, test} from 'bun:test';
-import {validateStartFromProps, validateTrimProps, validateMediaTrimProps} from '../validate-start-from-props.js';
+import {
+ validateMediaTrimProps,
+ validateStartFromProps,
+ validateTrimProps,
+} from '../validate-start-from-props.js';
import {expectToThrow} from './expect-to-throw.js';
describe('ValidateStartFrom props should throw', () => {
@@ -127,21 +131,39 @@ describe('ValidateTrim props should throw', () => {
describe('ValidateMediaTrim props should throw on conflicts', () => {
test('It should throw if both startFrom and trimLeft are provided', () => {
expectToThrow(
- () => validateMediaTrimProps(10, undefined, 5, undefined),
+ () =>
+ validateMediaTrimProps({
+ startFrom: 10,
+ endAt: undefined,
+ trimLeft: 5,
+ trimRight: 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),
+ () =>
+ validateMediaTrimProps({
+ startFrom: undefined,
+ endAt: 20,
+ trimLeft: undefined,
+ trimRight: 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),
+ () =>
+ validateMediaTrimProps({
+ startFrom: 10,
+ endAt: 20,
+ trimLeft: 5,
+ trimRight: 15,
+ }),
/Cannot use both startFrom and trimLeft props. Use trimLeft instead as startFrom is deprecated./,
);
});
diff --git a/packages/core/src/validate-start-from-props.ts b/packages/core/src/validate-start-from-props.ts
index b83e378dfa..cbf583f79a 100644
--- a/packages/core/src/validate-start-from-props.ts
+++ b/packages/core/src/validate-start-from-props.ts
@@ -88,12 +88,17 @@ export const validateTrimProps = (
}
};
-export const validateMediaTrimProps = (
- startFrom: number | undefined,
- endAt: number | undefined,
- trimLeft: number | undefined,
- trimRight: number | undefined,
-) => {
+export const validateMediaTrimProps = ({
+ startFrom,
+ endAt,
+ trimLeft,
+ trimRight,
+}: {
+ 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(
@@ -108,8 +113,10 @@ export const validateMediaTrimProps = (
}
// Validate using the appropriate validation function
- const hasNewProps = typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined';
- const hasOldProps = typeof startFrom !== 'undefined' || typeof endAt !== 'undefined';
+ const hasNewProps =
+ typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined';
+ const hasOldProps =
+ typeof startFrom !== 'undefined' || typeof endAt !== 'undefined';
if (hasNewProps) {
validateTrimProps(trimLeft, trimRight);
@@ -118,15 +125,20 @@ export const validateMediaTrimProps = (
}
};
-export const resolveTrimProps = (
- startFrom: number | undefined,
- endAt: number | undefined,
- trimLeft: number | undefined,
- trimRight: number | undefined,
-): {trimLeftValue: number; trimRightValue: number} => {
+export const resolveTrimProps = ({
+ startFrom,
+ endAt,
+ trimLeft,
+ trimRight,
+}: {
+ 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.tsx
index 60c09b00a3..eb92e97e9d 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -2,7 +2,10 @@ 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 {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';
+import {
+ resolveTrimProps,
+ validateMediaTrimProps,
+} from '../validate-start-from-props.js';
import {OffthreadVideoForRendering} from './OffthreadVideoForRendering.js';
import {VideoForPreview} from './VideoForPreview.js';
import type {RemotionOffthreadVideoProps} from './props.js';
@@ -45,14 +48,23 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = (
);
}
- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {
- validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);
+ validateMediaTrimProps({startFrom, endAt, trimLeft, trimRight});
- const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);
+ const {trimLeftValue, trimRightValue} = resolveTrimProps({
+ startFrom,
+ endAt,
+ trimLeft,
+ trimRight,
+ });
+
+ if (
+ typeof trimLeftValue !== 'undefined' ||
+ typeof trimRightValue !== 'undefined'
+ ) {
return (
<Sequence
layout="none"
- from={0 - trimLeftValue}
+ from={0 - (trimLeftValue ?? 0)}
showInTimeline={false}
durationInFrames={trimRightValue}
name={name}
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 358b5c6366..20995bae02 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -9,7 +9,10 @@ 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 {validateMediaTrimProps, resolveTrimProps} from '../validate-start-from-props.js';
+import {
+ resolveTrimProps,
+ validateMediaTrimProps,
+} from '../validate-start-from-props.js';
import {VideoForPreview} from './VideoForPreview.js';
import {VideoForRendering} from './VideoForRendering.js';
import {DurationsContext} from './duration-state.js';
@@ -71,6 +74,14 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
durations[getAbsoluteSrc(preloadedSrc)] ??
durations[getAbsoluteSrc(props.src)];
+ validateMediaTrimProps({startFrom, endAt, trimLeft, trimRight});
+ const {trimLeftValue, trimRightValue} = resolveTrimProps({
+ startFrom,
+ endAt,
+ trimLeft,
+ trimRight,
+ });
+
if (loop && durationFetched !== undefined) {
if (!Number.isFinite(durationFetched)) {
return (
@@ -87,10 +98,10 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
return (
<Loop
durationInFrames={calculateLoopDuration({
- endAt: trimRight ?? endAt,
+ endAt: trimRightValue ?? undefined,
mediaDuration,
playbackRate: props.playbackRate ?? 1,
- startFrom: trimLeft ?? startFrom,
+ startFrom: trimLeftValue ?? undefined,
})}
layout="none"
name={name}
@@ -104,14 +115,14 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
);
}
- if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined' || typeof trimLeft !== 'undefined' || typeof trimRight !== 'undefined') {
- validateMediaTrimProps(startFrom, endAt, trimLeft, trimRight);
-
- const {trimLeftValue, trimRightValue} = resolveTrimProps(startFrom, endAt, trimLeft, trimRight);
+ if (
+ typeof trimLeftValue !== 'undefined' ||
+ typeof trimRightValue !== 'undefined'
+ ) {
return (
<Sequence
layout="none"
- from={0 - trimLeftValue}
+ from={0 - (trimLeftValue ?? 0)}
showInTimeline={false}
durationInFrames={trimRightValue}
name={name}
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index 4adbbad5b1..385dd0ffff 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -4,19 +4,19 @@ import type {VolumeProp} from '../volume-prop.js';
export type RemotionMainVideoProps = {
/**
- * @deprecated Use `trimLeft` instead
+ * @deprecated `startFrom` was renamed to `trimLeft`
*/
startFrom?: number;
/**
- * @deprecated Use `trimRight` instead
+ * @deprecated `endAt` was renamed to `trimRight`
*/
endAt?: number;
/**
- * Trim the video from the left (start). Replaces the deprecated `startFrom` prop.
+ * Trim of th e video from the left (start) in frames.
*/
trimLeft?: number;
/**
- * Trim the video from the right (end). Replaces the deprecated `endAt` prop.
+ * Trim of the video from the right (end) in frames.
*/
trimRight?: number;
/**
diff --git a/packages/template-recorder/tsconfig.node.json b/packages/template-recorder/tsconfig.node.json
index 9d31e2aed9..99fbd14465 100644
--- a/packages/template-recorder/tsconfig.node.json
+++ b/packages/template-recorder/tsconfig.node.json
@@ -3,7 +3,8 @@
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
- "allowSyntheticDefaultImports": true
+ "allowSyntheticDefaultImports": true,
+ "noEmit": true
},
"include": ["vite.config.ts"]
}