export const getVideoCover = (data: string | Blob, seekTo: number = 0.0, width?: number, height?: number) : Promise => { const url = typeof data !== 'string' ? URL.createObjectURL(data) : data return new Promise(function (resolve, reject) { const video = document.createElement('video') video.setAttribute('crossOrigin', 'anonymous')// 处理跨域,需要服务器支持跨域 video.setAttribute('src', url) video.setAttribute('muted', '') if (width && height) { video.setAttribute('width', `${width}px`) video.setAttribute('height', `${height}px`) video.setAttribute('style', 'object-fit:scale-down') } video.load() video.addEventListener('loadedmetadata', function () { if (video.duration < seekTo) { reject(new Error('视频长度不够')); return; } setTimeout(() => video.currentTime = seekTo, 200); video.addEventListener('seeked', () => { const canvas = document.createElement('canvas') if (width && height) { canvas.width = width canvas.height = height } else { canvas.width = video.videoWidth canvas.height = video.videoHeight } canvas.getContext('2d')!.drawImage(video, 0, 0, width!, height!) const dataURL = canvas.toDataURL('image/jpeg') if (typeof data !== 'string') { video.pause() URL.revokeObjectURL(url) } resolve(dataURL) }) }) }) }