前端视频截图方法canvas.drawImage()
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script> </head> <body> <div><button onclick="getVideoImg()">获取视频图片</button></div> <div><img src="" id="testImg" /></div> </body> <script type="text/javascript"> function getVideoImg() { var url = "https://xxxxx/test.mp4"; //你的视频地址 return new Promise(function (resolve, reject) { 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('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 setTimeout(function () { dataURL = canvas.toDataURL('image/jpeg'); //转换为base64 console.log(dataURL) $("#testImg").attr("src", dataURL); resolve(dataURL); }, 2000); }); }) } </script> </html>