js截取视频第一帧(截取图片不会是黑色的)

场景

最近测试提的bug里有个我做的上传视频的功能,编辑回显的视频封面有时候是黑色的。经排查是在截取视频第一帧时出的问题。

问题

我的电脑是win7系统,测试们的电脑是win10系统,且都是用的chrome浏览器,我的是截图的图片每次都是正常的,但是他们的电脑使用该功能时确实偶尔会出现黑色图片封面的问题。经从网上搜索终于解决了,由于复现不了bug搞了一下午。

解决

最终不知道加的哪行代码起的作用,反正这些代码是起了解决截取视频第一帧图片不会是黑色的问题的。
bash 复制代码
video.setAttribute('crossOrigin', 'anonymous');
video.setAttribute('preload', 'auto');
video.setAttribute('autoplay', true);
video.setAttribute('muted', true);
video.preload = 'auto';
video.autoplay = true;
video.muted = true;
下面附上js截取视频第一帧的完整方法
bash 复制代码
cutPicture(item) {
  let video = document.createElement("video");
  video.style = 'position:fixed; top: 9999px;left:9999px;z-index:-9999'
  video.src = item.location_url
  video.setAttribute('crossOrigin', 'anonymous');
  video.setAttribute('preload', 'auto');
  video.setAttribute('autoplay', true);
  video.setAttribute('muted', true);
  video.preload = 'auto';
  video.autoplay = true;
  video.muted = true;
  // video.currentTime = 1   //截取的视频第一秒作为图片
  video.width = 56
  video.height = 56
  document.body.appendChild(video)
  let oGrayImg=require('@/assets/images/icon/txt.png');
  return new Promise((resolve,reject)=>{
    video.onloadeddata = function () {
      let canvas = document.createElement('canvas')
      canvas.width = 56
      canvas.height = 56
      canvas.getContext('2d').drawImage(video, 0, 0, video.clientWidth, video.clientHeight)
      oGrayImg = canvas.toDataURL('image/jpeg');
      this.remove()
      resolve(oGrayImg)
    }
  })
}
相关推荐
像风一样自由202029 分钟前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem1 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊1 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术1 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
GISer_Jing1 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止2 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall2 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴2 小时前
简单入门Python装饰器
前端·python
袁煦丞3 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作
天天扭码3 小时前
从图片到语音:我是如何用两大模型API打造沉浸式英语学习工具的
前端·人工智能·github