js截取video視頻某一幀做封面的簡(jiǎn)單案例(附完整代碼)
可以使用 canvas元素來截取視頻某一幀并生成封面。
首先,在 video 標(biāo)簽上設(shè)置視頻源地址和自動(dòng)播放屬性:
<video src="video.mp4" autoplay></video>
然后,在 canvas 標(biāo)簽上定義寬高和樣式,并通過 JavaScript 獲取視頻元素和 canvas 元素:
<canvas width="640" height="360" style="display:none;"></canvas>
<script>
const video = document.querySelector('video');
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
</script>
接著,定義一個(gè)函數(shù)來截取視頻某一幀,并將其渲染到 canvas 上:
<script>
function captureFrame() {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
}
</script>
在需要生成封面的地方調(diào)用該函數(shù)即可:
<button onclick="captureFrame()">生成封面</button>
最后,將 canvas 元素轉(zhuǎn)換為圖片元素并插入到頁面中:
<script>
function captureFrame() {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL();
const img = document.createElement('img');
img.src = dataURL;
document.body.appendChild(img);
}
</script>
完整代碼如下:
<video src="video.mp4" autoplay></video>
<canvas width="640" height="360" style="display:none;"></canvas>
<button onclick="captureFrame()">生成封面</button>
<script>
const video = document.querySelector('video');
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
function captureFrame() {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL();
const img = document.createElement('img');
img.src = dataURL;
document.body.appendChild(img);
}
</script>附:js截取視頻第一幀作為封面
在vue的v-for循環(huán)中,直接video標(biāo)簽進(jìn)行列表渲染時(shí),滾動(dòng)列表頁面會(huì)很卡,所以最好以img標(biāo)簽渲染視頻封面圖,點(diǎn)擊后再播放視頻。
具體代碼如下:
//取視頻的第一幀作為封面
function cutPicture(item) { //這里的item是列表的每一項(xiàng)
let video = document.createElement("video");
video.style = "position:fixed; top: 9999px;left:9999px;z-index:-9999";
video.preload = "metadata";
video.currentTime = 1; //截取的視頻第一秒作為圖片
video.src = item.videoUrl;
video.setAttribute("crossOrigin", "anonymous");
video.width = 113;
video.height = 75;
document.body.appendChild(video);
video.onloadeddata = function () {
let canvas = document.createElement("canvas");
canvas.width = 113;
canvas.height = 75;
canvas.getContext("2d").drawImage(video, 0, 0, video.clientWidth, video.clientHeight);
var oGrayImg = canvas.toDataURL("image/jpeg");
item.imgUrl = oGrayImg; //這里輸出的imgUrl就是視頻的封面圖
this.remove();
};
return item;
}
總結(jié)
到此這篇關(guān)于js截取video視頻某一幀做封面的文章就介紹到這了,更多相關(guān)js截取video視頻幀做封面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript字符串_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
JavaScript中的字符串就是用''或""括起來的字符表示。下面通過本文給大家介紹JavaScript字符串的相關(guān)知識(shí),感興趣的朋友一起看看吧2017-06-06
Bootstrap每天必學(xué)之導(dǎo)航條(二)
Bootstrap每天必學(xué)之導(dǎo)航條,進(jìn)一步向大家講解了導(dǎo)航條養(yǎng)殖,以及導(dǎo)航條中元素的使用方法,感興趣的小伙伴們可以參考一下2016-03-03
Js數(shù)組對(duì)象如何根據(jù)多個(gè)key值進(jìn)行分類
這篇文章主要介紹了Js數(shù)組對(duì)象如何根據(jù)多個(gè)key值進(jìn)行分類,每周從 npm 下載?lodash.groupBy?的次數(shù)在 150 萬到 200 萬之間,很高興看到 JavaScript 填補(bǔ)了這些空白,讓我們的工作變得更加輕松,需要的朋友可以參考下2024-02-02
移動(dòng)端點(diǎn)擊圖片放大特效PhotoSwipe.js插件實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了移動(dòng)端點(diǎn)擊圖片放大特效PhotoSwipe.js插件實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
JavaScript中遍歷跳出循環(huán)方法總結(jié)
這篇文章主要給大家介紹了關(guān)于JavaScript中遍歷跳出循環(huán)方法的相關(guān)資料,一想到想到循環(huán)的跳出,立馬就會(huì)想到三個(gè)關(guān)鍵,break、return、continue,在業(yè)務(wù)中也會(huì)需要在遍歷的時(shí)候退出循環(huán),需要的朋友可以參考下2023-12-12
JS實(shí)現(xiàn)將數(shù)字金額轉(zhuǎn)換為大寫人民幣漢字的方法
這篇文章主要介紹了JS實(shí)現(xiàn)將數(shù)字金額轉(zhuǎn)換為大寫人民幣漢字的方法,涉及javascript字符串與數(shù)組操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08

