详解js获取video任意时间的画面截图
首先就是要把视频加载出来,然后使用canvas.getContext(‘2d').drawImage(videoElement, 0, 0, canvas.width, canvas.height);获取到当前视频时间的截图,需要不同时间的video视频图,设置video的currentTime(单位秒),然后videoElement这个对象信息会实时更新。
如果是视频是在阿里云OSS上就更方便了,poster=“http://a-image-demo.oss-cn-qingdao.aliyuncs.com/demo.mp4?x-oss-process=video/snapshot,t_6000,m_fast”
<div contenteditable="true" id="in-box" style="width:1000px;margin: 20px auto;"></div> <div style="width:1000px;margin: 20px auto;"> <input type="file" name="" id="upload-ipt"> <div class="review" id="out-box"></div> </div>
function getVideoImage() { var obj_file = document.getElementById("upload-ipt"); var file = obj_file.files[0]; var blob = new Blob([file]), // 文件转化成二进制文件 url = URL.createObjectURL(blob); //转化成url if (file && /video/g.test(file.type)) { var $video = $('<div><video controls src="' + url + '"></video></div><div> </div>'); //后面加一个空格div是为了解决在富文本中按Backspace时删除无反应的问题 $('#in-box').html($video); var videoElement = $("video")[0]; videoElement.addEventListener("canplay", function (_event) { var canvas = document.createElement("canvas"); canvas.width = videoElement.videoWidth; canvas.height = videoElement.videoHeight; console.log(videoElement.videoWidth) canvas.getContext('2d').drawImage(videoElement, 0, 0, canvas.width, canvas.height); var img = document.createElement("img"); img.src = canvas.toDataURL("image/png"); $("#out-box").html(img); URL.revokeObjectURL(this.src); // 释放createObjectURL创建的对象 console.log("loadedmetadata") }); }else{ alert("请上传一个视频文件!"); obj_file.value = "" } };
相关推荐
ShoppingChen 2020-06-25
WebVincent 2020-06-06
行吟阁 2020-04-10
爱好HtmlCssJs 2020-01-18
冰蝶 2020-01-08
Jym 2012-07-05
tumobi 2016-12-08
一点一刻 2013-09-17
HandsomeHong 2010-08-13
Charliewolf 2019-11-10
WenFY的CSDN 2019-10-24
89510194 2019-10-22
nercon 2019-10-20
行吟阁 2019-10-22
zhangli 2014-11-12