js截取视频的封面图片
之前我听过有人说过这个需求,这几天偶然闲下来了,就写写吧
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<video id="video" controls="controls">
<source class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="XXX.MP4">
</video>
<div id="imgbox"></div>
<script type="text/javascript">
(function(){
var video, output;
var scale = 0.8;
var init = function() {
imgbox = document.getElementById("imgbox");
video = document.getElementById("video");
video.addEventListener('loadeddata',captureImage);
};
var captureImage = function() {
var canvas = document.createElement("canvas");//创建一个canvas
canvas.width = video.videoWidth * scale;//设置canvas的宽度为视频的宽度
canvas.height = video.videoHeight * scale;//设置canvas的高度为视频的高度
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);//绘制图像
var img = document.createElement("img");//创建img
img.src = canvas.toDataURL("image/png");//将绘制的图像用img显示出来
imgbox.appendChild(img);//将img添加到imgbox里
};
init();
})();
</script>
</body>
</html>
完结
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦