vue截取视频第一帧做封面图

利用画布canvas来截取第一帧作为图片

cutPicture(item) {
      let video = document.createElement("video");
      video.style = 'position:fixed; top: 9999px;left:9999px;z-index:-9999'
      video.preload = 'metadata'
      video.currentTime = 1   //截取的视频第一秒作为图片
      video.src = item.fileUrl
      video.setAttribute('crossOrigin', 'anonymous');
      video.width = 113
      video.height = 75
      document.body.appendChild(video)
      video.onloadeddata = function () {
        let canvas = document.createElement('canvas')
        canvas.width = 113
        canvas.height = 75
        canvas.getContext('2d').drawImage(video, 0, 0, video.clientWidth, video.clientHeight)
        var oGrayImg = canvas.toDataURL('image/jpeg');
        item.fileUrl = oGrayImg
        this.remove()
      }
      return item
    },

图片大小可以根据实际业务情况来做修改

你可能感兴趣的:(vue.js,音视频,javascript)