当我试图获取屏幕截图并将其保存为PNG,然后将视频上传到服务器,我遇到以下问题
希望你能解决我的问题
/*Output image show view*/ $('#file_browse').change(function(e){ getVideo(this); }); var capbtn = document.querySelector('#video_capture'); var video = document.querySelector('video'); var canvas = document.querySelector('canvas'); var context = canvas.getContext('2d'); var w,h,ratio; video.addEventListener('loadedMetadata',function() { ratio = video.videoWidth / video.videoHeight; w = video.videoWidth - 100; h = parseInt(w / ratio,10); canvas.width = w; canvas.height = h; },false); capbtn.addEventListener("click",function(){ context.fillRect(0,w,h); context.drawImage(video,h); var objImageData = canvas.toDataURL("data:image/png;"); }); function getVideo(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { var video = document.getElementsByTagName('video')[0]; var sources = video.getElementsByTagName('source'); sources[0].src = e.target.result; video.load(); video.style.display="block"; } reader.readAsDataURL(input.files[0]); } }
<input id="video_capture" type="submit" value="Capture" /> <video id="video_view" controls> <source src="movie.mp4" type="video/mp4"> </video> <canvas width="300" height="300"></canvas>
解决方法
@H_403_11@ 听起来像CORS问题.视频与Web服务器的起源不同.
如果您可以在响应中包含“Access-Control-Allow-Origin:*”头像,您可以设置video.crossorigin =“Anonymous”,那么您可以将其关闭.
我使用Charles Web Proxy将标题添加到我想要使用的任何图像或视频中.
见https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
另见https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
这是一个小提琴工作与图像:http://jsfiddle.net/mcepc44p/2/
var canvas = document.getElementById("canvas").getContext("2d"); var button = document.getElementById("button"); var image = new Image(); image.crossOrigin = "anonymous"; // This enables CORS image.onload = function (event) { try { canvas.drawImage(image,200,200); button.download = "cat.png"; button.href = canvas.canvas.toDataURL(); } catch (e) { alert(e); } }; image.src = "https://i.chzbgr.com/maxW500/1691290368/h07F7F378/"
这是你要找的吗?