Javascript DOM HTML Video duration Property

Introduction

Get the length of a video:

var x = document.getElementById("myVideo").duration;

Click the button to get the exact length (duration) of the video, in seconds.

View in separate window

<!DOCTYPE html>
<html>
<body>

<video id="myVideo" width="100" height="100" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>//from w ww  . ja  va2 s . c o  m
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myVideo").duration;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The duration property returns the length of a video, in seconds.

Different browsers return different values.

This property is read-only.

The duration property return a Number representing the length of the video, in seconds.

If no video is set, "NaN" (Not-a-Number) is returned.

If the video is streamed and has no predefined length, "Inf" (Infinity) is returned.




PreviousNext

Related