一、安装Redis
官网下载地址:https://redis.io/download
Redis centos安装过程:
yum -y install gcc gcc-c++ wget http://download.redis.io/releases/redis-4.0.10.tar.gz tar -zxvf redis-4.0.10.tar.gz cd redis-4.0.10 make && make install ./utils/install_server.sh #测试redis是否运行 redis-cli > ping #返回pong表示成功
安装flask-redis
#清华源 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple flask-redis
二、安装开源插件Dplayer
Dplayer官网地址:http://dplayer.js.org/#/ (有时候会打不开哦)
Dplayer github地址:https://github.com/MoePlayer/DPlayer
#html引入css/js
<link rel="stylesheet" href="{{ url_for('static',filename='dplayer/dist/Dplayer.min.css') }}" >
<script src="{{ url_for('static',filename='dplayer/dist/DPlayer.min.js') }}"></script>
#引用到页面
<div id="dplayer" style="height: 500px;width: 774px"></div>
#弹幕和视频接口
<script>
var dp1 = new DPlayer({
container: document.getElementById('dplayer'),
video: {
url: "{{ url_for('static',filename='uploads/'+movie.url) }}"
},
danmaku: {
id: '{{ movie.id }}',
api: "/tm/"
}
});
</script>
#Python返回弹幕的处理方法
@home.route("/tm/v3/",methods=["GET","POST"])
def tm():
import json
if request.method=="GET":
#获取弹幕消息队列
mid = request.args.get("id")
key="movie"+str(mid)
if rd.llen(key):
msgs=rd.lrange(key,0,2999)
res={
"code":0,
"data":[json.loads(v) for v in msgs]
}
else:
res={
"code":1,
"danmaku":[]
}
resp=json.dumps(res)
if request.method=="POST":
#添加弹幕
data=json.loads(request.get_data())
msg= {
"__v": 0,
"_id": datetime.datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex,
"author": data["author"],
"time": data["time"],
"text": data["text"],
"color": data["color"],
"type": data["type"],
"ip": request.remote_addr,
"player": data["id"]
}
res = {
"code": 0,
"danmaku":msg
}
resp=json.dumps(res)
msg=[data["time"],data["type"],data["color"],data["author"],data["text"]]
rd.lpush("movie"+str(data["id"]),json.dumps(msg))
return Response(resp,mimetype="application/json")
播放效果: