(共556篇)
全部分类

选择视频获取视频文件指定第几秒做封面图
[ 未分类 ] 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    // 获取文件临时地址
    getObjectURL(file) {
      var url = null;
      if (window.createObjcectURL != undefined) {
        url = window.createOjcectURL(file);
      } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file);
      } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file);
      }
      return url;
    },
    
   /**
   * 生成封面图
   * @param {*} event 
   * @param {*} width 图片长度 
   * @param {*} height 图片高度
   * @returns 
   */
    getCoverURL(event, width, height) {
      let _this = this;
      return new Promise((reslove) => {
        const url = this.getObjectURL(event.target.files[0]);
        let video = document.createElement('video');
        video.src = url;
        video.crossOrigin = 'anonymous';
        video.controls = 'controls';
        video.currentTime = 2; //指定当前截屏为第秒数
        video.width = width;
        video.height = height;
        let canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;
        video.onloadeddata = () => {
          canvas.getContext('2d').drawImage(video, 0, 0, width, height);
          let dataURL = canvas.toDataURL('image/jpeg');
          window.URL.revokeObjectURL(url);
          reslove(dataURL);
        };
      });
    },