Skip to content

🎥 Make videos programmatically with React

fallback

JonnyBurger7e6c5fe9e1e8
diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsx
index c4211d1909..6d60816f18 100644
--- a/packages/media/src/audio/audio-for-rendering.tsx
+++ b/packages/media/src/audio/audio-for-rendering.tsx
@@ -98,6 +98,15 @@ export const AudioForRendering: React.FC<AudioProps> = ({
return;
}
+ if (result === 'network-error') {
+ Internals.Log.info(
+ {logLevel, tag: '@remotion/media'},
+ `Network error fetching ${src}, falling back to <Audio>`,
+ );
+ setReplaceWithOffthreadVideo(true);
+ return;
+ }
+
const {audio, durationInSeconds: assetDurationInSeconds} = result;
const volumePropsFrame = frameForVolumeProp({
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index 9e3d8a3827..84ec00d76b 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -28,43 +28,60 @@ export const extractFrameAndAudio = async ({
durationInSeconds: number | null;
}
| 'cannot-decode'
+ | 'network-error'
> => {
- const [frame, audio] = await Promise.all([
- includeVideo
- ? extractFrame({
- src,
- timeInSeconds,
- logLevel,
- loop,
- })
- : null,
- includeAudio
- ? extractAudio({
- src,
- timeInSeconds,
- durationInSeconds,
- logLevel,
- loop,
- playbackRate,
- })
- : null,
- ]);
+ try {
+ const [frame, audio] = await Promise.all([
+ includeVideo
+ ? extractFrame({
+ src,
+ timeInSeconds,
+ logLevel,
+ loop,
+ })
+ : null,
+ includeAudio
+ ? extractAudio({
+ src,
+ timeInSeconds,
+ durationInSeconds,
+ logLevel,
+ loop,
+ playbackRate,
+ })
+ : null,
+ ]);
- if (frame === 'cannot-decode') {
- return 'cannot-decode';
- }
+ if (frame === 'cannot-decode') {
+ return 'cannot-decode';
+ }
+
+ if (audio === 'cannot-decode') {
+ if (frame !== null) {
+ frame?.close();
+ }
- if (audio === 'cannot-decode') {
- if (frame !== null) {
- frame?.close();
+ return 'cannot-decode';
}
- return 'cannot-decode';
- }
+ return {
+ frame: frame?.toVideoFrame() ?? null,
+ audio: audio?.data ?? null,
+ durationInSeconds: audio?.durationInSeconds ?? null,
+ };
+ } catch (err) {
+ const error = err as Error;
+ if (
+ // Chrome
+ error.message.includes('Failed to fetch') ||
+ // Safari
+ error.message.includes('Load failed') ||
+ // Firefox
+ error.message.includes('NetworkError when attempting to fetch resource')
+ ) {
+ return 'network-error';
+ }
- return {
- frame: frame?.toVideoFrame() ?? null,
- audio: audio?.data ?? null,
- durationInSeconds: audio?.durationInSeconds ?? null,
- };
+ throw err;
+ }
};
diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.ts
index 9255b9c6c2..4f1e95c922 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -21,6 +21,10 @@ test('Should be able to extract a frame', async () => {
throw new Error('Cannot decode');
}
+ if (result === 'network-error') {
+ throw new Error('Network error');
+ }
+
const {audio, frame} = result;
assert(audio);
@@ -59,6 +63,10 @@ test('Should be able to extract the last frame', async () => {
throw new Error('Cannot decode');
}
+ if (result === 'network-error') {
+ throw new Error('Network error');
+ }
+
const {audio, frame} = result;
assert(frame);
@@ -108,6 +116,10 @@ test('Should be apply volume correctly', async () => {
throw new Error('Cannot decode');
}
+ if (result === 'network-error') {
+ throw new Error('Network error');
+ }
+
const {audio: audioAtFullVolume, frame} = result;
const totalAudioAtFullVolume = audioAtFullVolume?.data.reduce((acc, curr) => {
@@ -144,6 +156,10 @@ test('Should be able to loop', async () => {
throw new Error('Cannot decode');
}
+ if (result === 'network-error') {
+ throw new Error('Network error');
+ }
+
const {frame} = result;
expect(frame?.timestamp).toBe(41_000_000);
diff --git a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
index 1b6e073cc5..730fb13573 100644
--- a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
+++ b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
@@ -31,6 +31,10 @@ type ExtractFrameResponse =
| {
type: 'response-cannot-decode';
id: string;
+ }
+ | {
+ type: 'response-network-error';
+ id: string;
};
// Doesn't exist in studio
@@ -62,6 +66,16 @@ if (window.remotion_broadcastChannel && window.remotion_isMainTab) {
return;
}
+ if (result === 'network-error') {
+ const networkErrorResponse: ExtractFrameResponse = {
+ type: 'response-network-error',
+ id: data.id,
+ };
+
+ window.remotion_broadcastChannel!.postMessage(networkErrorResponse);
+ return;
+ }
+
const {frame, audio, durationInSeconds} = result;
const videoFrame = frame;
@@ -125,6 +139,7 @@ export const extractFrameViaBroadcastChannel = ({
durationInSeconds: number | null;
}
| 'cannot-decode'
+ | 'network-error'
> => {
if (isClientSideRendering || window.remotion_isMainTab) {
return extractFrameAndAudio({
@@ -148,6 +163,7 @@ export const extractFrameViaBroadcastChannel = ({
durationInSeconds: number | null;
}
| 'cannot-decode'
+ | 'network-error'
>((resolve, reject) => {
const onMessage = (event: MessageEvent) => {
const data = event.data as ExtractFrameResponse;
@@ -183,6 +199,15 @@ export const extractFrameViaBroadcastChannel = ({
'message',
onMessage,
);
+ } else if (
+ data.type === 'response-network-error' &&
+ data.id === requestId
+ ) {
+ resolve('cannot-decode');
+ window.remotion_broadcastChannel!.removeEventListener(
+ 'message',
+ onMessage,
+ );
}
};
diff --git a/packages/media/src/video-extraction/get-frames-since-keyframe.ts b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
index 1cc880ab57..3416a255cb 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -1,4 +1,4 @@
-import type {EncodedPacket} from 'mediabunny';
+import type {EncodedPacket, UrlSourceOptions} from 'mediabunny';
import {
ALL_FORMATS,
AudioSampleSink,
@@ -26,10 +26,29 @@ export type AudioSinkResult =
| 'cannot-decode-audio';
export type VideoSinkResult = VideoSinks | 'no-video-track' | 'cannot-decode';
+const getRetryDelay = ((previousAttempts, err) => {
+ const error = err as Error;
+ // We suspect CORS error
+ if (
+ // Chrome
+ error.message.includes('Failed to fetch') ||
+ // Safari
+ error.message.includes('Load failed') ||
+ // Firefox
+ error.message.includes('NetworkError when attempting to fetch resource')
+ ) {
+ return null;
+ }
+
+ return Math.min(2 ** (previousAttempts - 2), 16);
+}) satisfies UrlSourceOptions['getRetryDelay'];
+
export const getSinks = async (src: string) => {
const input = new Input({
formats: ALL_FORMATS,
- source: new UrlSource(src),
+ source: new UrlSource(src, {
+ getRetryDelay,
+ }),
});
const format = await input.getFormat();
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 8d48c04d67..3879fb74e1 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -109,6 +109,15 @@ export const VideoForRendering: React.FC<VideoProps> = ({
return;
}
+ if (result === 'network-error') {
+ Internals.Log.info(
+ {logLevel, tag: '@remotion/media'},
+ `Network error fetching ${src}, falling back to <OffthreadVideo>`,
+ );
+ setReplaceWithOffthreadVideo(true);
+ return;
+ }
+
const {
frame: imageBitmap,
audio,