前端上传视频截取某一帧

上传视频的el-upload

<el-upload

            class="upload-demo"

            :action="后端的文件上传地址"

            :on-preview="handlePreview"

            :on-remove="handleRemove"

            :before-remove="beforeRemove"

            :before-upload="onBeforeUploadVideo"

            :on-success="onVideoSuccess"

            multiple

            :limit="1"

            :on-exceed="handleExceed"

          >

            <el-button

              size="small"

              type="primary">

              点击上传

            </el-button>

          </el-upload>

接收某一帧的图片upload,这个是只存放图片,并且直截取图片,不能弹出图片选择框

 <el-upload

                class="avatar-uploader"

                :action="后端上传文件的地址"

                :show-file-list="false"

                :on-success="handleAvatarSuccessSmall"

              >

                <img

                  v-if="videoThumbnail"

                  :src="videoThumbnail"

                  class="avatar">

                <i v-else class="el-icon-plus avatar-uploader-icon"/>

              </el-upload>

script代码

 

  export default {

      data(){

         return {

              videoThumbnail:"",//截取某一帧的图片

          }   

     } ,

     methods: {

       getVideoBasicInfo (videoSrc) {

          return new Promise((resolve, reject) => {

            const video = document.createElement(`video`)

            video.src = videoSrc

            // 视频一定要添加预加载

            video.preload = `auto`

            // 视频一定要同源或者必须允许跨域

            video.crossOrigin = `Anonymous`

            // 监听:异常

            video.addEventListener(`error`, error => {

              debugger

              reject(error)

            })

            // 监听:加载完成基本信息,设置要播放的时常

            video.addEventListener(`loadedmetadata`, () => {

              const videoInfo = {

                video,

                width: video.videoWidth,

                height: video.videoHeight,

                duration: video.duration,

              }

              resolve(videoInfo)

            })

          })

        },



        getVideoPosterInfo (videoInfo) {

          return new Promise(resolve => {

            const { video, width, height } = videoInfo

            video.addEventListener(`canplay`, () => {

              const canvas = document.createElement(`canvas`)

              canvas.width = width

              canvas.height = height

              const ctx = canvas.getContext(`2d`)

              ctx.drawImage(video, 0, 0, width, height)

              const posterUrl = canvas.toDataURL(`image/jpg`)

              resolve({ posterUrl })

            })

          })

        },



        getVideoPosterByFrame (videoSrc, targetTime) {

          return this.getVideoBasicInfo(videoSrc).then(videoInfo => {

            const { video, duration } = videoInfo

            video.currentTime = targetTime

            return this.getVideoPosterInfo(videoInfo)

          })

        },



        /**

         * 获取视频的图片

         * @param {*} url 视频的url

         * @param {*} curTime 时间 节点 当前需要截图的视频,大于视频时间的情况,总时间 除以2 就取中间的

         */

        async getBestPoster (videoSrc, curTime) {

          const videoInfo = await this.getVideoBasicInfo(videoSrc)

          const { duration } = videoInfo

          let curt = curTime

          if (curTime > duration) {

            curt = duration / 2

          }

          const posterInfo = await this.getVideoPosterByFrame(videoSrc, curt)

          const { posterUrl } = posterInfo

          return posterUrl

        },

       //这个是点击上传视频成功的事件 

     onVideoSuccess (response) {

          this.videoSuccess = response.url

          // 获取视频缩略图

          console.log(`this.videoSuccess`, this.videoSuccess)



          this.getBestPoster(response.url, 6).then(imgUrl => {

            console.log(`imgUrl`, imgUrl)

            this.videoThumbnail = imgUrl

          })

            .catch(err  => {

              console.error(`err`, err)

            })

        },

    }

  }

上述代码写完之后,记得让后端进行一下跨域,因为这是上传到文件服务器上了,现在的操作还拿不到截取的图片,一定要让后端进行Access-Control-Allow-Origin' header跨域

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值