Created
April 11, 2025 07:10
-
-
Save barsbek/97472e6978b0100cb1d84442043453ca to your computer and use it in GitHub Desktop.
Get media duration
This file contains hidden or 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
const getMediaDuration = async (file, type) => { | |
// type either video or audio | |
return new Promise((resolve, reject) => { | |
const element = document.createElement(type); | |
element.preload = "metadata"; | |
element.onloadedmetadata = () => { | |
window.URL.revokeObjectURL(element.src); | |
resolve(element.duration); | |
}; | |
element.onerror = (error) => { | |
reject(error); | |
}; | |
element.src = URL.createObjectURL(file); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment