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

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

Output:

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

    0
    0
    Reply

Submit a Comment

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

Subscribe

Select Categories