Skip to main content

resolveRedirect()

跟随 URL 的重定向(通常是远程视频或音频),直到最终的 URL 被解析并返回。

如果资源不支持 CORS,该函数将抛出异常。如果资源重定向,并且不支持 CORS,您无法预加载该资产。

tsx
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);
});
tsx
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);
});

示例:解析并预加载视频

此代码片段尝试尽最大努力预加载视频。如果无法解析重定向,它将尝试预加载原始 URL。

tsx
import { preloadVideo, resolveRedirect } from "@remotion/preload";
import { Video } from "remotion";
 
// 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 support
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>;
};
tsx
import { preloadVideo, resolveRedirect } from "@remotion/preload";
import { Video } from "remotion";
 
// 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 support
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>;
};

参见