vue、js截取视频任意一帧图片

html有本地上传替换部分,可以不看

原理:通过video标签对视频进行加载,随后使用canvas对截取的视频帧生成需要的图片

html 复制代码
<template>
<el-row  :gutter="18"  class="preview-video">
          <h4>视频预览<span>系统默认选中第一帧为封面</span></h4>
          <div class="screenshot-box">
            <video
              :class="`screenshot-video screenshot-video${id}`"
              :src="videoUrl"
              controls
              crossorigin="anonymous"
            />
          </div>
          <div class="preview-btn">
            <el-button size="small" type="primary" class="set-btn" @click="setWidthHeight">截取封面</el-button>
              <el-upload
                action=""
                :key="componentImgKey"
                :on-change="handleAddLocalImage"
                accept=".jpg,.png,.jpeg,.JPG,.PNG,.JPEG"
                :auto-upload="false"
                :show-file-list="false"
                :limit="1"
                :multiple="false"
                ref="uploads"
              >
                <el-button @click.native="handleAddLocalImageBefore" size="small" type="primary">本地上传</el-button>
              </el-upload>
          </div>
          <canvas
            :class="`myCanvas${id}`"
            class="myCanvas"
            :width="isWidth"
            :height="isHeight"
          />
        </el-row>
      </template>
<script>
export default {
	data(){
	return {
		videoUrl:"",
      id: 0,
      isWidth: 320,
      isHeight: 180,
      imgSrc:"",
	},
	methods:{
		setWidthHeight() {
	      if (true) {
	        this.$nextTick((_) => {
	          var v = document.querySelector(`.screenshot-video${this.id}`);
	          this.isWidth = v.offsetWidth;
	          this.isHeight = v.offsetHeight;
	          this.cutPicture();
	        });
	      }
	    },
	    //截取当前帧的图片
    cutPicture() {
      if (process.browser) {
        this.$nextTick((_) => {
          var v = document.querySelector(`.screenshot-video${this.id}`);
          let canvas = document.querySelector(`.myCanvas${this.id}`);
          var ctx = canvas.getContext("2d");
          ctx.drawImage(v, 0, 0, this.isWidth, this.isHeight);
          var oGrayImg = canvas.toDataURL("image/png");
          this.imgSrc = oGrayImg;
          this.file_ext="png"
          let size=this.imageSize(oGrayImg)/1024/1024
          if(size>2){
            this.$message.error("图片大小不能超过2MB!")
            return
          }
        });
      }
    },
    imageSize(base64Str) {
      const indexBase64 = base64Str.indexOf('base64,');
      if (indexBase64 < 0) return -1;
      const str = base64Str.substr(indexBase64 + 6);
      return (str.length * 0.75).toFixed(2);
    },
	}
}
</script>
相关推荐
1***s6328 分钟前
JavaScript微服务
javascript·微服务·devops
小云朵爱编程39 分钟前
Vue项目Iconify的使用以及自定义图标,封装图标选择器
前端·javascript·vue.js
P***25391 小时前
JavaScript部署
开发语言·前端·javascript
一只小阿乐1 小时前
react 状态管理mobx中的行为模式
前端·javascript·react.js·mobx·vue开发·react开发
l***O5201 小时前
前端路由历史监听,React与Vue实现
前端·vue.js·react.js
大雷神1 小时前
DevUI 实战教程:从零构建电商后台管理系统(完整版)
前端·javascript·华为·angular.js
E***q5392 小时前
JavaScript数据挖掘开发
开发语言·javascript·数据挖掘
滿2 小时前
vue3 elementplus el-table toggleRowSelection使用方法
javascript·vue.js·elementui
猪八戒1.02 小时前
onenet接口
开发语言·前端·javascript·嵌入式硬件
h***83932 小时前
JavaScript开源
开发语言·javascript·ecmascript