在vue项目中如何获取视频的时长

Octavia ·
更新时间:2024-09-20
· 1847 次阅读

目录

vue获取视频时长

vue-video-player获取播放时间

全局引用

局部引用

html

methods获取播放时间

vue获取视频时长

传入参数为视频文件对象

js的代码如下:

getVideoDuration(file) {   var url = URL.createObjectURL(file);   var audioElement = new Audio(url);   var self = this;   var result;   audioElement.addEventListener("loadedmetadata", function() {     // 视频时长值的获取要等到这个匿名函数执行完毕才产生     result = audioElement.duration; //得到时长为秒,小数,182.36     self.ruleForm.videoDuration = parseInt(result); //转为int值   }); } vue-video-player获取播放时间 npm install vue-video-player -S 全局引用 import VideoPlayer from 'vue-video-player' import 'vue-video-player/src/custom-theme.css' import 'video.js/dist/video-js.css' Vue.use(VideoPlayer) 局部引用   import {     videoPlayer   } from 'vue-video-player'   import 'video.js/dist/video-js.css'   export default {   components: {     videoPlayer   } } html    <video-player class="video-player vjs-custom-skin"              ref="videoPlayer"              :playsinline="true"              :options="playerOptions">       </video-player> methods获取播放时间   this.$nextTick(() => {        setTimeout(() => {        let du = document.getElementById("vjs_video_3_html5_api") //获取组件下的video        var hour = parseInt((du.duration) / 3600);        var minute = parseInt((du.duration % 3600) / 60);        var second = parseInt(du.duration % 60);        let result = ''        if (hour > 0) {        result = this.formatTimeStr(hour) + ':' + this.formatTimeStr(minute) + ':' + this        .formatTimeStr(second)                  } else {                     result = this.formatTimeStr(minute) + ':' +                    this.formatTimeStr(second)                   }                   return result //转化成分,秒                 }, 200)               })      formatTimeStr(val) {         if (val > 9) {           return val         } else {           return '0' + val         }       },

以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。 



VUE

需要 登录 后方可回复, 如果你还没有账号请 注册新账号