Skip to main content

resolveRedirect()

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

🌐 Follows the redirects of a URL (most commonly a remote video or audio) until the final URL is resolved and returns that.

如果资源不支持 CORS,该函数将抛出错误。如果资源重定向,并且不支持 CORS,则无法预加载该资源。

🌐 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.

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

这段代码尝试在尽力的基础上预加载视频。如果无法解析重定向,它会尝试预加载原始 URL。

🌐 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.

import {preloadVideo, resolveRedirect} from '@remotion/preload';
import {OffthreadVideo} 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 <OffthreadVideo src={urlToLoad}></OffthreadVideo>;
};

另请参阅

🌐 See also