vue项目中利用后端接口返回的视频地址获取第一帧作为数据封面展示

1:利用转成base64进行获取封面

javascript 复制代码
//封装函数 
 getVideoBase64(url) {
            return new Promise(function (resolve, reject) {
                let dataURL = "";
                let video = document.createElement("video");
                video.setAttribute("crossOrigin", "anonymous"); //处理跨域
                video.setAttribute("src", url);
                video.setAttribute("width", 400);
                video.setAttribute("height", 240);
                video.setAttribute("preload", "auto");
                video.setAttribute('crossOrigin', 'anonymous');
                video.addEventListener("loadeddata", function () {
                    let canvas = document.createElement("canvas"),
                        width = video.width, //canvas的尺寸和图片一样
                        height = video.height;
                    canvas.width = width;
                    canvas.height = height;
                    canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
                    dataURL = canvas.toDataURL("image/jpeg"); //转换为base64
                    resolve(dataURL);
                });
            })
        },




//使用该函数
       if (res.data.data.clipList) {  //数据
          setTimeout(() => {
            //item.videoUrl  视频地址    item.coverUrl  图片地址
            res.data.data.clipList.forEach((item, index) => {
              this.getVideoBase64(item.videoUrl).then((data) => {
                item.coverUrl = data
              });
            });
          }, 1000);
        }

2:利用canvas来获取视频第一帧作为封面

javascript 复制代码
    //转换第一帧
    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.videoUrl
      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.videoUrl = oGrayImg
        this.remove()
      }
      return item
    },
相关推荐
陈随易28 分钟前
编程语言级别的Skill市场,AI Agent 的未来形态
前端·后端·程序员
SoaringHeart1 小时前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒3 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰4 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
竹林8184 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花5 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12275 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪6 小时前
Vue3-生命周期
前端
莪_幻尘6 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4537 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端