js获取视频第一帧作为封面

/*
* 截取视频的第一帧
*/
function getVideoBase64(url) {
    return new Promise(function (resolve) {
        let dataURL = '';
        let video = document.createElement("video");
        video.setAttribute('crossOrigin', 'anonymous');//处理跨域
        video.setAttribute('src', url);
        video.setAttribute('width', 400);
        video.setAttribute('height', 240);
        video.setAttribute('autoplay', 'autoplay');
        video.addEventListener('canplaythrough', 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);
        });
    })
}
/*
* 调用方法
*/
getVideoBase64("https://fld-wenzhi-1305125648.cos.ap-guangzhou.myqcloud.com/file/2.mp4").then(res => {
    console.log(res);
})

已发布

分类

来自

标签:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注