1

如何搭建一个自己的音乐服务器

 1 year ago
source link: https://www.cnblogs.com/niumoo/p/16951437.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

点赞再看,动力无限。 微信搜「 程序猿阿朗 」。

本文 Github.com/niumoo/JavaNotes未读代码博客 已经收录,有很多知识点和系列文章。

最近发现,经常用的网易云音乐,有很多歌曲下架了,能听的越来越少了;歌单里的一些歌曲,现在要开通 VIP 才能听了。其实自己常听的歌曲不是很多,现在却有很多听不了了。

1202902-20221205085930490-1410977353.png

怎么办呢,付费吗?花钱当然是一个好方式,花 1 分钟开通 VIP,立马就可以畅听起来。

不过前两天翻东西时刚好发现自己还有一个吃灰多年的大学时代的树莓派,不如废物利用起来,使用树莓派搭建一个自己的音乐服务器用来听歌吧。

1202902-20221205090527528-1370911565.png

下面的文章是使用树莓派搭建音乐服务器,如果你手上没有树莓派,那么使用任何一个云服务器都是一样的,甚至因为云服务器具备公网 IP ,使用场景更加广泛。

以上只是引言,作为技术人员,本文所做介绍只为分享一些有趣好玩的东西,请不要传播具有版权的音乐。

树莓派启动

说干就干,树莓派通电。结果太久不用,密码忘了,只能为树莓派重新烧录系统。我这里选择了 Raspberry Pi OS 轻量无桌面 Linux 系统,可以减少资源的占用。因为后面的服务都是通过 Docker 安装,其实这里选择什么发行版的 Linux 区别并不大。

1202902-20221205090552126-1982379440.png

链接:Raspberry Pi OS 64 位系统下载

安装 Docker

安装 Docker 过程网上教程很多,下面只简单记录。

Ubuntu 系统为了安装软件速度更快,可以先更换软件源为国内软件源。

编辑文件:

sudo vim /etc/apt/sources.list

使用下面的阿里云软件源配置进行内容替换。

deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

安装 Dokcer,直接使用一句话脚本安装。

curl -sSL https://get.daocloud.io/docker | sh

安装过程:

linux@darcy:~ $ curl -sSL https://get.daocloud.io/docker | sh
# Executing docker install script, commit: 4f282167c425347a931ccfd95cc91fab041d414f
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sudo -E sh -c mkdir -p /etc/apt/keyrings && chmod -R 0755 /etc/apt/keyrings
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/debian/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
+ sudo -E sh -c chmod a+r /etc/apt/keyrings/docker.gpg
+ sudo -E sh -c echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bullseye stable" > /etc/apt/sources.list.d/docker.list
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null
+ version_gte 20.10
+ [ -z  ]
+ return 0
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null
+ sudo -E sh -c docker version
Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:01:19 2022
 OS/Arch:           linux/arm64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 17:59:41 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.10
  GitCommit:        770bd0108c32f3fb5c73ae1264f7e503fe7b2661
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

    dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================

darcy@darcy:~ $

安装 docker-compose.

linux@darcy:~/music $ sudo apt install docker-compose
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libslirp0 slirp4netns
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  apparmor cgroupfs-mount containerd docker.io git git-man liberror-perl libintl-perl libintl-xs-perl libmodule-find-perl libmodule-scandeps-perl libproc-processtable-perl libsort-naturally-perl libterm-readkey-perl
  needrestart python3-attr python3-cached-property python3-distutils python3-docker python3-dockerpty python3-docopt python3-importlib-metadata python3-jsonschema python3-lib2to3 python3-more-itertools python3-pyrsistent
  python3-setuptools python3-texttable python3-websocket python3-yaml python3-zipp runc tini
Suggested packages:
  apparmor-profiles-extra apparmor-utils containernetworking-plugins docker-doc aufs-tools btrfs-progs debootstrap rinse rootlesskit xfsprogs zfs-fuse | zfsutils-linux git-daemon-run | git-daemon-sysvinit git-doc git-el
  git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn needrestart-session | libnotify-bin iucode-tool python-attr-doc python-jsonschema-doc python-setuptools-doc
Recommended packages:
  criu
The following packages will be REMOVED:
  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras
The following NEW packages will be installed:
  apparmor cgroupfs-mount containerd docker-compose docker.io git git-man liberror-perl libintl-perl libintl-xs-perl libmodule-find-perl libmodule-scandeps-perl libproc-processtable-perl libsort-naturally-perl
  libterm-readkey-perl needrestart python3-attr python3-cached-property python3-distutils python3-docker python3-dockerpty python3-docopt python3-importlib-metadata python3-jsonschema python3-lib2to3
  python3-more-itertools python3-pyrsistent python3-setuptools python3-texttable python3-websocket python3-yaml python3-zipp runc tini
0 upgraded, 34 newly installed, 4 to remove and 39 not upgraded.
Need to get 55.9 MB of archives.
After this operation, 45.4 MB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 https://mirrors.aliyun.com/debian bullseye/main arm64 runc arm64 1.0.0~rc93+ds1-5+deb11u2 [2,078 kB]
Get:2 https://mirrors.aliyun.com/debian bullseye/main arm64 containerd arm64 1.4.13~ds1-1~deb11u2 [14.7 MB]
Get:3 https://mirrors.aliyun.com/debian bullseye/main arm64 tini arm64 0.19.0-1 [209 kB]
Get:4 https://mirrors.aliyun.com/debian bullseye/main arm64 docker.io arm64 20.10.5+dfsg1-1+deb11u2 [28.8 MB]
Get:5 https://mirrors.aliyun.com/debian bullseye/main arm64 apparmor arm64 2.13.6-10 [601 kB]
Get:6 https://mirrors.aliyun.com/debian bullseye/main arm64 cgroupfs-mount all 1.4 [6,276 B]
Get:7 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-cached-property all 1.5.2-1 [12.5 kB]
Get:8 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-lib2to3 all 3.9.2-1 [77.8 kB]
Get:9 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-distutils all 3.9.2-1 [143 kB]
Get:10 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-websocket all 0.57.0-1 [34.3 kB]
Get:11 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-docker all 4.1.0-1.2 [85.2 kB]
Get:12 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-dockerpty all 0.4.1-2 [11.1 kB]
Get:13 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-docopt all 0.6.2-3 [26.6 kB]
Get:14 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-attr all 20.3.0-1 [52.9 kB]
Get:15 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-more-itertools all 4.2.0-3 [42.7 kB]
Get:16 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-zipp all 1.0.0-3 [6,060 B]
Get:17 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-importlib-metadata all 1.6.0-2 [10.3 kB]
Get:18 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-setuptools all 52.0.0-4 [366 kB]
Get:19 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-pyrsistent arm64 0.15.5-1+b3 [58

启动 docker

# 查看docker服务是否启动
sudo systemctl status docker
# 如果没有启动
sudo systemctl start docker

安装音乐服务器

网上搜索了很多音乐服务器,综合对比之后选择了 Navidrome,Navidrome是一款完全开源免费的音乐服务器,且有很多开源客户端可以直接使用,十分方便。

1202902-20221205090627792-235369162.png

官方也给出了如何使用 Docker 安装 Navidrome 的教程,可以点击查看。

链接:Docker 安装 Navidrome 音乐服务器

编写 docker-compose.yml 文件,配置中使用 volumes 指定了配置文件夹 data,歌曲存放文件夹 folder.

version: "3"
services:
  navidrome:
    image: deluan/navidrome:latest
    user: 1000:1000 # should be owner of volumes
    ports:
      - "4533:4533"
    restart: unless-stopped
    environment:
      # Optional: put your config options customization here. Examples:
      ND_SCANSCHEDULE: 1h
      ND_LOGLEVEL: info
      ND_SESSIONTIMEOUT: 24h
      ND_BASEURL: ""
    volumes:
      - "~/app/music/data:/data"
      - "~/app/music/folder:/music:ro"

下载 docker 镜像,等待 navidrome 镜像下载完成。

linux@darcy:~/music $ sudo docker-compose up -d
Creating network "music_default" with the default driver
Pulling navidrome (deluan/navidrome:latest)...
latest: Pulling from deluan/navidrome
9b18e9b68314: Extracting [==================================================>]  2.708MB/2.708MB
6faacaf0d83b: Downloading [=================>                                 ]  11.49MB/32.64MB
4f4fb700ef54: Download complete
e67430195958: Downloading [========>                                          ]  2.203MB/13.64MB

歌曲的来源这里不细说,我电脑上刚好有几首不知何年何月使用网易云音乐时保存的歌曲,上传歌曲文件到树莓派服务器。

这里直接使用 scp 命令进行上传,需要在歌曲所在文件夹下运行。

  • ./* 表示上传当前目录所有文件。
  • [email protected] 为服务器用户名和 IP.
  • :/home/darcy/music/folder 为上传到的路径。
➜  folder scp ./* [email protected]:/home/darcy/music/folder
[email protected]'s password:
GALA - 追梦赤子心.mp3                        100%   13MB   7.0MB/s   00:01
伍佰 - 晚风.mp3                              100% 4102KB   9.4MB/s   00:00
暗杠 - 狂草.mp3                              100% 9693KB   9.1MB/s   00:01
被动(Live).mp3                               100% 4463KB  10.4MB/s   00:00
刘德华 - 17岁 (Live).mp3                      100%   12MB  10.0MB/s   00:01
张雨生 - 我期待.mp3                            100%   14MB   6.8MB/s   00:02
赵英俊 - 方的言.ncm

启动音乐服务器

先使用 sudo docker ps -a 查看 Navidrome 是否已经启动了,如果没有启动可以使用 docker start id 进行启动。服务的默认端口是 4533

linux@darcy:~ $ sudo docker ps -a
CONTAINER ID   IMAGE                     COMMAND            CREATED       STATUS                 PORTS                    NAMES
97a8a2681179   deluan/navidrome:latest   "/app/navidrome"   2 weeks ago   Up 6 hours (healthy)   0.0.0.0:4533->4533/tcp   music_navidrome_1

浏览器访问

IP:PORT 访问音乐服务,Navidrome 为 PC 端和手机端已经适配了不同的 UI 界面。

PC 浏览器:

1202902-20221205090710462-1931602846.png

移动浏览器:

1202902-20221205090741392-1566761392.png

Navidrome 的管理功能尚可,点击右上角的头像,可以新增用户,可以为其他人创建一个登录账户。

使用 APP 访问

兼容 Navidrome 的 APP 有很多,这里我使用 Substreamer 这款 APP,界面算得上简洁,使用体验尚可。

1202902-20221205090811587-302902617.png

初次启动需要进行登录,可以使用刚刚新增的用户进行登录。

1202902-20221205090853184-1249893411.png

歌曲界面:

1202902-20221205090925176-955360772.png

如果已经是使用云服务器进行部署,那么因为有相应的公网 IP,其实已经可以直接使用公网 IP 访问了。

但是如果使用的树莓派或者家里的其他机器进行部署的,因为没有公网 IP ,只能局限在家庭局域网内,就会让公网访问十分困难。这时有下面的几个方案。

  1. 如果你的宽带运营商每次会分配公网 IP,刚好你又有一个域名,那么可以使用 DNS 服务商提供的 API 进行动态 DNS 配置,让域名总能解析到运营商分配给你的公网 IP。
  2. 如果你有公网服务器,可以使用类似于 FRP 这样的内网穿透工具,借助公网服务器暴露局域网服务。

FRP GitHub:https://github.com/fatedier/frp/releases

FRP 通过自定义域名访问内网的 Web 服务:https://gofrp.org/docs/examples/vhost-http/

<完>

文章持续更新,可以微信搜一搜「 程序猿阿朗 」或访问「程序猿阿朗博客 」第一时间阅读。本文 Github.com/niumoo/JavaNotes 已经收录,有很多知识点和系列文章,欢迎Star。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK