如何从浏览器的右键菜单中禁用“另存为…”以防止客户端下载视频?
是否有更完整的解决方案来阻止客户端直接访问文件路径?
如何从浏览器的右键菜单中禁用“另存为…”以防止客户端下载视频?
是否有更完整的解决方案来阻止客户端直接访问文件路径?
你不能。
这是因为浏览器的设计目的就是:提供内容。但你可以让它更难下载。
方便的“解决方案”
我会把我的视频上传到第三方视频网站,比如YouTube或Vimeo。他们有很好的视频管理工具,优化设备的播放,他们努力防止他们的视频被撕掉,而你却没有付出任何努力。
解决方案1、禁用“右键单击”
你可以禁用上下文菜单事件,也就是“右键单击”。这将防止你的常规skiddie公然撕扯你的视频右击和另存为。但他们可以禁用JS,绕过这个问题,或者通过浏览器的调试器找到视频源。另外,这是糟糕的用户体验。在上下文菜单中有很多合法的东西,而不仅仅是另存为。
解决方案2,视频播放器库
使用自定义视频播放器库。它们中的大多数实现了视频播放器,可以根据您的喜好定制上下文菜单。你不会得到默认的浏览器上下文菜单。如果他们确实提供类似于另存为的菜单项,你可以禁用它。但同样,这是一个JS的解决方案。弱点类似于解决方案1。
解决方案3,HTTP直播
另一种方法是使用HTTP Live Streaming提供视频。它本质上是把视频切成块,然后一个接一个地播放。这就是大多数流媒体网站提供视频的方式。因此,即使你设法另存为,你只保存了一部分,而不是整个视频。收集所有的块并使用一些专用软件将它们缝合起来需要更多的努力。
解决方法4,在画布上作画
另一种技巧是在<canvas>上绘制<video>。在这种技术中,使用一些JavaScript,您在页面上看到的是一个<canvas>元素从隐藏的<video>渲染帧。因为它是一个<canvas>,上下文菜单将使用一个<img>的菜单,而不是一个<video>的。你会得到另存图像,而不是另存视频。
解决方案5,CSRF令牌
您还可以利用CSRF令牌。您可以让服务器在页面上发送一个令牌。然后使用该令牌获取视频。您的服务器在提供视频之前检查它是否是一个有效的令牌,或者获得HTTP 401。这个想法是,你只能通过有一个令牌来获得一个视频,你只能从页面来,而不是直接访问视频url。
这是一个简单的解决方案,为那些希望简单地从html5视频删除右键单击“保存”选项
$(document).ready(function(){
$('#videoElementID').bind('contextmenu',function() { return false; });
});
我通常使用的最好的方法很简单,我完全禁用整个页面的上下文菜单,纯html+javascript:
<body oncontextmenu="return false;">
就是这样!我这么做是因为你总是可以通过右键点击看到源代码。 好吧,你说:“我可以直接使用浏览器查看源代码”,这是真的,但我们从事实开始,你不能停止下载html5视频。
使用Vimeo等服务:登录Vimeo > Goto Video >设置>隐私>标记为安全,也可以选择嵌入域。一旦设置了嵌入域,它将不允许任何人嵌入视频或从浏览器显示它,除非从指定的域连接。所以,如果你有一个页面在你的服务器上是安全的,它在iframe中加载Vimeo播放器,这使得它很难绕过。
@Clayton-Graul有我正在寻找的东西,除了我需要一个使用AngularJS的网站的CoffeeScript版本。以防你也需要它,下面是你在AngularJS控制器中输入的内容:
# This is how to we do JQuery ready() dom stuff
$ ->
# let's hide those annoying download video options.
# of course anyone who knows how can still download
# the video, but hey... more power to 'em.
$('#my-video').bind 'contextmenu', ->
false
“奇怪的事情正在k圈发生”(这是真的)
PHP将html5视频标记和一个会话一起发送,其中键是一个随机字符串,值是文件名。
ini_set('session.use_cookies',1);
session_start();
$ogv=uniqid();
$_SESSION[$ogv]='myVideo.ogv';
$webm=uniqid();
$_SESSION[$webm]='myVideo.webm';
echo '<video autoplay="autoplay">'
.'<source src="video.php?video='.$ogv.' type="video/ogg">'
.'<source src="video.php?video='.$webm.' type="video/webm">'
.'</video>';
现在PHP被要求发送视频。PHP恢复文件名;删除会话并立即发送视频。此外,所有“无缓存”和mime类型的头必须存在。
ini_set('session.use_cookies',1);
session_start();
$file='myhiddenvideos/'.$_SESSION[$_GET['video']];
$_SESSION=array();
$params = session_get_cookie_params();
setcookie(session_name(),'', time()-42000,$params["path"],$params["domain"],
$params["secure"], $params["httponly"]);
if(!file_exists($file) or $file==='' or !is_readable($file)){
header('HTTP/1.1 404 File not found',true);
exit;
}
readfile($file);
exit:
现在,如果用户复制url在一个新的选项卡或使用上下文菜单,他将没有运气。
它似乎通过websocket流视频是一个可行的选择,就像在流帧和绘制他们在画布之类的东西。
使用JavaScript在websockets上传输视频流
我认为这将提供另一个级别的保护,使客户更难获取视频,当然解决了您的问题,“另存视频为…”右键单击上下文菜单选项(过量?!).
你至少可以阻止那些不懂技术的人使用右键上下文菜单下载你的视频。你可以使用oncontextmenu属性禁用任何元素的上下文菜单。
oncontextmenu="return false;"
这适用于body元素(整个页面)或在video标签中使用它的单个视频。
<video oncontextmenu="return false;" controls>...</video>
我们最终使用了url过期的AWS CloudFront。视频将加载,但当用户右键单击并选择另存为最初收到的视频url时,该视频已过期。搜索CloudFront Origin Access Identity。
生成视频url需要一个密钥对,这个密钥对可以在AWS CLI中创建。供参考,这不是我的代码,但它工作得很好!
$resource = 'http://cdn.yourwebsite.com/videos/yourvideourl.mp4';
$timeout = 4;
//This comes from key pair you generated for cloudfront
$keyPairId = "AKAJSDHFKASWERASDF";
$expires = time() + $timeout; //Time out in seconds
$json = '{"Statement":[{"Resource":"'.$resource.'","Condition" {"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}';
//Read Cloudfront Private Key Pair
$fp=fopen("/absolute/path/to/your/cloudfront_privatekey.pem","r");
$priv_key=fread($fp,8192);
fclose($fp);
//Create the private key
$key = openssl_get_privatekey($priv_key);
if(!$key)
{
echo "<p>Failed to load private key!</p>";
return;
}
//Sign the policy with the private key
if(!openssl_sign($json, $signed_policy, $key, OPENSSL_ALGO_SHA1))
{
echo '<p>Failed to sign policy: '.openssl_error_string().'</p>';
return;
}
//Create url safe signed policy
$base64_signed_policy = base64_encode($signed_policy);
$signature = str_replace(array('+','=','/'), array('-','_','~'), $base64_signed_policy);
//Construct the URL
$url = $resource.'?Expires='.$expires.'&Signature='.$signature.'&Key-Pair-Id='.$keyPairId;
return '<div class="videowrapper" ><video autoplay controls style="width:100%!important;height:auto!important;"><source src="'.$url.'" type="video/mp4">Your browser does not support the video tag.</video></div>';
是的,你可以通过三个步骤做到这一点:
将要保护的文件放置在运行代码的目录的子目录中。 www.foo.com/player.html www.foo.com/videos/video.mp4 在该子目录中保存一个名为“”的文件。并添加下面的行。 www.foo.com/videos/.htaccess # .htaccess的内容 RewriteEngine上 RewriteCond % {HTTP_REFERER} ! ^ http://foo.com/。* $(数控) ^http://www.foo.com/.*$ [NC] 重写规则(mp4|mp3|avi)$ - [F]
现在源链接是无用的,但我们仍然需要确保任何试图下载该文件的用户不能直接获得该文件。
对于一个更完整的解决方案,现在为视频提供一个flash播放器(或html画布),不要直接链接到视频。只需删除右键菜单,添加到您的HTML: <body oncontextmenu="return false;">
结果:
www.foo.com/player.html可以正确播放视频,但如果你访问www.foo.com/videos/video.mp4:
错误代码403:禁止
这将工作直接下载,cURL,盗链,你的名字。
这是对两个问题的完整回答,而不是对“我能否阻止用户下载他们已经下载的视频”这个问题的回答。
作为客户端开发人员,我推荐使用blob URL, blob URL是一个引用二进制对象的客户端URL
<video id="id" width="320" height="240" type='video/mp4' controls > </video>
在HTML中,让你的视频src为空, 在JS中使用AJAX获取视频文件,确保响应类型是blob
window.onload = function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'mov_bbb.mp4', true);
xhr.responseType = 'blob'; //important
xhr.onload = function(e) {
if (this.status == 200) {
console.log("loaded");
var blob = this.response;
var video = document.getElementById('id');
video.oncanplaythrough = function() {
console.log("Can play through video without stopping");
URL.revokeObjectURL(this.src);
};
video.src = URL.createObjectURL(blob);
video.load();
}
};
xhr.send();
}
注意:大文件不建议使用此方法
EDIT
使用跨源阻塞和头标记检查来防止直接下载。 如果视频是通过API传递的;使用不同的http方法(PUT / POST)来代替'GET'
以下是我所做的:
function noRightClick() { alert("You cannot save this video for copyright reasons. Sorry about that."); } <body oncontextmenu="noRightClick();"> <video> <source src="http://calumchilds.com/videos/big_buck_bunny.mp4" type="video/mp4"> </video> </body> This also works for images, text and pretty much anything. However, you can still access the "Inspect" and the "View source" tool through keyboard shortcuts. (As the answer at the top says, you can't stop it entirely.) But you can try to put barriers up to stop them.
首先,要意识到完全阻止视频下载是不可能的,你所能做的只是让下载变得更加困难。也就是说,你隐藏了视频的来源。
网络浏览器会在缓冲区中临时下载视频,所以如果可以阻止下载,你也就阻止了视频被观看。
您还应该知道,世界上只有不到1%的人口能够理解源代码,这使得它相当安全。这并不意味着你不应该把它隐藏在源代码中——你应该这样做。
您不应该禁用右键单击,甚至更不应该显示一条消息说“由于版权原因,您无法保存此视频。对此我很抱歉。”正如这个答案所暗示的。
这对用户来说是非常烦人和困惑的。除此之外;如果他们在浏览器上禁用JavaScript,他们将能够右键单击并保存。
下面是一个你可以使用的CSS技巧:
video {
pointer-events: none;
}
CSS不能在浏览器中关闭,保护您的视频而不实际禁用右键单击。然而,一个问题是控件也不能启用,换句话说,它们必须设置为false。如果你打算添加自己的播放/暂停功能,或者使用一个有独立于视频标签的按钮的API,那么这是一个可行的选择。
控件也有一个下载按钮,所以使用它也不是一个好主意。
下面是一个JSFiddle的例子。
如果你打算使用JavaScript禁用右键,那么也要将视频源存储在JavaScript中。这样,如果用户禁用JavaScript(允许右击),视频将无法加载(它也更好地隐藏了视频源)。
从TxRegex回答:
<video oncontextmenu="return false;" controls>
<source type="video/mp4" id="video">
</video>
现在通过JavaScript添加视频:
document.getElementById("video").src = "https://www.w3schools.com/html/mov_bbb.mp4";
功能性JSFiddle
防止右击的另一种方法是使用embed标记。然而,这并没有提供运行视频的控件,所以它们需要在JavaScript中实现:
<embed src="https://www.w3schools.com/html/mov_bbb.mp4"></embed>
The
<body oncontextmenu="return false;">
不再有效。截至2018年6月,Chrome和Opera在时间轴上有一个子菜单,允许直接下载,所以用户不需要右键单击来下载视频。有趣的是,Firefox和Edge没有这个功能……
简单回答:像youtube那样加密链接,不知道怎么做,不如问问youtube/谷歌他们是怎么做的。(以防你想直奔主题。)
I would like to point out to anyone that this is possible because youtube does it and if they can so can any other website and it isn't from the browser either because I tested it on a couple browsers such as microsoft edge and internet explorer and so there is a way to disable it and seen that people still say it...I tries looking for an answer because if youtube can than there has to be a way and the only way to see how they do it is if someone looked into the scripts of youtube which I am doing now. I also checked to see if it was a custom context menu as well and it isn't because the context menu is over flowing the inspect element and I mean like it is over it and I looked and it never creates a new class and also it is impossible to actually access inspect element with javascript so it can't be. You can tell when it double right-click a youtube video that it pops up the context menu for chrome. Besides...youtube wouldn't add that function in. I am doing research and looking through the source of youtube so I will be back if I find the answer...if anyone says you can't than, well they didn't do research like I have. The only way to download youtube videos is through a video download.
Okay...I did research and my research stays that you can disable it except there is no javascript to it...you have to be able to encrypt the links to the video for you to be able to disable it because I think any browser won't show it if it can't find it and when I opened a youtube video link it showed as this "blob:https://www.youtube.com/e5c4808e-297e-451f-80da-3e838caa1275" without quotes so it is encrypting it so it cannot be saved...you need to know php for that but like the answer you picked out of making it harder, youtube makes it the hardest of heavy encrypting it, you need to be an advance php programmer but if you don't know that than take the person you picked as best answer of making it hard to download it...but if you know php than heavy encrypt the video link so it only is able to be read on yours...I don't know how to explain how they do it but they did and there is a way. The way youtube Encrypts there videos is quite smart so if you want to know how to than just ask youtube/google of how they do it...hope this helps for you although you already picked a best answer. So encrypting the link is best in short terms.
我们可以通过隐藏上下文菜单来让这变得不那么容易,就像这样:
<video oncontextmenu="return false;" controls>
<source src="https://yoursite.com/yourvideo.mp4" >
</video>
你不能百分百保护它,但你可以让它更难。我解释的这些方法,是我在PluralSight和BestDotNetTraining中学习保护方法时遇到的。尽管如此,这些方法都没有阻止我下载我想要的东西,但我很难策划下载者通过他们的保护。
除了其他提到的方法禁用上下文菜单。用户仍然可以使用第三方工具,如InternetDownload manager或其他类似软件下载视频。我在这里解释的保护方法是缓解这些第三方软件。
所有这些方法的要求是,当您确定有人正在下载您的视频时,阻止用户。这样,在你禁止他们访问你的网站之前,他们只能下载一到两个视频。
免责声明
如果有人滥用这些方法或用它来伤害其他人或我举的例子中的网站,我将不承担任何责任。它只是用来分享知识,帮助你保护你的知识产品。
生成带有过期的链接
这样做的要求是为每个用户创建一个下载链接。azure blob存储或amazon s3可以很容易地处理这个问题。您可以使用视频长度到期时间戳的两倍创建下载链接。然后,您需要捕获该视频链接和请求的时间。这是下一个方法所必需的。这种方法的问题在于,当用户单击播放按钮时,将生成下载链接。
在播放按钮事件中,您将向服务器发送一个请求,并获得链接并更新源代码。
限制视频请求速率
然后监视用户请求第二个视频的速度。如果用户请求下载链接的速度太快,那么你会立即阻止他们。你不能把这个阈值设得太大,因为你可能会错误地阻止那些只是浏览或浏览视频的用户。
启用HTTP范围
use some js library like videojs to play your video, also you need to return an AcceptRange in your header. Azure blob storage supports this out of the box. this way the browser starts to download the video chunk by chunk. usually, 32byte by 32byte. then you need to listen to videojs timeupdate change and update your server about the percentage that the video is watched. the percentage that the video is watched can't be more than the percentage that video is delivered. and if you are delivering a video content without receiving any percentage change, then you can block the user. because for sure they are downloading.
实现这个很棘手,因为用户可以向前或向后跳过视频,所以在实现这个时要注意这一点。
这是BestDotnetTraining处理时间更新的方式
myPlayer.ready(function () {
//var player = this;
this.src({
type: "video/mp4",
src: videoURL
});
if (videoId) {
myPlayer.play();
this.on('timeupdate', function () {
var currentPercent = parseInt(100 * myPlayer.currentTime() / myPlayer.duration());//calcualte as percentage
if (currentPercent % 5 == 0) {
//send percentage to server
SaveVideoDurationWatched(currentPercent, videoId);
}
});
}
});
anyway, the user is able to work around this by using some download method that downloads a file through streaming. almost c# do it out of the box and for nodejs, you can use request module. then you need to start a stopWatch, listen to a package received and compare the total byte received compare to the total size. this way you can calculate a percentage and the time spent to get that amount of percentage. then use the Thread.Sleep() or something like that to delay the thread the amount that you have to wait if you watch the video normally. also before the sleep the user can call the server and update the percentage that is received. so the server thinks that the user is actually watching a video.
例如,如果你计算出到目前为止你收到了1%,那么你可以计算出你应该等待休眠下载线程的数量。通过这种方式,你下载视频的速度不会超过它的实际长度。如果一个视频是24分钟,那么下载它就需要24分钟。(加上我们在第一个方法中设置的阈值)
original video length 24 minute
24 min *60000 = 1,440,000 miliseconds
1,440,000 % 100 = 14,400 milisecond is needed to download one percent
检查浏览器代理
当您正在提供网页和视频链接或接受进度更新请求时,您可以查看浏览器代理。如果它是不同的,然后禁止用户。
请注意,一些旧的浏览器不会传递此信息。因此,当视频请求和网页请求都没有浏览器代理时,可以忽略这一点。但是如果一个请求有,而另一个没有,那么你应该禁止这个用户。
为了解决这个问题,用户可以手动将浏览器代理头设置为与他们用来捕获下载链接的无头浏览器相同。
检查引用标头
当引用者不是你的主机URL或你提供视频的页面URL时,你可以禁止该用户,因为他们将下载链接放在另一个选项卡或另一个应用程序中。甚至对于进度更新请求也可以这样做。
这样做的要求是有一个视频和显示该视频的页面的映射。你可以创建一些惯例或模式来理解URL应该是什么,这取决于你的设计。
为了解决这个问题,用户可以在下载视频时手动设置与下载页面URL相等的referrer头。
计算请求之间的时间间隔
if you receive so many requests that the time between them is the same, then you should block the user. you should put this to capture how much is time between the video link generation request. if they are the same (plus/minus some threshold) and it happens more than a number of times, then you can ban the user. because if there is a bot that is going to crawl your website or videos, then usually they have the same sleep time between their request. so if you receive each request, for example, every 1.3(plus/mins some deviation) minutes. then you raise an alarm. for this, you can use some statistic calculation to know the deviation between the requests.
为了解决这个问题,用户可以在请求之间设置一个随机的睡眠时间。
示例代码
我有一个回购PluralSight-Downloader正在中途做它。我在5年前创建了这个回购。因为我写它是为了学习目的和自己的个人使用,到目前为止,回购没有收到任何更新,我不打算更新或使它易于使用。这只是一个可以做到的例子。
你可以使用
<video src="..." ... controlsList="nodownload">
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/controlsList
它不会阻止保存视频,但它会删除上下文菜单中的下载按钮和“另存为”选项。
阻止HTML5视频下载(右键保存)
<video type="video/mp4" width="330" height="300" controlsList="nodownload" oncontextmenu="return false;" controls></video>
这是一个完整的解决方案禁用下载,包括右击>另存为…在上下文菜单中:
<video oncontextmenu="return false;" controlsList="nodownload">
</video>
你不能。
例如,人们可以使用一些api例如desktopCapture, getUserMedia 允许用户记录屏幕、窗口、选项卡。
人们可以使用它并将其写入画布,然后将所有的块连接在一起以获得视频,
所以如果他们真的想要视频,没有办法阻止他们下载。
controlsList在不添加任何其他JavaScript函数的情况下阻止下载开始全屏等操作
<video width="400" controlsList="nofullscreen nodownload" controls>
你在浏览器中看到的一切都是下载的内容。这里提到的问题是如何在浏览器中保存这些内容。要查看内容,客户端浏览器从内容服务器下载并在本地提供。
一种流行的解决方案是将(短暂的)内容仅保存在浏览器中,并在有限的时间内,以一种不能直接保存的方式保存。blob是这种方法的一种实现,它具有减少带宽和存储开销的额外好处,因为内容存储在二进制对象中。
内容的短有效期使得普通用户几乎不可能持久存储,因为在用户尝试保存过期内容之前会显示新内容。
我发现了一个类似问题的好答案,使用PHP而不是JavaScript以获得更好的安全性。
我想使用浏览器的默认播放器在用户的浏览器中播放test.mp4(就像在Web页面上单击了URL/test.mp4一样),但需要密码,该密码由用户提供或由软件内部提供。
下面是这个想法的一个简要概述。它开始于用户去(运行)一个我写的叫做secure.php的程序来播放test.mp4。
文件test.mp4位于一个子目录("secureSubdirectory")中,该子目录包含一个包含"Require all denied"的.htaccess。这立即阻止了任何通过URL的直接访问。
当secure.php运行时,它提供一个密码(或向用户查询密码),然后对自己执行一个包含密码的POST,使用一个salt,使用PHP命令验证它:
$Hash=base64_encode(hash_hmac("sha256",$Pwd,$Salt,true));
$HashesAreSame=hash_equals($Hash,$GoalHash);
然后测试test.mp4是否存在,并执行以下PHP代码将test.mp4文件作为字节流返回给用户的浏览器:
header("Content-Type: video/mp4");
echo file_get_contents("secureSubdirectory/$path");
exit;
视频和预期的一样。如果我然后右键单击显示视频的页面并尝试保存视频,结果文件将只包含一个错误字符串,如“错误:密码未找到”,因为test.mp4是使用普通的secure.php URL查询的,而不是使用正确的密码通过POST。
当然,您可以使用浏览器调试工具的Network选项来获得响应有效负载(视频字节),但是如果浏览器提供了阻止访问调试工具的选项,那么PHP程序或.htaccess文件就可以阻止这种情况。
我无法想象失败的案例,但如果存在的话我会非常感兴趣,因为简单而完美的授权是非常罕见的事情。(请注意,由于这种方法依赖于密码,因此将其与用户关联并不是一种安全的身份验证方式,因为用户可能会意外或故意地发布或共享密码。)