js 获取视频封面

·

1 min read

    // 获取封面
    drawVideo(video) {
      return new Promise((res, rej) => {
        const canvas = document.createElement('canvas')
        const ctx = canvas.getContext('2d')
        canvas.width = video.width
        canvas.height = video.height
        ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
        canvas.toBlob(bolb => {
          res({ bolb, url: URL.createObjectURL(bolb) })
        })
        document.body.appendChild(canvas)
      })

    },
    // 根据视频地址获取获取封面
    getVideoPic(url, time = 0) {
      return new Promise((res, rej) => {
        const video = document.createElement('video')
        video.currentTime = time
        video.muted = true
        video.autoplay = true
        video.oncanplay = async () => {
          const frame = await drawVideo(video)
          res(frame)
        }
        video.src = URL.createObjectURL(url)
      })
    },