Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

canvas 对video播放m3u8截图提示跨域

请问有人遇到过这个问题吗?使用videojs 播放 m3u8视频是用canvas进行截图。在ios上和pc的chrome浏览器都能正常运行。但是在安卓chrome浏览器提示:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

视频源设置了跨域。前端设置了crossOrigin=Anonymous
image.png
这是我的截图代码

  var video = document.querySelector("#myVideo_html5_api"); 
  video.setAttribute('crossOrigin', 'Anonymous')
  var canvas = (window.canvas = document.createElement("canvas"));
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  canvas.getContext("2d").drawImage(video, 0, 0, canvas.width, canvas.height);
  var strDataURL = canvas.toDataURL("image/" + fileType); // canvas中video中取一帧图片并转成dataURL
  var arr = strDataURL.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  var blob = new Blob([u8arr], {
    type: mime,
  });
  return blob

需求是能够对当前播放的视频画面截图。
如果有人能够解答,或者提供思路。万分感谢。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

是否支持跨域也需要后端支持的 需要配置 Access-Control-Allow-Origin 如果资源在oss上那么就在oss上配置一下允许跨域的域名就好了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...