Detect if video has audio, using javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasAudio(video) { | |
return ( | |
video.mozHasAudio || | |
Boolean(video.webkitAudioDecodedByteCount) || | |
Boolean(video.audioTracks?.length) | |
); | |
} | |
function hasVideoGotAudio(src) { | |
const video = document.createElement('video'); | |
video.muted = true; | |
video.crossOrigin = 'anonymous'; | |
video.preload = 'auto'; | |
return new Promise((resolve, reject) => { | |
video.addEventListener('error', reject); | |
video.addEventListener( | |
'canplay', | |
() => { | |
video.currentTime = 0.99; | |
}, | |
{ once: true } // Important because 'canplay' can be fired hundreds of times. | |
); | |
video.addEventListener('seeked', () => resolve(hasAudio(video)), { | |
once: true, | |
}); | |
video.src = src; | |
}); | |
} | |
export default hasVideoGotAudio; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment