检查视频是否可以使用 Mediabunny 解码
在尝试将视频加载到 <Video> 之前,你可能想要检查视频是否可以被浏览器解码。这可以使用 Mediabunny 来完成。
🌐 Before attempting to load a video into <Video>, you may want to check if the video can be decoded by the browser. This can be done using Mediabunny.
这是一个 canDecode() 函数,你可以将其复制并粘贴到你的项目中:
🌐 Here's a canDecode() function you can copy and paste into your project:
can-decode.tsimport {ALL_FORMATS ,Input ,UrlSource ,BlobSource } from 'mediabunny'; export constcanDecode = async (src : string |Blob ) => { constinput = newInput ({formats :ALL_FORMATS ,source : typeofsrc === 'string' ? newUrlSource (src ) : newBlobSource (src ), }); try { awaitinput .getFormat (); } catch { return false; } constvideoTrack = awaitinput .getPrimaryVideoTrack (); if (videoTrack && !(awaitvideoTrack .canDecode ())) { return false; } constaudioTrack = awaitinput .getPrimaryAudioTrack (); if (audioTrack && !(awaitaudioTrack .canDecode ())) { return false; } return true; };
用法
🌐 Usage
检查视频是否可以解码
🌐 Check if a video can be decoded
const src = 'https://remotion.media/video.mp4';
const isDecodable = await canDecode(src);
if (isDecodable) {
console.log('Video can be decoded');
} else {
console.log('Video cannot be decoded');
}另请参阅
🌐 See also