Check if a video can be decoded using 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.
Here's a canDecode() function you can copy and paste into your project:
can-decode.tstsimport {ALL_FORMATS ,Input ,UrlSource } from 'mediabunny';export constcanDecode = async (src : string) => {constinput = newInput ({formats :ALL_FORMATS ,source : newUrlSource (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
tsconst 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');}