上传 并获取视频第一帧

该博客介绍了如何在前端处理视频上传,包括限制上传数量、使用Element UI组件、视频成功上传后的回调处理。文章详细阐述了如何截取视频第一帧作为播放前的默认图片,并将该图片上传到服务器。此外,还展示了如何处理上传错误及视频信息的保存过程。
摘要由CSDN通过智能技术生成
 <el-upload
                accept="video/mp4"
                :limit="2"
                ref="videoUploadPr"
                action="https://api.vchatgood.com/api/v2/third/resource/batchUploadAndGetFileInformation"
                list-type="picture-card"
                :on-success="videoSuccessPr"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :append-to-body="true">
                <video
                  id="upvideo"
                  width="100%"
                  autoplay
                  height="500"
                  :src="dialogImageUrl"
                  controls
                ></video>
              </el-dialog>```
videoSuccessPr(res, file, fileList) {
  this.findvideocover(file.url, file, 4, res.data[0]);
  console.log(res, res.code, res.data[0].url);
  if (res.code == 200) {
    this.pathUrl = res.data[0].url;
  } else {
    this.$message.error("发布失败!");
  }
},

//截取视频第一帧作为播放前默认图片
findvideocover(url, file, type, res) {
console.log(res);
// const video = document.getElementById(“myvideo”); // 获取视频对象
const video = document.createElement(“video”); // 也可以自己创建video
video.src = url; // url地址 url跟 视频流是一样的

  // var canvas = document.getElementById('mycanvas') // 获取 canvas 对象
  var canvas = document.createElement("canvas"); // 获取 canvas 对象
  const ctx = canvas.getContext("2d"); // 绘制2d
  video.crossOrigin = "anonymous"; // 解决跨域问题,也就是提示污染资源无法转换视频
  video.currentTime = 1; // 第一帧

  video.oncanplay = () => {
    console.log(video.clientWidth, video.clientHeight);
    canvas.width = video.clientWidth ? video.clientWidth : 320; // 获取视频宽度
    canvas.height = video.clientHeight ? video.clientHeight : 320; //获取视频身高
    // 利用canvas对象方法绘图
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
    // 转换成base64形式
    this.videoFirstimgsrc = canvas.toDataURL("image/png"); // 截取后的视频封面
    file.url = this.videoFirstimgsrc;
    // base64转成bolb文件
    const fileBolb = this.base64toFile(
      this.videoFirstimgsrc,
      this.getFileName(file)
    );
    console.log(fileBolb, file.url, this.videoFirstimgsrc);
    // 把首图上传生成云上地址
    this.getFirstPngUrl(fileBolb, type, res);
    video.remove();
    canvas.remove();
  };
},

base64toFile(baseUrl, filename = "file") {
  let arr = baseUrl.split(",");
  let type = arr[0].match(/:(.*?);/)[1]; // 解锁图片类型
  let bytes = atob(arr[1]); // 解码base64
  let n = bytes.length;
  let bufferArray = new Uint8Array(n);
  while (n--) {
    bufferArray[n] = bytes.charCodeAt(n);
  }
  return new File([bufferArray], filename, { type });
},

getFirstPngUrl(file, type, resd) {
  const fd = new FormData();
  fd.append("file", file);
  console.log(fd, "fd");
  // this.$post为我自己封装的axios的post方法。可直接用axios的post方法
  uploadImg(fd, {
    headers: { "Content-Type": "multipart/form-data" },
  }).then((res) => {
    this.videoFirstimgsrc = res.url;
    console.log(this.videoFirstimgsrc, res);
    // this.videoWriteFileList[0].url = res.url;
    if (res.data.code == 200) {
      this.$refs.videoUpload.clearFiles();
      this.$refs.videoUploadPr.clearFiles();
      increaseMarketAnchorResource({
        userId: getQueryString("id"),
        url: this.pathUrl,
        videoCoverPicture: res.data.data.url,
        type: type,
        ext: resd.ext,
        height: resd.height,
        size: resd.size,
        width: resd.width,
        duration: resd.duration,
      }).then((res) => {
        if (res.code == 200) {
          this.getMarketAnchorInformation();
          this.$message({
            type: "success",
            message: "修改成功",
          });
        } else {
          this.$message.error("修改失败");
        }
      });
    }
  });
},

getFileName(file) {
  const type = file.raw.type.split("/")[1];
  let name = "";
  if (type) {
    name = file.raw.name.substring(0, file.raw.name.indexOf(`.${type}`));
  }
  return `${name}.png`;
},

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值