1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| function createPoster(val) { let videoCanList = [], curDateList = []; new Promise((reslove, reject) => { let video = document.createElement("VIDEO"); video.muted = true; video.autoplay = true; video.crossOrigin = "anonymous"; video.innerHTML = "<source src=" + val + ' type="audio/mp4">'; let canvas = document.createElement("canvas"); let ctx = canvas.getContext("2d"); video.addEventListener( "canplay", (e) => { let width = video.videoWidth; let height = video.videoHeight;
const maxWidth = 1024, maxHeight = 1024, ratio = maxWidth / maxHeight; let targetWidth = width, targetHeight = height; if (width > maxWidth || height > maxHeight) { if (width / height > ratio) { targetWidth = maxWidth; targetHeight = Math.round(maxWidth * (height / width)); } else { targetHeight = maxHeight; targetWidth = Math.round(maxHeight * (width / height)); } }
var anw = document.createAttribute("width"); anw.nodeValue = targetWidth; var anh = document.createAttribute("height"); anh.nodeValue = targetHeight; canvas.setAttributeNode(anw); canvas.setAttributeNode(anh); ctx.drawImage(video, 0, 0, targetWidth, targetHeight); var base64 = canvas.toDataURL("image/png", 0.8); video.pause();
reslove(base64); }, false ); }); }
|