Sometimes, we want to retrieve HTML5 video duration separately from the file with JavaScript.
In this article, we’ll look at how to retrieve HTML5 video duration separately from the file with JavaScript.
How to retrieve HTML5 video duration separately from the file with JavaScript?
To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange
event.
For instance, we write
const myVideoPlayer = document.getElementById("video_player");
myVideoPlayer.addEventListener("durationchange", () => {
console.log("Duration change", myVideoPlayer.duration);
});
to select the video element with getElementById
.
Then we listen for the durationchange
event with addEventListener
.
And we get the duration of the video with the duration
property.
Conclusion
To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange
event.