如何在JavaScript中检查上载视频的长度/持续时间 [英] How to Check Length / Duration of an Uploaded Video in JavaScript
本文介绍了如何在JavaScript中检查上载视频的长度/持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法检查用户上传的视频文件的长度?
Is there a way to check the length of a video file that is being uploaded by a user?
尝试。持续时间
,但这似乎只适用于已在DOM中引用的托管视频。
Tried .duration
, but this seems to only work on hosted videos that is already referenced in the DOM.
推荐答案
某事怎么样喜欢这个?
// 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">
这篇关于如何在JavaScript中检查上载视频的长度/持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文