获取视频第一帧图片并上传

获取视频第一帧图片并上传

需求

需要在上传视频时获取到视频第一帧的图片并上传到远程服务器上

过程

1、获取第一帧图片

 getIMg = (file)=>{
 //file参数传入的是视频文件流 
   const video =   document.createElement("video");
   video.setAttribute('crossorign', 'anonymous');
    video.setAttribute('controls', 'controls');
    video.setAttribute('autoplay', 'autoplay');
    // 这样就不用处理跨域问题了
    video.volume = 0;
   video.setAttribute('src', window.URL.createObjectURL(file)); 
   video.setAttribute('width', 400);
   video.setAttribute('height', 240);
    video.addEventListener('loadeddata', () => {
        // 创建canvas画布
        const canvas = document.createElement('canvas');
        canvas.width =  video.width;  // 设置画布的长宽也就是图片的长宽
        canvas.height = video.height;
        canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
        const img = document.createElement('img');
        img.src = canvas.toDataURL('image/png');
        //上传方法
        this.dataURLtoFile(img.src,"videoPic.png");
    })
 }

2、上传获取到的第一帧图片

 dataURLtoFile = (file,filename)=>{
    const arr = file.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }

    const newfile = new File([u8arr], filename, {type: mime});

  //获取到图片文件流 调用上传到远程服务器的方法
    const {dispatch} = this.props
    const {uploadHeaders} = this.props
    dispatch({
      type:"VideoPic/upload",
      payload:{files:newfile},
      headers:uploadHeaders,
      callback:(res)=>{
        // console.log("上传视频第一帧",res)
        if(res.result==="1"){
            this.setState({videoShot:res.path})
        }else{
          message.error("上传视频第一帧出错",res.msg)
        }
      }
    })
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值