Get HTML5 Video Duration

By  on  

HTML5 video is exciting, if only for the reason that we no longer need Flash or other third party plugins to render media.  We can also create custom controls and displays around the video, and one of those displays should be the duration of the video.  Let me show you how to get and display the video duration!

The JavaScript

video elements have a duration property which represents the number of seconds in the video.  To display the duration in a pretty fashion, you'll need to use parseInt and modulus (%):

// Assume "video" is the video node
var i = setInterval(function() {
	if(video.readyState > 0) {
		var minutes = parseInt(video.duration / 60, 10);
		var seconds = video.duration % 60;

		// (Put the minutes and seconds in the display)

		clearInterval(i);
	}
}, 200);

It's important to use setInterval and check the video's readyState in case the video hasn't loaded by the time you attempt to pull the duration.  parseInt is used to get the minute number and modulus is used to get the second count.

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • By
    jQuery Link Nudging

    A few weeks back I wrote an article about MooTools Link Nudging, which is essentially a classy, subtle link animation achieved by adding left padding on mouseover and removing it on mouseout. Here's how to do it using jQuery: The jQuery JavaScript It's important to keep...

  • By
    MooTools Accordion: Mouseover Style

    Everyone loves the MooTools Accordion plugin but I get a lot of requests from readers asking me how to make each accordion item open when the user hovers over the item instead of making the user click. You have two options: hack the original plugin...

Discussion

  1. alexander farkas

    There are two events you can use instead of an interval

    a) loadedmetadata
    b) durationchange

    Of course you still need to check readyState initially.

    (function(){
    	var video = document.querySelector('video');
    	var onDurationChange = function(){
    		if(video.readyState){
    			//to your thing
    		}
    	};
    
    	video.addEventListener('durationchange', onDurationChange);
    	onDurationChange();
    })();
    
  2. Sin

    You need to divide video duration by 60 to get minutes, then use parseInt with radix 10. We should test if video duration > 0 before our division.

    var minutes = parseInt(video.duration / 60, 10);
  3. Sin

    You need to divide video duration by 60 to get minutes, then use parseInt with radix 10.

    if (video.duration > 0) {
      var minutes = parseInt(video.duration / 60, 10);
      var seconds = video.duration % 60;  
    }
  4. Tune

    David,

    You forgot to divide the duration (seconds) by 60 to get minutes:

    var minutes = parseInt(video.duration / 60, 10);
    

    Thanks for the great articles!

  5. Hristo Chakarov

    Since when (and on which platforms) setInterval passes the interval ID as argument to the callback function?

  6. HTML5 video is really great, gone are the days struggling with Flash codes and easily display our videos. Thanks for the JS code.

  7. Ron

    Thanks for the code! What if I want also to get the hours and display them as a HH:mm:ss format accordingly?

  8. I m trying to buid the video using jquery. Please help to fetch the video duration before loaded

    var video=$('');    				
    var source= $('');
    $(source).appendTo(video);
    $(source).attr('src',php.mp4);
    $(source).attr('type','video/mp4');	
    //var d=	$(video).duration;
    var d=document.querySelector('video').duration;
    console.log(d);
    
  9. You could just use the javascript while function…

     while (true) {
     if (video.readyState) {
      //blabla code
      break;
     }
     //code to execute if not accomplished
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!