原理:
利用http传输协议,将本地视频播放,通过http传输,在传输前,先进行解密,再发送到客户端。
这样,客户端接收到的,就是解密后的视频,可以正常播放。
而通过其他播放器,无法进行解密,所以也就无法正常播放。
注:本文旨在介绍一种思路,加密方式很简单,各位自行深入研究。
这里以一个正常视频文件:a.mp4 为例,介绍如何加密、如何解密播放:
1、先对视频文件进行加密。为了测试,只做了简单加密,将每个字节进行取反后另行保存为b.mp4。下面是已经加密好的b.mp4文件,可下载后解压到桌面上,以便测试。
2、架设http服务端口。
3、解密传输。
以下为架设服务端的代码,运行后,在浏览器中访问:
http://127.0.0.1:8876/video.aardio
即可播放该视频。
//播放加密视频
//参考文章: https://blog.youkuaiyun.com/qq_41787812/article/details/129662076
import win.ui;
/*DSG{{*/
var winform = win.form(text="WebSocket单线程异步服务端演示";left=10;top=4;right=774;bottom=467)
winform.add(
txtMessage={cls="edit";left=29;top=22;right=741;bottom=432;db=1;dl=1;dr=1;dt=1;edge=1;multiline=1;z=1}
)
/*}}*/
import web.socket.server;
var wsrv = web.socket.server();
//启动服务端
if( wsrv.start(,8876) ){
winform.txtMessage.print( wsrv.httpServer.getUrl(),"已启动HTTP服务器");
}
else {
winform.txtMessage.print("启动失败,建议修改端口号")
}
wsrv.httpServer.run(
function(response,request){
winform.txtMessage.print("HTTP协议访问:",request.url);
var f = ..string.load("C:\Users\Administrator\Desktop\b.mp4")
var length = #f;
var range = request.headers.range
if range { // 分段传输
var s = ..string.split(..string.trim(range,'bytes='),"-");
var start = tonumber(s[[1]]):0
var ed = tonumber(s[[2]]):length-1
if start>=length { // 请求超过总数
f = "";
response.headers={
"Accept-Ranges"="bytes";
"Cache-Control"="no-cache, no-store, must-revalidate";
"Content-Length"=0;
"Content-Type"="video/mp4";
"Date"=..time.gmt();
"ETag"=`"894afd8981c0d81:0"`;
"Expires"=0;
"Last-Modified"=..time.gmt();
"Pragma"="no-cache";
"Server"="Microsoft-IIS/8.5";
}
} else { // 请求合理
if start==0 and ed==#f-1 { //全部发送
response.headers={
"Accept-Ranges"="bytes";
"Cache-Control"="no-cache, no-store, must-revalidate";
"Content-Length"=#f;
"Content-Type"="video/mp4";
"Date"=..time.gmt();
"ETag"=`"894afd8981c0d81:0"`;
"Expires"=0;
"Last-Modified"=..time.gmt();
"Pragma"="no-cache";
"Server"="Microsoft-IIS/8.5";
}
} else { //分段发送
response.status=206;
f = string.slice(f,start+1,ed+1);
response.headers={
"Accept-Ranges"="bytes";
"Cache-Control"="no-cache, no-store, must-revalidate";
"Content-Length"=#f;
"Content-Type"="video/mp4";
"Date"=..time.gmt();
"ETag"=`"894afd8981c0d81:0"`;
"Expires"=0;
"Last-Modified"=..time.gmt();
"Pragma"="no-cache";
"Server"="Microsoft-IIS/8.5";
"Content-Range"="bytes "++start++"-"++(start+#f-1)++"/"++length;
}
}
}
} else { // 没有range,下载整个视频
response.headers={
"Accept-Ranges"="bytes";
"Cache-Control"="no-cache, no-store, must-revalidate";
"Content-Length"=#f;
"Content-Type"="video/mp4";
"Date"=..time.gmt();
"ETag"=`"894afd8981c0d81:0"`;
"Expires"=0;
"Last-Modified"=..time.gmt();
"Pragma"="no-cache";
"Server"="Microsoft-IIS/8.5";
}
}
f = ..raw.buffer(f)
for(i=1;#f;1){
f[i] = ~f[i]
}
response.writeBuffer(f,#f)
}
);
winform.show()
win.loopMessage();