Skip to content

🎥 Make videos programmatically with React

simplification

Jonny Burger85bab930a600
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index f59cefdead..4501ee4a24 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -1,5 +1,6 @@
import React from 'react';
import {Sequence} from '../sequencing';
+import {validateStartFromProps} from '../validate-start-from-props';
import {AudioForDevelopment} from './AudioForDevelopment';
import {RemotionAudioProps, RemotionMainAudioProps} from './props';
@@ -8,41 +9,7 @@ export const Audio: React.FC<RemotionAudioProps & RemotionMainAudioProps> = (
) => {
const {startAt, endAt, ...otherProps} = props;
if (typeof startAt !== 'undefined' || typeof endAt !== 'undefined') {
- if (typeof startAt !== 'undefined') {
- if (typeof startAt !== 'number') {
- throw new TypeError(
- `type of startAt prop must be a number, instead got type ${typeof startAt}.`
- );
- }
- if (isNaN(startAt) || startAt === Infinity) {
- throw new TypeError('startAt prop can not be NaN or Infinity.');
- }
- if (startAt < 0) {
- throw new TypeError(
- `startAt must be greater than equal to 0 instead got ${startAt}.`
- );
- }
- }
- if (typeof endAt !== 'undefined') {
- if (typeof endAt !== 'number') {
- throw new TypeError(
- `type of endAt prop must be a number, instead got type ${typeof endAt}.`
- );
- }
- if (isNaN(endAt)) {
- throw new TypeError('endAt prop can not be NaN.');
- }
- if (endAt <= 0) {
- throw new TypeError(
- `endAt must be a positive number, instead got ${endAt}.`
- );
- }
- }
- if (typeof startAt !== 'undefined' && typeof endAt !== 'undefined') {
- if (endAt < startAt) {
- throw new TypeError('endAt prop must be greater than startAt prop');
- }
- }
+ validateStartFromProps(startAt, endAt);
const startAtFrameNo = startAt ?? 0;
const endAtFrameNo = endAt ?? Infinity;
return (
diff --git a/packages/core/src/validate-start-from-props.ts b/packages/core/src/validate-start-from-props.ts
new file mode 100644
index 0000000000..e2156c15ad
--- /dev/null
+++ b/packages/core/src/validate-start-from-props.ts
@@ -0,0 +1,38 @@
+export const validateStartFromProps = (
+ startAt: number | undefined,
+ endAt: number | undefined
+) => {
+ if (typeof startAt !== 'undefined') {
+ if (typeof startAt !== 'number') {
+ throw new TypeError(
+ `type of startAt prop must be a number, instead got type ${typeof startAt}.`
+ );
+ }
+ if (isNaN(startAt) || startAt === Infinity) {
+ throw new TypeError('startAt prop can not be NaN or Infinity.');
+ }
+ if (startAt < 0) {
+ throw new TypeError(
+ `startAt must be greater than equal to 0 instead got ${startAt}.`
+ );
+ }
+ }
+ if (typeof endAt !== 'undefined') {
+ if (typeof endAt !== 'number') {
+ throw new TypeError(
+ `type of endAt prop must be a number, instead got type ${typeof endAt}.`
+ );
+ }
+ if (isNaN(endAt)) {
+ throw new TypeError('endAt prop can not be NaN.');
+ }
+ if (endAt <= 0) {
+ throw new TypeError(
+ `endAt must be a positive number, instead got ${endAt}.`
+ );
+ }
+ }
+ if ((endAt as number) < (startAt as number)) {
+ throw new TypeError('endAt prop must be greater than startAt prop');
+ }
+};
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 2dd9733e7f..b9c5781534 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -1,5 +1,6 @@
import React from 'react';
import {Sequence} from '../sequencing';
+import {validateStartFromProps} from '../validate-start-from-props';
import {RemotionMainVideoProps, RemotionVideoProps} from './props';
import {VideoForDevelopment} from './VideoForDevelopment';
import {VideoForRendering} from './VideoForRendering';
@@ -9,45 +10,12 @@ export const Video: React.FC<RemotionVideoProps & RemotionMainVideoProps> = (
) => {
const {startAt, endAt, ...otherProps} = props;
if (typeof startAt !== 'undefined' || typeof endAt !== 'undefined') {
- if (typeof startAt !== 'undefined') {
- if (typeof startAt !== 'number') {
- throw new TypeError(
- `type of startAt prop is a number, instead got type ${typeof startAt}.`
- );
- }
- if (startAt < 0) {
- throw new TypeError(
- `startAt must be greater than equal to 0 instead got ${startAt}.`
- );
- }
- if (isNaN(startAt) || startAt === Infinity) {
- throw new TypeError('startAt prop can not be NaN or Infinity.');
- }
- }
- if (typeof endAt !== 'undefined') {
- if (typeof endAt !== 'number') {
- throw new TypeError(
- `type of endAt prop is a number, instead got type ${typeof endAt}.`
- );
- }
- if (endAt <= 0) {
- throw new TypeError(
- `endAt must be a positive number, instead got ${endAt}.`
- );
- }
- if (isNaN(endAt)) {
- throw new TypeError('endAt prop can not be NaN.');
- }
- }
- if (typeof startAt !== 'undefined' && typeof endAt !== 'undefined') {
- if (endAt < startAt) {
- throw new TypeError('endAt prop must be greater than startAt prop');
- }
- }
- const startAtFrameNo = typeof startAt !== 'undefined' ? startAt : 0;
- const endAtFrameNO = typeof endAt !== 'undefined' ? endAt : Infinity;
+ validateStartFromProps(startAt, endAt);
+
+ const startAtFrameNo = startAt ?? 0;
+ const endAtFrameNo = endAt ?? Infinity;
return (
- <Sequence from={0 - startAtFrameNo} durationInFrames={endAtFrameNO}>
+ <Sequence from={0 - startAtFrameNo} durationInFrames={endAtFrameNo}>
<Video {...otherProps} />
</Sequence>
);