Get Duration Of Inputted Video File Using JavaScript

In this article, we will learn how to get a duration of inputted Video files using JavaScript.

We can use the duration property to get the length of the current video in seconds. Here we will implement it in a way to get the duration of inputted file directly before upload and also it will work for every file change.

<!DOCTYPE html>
    <input type="file" accept="video/*" onchange="getDuration(this)">
    window.URL = window.URL || window.webkitURL;
    function getDuration(control) {
        var video = document.createElement('video');
        video.preload = 'metadata';
        video.onloadedmetadata = function () {
            alert("Duration : " + video.duration + " seconds");
        video.src = URL.createObjectURL(control.files[0]);

The above code will not work with the .avi files.


Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Also, check Infinity Audio/Video Duration Issue Fixed Using JavaScript

1 Comment

  1. ravi

    This way isn’t working for hight resolution videos say 1920*1080 video


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories