HTML DOM Video duration Property


The HTML DOM Video duration property returns a number corresponding to the length of the video (in seconds).

NOTE − For live streams it returns ‘Inf’ which is infinite because live streams have no predefined duration.

Syntax

Following is the syntax −

Returning string value

mediaObject.duration

Let us see an example of HTML DOM Video duration property −

Example

Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Video duration</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-Video-duration</legend>
         <video id="demo" width="320" controls><source          src="http://www.tutorialspoint.com/html5/foo.mp4" type="video/mp4"></video><br>
         <input type="button" onclick="getTrackDetails()" value="How many seconds of content is this             video?">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var demo = document.getElementById("demo");
   function getTrackDetails() {
      divDisplay.textContent = 'Duration: '+demo.duration+' seconds';
   }
</script>
</body>
</html>

Output

Before clicking ‘How many seconds of content is this video?’ button −

After clicking ‘How many seconds of content is this video?’ button −

Updated on: 18-Feb-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements