Skip to content

🎥 Make videos programmatically with React

Thread credentials through entire rendering pipeline and document for Audio

Jonny Burger5b160e10c21f
diff --git a/packages/docs/docs/media/audio.mdx b/packages/docs/docs/media/audio.mdx
index bf8aa3c0c6..edef83f6ca 100644
--- a/packages/docs/docs/media/audio.mdx
+++ b/packages/docs/docs/media/audio.mdx
@@ -227,6 +227,27 @@ export const MyComposition = () => {
};
```
+### `credentials?`<AvailableFrom v="4.0.436" />
+
+Controls the [`credentials`](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials) option of the `fetch()` requests made to retrieve the audio data.
+
+Accepts `"omit"`, `"same-origin"` (default behavior of `fetch()`) or `"include"`.
+Set to `"include"` if you need to send cookies or authentication headers to a cross-origin audio URL.
+
+```tsx twoslash
+import {AbsoluteFill} from 'remotion';
+import {Audio} from '@remotion/media';
+
+// ---cut---
+export const MyComposition = () => {
+ return (
+ <AbsoluteFill>
+ <Audio credentials="include" src="https://example.com/protected-audio.mp3" />
+ </AbsoluteFill>
+ );
+};
+```
+
### `toneFrequency?`
Accepts a number between `0.01` and `2`, where `1` represents the original pitch. Values less than `1` will decrease the pitch, while values greater than `1` will increase it.
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdx
index bce05619f3..98e70d89de 100644
--- a/packages/docs/docs/media/video.mdx
+++ b/packages/docs/docs/media/video.mdx
@@ -269,7 +269,7 @@ export const MyComposition = () => {
};
```
-### `credentials?`
+### `credentials?`<AvailableFrom v="4.0.436" />
Controls the [`credentials`](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials) option of the `fetch()` requests made to retrieve the video data.
diff --git a/packages/media/src/audio-extraction/extract-audio.ts b/packages/media/src/audio-extraction/extract-audio.ts
index ef65d4d17d..e4a5b0cb85 100644
--- a/packages/media/src/audio-extraction/extract-audio.ts
+++ b/packages/media/src/audio-extraction/extract-audio.ts
@@ -31,6 +31,7 @@ type ExtractAudioParams = {
trimAfter: number | undefined;
fps: number;
maxCacheSize: number;
+ credentials: RequestCredentials | undefined;
};
const extractAudioInternal = async ({
@@ -45,6 +46,7 @@ const extractAudioInternal = async ({
trimAfter,
fps,
maxCacheSize,
+ credentials,
}: ExtractAudioParams): Promise<
| {
data: PcmS16AudioData | null;
@@ -55,7 +57,7 @@ const extractAudioInternal = async ({
| 'network-error'
> => {
const {getAudio, actualMatroskaTimestamps, isMatroska, getDuration} =
- await getSink(src, logLevel, undefined);
+ await getSink(src, logLevel, credentials);
let mediaDurationInSeconds: number | null = null;
if (loop) {
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 88256c38f1..50a15c51fe 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -52,6 +52,7 @@ type NewAudioForPreviewProps = {
readonly fallbackHtml5AudioProps: FallbackHtml5AudioProps | undefined;
readonly debugAudioScheduling: boolean;
readonly onError: MediaOnError | undefined;
+ readonly credentials: RequestCredentials | undefined;
};
type AudioForPreviewAssertedShowingProps = NewAudioForPreviewProps & {
@@ -79,6 +80,7 @@ const AudioForPreviewAssertedShowing: React.FC<
fallbackHtml5AudioProps,
debugAudioScheduling,
onError,
+ credentials,
controls,
}) => {
const videoConfig = useUnsafeVideoConfig();
@@ -237,7 +239,7 @@ const AudioForPreviewAssertedShowing: React.FC<
onVideoFrameCallback: null,
playing: initialPlaying.current,
sequenceOffset: initialSequenceOffset.current,
- credentials: undefined,
+ credentials,
});
mediaPlayerRef.current = player;
@@ -374,6 +376,7 @@ const AudioForPreviewAssertedShowing: React.FC<
buffer,
onError,
videoConfig.durationInFrames,
+ credentials,
]);
if (shouldFallbackToNativeAudio && !disallowFallbackToHtml5Audio) {
@@ -428,6 +431,7 @@ type InnerAudioProps = {
readonly fallbackHtml5AudioProps?: FallbackHtml5AudioProps;
readonly debugAudioScheduling?: boolean;
readonly onError?: MediaOnError;
+ readonly credentials?: RequestCredentials;
};
export const AudioForPreview: React.FC<
@@ -453,6 +457,7 @@ export const AudioForPreview: React.FC<
fallbackHtml5AudioProps,
debugAudioScheduling,
onError,
+ credentials,
controls,
}) => {
const preloadedSrc = usePreload(src);
@@ -509,6 +514,7 @@ export const AudioForPreview: React.FC<
toneFrequency={toneFrequency}
debugAudioScheduling={debugAudioScheduling ?? false}
onError={onError}
+ credentials={credentials}
fallbackHtml5AudioProps={fallbackHtml5AudioProps}
controls={controls}
/>
diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsx
index 41119b03ff..f4b6559af1 100644
--- a/packages/media/src/audio/audio-for-rendering.tsx
+++ b/packages/media/src/audio/audio-for-rendering.tsx
@@ -37,6 +37,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({
trimAfter,
trimBefore,
onError,
+ credentials,
}) => {
const defaultLogLevel = Internals.useLogLevel();
const logLevel = overriddenLogLevel ?? defaultLogLevel;
@@ -129,6 +130,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
})
.then((result) => {
const handleError = (
@@ -266,6 +268,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({
maxCacheSize,
audioEnabled,
onError,
+ credentials,
]);
if (replaceWithHtml5Audio) {
diff --git a/packages/media/src/audio/props.ts b/packages/media/src/audio/props.ts
index 668f522ec6..ea3f3981a0 100644
--- a/packages/media/src/audio/props.ts
+++ b/packages/media/src/audio/props.ts
@@ -35,4 +35,5 @@ export type AudioProps = {
delayRenderTimeoutInMilliseconds?: number;
debugAudioScheduling?: boolean;
onError?: MediaOnError;
+ credentials?: RequestCredentials;
};
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index 0584a79ffd..68ccb40453 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -19,6 +19,7 @@ export const extractFrameAndAudio = async ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
}: {
src: string;
timeInSeconds: number;
@@ -33,6 +34,7 @@ export const extractFrameAndAudio = async ({
trimBefore: number | undefined;
fps: number;
maxCacheSize: number;
+ credentials: RequestCredentials | undefined;
}): Promise<ExtractFrameViaBroadcastChannelResult> => {
try {
const [video, audio] = await Promise.all([
@@ -47,6 +49,7 @@ export const extractFrameAndAudio = async ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
})
: null,
includeAudio
@@ -62,6 +65,7 @@ export const extractFrameAndAudio = async ({
fps,
trimBefore,
maxCacheSize,
+ credentials,
})
: null,
]);
diff --git a/packages/media/src/test/audio-encoding.test.ts b/packages/media/src/test/audio-encoding.test.ts
index df59619e98..7bec8e1293 100644
--- a/packages/media/src/test/audio-encoding.test.ts
+++ b/packages/media/src/test/audio-encoding.test.ts
@@ -35,6 +35,7 @@ test('Audio samples from MP3 should produce identical s16 output on Chrome and F
maxCacheSize: getMaxVideoCacheSize('info'),
durationInSeconds: 1 / 30,
audioStreamIndex: 0,
+ credentials: undefined,
});
if (a === 'cannot-decode') {
diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.ts
index 678af15e01..476def6101 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -20,6 +20,7 @@ test('Should be able to extract a frame', async () => {
trimBefore: undefined,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (result.type === 'cannot-decode') {
@@ -71,6 +72,7 @@ test('Should be able to extract the last frame', async () => {
trimBefore: undefined,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (result.type === 'cannot-decode') {
@@ -121,6 +123,7 @@ test('Should manage the cache', async (t) => {
trimBefore: undefined,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
}
@@ -150,6 +153,7 @@ test('Should be apply volume correctly', async () => {
trimBefore: undefined,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (result.type === 'cannot-decode') {
@@ -204,6 +208,7 @@ test('Should be able to loop', async () => {
trimBefore: undefined,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (result.type === 'cannot-decode') {
diff --git a/packages/media/src/test/editlist-offset.test.ts b/packages/media/src/test/editlist-offset.test.ts
index 14b8516c64..70b3f5c306 100644
--- a/packages/media/src/test/editlist-offset.test.ts
+++ b/packages/media/src/test/editlist-offset.test.ts
@@ -16,6 +16,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
trimBefore: undefined,
trimAfter: undefined,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(audio1 !== 'cannot-decode');
assert(audio1 !== 'unknown-container-format');
@@ -36,6 +37,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
trimBefore: undefined,
trimAfter: undefined,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(audio2 !== 'cannot-decode');
assert(audio2 !== 'unknown-container-format');
@@ -59,6 +61,7 @@ test('Audio extraction should be correct if there is edit list offset', async ()
trimBefore: undefined,
trimAfter: undefined,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(audio3 !== 'cannot-decode');
assert(audio3 !== 'unknown-container-format');
diff --git a/packages/media/src/test/extract-accuracy-uneven.test.ts b/packages/media/src/test/extract-accuracy-uneven.test.ts
index 66cc23e252..756aed1da5 100644
--- a/packages/media/src/test/extract-accuracy-uneven.test.ts
+++ b/packages/media/src/test/extract-accuracy-uneven.test.ts
@@ -21,6 +21,7 @@ test('Extract accuracy over 100 frames with playback rate 1.75', async () => {
trimBefore: undefined,
trimAfter: undefined,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (audio === 'cannot-decode') {
throw new Error(`Cannot decode at frame ${i}`);
diff --git a/packages/media/src/test/extract-accuracy.test.ts b/packages/media/src/test/extract-accuracy.test.ts
index eabc06b37f..5babcb6a17 100644
--- a/packages/media/src/test/extract-accuracy.test.ts
+++ b/packages/media/src/test/extract-accuracy.test.ts
@@ -21,6 +21,7 @@ test('Extract accuracy over 100 frames with playback rate 2', async () => {
trimBefore: undefined,
trimAfter: undefined,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
if (audio === 'cannot-decode') {
throw new Error(`Cannot decode at frame ${i}`);
diff --git a/packages/media/src/test/looping.test.ts b/packages/media/src/test/looping.test.ts
index 858c3b8d9c..08d93cc1af 100644
--- a/packages/media/src/test/looping.test.ts
+++ b/packages/media/src/test/looping.test.ts
@@ -41,6 +41,7 @@ test(
playbackRate,
fps,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
expect(result.type).toBe('success');
assert(result.type === 'success');
diff --git a/packages/media/src/test/video-after-end.test.ts b/packages/media/src/test/video-after-end.test.ts
index 2d603e0c5c..e1bae94d69 100644
--- a/packages/media/src/test/video-after-end.test.ts
+++ b/packages/media/src/test/video-after-end.test.ts
@@ -15,6 +15,7 @@ test('Should render last frame for timestamps after video end', async () => {
playbackRate: 1,
fps: 24,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(result.type === 'success');
diff --git a/packages/media/src/test/video-non-zero-start.test.ts b/packages/media/src/test/video-non-zero-start.test.ts
index 4e0fb29536..c557c0a9a6 100644
--- a/packages/media/src/test/video-non-zero-start.test.ts
+++ b/packages/media/src/test/video-non-zero-start.test.ts
@@ -19,6 +19,7 @@ test('Should render first frame for videos starting after timestamp 0', async ()
playbackRate: 1,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
// Should successfully extract (no error thrown)
diff --git a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
index b9205f7707..06f0808782 100644
--- a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
+++ b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
@@ -145,6 +145,7 @@ test('in rendering, should also be smart', async (t) => {
playbackRate: 1,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(frame.type === 'success');
if (lastFrame) {
@@ -167,6 +168,7 @@ test('in rendering, should also be smart', async (t) => {
playbackRate: 1,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
+ credentials: undefined,
});
assert(firstRealFrame.type === 'success');
diff --git a/packages/media/src/video-extraction/add-broadcast-channel-listener.ts b/packages/media/src/video-extraction/add-broadcast-channel-listener.ts
index 99dad4b3ca..d8a6beb3a4 100644
--- a/packages/media/src/video-extraction/add-broadcast-channel-listener.ts
+++ b/packages/media/src/video-extraction/add-broadcast-channel-listener.ts
@@ -53,6 +53,7 @@ export type ExtractFrameRequest = {
trimBefore: number | undefined;
fps: number;
maxCacheSize: number;
+ credentials: RequestCredentials | undefined;
};
// Send to other channels a message to let them know that the
@@ -102,6 +103,7 @@ export const addBroadcastChannelListener = () => {
trimBefore: data.trimBefore,
fps: data.fps,
maxCacheSize: data.maxCacheSize,
+ credentials: data.credentials,
});
if (result.type === 'cannot-decode') {
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 f936d6d858..a6285cfef2 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
@@ -39,6 +39,7 @@ export const extractFrameViaBroadcastChannel = async ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
}: {
src: string;
timeInSeconds: number;
@@ -54,6 +55,7 @@ export const extractFrameViaBroadcastChannel = async ({
trimBefore: number | undefined;
fps: number;
maxCacheSize: number;
+ credentials: RequestCredentials | undefined;
}): Promise<ExtractFrameViaBroadcastChannelResult> => {
if (isClientSideRendering || window.remotion_isMainTab) {
return extractFrameAndAudio({
@@ -70,6 +72,7 @@ export const extractFrameViaBroadcastChannel = async ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
});
}
@@ -195,6 +198,7 @@ export const extractFrameViaBroadcastChannel = async ({
trimBefore,
fps,
maxCacheSize,
+ credentials,
};
window.remotion_broadcastChannel!.postMessage(request);
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index fa9e0d14f2..ce47e042c6 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -25,6 +25,7 @@ type ExtractFrameParams = {
playbackRate: number;
fps: number;
maxCacheSize: number;
+ credentials: RequestCredentials | undefined;
};
const extractFrameInternal = async ({
@@ -37,8 +38,9 @@ const extractFrameInternal = async ({
playbackRate,
fps,
maxCacheSize,
+ credentials,
}: ExtractFrameParams): Promise<ExtractFrameResult> => {
- const sink = await getSink(src, logLevel, undefined);
+ const sink = await getSink(src, logLevel, credentials);
const [video, mediaDurationInSecondsRaw] = await Promise.all([
sink.getVideo(),
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 2645703f10..b96a88ccf5 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -51,6 +51,7 @@ type InnerVideoProps = {
readonly trimAfterValue: number | undefined;
readonly headless: boolean;
readonly onError: MediaOnError | undefined;
+ readonly credentials: RequestCredentials | undefined;
};
type FallbackToOffthreadVideo = {
@@ -80,6 +81,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
trimBeforeValue,
headless,
onError,
+ credentials,
}) => {
if (!src) {
throw new TypeError('No `src` was passed to <Video>.');
@@ -184,6 +186,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
trimBefore: trimBeforeValue,
fps,
maxCacheSize,
+ credentials,
})
.then((result) => {
const handleError = (
@@ -383,6 +386,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
cancelRender,
headless,
onError,
+ credentials,
]);
const classNameValue = useMemo(() => {
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 77741e14d8..af47cd243e 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -143,6 +143,7 @@ const InnerVideo: React.FC<
trimBeforeValue={trimBeforeValue}
headless={headless}
onError={onError}
+ credentials={credentials}
/>
);
}