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

前端 未结 2 914
借酒劲吻你
借酒劲吻你 2020-12-17 06:20

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

相关标签:
2条回答
  • 2020-12-17 07:00

    Video files need to be decoded by an actual player in order to determine the duration. JavaScript can only count bytes.

    0 讨论(0)
  • 2020-12-17 07:12

    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">

    0 讨论(0)
提交回复
热议问题