简体   繁体   English

如何在 Chrome 中使用 javascript 检测 HTML 视频是否受 DRM 保护?

[英]How to detect whether a HTML video is DRM-protected with javascript in Chrome?

Quick background: I am writing a browser extension that manipulates video while it's being played in a browser.快速背景:我正在编写一个浏览器扩展程序,它可以在浏览器中播放视频时对其进行操作。 The script itself is supposed to be as general purpose as it gets, and it should run on any site that has a video on it.脚本本身应该是通用的,它应该在任何有视频的网站上运行。

My video manipulations rely on being able to manipulate pixel data from the video.我的视频操作依赖于能够操作视频中的像素数据。 The HTML5-blesed way to do get video pixels into something you can work with in javascript is canvas2dContext.drawImage() function to draw the current video frame to a canvas, and context.getImageData() to get said data.将视频像素转换为您可以在 javascript 中使用的 HTML5 的方法是canvas2dContext.drawImage() context.getImageData()将当前视频帧绘制到 ZFCC790C72A86190DE1B549D0DDDC6F5 和上下文中。

The current code boils down to (I am simplifying things a fair bit):当前代码归结为(我正在简化一些事情):


let video = document.findElementsByTagName('video')[0];
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');

handleVideoFrame() {
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
  let data = context.getImageData(0,0,canvas.width, canvas.height);

  processData(data);
}

processData(data) {
  // a lot of heavy calculations going on here
}

window.requestAnimationFrame(handleVideoFrame);

The Problem: DRM问题:DRM

This works well and good, until you try to do this on Netflix, Disney+ or a different site of the same caliber.这很好用,直到您尝试在 Netflix、Disney+ 或相同口径的不同网站上执行此操作。 Those sites use DRM, and if there's DRM on that video, context.drawImage() will not work.这些网站使用 DRM,如果该视频上有 DRM,则context.drawImage()将不起作用。 Note that I do not wish to capture the actual frames of DRM protected videos, but I want to know whether the DRM is there.请注意,我不希望捕获受 DRM 保护的视频的实际帧,但我想知道 DRM 是否存在。

In Firefox, this is not a big deal, because if you try to call context.drawImage() on a DRM-protected video, you will get an exception .在 Firefox 中,这没什么大不了的,因为如果你尝试在受 DRM 保护的视频上调用context.drawImage()你会得到一个异常 You can then catch that exception, don't run the heavy processData() , alert the user to the fact that your script won't be working on that site because of DRM, and then shut the entire thing down.然后,您可以捕获该异常,不要运行繁重的processData() ,提醒用户您的脚本由于 DRM 而无法在该站点上运行,然后关闭整个程序。

In Chrome (and other Chromium reskins for that matter), on the other hand, context.drawImage() will not fail.另一方面,在 Chrome(以及其他 Chromium reskins)中, context.drawImage()不会失败。 Instead, context.drawImage() will draw a 100% opaque black square without throwing an exception, and this is a problem because:相反, context.drawImage()将绘制一个 100% 不透明的黑色方块而不会引发异常,这是一个问题,因为:

  • you can never definitely tell whether the video is DRM-protected or not永远无法确定视频是否受 DRM 保护
  • therefore you cannot inform the user, who will blame your script因此你不能通知用户,谁会责怪你的脚本
  • and while you can check whether the frame is black and avoid calling the heavy processData() if it is, you're still doing drawImage() calls that you don't need to be doing虽然您可以检查框架是否为黑色并避免调用繁重的processData()如果是,但您仍在执行您不需要执行的drawImage()调用

Solutions that I've tried我尝试过的解决方案

  • context.getImageData() returns an object that contains an array with RGBA values for each pixel. context.getImageData()返回一个 object,其中包含每个像素的 RGBA 值的数组。 I initially hoped that I could determine whether the video was being DRM-protected by looking at alpha values.我最初希望我可以通过查看 alpha 值来确定视频是否受到 DRM 保护。 However, the frame drawn by drawImage() is always¹ completely opaque, which means that this is a dead end.但是, drawImage()绘制的帧总是¹完全不透明,这意味着这是一个死胡同。

¹ Unless the video hasn't been loaded yet, in that case it's transparent. ¹ 除非视频尚未加载,否则它是透明的。

Solutions that I wish to avoid我希望避免的解决方案

  • Anything that involves me making assumptions based on the fact that the frame has been black for some amount of time.任何涉及我根据框架已经变黑一段时间的事实做出假设的事情。 Sure, I could run the video for n seconds and throw up a warning if all the frames I've checked up to that point were completely black.当然,如果我检查过的所有帧都是全黑的,我可以运行视频n秒并发出警告。 If the n is too low, I risk false positives.如果n太低,我就有误报的风险。 If the n is too high, the delay between video playback and 'whoops DRM' warning could be too long.如果n太高,视频播放和“哎呀 DRM”警告之间的延迟可能会太长。
  • Maintaining a list of known sites that I know utilize DRM维护我知道使用 DRM 的已知站点列表

You can simply look at the HTMLMediaElement's mediaKeys property, if it is set, the video is DRM protected:您可以简单地查看HTMLMediaElement 的mediaKeys属性,如果已设置,则视频受 DRM 保护:

const isDRMProtected = (elem) => elem.mediaKeys instanceof MediaKeys;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过JavaScript检测视频是否处于静音状态? - How to detect whether a video is on mute via JavaScript? 如何通过 JavaScript 检测 Linux 上是否安装了 Chrome? - How to detect whether Chrome is installed on Linux via JavaScript? 如何检测 HTML5 视频是否已暂停缓冲? - How to detect whether HTML5 video has paused for buffering? 如何检测用户是否在HTML和JavaScript中选择了整个文档? - How to detect whether user selected entire document or not in HTML and JavaScript? HTML5视频DRM-检测HDCP(输出保护)然后降级为SD内容? - HTML5 Video DRM - detect HDCP (output protection) then downgrade to SD content? Azure 媒体播放器 drm 保护 iPhone 中的视频问题 - Azure media player drm protected video issue in iPhone Javascript / HTML 5视频-检测视频是否未加载 - Javascript/HTML 5 Video - Detect if video is not loading 如何检测设备是否使用自己的视频播放器而不是HTML5播放器? - How can I detect whether or not a device uses it's own video player rather than the HTML5 player? HTML & Javascript - 如何在全屏视频开启时检测并发出警报 - HTML & Javascript - how to detect and put an alert on when fullscreen Video is on 如何检测浏览器是否可移动,并使用javascript“ if else”以不同大小显示视频? - How can I detect whether or not a browser is mobile and display a video at different sizes with javascript “if else”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM