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>
相关推荐
EasyDSS7 分钟前
智能会议管理系统/私有化视频会议平台EasyDSS私有化部署构建企业级私域视频全场景解决方案
人工智能·音视频
橘子编程33 分钟前
JavaScript与TypeScript终极指南
javascript·ubuntu·typescript
叫我一声阿雷吧1 小时前
JS 入门通关手册(45):浏览器渲染原理与重绘重排(性能优化核心,面试必考
javascript·前端面试·前端性能优化·浏览器渲染·浏览器渲染原理,重排重绘·reflow·repaint
大家的林语冰1 小时前
《前端周刊》尤大开源 Vite+ 全家桶,前端工业革命启动;尤大爆料 Void 云服务新产品,Vite 进军全栈开发;ECMA 源码映射规范......
前端·javascript·vue.js
jiayong232 小时前
第 8 课:开始引入组合式函数
前端·javascript·学习
天若有情6732 小时前
【C++原创开源】formort.h:一行头文件,实现比JS模板字符串更爽的链式拼接+响应式变量
开发语言·javascript·c++·git·github·开源项目·模版字符串
M ? A3 小时前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
yuki_uix3 小时前
重排、重绘与合成——浏览器渲染性能的底层逻辑
前端·javascript·面试
Burt3 小时前
我的 2026 全栈选型:Vue3 + Elysia + Bun + AlovaJS
vue.js·全栈·bun
止观止3 小时前
拥抱 ESNext:从 TC39 提案到生产环境中的现代 JS
开发语言·javascript·ecmascript·esnext