最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Check LengthDuration of an Uploaded Video in JavaScript - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Dec 18, 2014 at 15:50 Lloyd BanksLloyd Banks 36.7k58 gold badges170 silver badges259 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 27

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

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

发布评论

评论列表(0)

  1. 暂无评论