Skip to content

🎥 Make videos programmatically with React

Add crossOrigin support to @remotion/media <Video> parser fetches.

Alec Watts4f683251392a
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 0bed0e2113..08e82597ae 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -24,6 +24,20 @@ export type MediaPlayerInitResult =
| {type: 'no-tracks'}
| {type: 'disposed'};
+const crossOriginToRequestCredentials = (
+ crossOrigin: '' | 'anonymous' | 'use-credentials' | undefined,
+): RequestCredentials | undefined => {
+ if (crossOrigin === 'use-credentials') {
+ return 'include';
+ }
+
+ if (crossOrigin === 'anonymous' || crossOrigin === '') {
+ return 'same-origin';
+ }
+
+ return undefined;
+};
+
export class MediaPlayer {
private canvas: HTMLCanvasElement | OffscreenCanvas | null;
private context:
@@ -90,6 +104,7 @@ export class MediaPlayer {
onVideoFrameCallback,
playing,
sequenceOffset,
+ crossOrigin,
}: {
canvas: HTMLCanvasElement | OffscreenCanvas | null;
src: string;
@@ -111,6 +126,7 @@ export class MediaPlayer {
onVideoFrameCallback: null | ((frame: CanvasImageSource) => void);
playing: boolean;
sequenceOffset: number;
+ crossOrigin: '' | 'anonymous' | 'use-credentials' | undefined;
}) {
this.canvas = canvas ?? null;
this.src = src;
@@ -133,9 +149,17 @@ export class MediaPlayer {
this.onVideoFrameCallback = onVideoFrameCallback;
this.playing = playing;
this.sequenceOffset = sequenceOffset;
+ const requestCredentials = crossOriginToRequestCredentials(crossOrigin);
this.input = new Input({
- source: new UrlSource(this.src),
+ source: new UrlSource(
+ this.src,
+ requestCredentials
+ ? {
+ requestInit: {credentials: requestCredentials},
+ }
+ : undefined,
+ ),
formats: ALL_FORMATS,
});
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index fa29c728d5..c26c6f47a1 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -58,6 +58,7 @@ type OptionalVideoProps = {
debugAudioScheduling: boolean;
headless: boolean;
onError: MediaOnError | undefined;
+ crossOrigin: '' | 'anonymous' | 'use-credentials' | 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 19c43244ae..3ce96d71ae 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -64,6 +64,7 @@ type VideoForPreviewProps = {
readonly debugAudioScheduling: boolean;
readonly headless: boolean;
readonly onError: MediaOnError | undefined;
+ readonly crossOrigin: '' | 'anonymous' | 'use-credentials' | undefined;
};
type VideoForPreviewAssertedShowingProps = VideoForPreviewProps & {
@@ -95,6 +96,7 @@ const VideoForPreviewAssertedShowing: React.FC<
debugAudioScheduling,
headless,
onError,
+ crossOrigin,
controls,
}) => {
const src = usePreload(unpreloadedSrc);
@@ -229,6 +231,7 @@ const VideoForPreviewAssertedShowing: React.FC<
onVideoFrameCallback: initialOnVideoFrameRef.current ?? null,
playing: initialPlaying.current,
sequenceOffset: initialSequenceOffset.current,
+ crossOrigin,
});
mediaPlayerRef.current = player;
@@ -361,6 +364,7 @@ const VideoForPreviewAssertedShowing: React.FC<
videoConfig.fps,
onError,
videoConfig.durationInFrames,
+ crossOrigin,
]);
const classNameValue = useMemo(() => {
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 1c1f2566fb..a2ce634519 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -85,6 +85,7 @@ const InnerVideo: React.FC<
debugAudioScheduling,
headless,
onError,
+ crossOrigin,
controls,
}) => {
const environment = useRemotionEnvironment();
@@ -170,6 +171,7 @@ const InnerVideo: React.FC<
debugAudioScheduling={debugAudioScheduling ?? false}
headless={headless ?? false}
onError={onError}
+ crossOrigin={crossOrigin}
controls={controls}
/>
);
@@ -205,6 +207,7 @@ const VideoInner: React.FC<
debugAudioScheduling,
headless,
onError,
+ crossOrigin,
controls,
}) => {
const fallbackLogLevel = Internals.useLogLevel();
@@ -239,6 +242,7 @@ const VideoInner: React.FC<
debugAudioScheduling={debugAudioScheduling ?? false}
headless={headless ?? false}
onError={onError}
+ crossOrigin={crossOrigin}
controls={controls}
/>
);