其他分享
首页 > 其他分享> > 前端截取视频第一帧作为封面

前端截取视频第一帧作为封面

作者:互联网

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', 750);
                    video.setAttribute('height', 750);
                    video.setAttribute('preload', 'auto');
                    video.addEventListener('loadeddata', function() {
                        let canvas = document.createElement("canvas"),
                            width = video.width, //canvas的尺寸和图片一样
                            height = video.height;
                        canvas.width = width;
                        canvas.height = height;
                        canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
                        dataURL = canvas.toDataURL('image/jpeg'); //转换为base64
                        resolve(dataURL);
                    });
                })
            },
//转为file 进行上传 dataURLtoFile(dataurl, filename = 'file') { let arr = dataurl.split(',') let mime = arr[0].match(/:(.*?);/)[1] let suffix = mime.split('/')[1] let bstr = atob(arr[1]) let n = bstr.length let u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], `${filename}.${suffix}`, { type: mime }) },

 

标签:canvas,封面,截取,setAttribute,height,width,let,video,前端
来源: https://www.cnblogs.com/syzyn/p/16540486.html