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.
![Conquering Impostor Syndrome]()
Two years ago I documented my struggles with Imposter Syndrome and the response was immense. I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions. I've even caught myself reading the post...
![9 More Mind-Blowing WebGL Demos]()
With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities. I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...
![Using MooTools For Opacity]()
Although it's possible to achieve opacity using CSS, the hacks involved aren't pretty. If you're using the MooTools JavaScript library, opacity is as easy as using an element's "set" method. The following MooTools snippet takes every image with the "opacity" class and sets...
![Advanced CSS Printing – Using JavaScript Double-Click To Remove Unwanted DIVs]()
Like any good programmer, I'm constantly searching around the internet for ideas and articles that can help me improve my code. There are thousands of talented programmers out there so I stumble upon some great articles and code snippets that I like to print out...
There are two events you can use instead of an interval
a)
loadedmetadata
b)
durationchange
Of course you still need to check
readyState
initially.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.
David,
You forgot to divide the duration (seconds) by 60 to get minutes:
Thanks for the great articles!
Since when (and on which platforms)
setInterval
passes 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:ss
format accordingly?I m trying to buid the video using jquery. Please help to fetch the video duration before loaded
You could just use the javascript
while
function…