How to get duration of video when I am using filereader to read the video file?

13,501

you can do something like below, the trick is to use readAsDataURL:

var reader = new FileReader();
reader.onload = function() {
    var media = new Audio(reader.result);
    media.onloadedmetadata = function(){
         media.duration; // this would give duration of the video/audio file
    };    
};
reader.readAsDataURL(file); 

Fiddle Demo

Share:
13,501
ShivangiBilora
Author by

ShivangiBilora

Happy to help!!! :)

Updated on June 09, 2022

Comments

  • ShivangiBilora
    ShivangiBilora almost 2 years

    I am trying to upload a video to server, and on client end. I am reading it using FileReader's readAsBinaryString().

    Now, my problem is, I don't know how to read duration of this video file.

    If i try reading the file, and assigning the reader's data to a video tag's source, then none of the events associated to the video tag are fired. I need to find the duration of file uploaded on client end.

    Can somebody please suggest me something?

  • Admin
    Admin almost 9 years
    Just note that data-uri's has size limits (varies depending on browser) which may be essential with video files, and has a noticeable encoding/decoding (base-64) overhead.