詳解js獲取video任意時(shí)間的畫面截圖
首先就是要把視頻加載出來,然后使用canvas.getContext(‘2d').drawImage(videoElement, 0, 0, canvas.width, canvas.height);獲取到當(dāng)前視頻時(shí)間的截圖,需要不同時(shí)間的video視頻圖,設(shè)置video的currentTime(單位秒),然后videoElement這個(gè)對(duì)象信息會(huì)實(shí)時(shí)更新。
如果是視頻是在阿里云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]), // 文件轉(zhuǎn)化成二進(jìn)制文件 url = URL.createObjectURL(blob); //轉(zhuǎn)化成url if (file && /video/g.test(file.type)) { var $video = $('<div><video controls src="' + url + '"></video></div><div> </div>'); //后面加一個(gè)空格div是為了解決在富文本中按Backspace時(shí)刪除無反應(yīng)的問題 $('#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創(chuàng)建的對(duì)象 console.log("loadedmetadata") }); }else{ alert("請(qǐng)上傳一個(gè)視頻文件!"); obj_file.value = "" } };
以上所述是小編給大家介紹的js獲取video任意時(shí)間的畫面截圖詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript正則表達(dá)式小結(jié)(test|match|search|replace|split|exec)
這篇文章主要介紹了JavaScript正則表達(dá)式小結(jié)(test|match|search|replace|split|exec)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12輕松實(shí)現(xiàn)js彈框顯示選項(xiàng)
這篇文章主要為大家詳細(xì)介紹了js輕松實(shí)現(xiàn)彈框顯示選項(xiàng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09JavaScript變量提升和嚴(yán)格模式實(shí)例分析
這篇文章主要介紹了JavaScript變量提升和嚴(yán)格模式,結(jié)合實(shí)例形式分析了javascript變量提升和嚴(yán)格模式的原理及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-01-01Swiper 4.x 使用方法(移動(dòng)端網(wǎng)站的內(nèi)容觸摸滑動(dòng))
Swiper是純javascript打造的滑動(dòng)特效插件,面向手機(jī)、平板電腦等移動(dòng)終端,這里為大家簡單介紹一下Swiper4的用法,需要的朋友可以參考下2018-05-05javascript中的toFixed固定小數(shù)位數(shù) 簡單實(shí)例分享
這篇文章介紹了toFixed固定小數(shù)位數(shù)的簡單例子,有需要的朋友可以參考一下2013-07-07bootstrapvalidator之API學(xué)習(xí)教程
這篇文章為大家分享了bootstrapvalidator之API學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06