这个功能经常用到, 代码逻辑记录一下:
首选我们在change事件之后操作:
1 2 3 4 5 6 7 8 |
let files = file.target.files; if (files.length > 0) { let img = new Image(); this.thumbUrl = img.src = URL.createObjectURL(files[0]); setTimeout(()=> { this.viedoFirst(files[0]); }) } |
viedoFirst 方法:
1 2 3 4 5 6 7 8 9 10 11 |
viedoFirst (files) { var scale = 0.8; var video = document.getElementById("videoHide"); video.addEventListener('loadeddata', () => { var canvas = document.createElement("canvas"); canvas.width = video.videoWidth * scale; canvas.height = video.videoHeight * scale; canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height); this.firstBaseData = canvas.toDataURL("image/png"); }); }, |
最终视频的首帧图已经保存在我们 this.firstBaseData 里面了, 简单效果如下:
这是刚从视频获取到的首帧图!