HTML5 Video duration without playing video

For HTML5 you should be able to use the video tag’s duration property, once the file’s metadata is loaded. See this answer for a good way to do it:

Retrieving HTML5 video duration separately from the file

To quote the anwser by Mikushi:

myVideoPlayer.addEventListener('loadedmetadata', function() {
    console.log(videoPlayer.duration);
});

By listening for the ‘loadedmetadata’ event you will ensure the duration value has been loaded.

More details on the loadedmetadata can be found here: http://www.w3schools.com/tags/av_event_loadedmetadata.asp

EDIT

As per @lucas-vasques ‘s comment, instead of the loadmetadata event, you could use durationchange which the MDN Media Events list describes as …

The metadata has loaded or changed, indicating a change in duration of
the media. This is sent, for example, when the media has loaded
enough that the duration is known.

Leave a Comment