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. xiaoxi

    谢谢不过教程不完整,容易出错,我打算录视频教大家,摸索了2天哈哈

    1. xiaoxi
      @xiaoxi

      不要用反代也没有关系,更简单

    2. xiaoxi
      @xiaoxi

      我使用debian

  2. hellow

    请问弹幕加载出来之后不动是什么情况,视频一直播,但是弹幕一直在那里不消失

  3. 彭于晏

    群晖docker能搭建嘛,我试了好多次都不行

    1. Rat's
      @彭于晏

      可以,问题不大

  4. SmileForYa

    请问反代之后首页404怎么解决?还有就是如何修改默认数据库地址,我想连接到公网的独立数据库,还请指教。

    1. Rat's
      @SmileForYa

      手动还是docker安装的?

  5. 孟大佐

    为何用你的播放器代码可以加载弹幕。用别的播放器加载弹幕就失败?有没有解决方法?
    我自己搭建的弹幕服务器。

    1. Rat's
      @孟大佐

      可以浏览器f12看下控制台

  6. w

    请问java自行搭建的话,弹幕返回的数据格式是什么样的

  7. NiceBowl

    请问有什么方法可以把弹幕的效果改为实时呈现(而不是按照视频时间轴顺序呈现),以供直播间使用呢?

  8. J

    请问有没有windows server 2012或以上版本的安装教程。

    1. Rat's
      @J

      win应该没问题,就是有点麻烦,你可以了解下scoop,用这个安装相关环境

      1. J
        @Rat's

        请问有办法把弹幕库的效果改为实时弹幕,供直播间使用。

  9. Jack

    博主, 貌似挂了吧。

    DPlayer-node 发生了一些意外:
    MongoError: Topology was destroyed

    at nextFunction (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb-core/lib/cursor.js:599:27) at Cursor.next (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb-core/lib/cursor.js:810:3) at Cursor._next (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/cursor.js:202:36) at fetchDocs (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/cursor.js:897:10) at toArray (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/cursor.js:927:3) at executeOperation (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/utils.js:420:24) at Cursor.toArray (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/cursor.js:883:10) at /www/wwwroot/dplayer.moerats.com/node_modules/mquery/lib/collection/node.js:30:14 at handleCallback (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/utils.js:128:55) at Collection.find (/www/wwwroot/dplayer.moerats.com/node_modules/mongodb/lib/collection.js:373:12) at NativeCollection. [as find] (/www/wwwroot/dplayer.moerats.com/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:142:28) at NodeCollection.find (/www/wwwroot/dplayer.moerats.com/node_modules/mquery/lib/collection/node.js:26:19) at model.Query.Query._find (/www/wwwroot/dplayer.moerats.com/node_modules/mongoose/lib/query.js:1392:20) at /www/wwwroot/dplayer.moerats.com/node_modules/kareem/index.js:333:33 at processTicksAndRejections (internal/process/task_queues.js:76:11)
    1. Rat's
      @Jack

      好像数据库挂了,你再试试

      1. 福利社
        @Rat's

        老板,接口又挂了~

        1. 福利社
          @福利社

          可以了,应该调用的人太多了

          1. Rat's
            @福利社

            告诉你一个秘密,弹幕数据库都好多G了,每秒高峰期几百个请求

  10. 铭心

    博主上面的html代码写进文章里视频界面右边会出现大黑边,但是我把代码单独写一个html页面却并不会出现,这是什么原因啊,我的blog是emlog的

    1. Rat's
      @铭心

      不清楚,em没咋玩过