1:利用转成base64进行获取封面
getVideoBase64(url) {
return new Promise(function (resolve, reject) {
let dataURL = "";
let video = document.createElement("video");
video.setAttribute("crossOrigin", "anonymous");
video.setAttribute("src", url);
video.setAttribute("width", 400);
video.setAttribute("height", 240);
video.setAttribute("preload", "auto");
video.setAttribute('crossOrigin', 'anonymous');
video.addEventListener("loadeddata", function () {
let canvas = document.createElement("canvas"),
width = video.width,
height = video.height;
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(video, 0, 0, width, height);
dataURL = canvas.toDataURL("image/jpeg");
resolve(dataURL);
});
})
},
if (res.data.data.clipList) {
setTimeout(() => {
res.data.data.clipList.forEach((item, index) => {
this.getVideoBase64(item.videoUrl).then((data) => {
item.coverUrl = data
});
});
}, 1000);
}
2:利用canvas来获取视频第一帧作为封面
cutPicture(item) {
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.videoUrl = oGrayImg
this.remove()
}
return item
},