话不多说,直接上代码
<template>
<view class="content">
<image v-if="demo != ''" class="logo" :src="demo"></image>
<video :src="url"></video>
</view>
</template>
<script>
export default {
data() {
return {
url: 'https://a1.easemob.com/1175220727113829/tikangbao2022/chatfiles/f2274ac0-3958-11ed-8939-9d38195bdc46',
videoImg:"?vframe/jpg/offset/0",
demo:'',
}
},
onLoad() {
this.getVideoPoster(this.url)
},
methods: {
getVideoPoster(url) {
const that = this;
let video = document.createElement('video')
video.setAttribute('crossOrigin', 'anonymous')
video.setAttribute('src', url)
video.setAttribute('width', 400)
video.setAttribute('height', 400)
video.setAttribute('preload', 'auto')
video.addEventListener('loadeddata', function () {
console.log('视频第一帧加载完')
let canvas = document.createElement('canvas')
let width = video.width
let height = video.height
canvas.width = width
canvas.height = height
canvas.getContext('2d').drawImage(video, 0, 0, width, height)
const dataURL = canvas.toDataURL('image/jpeg')
that.demo = dataURL;
console.log('getVideoPoster-dataURL', dataURL.slice(0, 16))
})
video.addEventListener('error', err => {
console.log('视频加载失败', err)
})
},
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.logo {
height: 500rpx;
width: 500rpx;
border: 3px solid red;
}
</style>
效果图