Prevent HTML5 video from being downloaded (right-click saved)?

前端 未结 20 2107
醉酒成梦
醉酒成梦 2020-11-22 15:33

How can I disable \"Save Video As...\" from a browser\'s right-click menu to prevent clients from downloading a video?

Are there more complete solutions that prevent

20条回答
  •  囚心锁ツ
    2020-11-22 16:12

    Yes, you can do this in three steps:


    1. Place the files you want to protect in a subdirectory of the directory where your code is running.

      www.foo.com/player.html
      www.foo.com/videos/video.mp4

    2. Save a file in that subdirectory named ".htaccess" and add the lines below.

      www.foo.com/videos/.htaccess

      #Contents of .htaccess
      
      RewriteEngine on
      RewriteCond %{HTTP_REFERER} !^http://foo.com/.*$ [NC]
      RewriteCond %{HTTP_REFERER} !^http://www.foo.com/.*$ [NC]
      RewriteRule .(mp4|mp3|avi)$ - [F]
      

    Now the source link is useless, but we still need to make sure any user attempting to download the file cannot be directly served the file.

    1. For a more complete solution, now serve the video with a flash player (or html canvas) and never link to the video directly. To just remove the right click menu, add to your HTML:

      
      


    The Result:

    www.foo.com/player.html will correctly play video, but if you visit www.foo.com/videos/video.mp4:

    Error Code 403: FORBIDDEN


    This will work for direct download, cURL, hotlinking, you name it.

    This is a complete answer to the two questions asked and not an answer to the question: "can I stop a user from downloading a video they have already downloaded."

提交回复
热议问题