function vSetImg(obj) {
const canvas = document.createElement("canvas");
canvas.width = obj.videoWidth;
canvas.height = obj.videoHeight;
canvas.getContext('2d').drawImage(obj, 0, 0, canvas.width, canvas.height);
obj.setAttribute('poster', canvas.toDataURL("image/png"));
//插入img标签查看封面图(调试用)
const img = document.createElement("img");
img.src = canvas.toDataURL("image/png");
document.body.appendChild(img);
}
<video preload="auto" onloadeddata="vSetImg(this)" controls>
<source src="wel.mp4">
</video>
canvas获取视频第一帧设置为视频封面
于 2021-04-29 15:12:27 首次发布