Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check Length / Duration of an Uploaded Video in JavaScript

Tags:

javascript

Is there a way to check the length of a video file that is being uploaded by a user?

Tried .duration, but this seems to only work on hosted videos that is already referenced in the DOM.

like image 745
Lloyd Banks Avatar asked Dec 01 '22 18:12

Lloyd Banks


1 Answers

How about something like this?

// create the video element but don't add it to the page
var vid = document.createElement('video');
document.querySelector('#input').addEventListener('change', function() {
  // create url to use as the src of the video
  var fileURL = URL.createObjectURL(this.files[0]);
  vid.src = fileURL;
  // wait for duration to change from NaN to the actual duration
  vid.ondurationchange = function() {
    alert(this.duration);
  };
});
<input type="file" id="input">
like image 111
brbcoding Avatar answered Dec 04 '22 09:12

brbcoding