Get HTML5 Video Duration
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.
![7 Essential JavaScript Functions]()
I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent. Times have changed but there are still a few functions each developer should...
![From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!]()
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
![Create a Quick MooTools Slideshow with Preloading Images]()
I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you:
The following code snippet will show you how to create a simple slideshow with MooTools; the script will also...
![WebKit Marquee CSS: Bringin’ Sexy Back]()
We all joke about the days of Web yesteryear. You remember them: stupid animated GIFs (flames and "coming soon" images, most notably), lame counters, guestbooks, applets, etc. Another "feature" we thought we had gotten rid of was the marquee. The marquee was a rudimentary, javascript-like...
There are two events you can use instead of an interval
a)
loadedmetadatab)
durationchangeOf course you still need to check
readyStateinitially.(function(){ var video = document.querySelector('video'); var onDurationChange = function(){ if(video.readyState){ //to your thing } }; video.addEventListener('durationchange', onDurationChange); onDurationChange(); })();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.
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; }David,
You forgot to divide the duration (seconds) by 60 to get minutes:
Thanks for the great articles!
Since when (and on which platforms)
setIntervalpasses the interval ID as argument to the callback function?HTML5 video is really great, gone are the days struggling with Flash codes and easily display our videos. Thanks for the JS code.
Thanks for the code! What if I want also to get the hours and display them as a
HH:mm:ssformat accordingly?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);You could just use the javascript
whilefunction…while (true) { if (video.readyState) { //blabla code break; } //code to execute if not accomplished }