js获取音频/视频时长+转换时间格式
html
<input type="file" accept="video/*" οnchange="setVideoSrc(this)" />
获取音视频时长+转换时间格式
function setVideoSrc(el){ //获取视频对象 var content = el.files[0] console.log(content) //获取音频、视频时长 var url = URL.createObjectURL(content); var audioElement = new Audio(url); var duration; audioElement.addEventListener("loadedmetadata", function (_event) { duration = audioElement.duration; console.log(duration+'s'); console.log(durationTrans(duration)) }); } // 时长转换 function durationTrans(a){ var b = "" var h = parseInt(a/3600), m = parseInt(a%3600/60), s = parseInt(a%3600%60); if(h>0){ h = h<10 ? '0'+h : h b += h+":" } m = m<10 ? '0'+m : m s = s<10 ? '0'+s : s b+=m+":"+s return b; }
应用效果:
获取时长的方式参考:https://blog.csdn.net/qq_24943037/article/details/85236127