如何从浏览器的右键菜单中禁用“另存为…”以防止客户端下载视频?
是否有更完整的解决方案来阻止客户端直接访问文件路径?
如何从浏览器的右键菜单中禁用“另存为…”以防止客户端下载视频?
是否有更完整的解决方案来阻止客户端直接访问文件路径?
当前回答
你不能。
这是因为浏览器的设计目的就是:提供内容。但你可以让它更难下载。
方便的“解决方案”
我会把我的视频上传到第三方视频网站,比如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。
其他回答
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.
我们最终使用了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>';
这是一个完整的解决方案禁用下载,包括右击>另存为…在上下文菜单中:
<video oncontextmenu="return false;" controlsList="nodownload">
</video>
@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圈发生”(这是真的)