Rat's

使用Docker搭建DPlayer视频弹幕接口API后端
说明:之前有同学要求博主出个DPlayer弹幕后端搭建教程,刚好本博客的Handsome主题更新并完美适配了Dpl...
扫描右侧二维码阅读全文
03
2019/01

使用Docker搭建DPlayer视频弹幕接口API后端

说明:之前有同学要求博主出个DPlayer弹幕后端搭建教程,刚好本博客的Handsome主题更新并完美适配了Dplayer,然后就研究了下,发现了点小问题,如作者提供的弹幕API加载不出弹幕,而且Typecho当中的Dplayer插件有点旧无法对接V3后端,不知道其它程序插件是不是这样,这里博主只能使用HTML代码直接输出调用,经测试已完全正常加载弹幕和观看。这里就说下搭建及使用方法。

【2020.10.3】
弹幕api接口已修复,长期可用,https://dplayer.moerats.com,如出现问题留言即可。

【2020.10.27】
由于有些人不太喜欢Docker搭建,所以下面补齐CentOS、Debian、Ubuntu手动搭建教程。

简介

DPlayer是一个支持弹幕的HTML5视频播放器。支持Bilibili视频和danmaku,支持HLSFLVMPEG DASHWebTorrent以及其他视频格式,支持截屏、热键、切换清晰度以及字幕等。

安装

作者提供的弹幕后端搭建方法挺多的,这里选择一个搭建最快,版本最新的一种。

Github地址:https://github.com/MoePlayer/DPlayer-node

Docker安装

1、安装Docker

#CentOS 6
rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum update -y
yum -y install docker-io
service docker start
chkconfig docker on

#CentOS 7、Debian、Ubuntu
curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker

2、安装Docker Compose

curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

3、运行镜像
安装git

#Debian、Ubuntu系统
apt install git -y

#CentOS系统
yum install -y git

再使用命令:

#拉取源码
git clone https://github.com/MoePlayer/DPlayer-node.git
cd DPlayer-node
#新建镜像
docker-compose build
#拉取其它镜像并后台运行
docker-compose up -d

此时api地址为http://ip:1207,数据和日志存放在/root/dplayer文件夹。

当然如果你想其它端口,或者修改存放文件夹路径,那你在上面的新建镜像之前,作出如下操作:

#编辑DPlayer-node文件夹的docker-compose.yml文件,部分修改如下
mongo:
  volumes:
      - ~/dplayer/db:/data/db  #数据库存放文件夹,~/dplayer/db为映射在外部的路径,自行修改,
web:
  ports:
    - 1207:1207  #api映射到外部的端口,将前面的1207修改成你想要的即可
  volumes:
    - ~/dplayer/logs:/usr/src/app/logs  #同数据库操作
    - ~/dplayer/pm2logs:/root/.pm2/logs  #同上

改完后再新建镜像即可,如果你已经新建镜像了,但想改,那就清空之前的镜像再修改,方法参考→传送门

CentOS安装

提示:教程适用于CentOS 7、8系统。

1、安装NodeJS

curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install nodejs git -y

2、安装Mongodb

#将下面命令一起复制进SSH客户端运行
cat <<EOF > /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF
#安装mongodb
yum -y install mongodb-org
systemctl start mongod
systemctl enable mongod

3、安装Redis

#CentOS 7系统
yum install epel-release -y
yum install redis -y
systemctl start redis
systemctl enable redis

#CentOS 8系统
yum install redis -y
systemctl start redis
systemctl enable redis

4、安装弹幕服务器

#拉取源码
git clone https://github.com/MoePlayer/DPlayer-node.git
cd DPlayer-node
npm i
npm i -g pm2
pm2 start index.js --name danmuapi

此时api地址为http://ip:1207

Debian安装

提示:教程适用于Debian 8、9、10系统。

1、安装NodeJS

curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt install -y git nodejs 

2、安装Mongodb

#Debian 8系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | apt-key add -
echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/4.0 main" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list
apt update -y
apt -y install mongodb-org
systemctl start mongod
systemctl enable mongod

#Debian 9系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | apt-key add -
echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list
apt update -y
apt -y install mongodb-org
systemctl start mongod
systemctl enable mongod

#Debian 10系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
apt update -y
apt -y install mongodb-org
systemctl start mongod
systemctl enable mongod

3、安装Redis

apt install redis-server -y

4、安装弹幕服务器

#拉取源码
git clone https://github.com/MoePlayer/DPlayer-node.git
cd DPlayer-node
npm i
npm i -g pm2
pm2 start index.js --name danmuapi

此时api地址为http://ip:1207

Ubuntu安装

提示:教程适用于Ubuntu 16.04、18.04、20.04系统。

1、安装NodeJS

curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt install -y git nodejs 

2、安装Mongodb

#Ubuntu 16.04系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update -y
apt install -y mongodb-org
systemctl start mongod
systemctl enable mongod

#Ubuntu 18.04系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update -y
apt install -y mongodb-org
systemctl start mongod
systemctl enable mongod

#Ubuntu 20.04系统
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update -y
apt install -y mongodb-org
systemctl start mongod
systemctl enable mongod

3、安装Redis

apt install redis-server -y

4、安装弹幕服务器

#拉取源码
git clone https://github.com/MoePlayer/DPlayer-node.git
cd DPlayer-node
npm i
npm i -g pm2
pm2 start index.js --name danmuapi

此时api地址为http://ip:1207

域名反代

如果你的博客已经开启了https访问,那api也需要https地址,所以只使用ip:端口是不行的,这里就需要使用域名反代了。博主喜欢Caddy,所以这里说下宝塔和Caddy反代,其它一键环境的建议看官方文档或自行搜索。

如果你服务器没有安装Nginx/Apache的,可以用下Caddy,很方便很快,记得提前将域名解析到服务器。

1、宝塔反代
先进入宝塔面板,然后点击左侧网站,添加站点,然后再点击添加好了的域名名称,这时候就进入了站点配置,点击反向代理,目标URL填入http://127.0.0.1:1207,再启用反向代理即可。至于启用SSL就不说了,直接在站点配置就可以看到。

2、Caddy反代
安装Caddy

wget -N --no-check-certificate https://raw.githubusercontent.com/iiiiiii1/doubi/master/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh
#备用地址
wget -N --no-check-certificate https://www.moerats.com/usr/shell/Caddy/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh

配置Caddy

#以下全部内容是一个整体,请修改域名后一起复制到SSH运行!
echo "xx.com {
 tls admin@moerats.com
 proxy / http://127.0.0.1:1207
}" > /usr/local/caddy/Caddyfile

tls参数会自动帮你签发ssl证书,如果你要使用自己的ssl,改为tls /root/xx.crt /root/xx.key即可。后面为ssl证书路径。

启动Caddy

/etc/init.d/caddy start

反代好了后,你的API地址就为https://xx.com

使用

作者提供了很多插件,这里列举一点:

Typecho:https://github.com/volio/DPlayer-for-typecho
Hexo:https://github.com/NextMoe/hexo-tag-dplayer
Z-Blog:https://github.com/fghrsh/DPlayer_for_Z-BlogPHP
Discuz!:https://coding.net/u/Click_04/p/video/git
WordPress:https://github.com/BlueCocoa/DPlayer-WordPress

不过Typecho插件暂时用不了,其它程序暂时不清楚,所以这里提供一个播放器的HTML代码,代码如下:

<link href="https://www.moerats.com/usr/dplayer/DPlayer.min.css" rel="stylesheet">
<div id="dplayer"></div>
<script src="https://www.moerats.com/usr/dplayer/DPlayer.min.js"></script>
<script src="https://cdnjs.loli.net/ajax/libs/blueimp-md5/2.10.0/js/md5.min.js"></script>
<script>
var url="https://www.moerats.com/xx.mp4";    //这里填写视频地址
var id=md5(url);
const dp = new DPlayer({
    container: document.getElementById('dplayer'),
    video: {
        url: url
  },
  danmaku: {
        id: id,
        api: 'https://dplayer.moerats.com/'    //这里填写弹幕地址
    }
});
</script>

直接将代码贴进文章里即可,如果在Typecho开发版中使用不正常的话,可能还需要用两排!!!将代码上下围住使其强制输出,比如:

#特殊原因,不得已才加上o,使用的时候记得去掉
o!!!
代码
o!!!

还有更多功能及使用方法可以查看→传送门

演示

博主知道有人懒搭建的,所以这里提供个弹幕API地址:https://dplayer.moerats.com/。

好了,可以在视频里发彩色弹幕了,如果该视频播放器不显示的话,刷新一下就行了。

Vultr新用户注册送100美元/16个机房按小时计费,支持支付宝,【点击查看】。
最后修改:2020 年 10 月 27 日 07 : 01 PM

发表评论 取消回复

245 条评论

  1. 大会

    发布弹幕 报错:

    undefinedv3/ 404 这个怎么弄, 发布以后屏幕显示弹幕,刷新就没有了,怎么办,十分感谢

    1. 02
      @大会

      弹幕池id问题,设为视频链接就行

  2. 菲亚特

    请问大佬搭建这个需要的配置最少是啥?我给自己的视频站用,用公用的怕哪天突然不能用了,网站用的dplayer每天大概1万pv的流量。

    1. Rat's
      @菲亚特

      就用我提供的,独立服务器在跑,再大并发都没事,基本上近几年不会关

  3. 事件视界

    大佬,我想请问一下,有没有方法能批量修改弹幕文件呢,我现在建好了api,视频也可以正常访问了,但弹幕池内弹幕太少,比如我可以通过这个页面访问到我的弹幕数据【https://player.ezsky.xyz/v3/?id=c34c7d04edaaf4aa83f1d38f46b0fc73】,我应该如何操作才能修改这些数据呢

    1. Rat's
      @事件视界

      看下mongo数据库,那个danmu表,可以研究下

  4. 9527

    接口返回 弹幕数据了为什么, 弹幕没有跑起来?

  5. AAA

    博主,api失效了嘛
    https://dplayer.moerats.com/v3/bilibili?aid=271
    DPlayer-node 发生了一些意外:
    TypeError: Cannot read property 'cid' of undefined

    at module.exports (/www/wwwroot/dplayer.moerats.com/routes/bilibili.js:13:29) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:94:5) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/access-control.js:12:9) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/header.js:13:5) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/onerror.js:5:9) at async bodyParser (/www/wwwroot/dplayer.moerats.com/node_modules/koa-bodyparser/index.js:86:5)
    1. Rat's
      @AAA

      没有,好像拉取b站出问题了,dplayer自身用没问题

  6. AAA

    博主,api失效了嘛
    https://dplayer.moerats.com/v3/bilibili?aid=271
    DPlayer-node 发生了一些意外:
    TypeError: Cannot read property 'cid' of undefined

    at module.exports (/www/wwwroot/dplayer.moerats.com/routes/bilibili.js:13:29) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:94:5) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/access-control.js:12:9) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/header.js:13:5) at async module.exports (/www/wwwroot/dplayer.moerats.com/middleware/onerror.js:5:9) at async bodyParser (/www/wwwroot/dplayer.moerats.com/node_modules/koa-bodyparser/index.js:86:5)
  7. 相交文

    博主你好,我在CenOS 7 按照您给的方法去搞api,然后我输入curl 127.0.0.1:1207时,还是显示了not found,您知道是什么原因吗?

    1. 相交文
      @相交文

      CentOS 7 ,不是那个打错了

      1. Rat's
        @相交文

        首页not found是正常的,得看api接口

  8. coco

    你好博主,小白刚接触,请问怎么加载自己后端的弹幕吖?是在@Controller
    @RequestMapping(value = "barrage")
    public class BarrageController {

    @ResponseBody @RequestMapping(value = "v3", method = RequestMethod.GET,produces = "text/json;charset=UTF-8") public String getv3(@RequestParam String id) throws Exception { System.out.println(id); Map map = new HashMap(); List data = new ArrayList();

    // data = DPlayerConstants.barrage_init(data);

    data.add(4.35433); data.add(1); data.add(16777215); data.add("618c713c"); data.add("第一!第一"); map.put("code", DPlayerConstants.DPLAYER_SUCCESS_CODE); map.put("data",data); return JSONObject.toJSONString(map); }
  9. coco

    你好博主,小白刚接触,请问怎么加载自己后端的弹幕吖,

    1. Rat's
      @coco

      搭建api后,直接在dplayer设置下就行了,具体可以看看文档http://dplayer.js.org/zh/guide.html#%E5%BC%B9%E5%B9%95%E6%8E%A5%E5%8F%A3

  10. 明月

    博主你好,这个引用哔哩哔哩弹幕怎么弄呀,aid在哪获取 你有弄嘛

    1. Rat's
      @明月

      bilibili弹幕接口https://dplayer.moerats.com/v3/bilibili?aid=xxxx。
      至于aid获取,给个在线转换地址你https://bv-av.cn/get-bv-av。

      1. 明月
        @Rat's

        大佬 方便加下扣扣嘛 0.0