Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Fix error message for when falling back due to network error

JonnyBurgerc646f23bd1b4
diff --git a/packages/media/src/audio-extraction/extract-audio.ts b/packages/media/src/audio-extraction/extract-audio.ts
index fb8e8c84fc..583a990c70 100644
--- a/packages/media/src/audio-extraction/extract-audio.ts
+++ b/packages/media/src/audio-extraction/extract-audio.ts
@@ -48,6 +48,7 @@ const extractAudioInternal = async ({
}
| 'cannot-decode'
| 'unknown-container-format'
+ | 'network-error'
> => {
const {getAudio, actualMatroskaTimestamps, isMatroska, getDuration} =
await getSink(src, logLevel);
@@ -59,6 +60,10 @@ const extractAudioInternal = async ({
const audio = await getAudio(audioStreamIndex);
+ if (audio === 'network-error') {
+ return 'network-error';
+ }
+
if (audio === 'no-audio-track') {
return {data: null, durationInSeconds: null};
}
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index fe54fa0a12..ccbe81fd83 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -84,6 +84,10 @@ export const extractFrameAndAudio = async ({
};
}
+ if (frame?.type === 'network-error') {
+ return {type: 'network-error'};
+ }
+
if (audio === 'unknown-container-format') {
if (frame !== null) {
frame?.frame?.close();
@@ -92,6 +96,14 @@ export const extractFrameAndAudio = async ({
return {type: 'unknown-container-format'};
}
+ if (audio === 'network-error') {
+ if (frame !== null) {
+ frame?.frame?.close();
+ }
+
+ return {type: 'network-error'};
+ }
+
if (audio === 'cannot-decode') {
if (frame?.type === 'success' && frame.frame !== null) {
frame?.frame.close();
diff --git a/packages/media/src/test/editlist-offset.test.ts b/packages/media/src/test/editlist-offset.test.ts
index 96437b3c56..14b8516c64 100644
--- a/packages/media/src/test/editlist-offset.test.ts
+++ b/packages/media/src/test/editlist-offset.test.ts
@@ -19,6 +19,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
});
assert(audio1 !== 'cannot-decode');
assert(audio1 !== 'unknown-container-format');
+ assert(audio1 !== 'network-error');
expect(audio1.data).toBe(null);
expect(audio1.durationInSeconds).toBe(null);
@@ -38,6 +39,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
});
assert(audio2 !== 'cannot-decode');
assert(audio2 !== 'unknown-container-format');
+ assert(audio2 !== 'network-error');
assert(audio2.data);
expect(audio2.data.data.length).toBe(3200);
expect(audio2.data.timestamp).toBe((1 / 30) * 1_000_000);
@@ -60,6 +62,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
});
assert(audio3 !== 'cannot-decode');
assert(audio3 !== 'unknown-container-format');
+ assert(audio3 !== 'network-error');
assert(audio3.data);
expect(audio3.data.data.length).toBe(3200);
expect(audio3.data.timestamp).toBe((2 / 30) * 1_000_000);
diff --git a/packages/media/src/test/extract-accuracy-uneven.test.ts b/packages/media/src/test/extract-accuracy-uneven.test.ts
index cc2cc17eb0..66cc23e252 100644
--- a/packages/media/src/test/extract-accuracy-uneven.test.ts
+++ b/packages/media/src/test/extract-accuracy-uneven.test.ts
@@ -30,6 +30,10 @@ test('Extract accuracy over 100 frames with playback rate 1.75', async () => {
throw new Error(`Unknown container format at frame ${i}`);
}
+ if (audio === 'network-error') {
+ throw new Error(`Network error at frame ${i}`);
+ }
+
assert(audio);
assert(audio.data);
diff --git a/packages/media/src/test/extract-accuracy.test.ts b/packages/media/src/test/extract-accuracy.test.ts
index fe94d74018..eabc06b37f 100644
--- a/packages/media/src/test/extract-accuracy.test.ts
+++ b/packages/media/src/test/extract-accuracy.test.ts
@@ -30,6 +30,10 @@ test('Extract accuracy over 100 frames with playback rate 2', async () => {
throw new Error(`Unknown container format at frame ${i}`);
}
+ if (audio === 'network-error') {
+ throw new Error(`Network error at frame ${i}`);
+ }
+
assert(audio);
assert(audio.data);
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index a0c4a0d34c..51e363e4a6 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -12,7 +12,8 @@ type ExtractFrameResult =
}
| {type: 'cannot-decode'; durationInSeconds: number | null}
| {type: 'cannot-decode-alpha'; durationInSeconds: number | null}
- | {type: 'unknown-container-format'};
+ | {type: 'unknown-container-format'}
+ | {type: 'network-error'};
type ExtractFrameParams = {
src: string;
@@ -53,6 +54,10 @@ const extractFrameInternal = async ({
return {type: 'unknown-container-format'};
}
+ if (video === 'network-error') {
+ return {type: 'network-error'};
+ }
+
let mediaDurationInSeconds: number | null = null;
if (loop) {
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 a6a0e7ae11..3ada382581 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, UrlSourceOptions} from 'mediabunny';
+import type {EncodedPacket, InputFormat, UrlSourceOptions} from 'mediabunny';
import {
ALL_FORMATS,
AudioSampleSink,
@@ -10,6 +10,7 @@ import {
WEBM,
} from 'mediabunny';
import type {LogLevel} from 'remotion';
+import {isNetworkError} from '../is-network-error';
import {makeKeyframeBank} from './keyframe-bank';
import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';
@@ -26,21 +27,29 @@ export type AudioSinkResult =
| AudioSinks
| 'no-audio-track'
| 'cannot-decode-audio'
- | 'unknown-container-format';
+ | 'unknown-container-format'
+ | 'network-error';
export type VideoSinkResult =
| VideoSinks
| 'no-video-track'
| 'cannot-decode'
- | 'unknown-container-format';
+ | 'unknown-container-format'
+ | 'network-error';
const getRetryDelay = (() => {
return null;
}) satisfies UrlSourceOptions['getRetryDelay'];
-const getFormatOrNull = async (input: Input) => {
+const getFormatOrNullOrNetworkError = async (
+ input: Input,
+): Promise<InputFormat | 'network-error' | null> => {
try {
return await input.getFormat();
- } catch {
+ } catch (err) {
+ if (isNetworkError(err as Error)) {
+ return 'network-error';
+ }
+
return null;
}
};
@@ -53,10 +62,14 @@ export const getSinks = async (src: string) => {
}),
});
- const format = await getFormatOrNull(input);
+ const format = await getFormatOrNullOrNetworkError(input);
const isMatroska = format === MATROSKA || format === WEBM;
const getVideoSinks = async (): Promise<VideoSinkResult> => {
+ if (format === 'network-error') {
+ return 'network-error';
+ }
+
if (format === null) {
return 'unknown-container-format';
}
@@ -99,6 +112,10 @@ export const getSinks = async (src: string) => {
return 'unknown-container-format';
}
+ if (format === 'network-error') {
+ return 'network-error';
+ }
+
const audioTracks = await input.getAudioTracks();
const audioTrack = audioTracks[index];
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 3893abcbe9..6574e9f42a 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -252,9 +252,9 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
}
if (window.remotion_isMainTab) {
- Internals.Log.info(
+ Internals.Log.warn(
{logLevel, tag: '@remotion/media'},
- `Network error fetching ${src}, falling back to <OffthreadVideo>`,
+ `Network error fetching ${src} (no CORS?), falling back to <OffthreadVideo>`,
);
}