Skip to content

Instantly share code, notes, and snippets.

@sebastiankade
Last active April 15, 2019 08:39
Show Gist options
  • Save sebastiankade/efa447c0c476201562cc9087e73e3f85 to your computer and use it in GitHub Desktop.
Save sebastiankade/efa447c0c476201562cc9087e73e3f85 to your computer and use it in GitHub Desktop.
Get duration of video or audio in react browser
export const getVideoDuration = async (file) =>
new Promise((resolve, reject) => {
const mediaType = file.type.split("/")[0];
if (mediaType === "video" || mediaType === "audio") {
var video = document.createElement(mediaType);
video.preload = "metadata";
video.onloadedmetadata = function() {
window.URL.revokeObjectURL(video.src);
resolve(video.duration);
};
video.onerror = reject;
video.src = URL.createObjectURL(file);
} else {
resolve();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment