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. 明月

    阔以了,哈哈哈 确实姿势不对 对了 博主这个弹幕需要装redis还需要mongodb 嘛,两个都存了是吧

    1. Rat's
      @明月

      对,用docker的话,就不需要安装,里面会自动拉取,手动就需要自己安装了

      1. 明月
        @Rat's
        该评论仅登录用户及评论双方可见
        1. Rat's
          @明月

          不会,用独立服务器托管的,内存没感觉

  2. 明月

    你好,博主 你这个支持hls 视频格式吗,我尝试着引入dplayer推荐的hls.js了 不生效 还是说我引入的文件不对哈

    1. Rat's
      @明月

      可以,应该姿势不对

      1. 明月
        @Rat's

        需要啥姿势,请教下 需要加type:hls嘛 我加了木有生效

  3. smake

    已用上,感谢博主

  4. 52的小窝

    danmaku的id怎么获取与填写?

    1. Rat's
      @52的小窝

      id好像是默认分配的,不用填写

      1. 52的小窝
        @Rat's

        啊?搭建好像没问题,用了反代理解决了跨域,在写弹屏id时,无论写什么,都是404,不写它就默认是id=undefined,也是404,求教。

        1. 52的小窝
          @52的小窝

          重装了数遍,终于好了,原来是js自身的问题,我用了你的js,终于成功了,感谢

          1. Rat's
            @52的小窝

            搭建啥,直接用我提供的api算了,长期可用。

  5. 橱窗外的小孩

    你好,想问下DPlayer弹幕接口的数据格式是怎么样的?

  6. 小白小白的

    Metadata cache created.

    '[' -n '' ']'sh -c 'yum install -y -q docker-ce'
    Error:

    Problem: package docker-ce-3:19.03.12-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed

    cannot install the best candidate for the jobpackage containerd.io-1.2.10-3.2.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.13-3.1.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.13-3.2.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.2-3.3.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.2-3.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.4-3.1.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.5-3.1.el7.x86_64 is filtered out by modular filteringpackage containerd.io-1.2.6-3.3.el7.x86_64 is filtered out by modular filtering
    [root@i]# systemctl start docker

    Failed to start docker.service: Unit docker.service not found.
    [root@]# systemctl enable docker
    Failed to enable unit: Unit file docker.service does not exist.

    楼主弹幕不能用了,这是是不是环境搭建问题 要如何解决,教一教

    1. Rat's
      @小白小白的

      我弹幕挂了,没管了,这好像是docker安装失败了,你用的centos 8系统?

  7. wallis

    萌新求教 反向代理的域名必须要有ssl证书吗?我用宝塔反向代理,前端连接一直说弹幕读取失败,反应是network error

    1. Rat's
      @wallis

      可以不用https,这个先看弹幕搭建正常不,然后看控制台分析问题

  8. yitzu

    你发的这些代码放在哪里呢?服务器怎么放代码?

    1. Rat's
      @yitzu

      哪个地方的代码?

  9. 求微信,dplayer-node后端配置出问题了,前端一直连接不上,求解决...

    1. Rat's
      @光

      啥问题

      1. @Rat's

        我本地mongo里是没有任务数据的,是不是因为没有对应的数据才会报错,但是报404应该是地址写错了吧

      2. @Rat's

        服务器上按照你的方法安装了DPlayer-node,docer-compose.yml文件中mongo那里显示的是ports:127.0.0.1:1207:1207
        并且设置了跨域 。nginx上配置的是:
        location /mongo/ {

        proxy_pass http://127.0.0.1:1207/; add_header Access-Control-Allow-Origin *; }

        然后在前段弹幕api那里写上了api:"http://服务器地址/mongo/"【mongo是我设置的nginx跨域标识】
        我前端用的是vue-dplayer播放器,最后访问弹幕地址的时候它就报这个错误:
        GET http://服务器地址/mongo/v2/?id=4157142 404 (Not Found)
        这里的v2是好像是它自己加上去的
        然后就不晓得怎么解决这个问题

  10. Summer

    大佬,可以出一期视频吗,小白表示看不懂啊啊啊啊

    1. Rat's
      @Summer

      懒的搞视频