Skip to content

🎥 Make videos programmatically with React

introduce resolveRedirect()

Jonny Burger14cd6033f4ad
diff --git a/packages/docs/docs/preload/preload-audio.md b/packages/docs/docs/preload/preload-audio.md
index 5f6fc156df..ae73623233 100644
--- a/packages/docs/docs/preload/preload-audio.md
+++ b/packages/docs/docs/preload/preload-audio.md
@@ -28,6 +28,47 @@ unpreload();
- On Firefox, it appends a `<link rel="preload" as="audio">` tag in the head element of the document.
- In other browsers, it appends a `<audio preload="auto">` tag in the body element of the document.
+## Handle redirects
+
+If the audio URL redirects to another URL, preloading the original URL does not work.
+
+If the URL you include is unknown, use [`resolveRedirect()`](/docs/preload/resolve-redirect) to programmatically obtain the final URL following potential redirects.
+
+If the resource does not support CORS, `resolveRedirect()` will fail. If the resource redirects, and does not support CORS, you cannot preload the asset.
+
+This snippet tries to preload a audio on a best-effort basis. If the redirect cannot be resolved, it tries to preload the original URL.
+
+```tsx twoslash
+import { Audio } from "remotion";
+import { resolveRedirect, preloadAudio } from "@remotion/preload";
+
+// This code gets executed immediately once the page loads
+let urlToLoad = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
+
+resolveRedirect(urlToLoad)
+ .then((resolved) => {
+ // Was able to resolve a redirect, setting this as the audio to load
+ urlToLoad = resolved;
+ })
+ .catch((err) => {
+ // Was unable to resolve redirect e.g. due to no CORS supoprt
+ console.log("Could not resolve redirect", err);
+ })
+ .finally(() => {
+ // In either case, we try to preload the original or resolved URL
+ preloadAudio(urlToLoad);
+ });
+
+// This code only executes once the component gets mounted
+const MyComp: React.FC = () => {
+ // If the component did not mount immediately, this will be the resolved URL.
+
+ // If the component mounted immediately, this will be the original URL.
+ // In that case preloading is ineffective anyway.
+ return <Audio src={urlToLoad}></Audio>;
+};
+```
+
## See also
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/preload/src/preload-audio.ts)
diff --git a/packages/docs/docs/preload/preload-video.md b/packages/docs/docs/preload/preload-video.md
index c2c935d892..1060c36291 100644
--- a/packages/docs/docs/preload/preload-video.md
+++ b/packages/docs/docs/preload/preload-video.md
@@ -29,6 +29,49 @@ unpreload();
- On Firefox, it appends a `<link rel="preload" as="video">` tag in the head element of the document.
- In other browsers, it appends a `<video preload="auto">` tag in the body element of the document.
+## Handle redirects
+
+If the video URL redirects to another URL, preloading the original URL does not work.
+
+If the URL you include is unknown, use [`resolveRedirect()`](/docs/preload/resolve-redirect) to programmatically obtain the final URL following potential redirects.
+
+If the resource does not support CORS, `resolveRedirect()` will fail. If the resource redirects, and does not support CORS, you cannot preload the asset.
+
+This snippet tries to preload a video on a best-effort basis. If the redirect cannot be resolved, it tries to preload the original URL.
+
+```tsx twoslash
+import { Video } from "remotion";
+import { resolveRedirect, preloadVideo } from "@remotion/preload";
+
+// This code gets executed immediately once the page loads
+let urlToLoad =
+ "https://player.vimeo.com/external/291648067.hd.mp4?s=94998971682c6a3267e4cbd19d16a7b6c720f345&profile_id=175&oauth2_token_id=57447761";
+
+resolveRedirect(urlToLoad)
+ .then((resolved) => {
+ // Was able to resolve a redirect, setting this as the video to load
+ urlToLoad = resolved;
+ })
+ .catch((err) => {
+ // Was unable to resolve redirect e.g. due to no CORS supoprt
+ console.log("Could not resolve redirect", err);
+ })
+ .finally(() => {
+ // In either case, we try to preload the original or resolved URL
+ preloadVideo(urlToLoad);
+ });
+
+// This code only executes once the component gets mounted
+const MyComp: React.FC = () => {
+ // If the component did not mount immediately, this will be the resolved URL.
+
+ // If the component mounted immediately, this will be the original URL.
+ // In that case preloading is ineffective anyway.
+ return <Video src={urlToLoad}></Video>;
+};
+```
+
## See also
+- [`resolveRedirect()`](/docs/preload/resolve-redirect)
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/preload/src/preload-video.ts)
diff --git a/packages/docs/docs/preload/preload.md b/packages/docs/docs/preload/preload.md
index b4c1fe21bd..866354c51a 100644
--- a/packages/docs/docs/preload/preload.md
+++ b/packages/docs/docs/preload/preload.md
@@ -7,10 +7,11 @@ title: "@remotion/preload"
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
-This package provides functions for preloading assets. While preload is not necessary for rendering, it can help with seamless playback in the [`<Player />`](/docs/player) and in the preview. Currently, two functions are implemented:
+This package provides functions for preloading assets. While preload is not necessary for rendering, it can help with seamless playback in the [`<Player />`](/docs/player) and in the preview. Currently, three functions are implemented:
- [`preloadVideo()`](/docs/preload/preload-video)
- [`preloadAudio()`](/docs/preload/preload-audio)
+- [`resolveRedirect()`](/docs/preload/resolve-redirect)
## Installation
diff --git a/packages/docs/docs/preload/resolve-redirect.md b/packages/docs/docs/preload/resolve-redirect.md
new file mode 100644
index 0000000000..819493b8a8
--- /dev/null
+++ b/packages/docs/docs/preload/resolve-redirect.md
@@ -0,0 +1,64 @@
+---
+id: resolve-redirect
+title: resolveRedirect()
+---
+
+Follows the redirects of a URL (most commonly a remote video or audio) until the final URL is resolved and returns that.
+
+If the resource does not support CORS, the function will throw. **If the resource redirects, and does not support CORS, you cannot preload the asset.**
+
+```tsx twoslash
+import { resolveRedirect } from "@remotion/preload";
+
+resolveRedirect(
+ "https://player.vimeo.com/external/291648067.hd.mp4?s=94998971682c6a3267e4cbd19d16a7b6c720f345&profile_id=175&oauth2_token_id=57447761"
+)
+ .then((src) => {
+ console.log(src); // "https://vod-progressive.akamaized.net/..."
+ })
+ .catch((err) => {
+ console.log("Could not resolve redirect", err);
+ });
+```
+
+## Example: Resolve and preload a video
+
+This snippet tries to preload a video on a best-effort basis. If the redirect cannot be resolved, it tries to preload the original URL.
+
+```tsx twoslash
+import { Video } from "remotion";
+import { resolveRedirect, preloadVideo } from "@remotion/preload";
+
+// This code gets executed immediately once the page loads
+let urlToLoad =
+ "https://player.vimeo.com/external/291648067.hd.mp4?s=94998971682c6a3267e4cbd19d16a7b6c720f345&profile_id=175&oauth2_token_id=57447761";
+
+resolveRedirect(urlToLoad)
+ .then((resolved) => {
+ // Was able to resolve a redirect, setting this as the video to load
+ urlToLoad = resolved;
+ })
+ .catch((err) => {
+ // Was unable to resolve redirect e.g. due to no CORS supoprt
+ console.log("Could not resolve redirect", err);
+ })
+ .finally(() => {
+ // In either case, we try to preload the original or resolved URL
+ preloadVideo(urlToLoad);
+ });
+
+// This code only executes once the component gets mounted
+const MyComp: React.FC = () => {
+ // If the component did not mount immediately, this will be the resolved URL.
+
+ // If the component mounted immediately, this will be the original URL.
+ // In that case preloading is ineffective anyway.
+ return <Video src={urlToLoad}></Video>;
+};
+```
+
+## See also
+
+- [Installing `@remotion/preload`](/docs/preload)
+- [preloadAudio()](/docs/preload/preload-audio)
+- [preloadVideo()](/docs/preload/preload-video)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 22fb5b5d3a..4cf6ebe287 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -202,11 +202,14 @@ module.exports = {
type: "doc",
id: "preload/preload",
},
- items: ["preload/preload-video", "preload/preload-audio"],
+ items: [
+ "preload/preload-video",
+ "preload/preload-audio",
+ "preload/resolve-redirect",
+ ],
},
{
- type: "link",
- href: "/docs/renderer",
+ type: "category",
link: {
type: "doc",
id: "renderer",
diff --git a/packages/preload/src/index.ts b/packages/preload/src/index.ts
index eaed1a9033..5dce934177 100644
--- a/packages/preload/src/index.ts
+++ b/packages/preload/src/index.ts
@@ -1,2 +1,3 @@
export {preloadAudio} from './preload-audio';
export {preloadVideo} from './preload-video';
+export {resolveRedirect} from './resolve-redirect';
diff --git a/packages/preload/src/preload-audio.ts b/packages/preload/src/preload-audio.ts
index 770b02096f..eb5eb0a41f 100644
--- a/packages/preload/src/preload-audio.ts
+++ b/packages/preload/src/preload-audio.ts
@@ -1,14 +1,4 @@
-const resolveRedirect = async (audio: string) => {
- try {
- const res = await fetch(audio);
- return res.url;
- } catch (err) {
- console.info(
- '[@remotion/preload] Resource does not support CORS. Cannot handle any potential redirects'
- );
- return audio;
- }
-};
+import {resolveRedirect} from './resolve-redirect';
export const preloadAudio = (src: string): (() => void) => {
if (typeof document === 'undefined') {
diff --git a/packages/preload/src/preload-video.ts b/packages/preload/src/preload-video.ts
index aaa2a4eebb..7290a6aff5 100644
--- a/packages/preload/src/preload-video.ts
+++ b/packages/preload/src/preload-video.ts
@@ -1,14 +1,4 @@
-const resolveRedirect = async (video: string) => {
- try {
- const res = await fetch(video);
- return res.url;
- } catch (err) {
- console.info(
- '[@remotion/preload] Resource does not support CORS. Cannot handle any potential redirects'
- );
- return video;
- }
-};
+import {resolveRedirect} from './resolve-redirect';
export const preloadVideo = (src: string): (() => void) => {
if (typeof document === 'undefined') {
diff --git a/packages/preload/src/resolve-redirect.ts b/packages/preload/src/resolve-redirect.ts
new file mode 100644
index 0000000000..0843d80b7f
--- /dev/null
+++ b/packages/preload/src/resolve-redirect.ts
@@ -0,0 +1,4 @@
+export const resolveRedirect = async (videoOrAudio: string) => {
+ const res = await fetch(videoOrAudio);
+ return res.url;
+};