Javascript: Problem with getting video duration when uploading multiple files

225 Views Asked by At

I have a function that checks the duration of uploaded videos, but it only works when one file is uploaded. When I try to upload multiple files, the video.duration returns the correct value only for the first file, but for the others, it returns "NaN". Can anyone help me figure out what the problem is? Thank you.

function CheckIfVideoDurationIsOk() {
   window.continueUpload = true;
   document.getElementById('msg').innerHTML = "";
   var ins = document.getElementById('multiFiles').files.length;
   for (var x = 0; x < ins; x++) {
      var video = document.createElement('video');
      video.preload = 'metadata';
      video.src = URL.createObjectURL(document.getElementById('multiFiles').files[x]);
      console.log(x);
      video.onloadedmetadata = function() {
         window.URL.revokeObjectURL(video.src);
         console.log(video.duration);
         if (video.duration > 300) {
            window.continueUpload = false;
             $('#msg').html('<span>Video is too long</span>');
            return;
         }
      }
   }
}
0

There are 0 best solutions below