詳解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)文章
Echarts動(dòng)態(tài)加載多條折線圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了Echarts動(dòng)態(tài)加載多條折線圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
JS實(shí)現(xiàn)多物體緩沖運(yùn)動(dòng)實(shí)例代碼
這篇文章主要介紹了JS實(shí)現(xiàn)多物體緩沖運(yùn)動(dòng)實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
很酷的星級(jí)評(píng)分系統(tǒng)原生JS實(shí)現(xiàn)
這篇文章主要weidajiaxiangxi介紹了很酷的星級(jí)評(píng)分系統(tǒng)原生JS實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
User Scripts: Video Download by User Scripts
User Scripts: Video Download by User Scripts...2007-05-05
Bootstrap Table表格一直加載(load)不了數(shù)據(jù)的快速解決方法
bootstrap-table是一個(gè)基于Bootstrap風(fēng)格的強(qiáng)大的表格插件神器。接下來通過本文給大家介紹Bootstrap Table表格一直加載(load)不了數(shù)據(jù)的快速解決方法,感興趣的朋友一起看看吧2016-09-09

