Categories
JavaScript Answers

How to retrieve HTML5 video duration separately from the file with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *