vue2获取视频时长

  1. 使用HTML5的video标签和JavaScript:

    javascript 复制代码
    <template>
      <video ref="video" autoplay controls loop muted @loadedmetadata="getVideoDuration">
        <source src="https://desktop-yikao.oss-cn-beijing.aliyuncs.com/avatar/kaissp.mp4" type="video/mp4">
      </video>
    </template>
     
    <script>
    export default {
      methods: {
        getVideoDuration() {
          const videoElement = this.$refs.video;
          const duration = videoElement.duration;
          console.log(duration);
        }
      }
    }
    </script>
  2. 使用Vue的@load事件:

    javascript 复制代码
    <template>
      <video ref="video" @load="getVideoDuration" :src="videoSource"></video>
    </template>
     
    <script>
    export default {
      data() {
        return {
          videoSource: "your_video_source",
        };
      },
      methods: {
        getVideoDuration() {
          const videoElement = this.$refs.video;
          const duration = videoElement.duration;
          console.log(duration);
        }
      }
    }
    </script>
  3. 使用第三方库,如video.js:

    javascript 复制代码
    <template>
      <video ref="video" class="video-js"></video>
    </template>
     
    <script>
    import videojs from "video.js";
    import "video.js/dist/video-js.css";
     
    export default {
      mounted() {
        this.initVideoPlayer();
      },
      methods: {
        initVideoPlayer() {
          const videoElement = this.$refs.video;
          const player = videojs(videoElement);
          
          player.on("loadedmetadata", () => {
            const duration = player.duration();
            console.log(duration);
          });
          
          player.src("your_video_source");
        }
      }
    }
    </script>
相关推荐
ma_king2 小时前
入门 java 和 数据库
java·数据库·后端
jiayou645 小时前
KingbaseES 实战:审计追踪配置与运维实践
数据库
NineData17 小时前
NineData 迁移评估功能正式上线
数据库·dba
NineData1 天前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
赵渝强老师1 天前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
全栈老石1 天前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
倔强的石头_2 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou643 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤4 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区5 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba