经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
如何搭建一个自己的音乐服务器
来源:cnblogs  作者:程序猿阿朗  时间:2022/12/5 9:12:06  对本文有异议

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

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

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

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

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

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

树莓派启动

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

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

安装 Docker

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

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

编辑文件:

  1. sudo vim /etc/apt/sources.list

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

  1. deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
  2. deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
  3. deb https://mirrors.aliyun.com/debian-security/ bullseye-security main
  4. deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main
  5. deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
  6. deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
  7. deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
  8. deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

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

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

安装过程:

  1. linux@darcy:~ $ curl -sSL https://get.daocloud.io/docker | sh
  2. # Executing docker install script, commit: 4f282167c425347a931ccfd95cc91fab041d414f
  3. + sudo -E sh -c apt-get update -qq >/dev/null
  4. + sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
  5. + sudo -E sh -c mkdir -p /etc/apt/keyrings && chmod -R 0755 /etc/apt/keyrings
  6. + sudo -E sh -c curl -fsSL "https://download.docker.com/linux/debian/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
  7. + sudo -E sh -c chmod a+r /etc/apt/keyrings/docker.gpg
  8. + 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
  9. + sudo -E sh -c apt-get update -qq >/dev/null
  10. + 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
  11. + version_gte 20.10
  12. + [ -z ]
  13. + return 0
  14. + sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null
  15. + sudo -E sh -c docker version
  16. Client: Docker Engine - Community
  17. Version: 20.10.21
  18. API version: 1.41
  19. Go version: go1.18.7
  20. Git commit: baeda1f
  21. Built: Tue Oct 25 18:01:19 2022
  22. OS/Arch: linux/arm64
  23. Context: default
  24. Experimental: true
  25. Server: Docker Engine - Community
  26. Engine:
  27. Version: 20.10.21
  28. API version: 1.41 (minimum version 1.12)
  29. Go version: go1.18.7
  30. Git commit: 3056208
  31. Built: Tue Oct 25 17:59:41 2022
  32. OS/Arch: linux/arm64
  33. Experimental: false
  34. containerd:
  35. Version: 1.6.10
  36. GitCommit: 770bd0108c32f3fb5c73ae1264f7e503fe7b2661
  37. runc:
  38. Version: 1.1.4
  39. GitCommit: v1.1.4-0-g5fd4c4d
  40. docker-init:
  41. Version: 0.19.0
  42. GitCommit: de40ad0
  43. ================================================================================
  44. To run Docker as a non-privileged user, consider setting up the
  45. Docker daemon in rootless mode for your user:
  46. dockerd-rootless-setuptool.sh install
  47. Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.
  48. To run the Docker daemon as a fully privileged service, but granting non-root
  49. users access, refer to https://docs.docker.com/go/daemon-access/
  50. WARNING: Access to the remote API on a privileged Docker daemon is equivalent
  51. to root access on the host. Refer to the 'Docker daemon attack surface'
  52. documentation for details: https://docs.docker.com/go/attack-surface/
  53. ================================================================================
  54. darcy@darcy:~ $

安装 docker-compose.

  1. linux@darcy:~/music $ sudo apt install docker-compose
  2. Reading package lists... Done
  3. Building dependency tree... Done
  4. Reading state information... Done
  5. The following packages were automatically installed and are no longer required:
  6. libslirp0 slirp4netns
  7. Use 'sudo apt autoremove' to remove them.
  8. The following additional packages will be installed:
  9. 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
  10. 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
  11. python3-setuptools python3-texttable python3-websocket python3-yaml python3-zipp runc tini
  12. Suggested packages:
  13. 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
  14. 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
  15. Recommended packages:
  16. criu
  17. The following packages will be REMOVED:
  18. containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras
  19. The following NEW packages will be installed:
  20. 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
  21. libterm-readkey-perl needrestart python3-attr python3-cached-property python3-distutils python3-docker python3-dockerpty python3-docopt python3-importlib-metadata python3-jsonschema python3-lib2to3
  22. python3-more-itertools python3-pyrsistent python3-setuptools python3-texttable python3-websocket python3-yaml python3-zipp runc tini
  23. 0 upgraded, 34 newly installed, 4 to remove and 39 not upgraded.
  24. Need to get 55.9 MB of archives.
  25. After this operation, 45.4 MB disk space will be freed.
  26. Do you want to continue? [Y/n] y
  27. Get:1 https://mirrors.aliyun.com/debian bullseye/main arm64 runc arm64 1.0.0~rc93+ds1-5+deb11u2 [2,078 kB]
  28. Get:2 https://mirrors.aliyun.com/debian bullseye/main arm64 containerd arm64 1.4.13~ds1-1~deb11u2 [14.7 MB]
  29. Get:3 https://mirrors.aliyun.com/debian bullseye/main arm64 tini arm64 0.19.0-1 [209 kB]
  30. Get:4 https://mirrors.aliyun.com/debian bullseye/main arm64 docker.io arm64 20.10.5+dfsg1-1+deb11u2 [28.8 MB]
  31. Get:5 https://mirrors.aliyun.com/debian bullseye/main arm64 apparmor arm64 2.13.6-10 [601 kB]
  32. Get:6 https://mirrors.aliyun.com/debian bullseye/main arm64 cgroupfs-mount all 1.4 [6,276 B]
  33. Get:7 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-cached-property all 1.5.2-1 [12.5 kB]
  34. Get:8 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-lib2to3 all 3.9.2-1 [77.8 kB]
  35. Get:9 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-distutils all 3.9.2-1 [143 kB]
  36. Get:10 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-websocket all 0.57.0-1 [34.3 kB]
  37. Get:11 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-docker all 4.1.0-1.2 [85.2 kB]
  38. Get:12 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-dockerpty all 0.4.1-2 [11.1 kB]
  39. Get:13 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-docopt all 0.6.2-3 [26.6 kB]
  40. Get:14 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-attr all 20.3.0-1 [52.9 kB]
  41. Get:15 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-more-itertools all 4.2.0-3 [42.7 kB]
  42. Get:16 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-zipp all 1.0.0-3 [6,060 B]
  43. Get:17 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-importlib-metadata all 1.6.0-2 [10.3 kB]
  44. Get:18 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-setuptools all 52.0.0-4 [366 kB]
  45. Get:19 https://mirrors.aliyun.com/debian bullseye/main arm64 python3-pyrsistent arm64 0.15.5-1+b3 [58

启动 docker

  1. # 查看docker服务是否启动
  2. sudo systemctl status docker
  3. # 如果没有启动
  4. sudo systemctl start docker

安装音乐服务器

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

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

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

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

  1. version: "3"
  2. services:
  3. navidrome:
  4. image: deluan/navidrome:latest
  5. user: 1000:1000 # should be owner of volumes
  6. ports:
  7. - "4533:4533"
  8. restart: unless-stopped
  9. environment:
  10. # Optional: put your config options customization here. Examples:
  11. ND_SCANSCHEDULE: 1h
  12. ND_LOGLEVEL: info
  13. ND_SESSIONTIMEOUT: 24h
  14. ND_BASEURL: ""
  15. volumes:
  16. - "~/app/music/data:/data"
  17. - "~/app/music/folder:/music:ro"

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

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

音乐上传

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

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

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

启动音乐服务器

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

  1. linux@darcy:~ $ sudo docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 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 浏览器:

移动浏览器:

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

使用 APP 访问

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

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

歌曲界面:

外网访问

如果已经是使用云服务器进行部署,那么因为有相应的公网 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。

原文链接:https://www.cnblogs.com/niumoo/p/16951437.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号