/**
* 获取视频的封面图信息
* @param url 视频地址
* @param second 秒数
*/
async function getVideoBase64(url: string, second: number = 0) {
const video = document.createElement('video');
video.setAttribute('crossOrigin', 'anonymous'); // 处理跨域
video.setAttribute('src', url);
// 静音操作,防止播放失败
video.setAttribute('muted', 'muted');
video.addEventListener('loadeddata', async () => {
const canvas = document.createElement('canvas');
canvas.width = video.width;
canvas.height = video.height;
if (second) {
video.currentTime = second;
// 播放到当前时间的帧,才能截取到当前的画面
await video.play();
await video.pause();
}
canvas.getContext('2d')?.drawImage(video, 0, 0, width, height);
return canvas.toDataURL('image/jpeg');
});
}
video截取某一帧作为封面的方法
最新推荐文章于 2024-04-26 16:34:13 发布