Skip to content

🎥 Make videos programmatically with React

easier resolution

Jonny Burger456a1ba0628c
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 5be4f9ef43..56565a8382 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -16,13 +16,10 @@ import {
} from 'remotion';
import {getTimeInSeconds} from '../get-time-in-seconds';
import {MediaPlayer} from '../media-player';
+import {type MediaOnError, callOnErrorAndResolve} from '../on-error';
import {useLoopDisplay} from '../show-in-timeline';
import {useMediaInTimeline} from '../use-media-in-timeline';
-import type {
- FallbackHtml5AudioProps,
- MediaErrorAction,
- MediaErrorEvent,
-} from './props';
+import type {FallbackHtml5AudioProps} from './props';
const {
useUnsafeVideoConfig,
@@ -54,7 +51,7 @@ type NewAudioForPreviewProps = {
readonly toneFrequency: number | undefined;
readonly audioStreamIndex: number | undefined;
readonly fallbackHtml5AudioProps: FallbackHtml5AudioProps | undefined;
- readonly onError: ((event: MediaErrorEvent) => MediaErrorAction) | undefined;
+ readonly onError: MediaOnError | undefined;
};
const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({
@@ -203,30 +200,22 @@ const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({
}
const handleError = (error: Error, fallbackMessage: string) => {
- if (onError) {
- const action = onError({error});
- if (action === 'fail') {
- throw error;
- }
-
- // action === 'fallback'
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
+ error,
+ disallowFallback: disallowFallbackToHtml5Audio,
+ isClientSideRendering: false,
+ clientSideError: error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
+ } else {
Internals.Log.warn(
{logLevel, tag: '@remotion/media'},
fallbackMessage,
);
setShouldFallbackToNativeAudio(true);
- return;
}
-
- if (disallowFallbackToHtml5Audio) {
- throw error;
- }
-
- Internals.Log.warn(
- {logLevel, tag: '@remotion/media'},
- fallbackMessage,
- );
- setShouldFallbackToNativeAudio(true);
};
if (result.type === 'unknown-container-format') {
@@ -272,32 +261,40 @@ const AudioForPreviewAssertedShowing: React.FC<NewAudioForPreviewProps> = ({
}
})
.catch((error) => {
- if (onError) {
- const action = onError({error});
- if (action === 'fail') {
- throw error;
- }
- }
-
- Internals.Log.error(
- {logLevel, tag: '@remotion/media'},
- '[AudioForPreview] Failed to initialize MediaPlayer',
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
error,
- );
- setShouldFallbackToNativeAudio(true);
+ disallowFallback: disallowFallbackToHtml5Audio,
+ isClientSideRendering: false,
+ clientSideError: error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
+ } else {
+ Internals.Log.error(
+ {logLevel, tag: '@remotion/media'},
+ '[AudioForPreview] Failed to initialize MediaPlayer',
+ error,
+ );
+ setShouldFallbackToNativeAudio(true);
+ }
});
} catch (error) {
- if (onError) {
- const action = onError({error: error as Error});
- if (action === 'fail') {
- throw error;
- }
+ const [action, errorToUse] = callOnErrorAndResolve({
+ error: error as Error,
+ onError,
+ disallowFallback: disallowFallbackToHtml5Audio,
+ isClientSideRendering: false,
+ clientSideError: error as Error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
}
Internals.Log.error(
{logLevel, tag: '@remotion/media'},
'[AudioForPreview] MediaPlayer initialization failed',
- error,
+ errorToUse,
);
setShouldFallbackToNativeAudio(true);
}
@@ -493,7 +490,7 @@ type InnerAudioProps = {
readonly toneFrequency?: number;
readonly audioStreamIndex?: number;
readonly fallbackHtml5AudioProps?: FallbackHtml5AudioProps;
- readonly onError?: (event: MediaErrorEvent) => MediaErrorAction;
+ readonly onError?: MediaOnError;
};
export const AudioForPreview: React.FC<InnerAudioProps> = ({
diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsx
index 9cacab4637..41119b03ff 100644
--- a/packages/media/src/audio/audio-for-rendering.tsx
+++ b/packages/media/src/audio/audio-for-rendering.tsx
@@ -13,6 +13,7 @@ import {useMaxMediaCacheSize} from '../caches';
import {applyVolume} from '../convert-audiodata/apply-volume';
import {TARGET_SAMPLE_RATE} from '../convert-audiodata/resample-audiodata';
import {frameForVolumeProp} from '../looped-frame';
+import {callOnErrorAndResolve} from '../on-error';
import {extractFrameViaBroadcastChannel} from '../video-extraction/extract-frame-via-broadcast-channel';
import type {AudioProps} from './props';
@@ -135,36 +136,22 @@ export const AudioForRendering: React.FC<AudioProps> = ({
clientSideError: Error,
fallbackMessage: string,
) => {
- if (environment.isClientSideRendering) {
- cancelRender(clientSideError);
- return;
- }
-
- if (onError) {
- const action = onError({error});
- if (action === 'fail') {
- cancelRender(error);
- return;
- }
-
- // action === 'fallback'
- Internals.Log.warn(
- {logLevel, tag: '@remotion/media'},
- fallbackMessage,
- );
- setReplaceWithHtml5Audio(true);
- return;
- }
-
- if (disallowFallbackToHtml5Audio) {
- cancelRender(error);
- return;
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
+ error,
+ disallowFallback: disallowFallbackToHtml5Audio ?? false,
+ isClientSideRendering: environment.isClientSideRendering,
+ clientSideError,
+ });
+ if (action === 'fail') {
+ cancelRender(errorToUse);
}
Internals.Log.warn(
{logLevel, tag: '@remotion/media'},
fallbackMessage,
);
+
setReplaceWithHtml5Audio(true);
};
diff --git a/packages/media/src/audio/props.ts b/packages/media/src/audio/props.ts
index b6bf5631af..af5b7bfd8c 100644
--- a/packages/media/src/audio/props.ts
+++ b/packages/media/src/audio/props.ts
@@ -1,10 +1,5 @@
import type {LogLevel, LoopVolumeCurveBehavior, VolumeProp} from 'remotion';
-
-export type MediaErrorAction = 'fallback' | 'fail';
-
-export type MediaErrorEvent = {
- error: Error;
-};
+import type {MediaOnError} from '../on-error';
export type FallbackHtml5AudioProps = {
crossOrigin?: '' | 'anonymous' | 'use-credentials' | undefined;
@@ -38,5 +33,5 @@ export type AudioProps = {
toneFrequency?: number;
delayRenderRetries?: number;
delayRenderTimeoutInMilliseconds?: number;
- onError?: (event: MediaErrorEvent) => MediaErrorAction;
+ onError?: MediaOnError;
};
diff --git a/packages/media/src/index.ts b/packages/media/src/index.ts
index d1d9767171..6bc908ec34 100644
--- a/packages/media/src/index.ts
+++ b/packages/media/src/index.ts
@@ -11,11 +11,7 @@ export const experimental_Audio = Audio;
export const experimental_Video = Video;
export {AudioForPreview} from './audio/audio-for-preview';
-export {
- AudioProps,
- FallbackHtml5AudioProps,
- MediaErrorAction,
- MediaErrorEvent,
-} from './audio/props';
+export {AudioProps, FallbackHtml5AudioProps} from './audio/props';
+export {MediaErrorAction} from './on-error';
export {FallbackOffthreadVideoProps, VideoProps} from './video/props';
export {Audio, Video};
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index 97b0dd15f7..f1239f133e 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -4,8 +4,7 @@ import type {
OnVideoFrame,
VolumeProp,
} from 'remotion';
-
-export type MediaErrorAction = 'fallback' | 'fail';
+import type {MediaOnError} from '../on-error';
export type MediaErrorEvent = {
error: Error;
@@ -54,7 +53,7 @@ type OptionalVideoProps = {
showInTimeline: boolean;
debugOverlay: boolean;
headless: boolean;
- onError: ((event: MediaErrorEvent) => MediaErrorAction) | undefined;
+ onError: MediaOnError | undefined;
};
export type InnerVideoProps = MandatoryVideoProps &
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 5be1d75077..7464bd3990 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -16,13 +16,10 @@ import {
} from 'remotion';
import {getTimeInSeconds} from '../get-time-in-seconds';
import {MediaPlayer} from '../media-player';
+import {type MediaOnError, callOnErrorAndResolve} from '../on-error';
import {useLoopDisplay} from '../show-in-timeline';
import {useMediaInTimeline} from '../use-media-in-timeline';
-import type {
- FallbackOffthreadVideoProps,
- MediaErrorAction,
- MediaErrorEvent,
-} from './props';
+import type {FallbackOffthreadVideoProps} from './props';
const {
useUnsafeVideoConfig,
@@ -59,7 +56,7 @@ type VideoForPreviewProps = {
readonly audioStreamIndex: number;
readonly debugOverlay: boolean;
readonly headless: boolean;
- readonly onError: ((event: MediaErrorEvent) => MediaErrorAction) | undefined;
+ readonly onError: MediaOnError | undefined;
};
const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({
@@ -211,25 +208,18 @@ const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({
}
const handleError = (error: Error, fallbackMessage: string) => {
- if (onError) {
- const action = onError({error});
- if (action === 'fail') {
- throw error;
- }
-
- // action === 'fallback'
- Internals.Log.warn(
- {logLevel, tag: '@remotion/media'},
- fallbackMessage,
- );
- setShouldFallbackToNativeVideo(true);
- return;
- }
-
- if (disallowFallbackToOffthreadVideo) {
- throw error;
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
+ error,
+ disallowFallback: disallowFallbackToOffthreadVideo,
+ isClientSideRendering: false,
+ clientSideError: error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
}
+ // action === 'fallback'
Internals.Log.warn(
{logLevel, tag: '@remotion/media'},
fallbackMessage,
@@ -275,32 +265,40 @@ const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({
}
})
.catch((error) => {
- if (onError) {
- const action = onError({error});
- if (action === 'fail') {
- throw error;
- }
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
+ error,
+ disallowFallback: disallowFallbackToOffthreadVideo,
+ isClientSideRendering: false,
+ clientSideError: error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
}
Internals.Log.error(
{logLevel, tag: '@remotion/media'},
'[VideoForPreview] Failed to initialize MediaPlayer',
- error,
+ errorToUse,
);
setShouldFallbackToNativeVideo(true);
});
} catch (error) {
- if (onError) {
- const action = onError({error: error as Error});
- if (action === 'fail') {
- throw error;
- }
+ const [action, errorToUse] = callOnErrorAndResolve({
+ error: error as Error,
+ onError,
+ disallowFallback: disallowFallbackToOffthreadVideo,
+ isClientSideRendering: false,
+ clientSideError: error as Error,
+ });
+ if (action === 'fail') {
+ throw errorToUse;
}
Internals.Log.error(
{logLevel, tag: '@remotion/media'},
'[VideoForPreview] MediaPlayer initialization failed',
- error,
+ errorToUse,
);
setShouldFallbackToNativeVideo(true);
}
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 51d9aae4fe..61d70f63c8 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -24,12 +24,9 @@ import {useMaxMediaCacheSize} from '../caches';
import {applyVolume} from '../convert-audiodata/apply-volume';
import {TARGET_SAMPLE_RATE} from '../convert-audiodata/resample-audiodata';
import {frameForVolumeProp} from '../looped-frame';
+import {type MediaOnError, callOnErrorAndResolve} from '../on-error';
import {extractFrameViaBroadcastChannel} from '../video-extraction/extract-frame-via-broadcast-channel';
-import type {
- FallbackOffthreadVideoProps,
- MediaErrorAction,
- MediaErrorEvent,
-} from './props';
+import type {FallbackOffthreadVideoProps} from './props';
type InnerVideoProps = {
readonly className: string | undefined;
@@ -53,9 +50,7 @@ type InnerVideoProps = {
readonly trimBeforeValue: number | undefined;
readonly trimAfterValue: number | undefined;
readonly headless: boolean;
- readonly onError:
- | ((event: MediaErrorEvent) => MediaErrorAction | undefined)
- | undefined;
+ readonly onError: MediaOnError | undefined;
};
type FallbackToOffthreadVideo = {
@@ -199,35 +194,20 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
return;
}
- if (onError) {
- const action =
- onError({error: err}) ??
- (disallowFallbackToOffthreadVideo ? 'fail' : 'fallback');
-
- if (action === 'fail') {
- cancelRender(err);
- return;
- }
-
- // action === 'fallback'
- if (window.remotion_isMainTab) {
- Internals.Log.warn(
- {logLevel, tag: '@remotion/media'},
- fallbackMessage,
- );
- }
-
- setReplaceWithOffthreadVideo({
- durationInSeconds: mediaDurationInSeconds,
- });
- return;
- }
+ const [action, errorToUse] = callOnErrorAndResolve({
+ onError,
+ error: err,
+ disallowFallback: disallowFallbackToOffthreadVideo,
+ isClientSideRendering: environment.isClientSideRendering,
+ clientSideError: err,
+ });
- if (disallowFallbackToOffthreadVideo) {
- cancelRender(err);
+ if (action === 'fail') {
+ cancelRender(errorToUse);
return;
}
+ // action === 'fallback'
if (window.remotion_isMainTab) {
Internals.Log.warn(
{logLevel, tag: '@remotion/media'},