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)
    }
  })
}
相关推荐
大家的林语冰几秒前
Rust 再次打败 Go,Astro 7 首发,什么叫“万物皆可锈化“!
前端·javascript·性能优化
lichenyang4532 分钟前
H5 如何调用 ArkTS:JavaScriptProxy、requestId 和 Promise 回调
前端
lichenyang4533 分钟前
我在 HarmonyOS 上写了一个轻量级小程序运行时:从 Web 容器到 JSBridge
前端
雪隐3 分钟前
用Flutter做背单词APP-02我画了设计稿,然后让AI帮我设计了数据库(顺便聊了会天)
前端·人工智能·后端
lichenyang4534 分钟前
不要把 JSBridge 写成 if else:Dispatcher、Registry 和 Biz/Imp 分层
前端
前端甜糖4 分钟前
前端如何实现在线预览office常见文件功能?
前端·vue.js
北鸟南游4 分钟前
学习-claude code 工程化实战的笔记
前端·ai编程·claude
Kyrie6784 分钟前
TypeScript 7.0 正式发布
前端
lichenyang4536 分钟前
JSBridge 不是发消息就完了:runJavaScript、Timeout 和 Callback Lost
前端
xiaofeichaichai10 分钟前
Vite配置实战
前端