Skip to content

🎥 Make videos programmatically with React

validate media props

Jonny Burger8b6d0edc0c2f
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsx
index cfb8eaf97f..357f697aa3 100644
--- a/packages/core/src/audio/AudioForDevelopment.tsx
+++ b/packages/core/src/audio/AudioForDevelopment.tsx
@@ -5,6 +5,7 @@ import {SequenceContext} from '../sequencing';
import {usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
+import {validateMediaProps} from '../validate-media-props';
import {RemotionAudioProps} from './props';
export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
@@ -21,6 +22,8 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
const [id] = useState(() => String(Math.random()));
+ validateMediaProps(props, 'Audio');
+
useEffect(() => {
const ref = audioRef.current;
if (!ref) {
diff --git a/packages/core/src/audio/AudioForRendering.tsx b/packages/core/src/audio/AudioForRendering.tsx
index 56882b8583..9251614f21 100644
--- a/packages/core/src/audio/AudioForRendering.tsx
+++ b/packages/core/src/audio/AudioForRendering.tsx
@@ -2,6 +2,7 @@ import React, {useContext, useEffect, useMemo} from 'react';
import {CompositionManager} from '../CompositionManager';
import {random} from '../random';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
+import {validateMediaProps} from '../validate-media-props';
import {RemotionAudioProps} from './props';
export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {
@@ -9,6 +10,8 @@ export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {
const sequenceFrame = useCurrentFrame();
const {registerAsset, unregisterAsset} = useContext(CompositionManager);
+ validateMediaProps(props, 'Audio');
+
// Generate a string that's as unique as possible for this asset
// but at the same time the same on all threads
const id = useMemo(
diff --git a/packages/core/src/test/media-validation.test.tsx b/packages/core/src/test/media-validation.test.tsx
new file mode 100644
index 0000000000..976d4f2c7d
--- /dev/null
+++ b/packages/core/src/test/media-validation.test.tsx
@@ -0,0 +1,100 @@
+import {render} from '@testing-library/react';
+import React, {useContext} from 'react';
+import {Audio} from '../audio';
+import {Internals} from '../internals';
+import {Video} from '../video';
+import {expectToThrow} from './expect-to-throw';
+
+const Wrapper: React.FC = ({children}) => {
+ const compositions = useContext(Internals.CompositionManager);
+ return (
+ <Internals.RemotionRoot>
+ <Internals.CompositionManager.Provider
+ value={{
+ ...compositions,
+ compositions: [
+ {
+ height: 1080,
+ width: 1080,
+ fps: 30,
+ durationInFrames: 30,
+ id: 'markup',
+ component: React.lazy(() =>
+ Promise.resolve({
+ default: (() => null) as React.ComponentType<unknown>,
+ })
+ ),
+ },
+ ],
+ currentComposition: 'markup',
+ }}
+ >
+ {children}
+ </Internals.CompositionManager.Provider>
+ </Internals.RemotionRoot>
+ );
+};
+
+test('It should not allow a video to have a negative volume', () => {
+ expectToThrow(
+ () =>
+ render(
+ <Wrapper>
+ <Video src="hi" volume={-1} />
+ </Wrapper>
+ ),
+ /You have passed a volume below 0 to your <Video \/> component./
+ );
+});
+
+test('It should not allow an audio element to have a negative volume', () => {
+ expectToThrow(
+ () =>
+ render(
+ <Wrapper>
+ <Audio src="hi" volume={-1} />
+ </Wrapper>
+ ),
+ /You have passed a volume below 0 to your <Audio \/> component./
+ );
+});
+
+test('It should not allow an audio element to have a wrong type', () => {
+ expectToThrow(
+ () =>
+ render(
+ <Wrapper>
+ {/**
+ *
+ // @ts-expect-error */}
+ <Audio src="hi" volume="wrong" />
+ </Wrapper>
+ ),
+ /You have passed a volume of type string to your <Audio \/> component. Volume must be a number or undefined./
+ );
+});
+
+test('It should not allow an video element to have a wrong type', () => {
+ expectToThrow(
+ () =>
+ render(
+ <Wrapper>
+ {/**
+ *
+ // @ts-expect-error */}
+ <Video src="hi" volume="wrong" />
+ </Wrapper>
+ ),
+ /You have passed a volume of type string to your <Video \/> component. Volume must be a number or undefined./
+ );
+});
+
+test('It should allow a valid volume', () => {
+ expect(() =>
+ render(
+ <Wrapper>
+ <Video src="hi" volume={1} />
+ </Wrapper>
+ )
+ ).not.toThrow();
+});
diff --git a/packages/core/src/validate-media-props.ts b/packages/core/src/validate-media-props.ts
new file mode 100644
index 0000000000..ca5368b117
--- /dev/null
+++ b/packages/core/src/validate-media-props.ts
@@ -0,0 +1,18 @@
+import {RemotionAudioProps} from './audio';
+import {RemotionVideoProps} from './video';
+
+export const validateMediaProps = (
+ props: RemotionVideoProps | RemotionAudioProps,
+ component: 'Video' | 'Audio'
+) => {
+ if (typeof props.volume !== 'number' && typeof props.volume !== 'undefined') {
+ throw new TypeError(
+ `You have passed a volume of type ${typeof props.volume} to your <${component} /> component. Volume must be a number or undefined.`
+ );
+ }
+ if (typeof props.volume === 'number' && props.volume < 0) {
+ throw new TypeError(
+ `You have passed a volume below 0 to your <${component} /> component. Volume must be between 0 and 1`
+ );
+ }
+};
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index a0830fcec3..817f3758f7 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -3,6 +3,7 @@ import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';
import {usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
+import {validateMediaProps} from '../validate-media-props';
import {RemotionVideoProps} from './props';
export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
@@ -12,6 +13,8 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
const videoConfig = useUnsafeVideoConfig();
const [playing] = usePlayingState();
+ validateMediaProps(props, 'Video');
+
// TODO: Register as an asset
useEffect(() => {
if (playing && !videoRef.current?.ended) {
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 9bfd2cd4f6..3ade249b14 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -6,6 +6,7 @@ import {random} from '../random';
import {continueRender, delayRender} from '../ready-manager';
import {useCurrentFrame} from '../use-frame';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
+import {validateMediaProps} from '../validate-media-props';
import {RemotionVideoProps} from './props';
export const VideoForRendering: React.FC<RemotionVideoProps> = ({
@@ -17,6 +18,8 @@ export const VideoForRendering: React.FC<RemotionVideoProps> = ({
const videoRef = useRef<HTMLVideoElement>(null);
const {registerAsset, unregisterAsset} = useContext(CompositionManager);
+ validateMediaProps(props, 'Video');
+
// Generate a string that's as unique as possible for this asset
// but at the same time the same on all threads
const id = useMemo(
diff --git a/packages/example/src/RemoteVideo/index.tsx b/packages/example/src/RemoteVideo/index.tsx
index 441a3b4072..d36ec277b7 100644
--- a/packages/example/src/RemoteVideo/index.tsx
+++ b/packages/example/src/RemoteVideo/index.tsx
@@ -1,11 +1,10 @@
-import {Video} from 'remotion';
+import {interpolate, useCurrentFrame, Video} from 'remotion';
const RemoteVideo: React.FC = () => {
+ const frame = useCurrentFrame();
return (
<Video
- style={{
- transform: `rotate(180deg)`,
- }}
+ volume={interpolate(frame, [0, 500], [1, 0], {extrapolateRight: 'clamp'})}
src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
/>
);