js获取视频或者音频时长
2019-5-16 hubo JavaScript
html:
<input type="file" id="fileUp" name="dance_video"> <input type="text" id="infos" name="duration_dance" hidden>
js:
var myVideos = []; window.URL = window.URL || window.webkitURL; document.getElementById('fileUp').onchange = setFileInfo; function setFileInfo() { var files = this.files; myVideos.push(files[0]); var video = document.createElement('video'); video.preload = 'metadata'; video.onloadedmetadata = function() { window.URL.revokeObjectURL(video.src); var duration = video.duration; myVideos[myVideos.length - 1].duration = duration; updateInfos(); } video.src = URL.createObjectURL(files[0]); } function updateInfos() { var infos = document.getElementById('infos'); infos.value = ""; for (var i = 0; i < myVideos.length; i++) { infos.value = myVideos[i].duration; } }
这样每次上传mp4视频或者mp3音频,文件的时长会自动获取,保存到name="duration_dance"这个input标签中。