vant-upoader 视频上传和截帧处理

使用vant-uploader 文件上传,视频截帧以及ios 视频的兼容的处理方式

<template>
<div>
 <van-uploader
   upload-icon="https://file.baojunev.com/group1/default/20210527/14/51/6/video_icon@2x.png"
   accept="video/*"
   :before-read="beforeRead"
   :after-read="afterRead"
 />
</div>
</template>
<script>
import Vue from "vue";
import { Uploader } from "vant";
Vue.use(Uploader);

export default {
data(){
return {
}
}
methods:{

async afterRead(file) {
      const that = this;
        // 将选中的上传文件转化为二进制文件,显示在页面上
        let uploadUrl = URL.createObjectURL(file.file); 
        // 截取视频帧数,获取封面
        await that.getVideoBase64(uploadUrl).then((res) => {
          that.videoPoster = res;
        });
        let formData = new FormData(); // 为上传文件定义一个formData对象
        let config = {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        };
        formData.append("file", file.file); 
        this.loading = true;
        let type = file.file.type.split('/')[1]
        axios({url: `/connector?prefix=${type}&isRelativePath=true`,
          method: "post",
          data: formData,
          ...config,
          withCredentials: false,
          timeout: 40000,
        })
          .then(function (data) {
            console.log(data,'测试的')
            that.loading = false;
            that.videoUrl = that.videoConfig + data.info.url
          })
          .catch(function (data) {
            console.log("data",data);
          });
    },
    beforeRead(file) {
      if (!file.type.includes("video")) {
        window.appAction("toast", ["请上传视频文件"]);
        return false;
      } else if (file.size > 400 * 1024 * 1024) {
        window.appAction("toast", ["视频大小超过限制"]);
        return false;
      } else {
        return true;
      }
    },
    //base64 转成文件流
    dataURLtoFile(dataurl, filename) {
      var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
       while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
       }
       // 方式1:
      // let newBlob = new Blob([u8arr],"file", {
      //         type: mime
      //     });
      //     newBlob.lastModifiedDate = new Date();
      //     newBlob.name = Date.now() + '.png';
      //     return newBlob
      return new File([u8arr], Date.now() + ".png", { type: mime });
    },
    //截取视频某帧
    getVideoBase64(url) {
      const that = this;
      return new Promise(function (resolve, reject) {
        let dataURL = "";
        let ua = navigator.userAgent.toLowerCase();
        //  ios 存在video标签不显示同时ios手机端视频上传的格式是.mov格式的文件,故ios视频上传的视频采用默认视频封面提的处理方式,
        let isIos = ua.match(/Android/i) ? false : true;
        if(url.includes('.mov') || isIos){
          let imgUrl ='https://file.baojunev.com/group1/default/20210601/19/05/6/poster_video.jpg'
           resolve(imgUrl)
        }
        else{
        let video = document.createElement("video");
        video.setAttribute("crossOrigin", "anonymous"); //处理跨域
        video.setAttribute("src", url);
        video.setAttribute("width", 304);
        video.setAttribute("height", 180);
        video.currentTime = 0.5;
        video.addEventListener("loadeddata", function () {
          let canvas = document.createElement("canvas");
          let width = video.width; //canvas的尺寸和图片一样
          let height = video.height;
          canvas.width = width;
          canvas.height = height;
          canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
          dataURL = canvas.toDataURL("image/png"); //转换为base64
          let file = that.dataURLtoFile(dataURL, "file");
          console.log(file, "file");
          const suffix = `.${file.type.split("/")[1]}`;
          const size_list = file.size;
          let formData = new FormData();
          formData.append("file", file);
          axios({
            url:
              `/upload?simple_name=1&suffix=${suffix}&size_list=` +
              size_list,
            method: "post",
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            dataType: "json",
            timeout: 20000,
            withCredentials: false,
          }).then(function (data) {
            resolve(data.TFS_FILE_NAME[0]);
          });
        });
        }
      });

    },
   }
</script>
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uniapp中使用和安装vant-weapp,您可以按照以下步骤进行操作: 安装vant-weapp: 1. 在您的uniapp项目根目录中,打开命令行工具(如终端)。 2. 运行以下命令来安装vant-weapp: ``` npm install vant-weapp -S --production ``` 或者,如果您使用的是yarn包管理器,可以运行以下命令: ``` yarn add vant-weapp --production ``` 3. 安装完成后,您将在项目的 `node_modules` 目录下找到 `vant-weapp` 文件夹。 使用vant-weapp: 1. 在uniapp的页面中,找到需要使用vant-weapp组件的页面。 2. 在页面的json配置文件中,添加以下代码以引入vant-weapp组件: ```json { "usingComponents": { "van-button": "vant-weapp/dist/button/index", "van-cell": "vant-weapp/dist/cell/index", // 添加其他需要使用的组件 } } ``` 3. 在页面的vue文件中,即 `.vue` 文件中,您可以使用vant-weapp的组件了。例如,在模板中使用van-button组件: ```html <template> <view> <van-button type="primary">按钮</van-button> </view> </template> ``` 4. 您可以根据vant-weapp的官方文档,自行查阅各个组件的使用方法和属性。 需要注意的是,vant-weapp是基于微信小程序开发的,因此在使用时需要注意一些微信小程序的特性和限制。另外,由于uniapp支持多端开发,包括H5、App、小程序等,vant-weapp在不同端的兼容性可能会有所差异,您可以参考vant-weapp的官方文档来了解更多详情。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值