JS获取上传视频时长-layui框架

注:在不是layui框架的情况下也可以参考,因为不管什么框架最原始的东西都是对js代码的封装
1:首先写个div,里面包含video,用来放上传的视频
在这里插入图片描述

2:上传之后获取到视频地址,赋值在video标签上,然后通过自定义方法formatSeconds获取到视频时长
在这里插入图片描述

3:自定义事件formatSeconds
function formatSeconds(value) {
var theTime = parseInt(value);// 秒
var theTime1 = 0;// 分
var theTime2 = 0;// 小时
if (theTime > 60) {
theTime1 = parseInt(theTime / 60);
theTime = parseInt(theTime % 60);
if (theTime1 > 60) {
theTime2 = parseInt(theTime1 / 60);
theTime1 = parseInt(theTime1 % 60);
}
}

        var result = "" + parseInt(theTime);//秒
        if (10 > theTime > 0) {
            result = "0" + parseInt(theTime);//秒
        } else {
            result = "" + parseInt(theTime);//秒
        }

        if (10 > theTime1 > 0) {
            result = "0" + parseInt(theTime1) + ":" + result;//分,不足两位数,首位补充0,
        } else {
            result = "" + parseInt(theTime1) + ":" + result;//分
        }
        if (theTime2 > 0) {
            result = "" + parseInt(theTime2) + ":" + result;//时
        }
        // console.log(result);
        return result;
    }

猜你喜欢

转载自blog.csdn.net/qq_37213281/article/details/109575744