0%

使用树莓派搭建音乐流媒体服务器

想用上闲置的树莓派利用200M的上行带宽整点东西,记录一下。(draft)

树莓派配置

系统配置

  1. 刷 Ubuntu Server 22.04 镜像。

    刷22版本主要是考虑到20的生命周期不剩太多了。注意可以通过imager配置ssh连入的authorized_key

  2. 在路由器内查设备ip,ssh上之后通过sudo passwd root修改root口令。

先整体更新一下,少点事

1
2
3
sudo apt update
sudo apt upgrade
sudo reboot

webmin 配置

  1. curl -o setup-repos.sh https://ghproxy.com/https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
  2. sudo sh setup-repos.sh
  3. sudo apt-get update
  4. sudo apt-get install --install-recommends webmin
  5. 访问 https://xxxx:10000

webmin的主要目的是监视流量,对服务可用性有基本的了解。

参考 https://www.navidrome.org/docs/installation/linux/

upgrade的时候可能要重启

sudo apt update
sudo apt upgrade
sudo apt install vim ffmpeg

配置组。我们使用专门的组来配置服务

sudo groupadd navidrome
sudo useradd -m -g navidrome navidrome
sudo install -d -o navidrome -g navidrome /opt/navidrome
sudo install -d -o navidrome -g navidrome /var/lib/navidrome

wget https://github.com/navidrome/navidrome/releases/download/v0.49.3/navidrome_0.49.3_Linux_arm64.tar.gz -O Navidrome.tar.gz
sudo tar -xvzf Navidrome.tar.gz -C /opt/navidrome/
sudo chown -R navidrome:navidrome /opt/navidrome
sudo -u navidrome -s

挂载移动硬盘

sudo fdisk -l
sudo mkdir /mnt/storage
sudo mount /dev/sdb1 /mnt/storage

Create Configuration File:

sudo vi /var/lib/navidrome/navidrome.toml

注意这个引号很重要

1
MusicFolder="/mnt/storage/music"

sudo vi /etc/systemd/system/navidrome.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[Unit]
Description=Navidrome Music Server and Streamer compatible with Subsonic/Airsonic
After=remote-fs.target network.target
AssertPathExists=/var/lib/navidrome

[Install]
WantedBy=multi-user.target

[Service]
User=navidrome
Group=navidrome
Type=simple
ExecStart=/opt/navidrome/navidrome --configfile "/var/lib/navidrome/navidrome.toml"
WorkingDirectory=/var/lib/navidrome
TimeoutStopSec=20
KillMode=process
Restart=on-failure

# See https://www.freedesktop.org/software/systemd/man/systemd.exec.html
DevicePolicy=closed
NoNewPrivileges=yes
PrivateTmp=yes
PrivateUsers=yes
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
SystemCallFilter=~@clock @debug @module @mount @obsolete @reboot @setuid @swap
ReadWritePaths=/var/lib/navidrome

# You can uncomment the following line if you're not using the jukebox This
# will prevent navidrome from accessing any real (physical) devices
#PrivateDevices=yes

# You can change the following line to `strict` instead of `full` if you don't
# want navidrome to be able to write anything on your filesystem outside of
# /var/lib/navidrome.
ProtectSystem=full

# You can uncomment the following line if you don't have any media in /home/*.
# This will prevent navidrome from ever reading/writing anything there.
#ProtectHome=true

# You can customize some Navidrome config options by setting environment variables here. Ex:
#Environment=ND_BASEURL="/navidrome"
1
2
3
4
sudo systemctl daemon-reload
sudo systemctl start navidrome.service
sudo systemctl status navidrome.service
sudo systemctl enable navidrome.service

nginx 配置

安装
https://nginx.org/en/linux_packages.html#Ubuntu

vi /etc/nginx/nginx.conf

server {
listen 765 ssl;
server_name www.imas-music.top;

ssl_certificate /etc/letsencrypt/live/imas-music.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/imas-music.top/privkey.pem;

location / {
    proxy_pass http://localhost:4533;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

一会儿reload

let’s encrypt 配置

  1. 安装snap。snap为默认安装项,不过可以通过以下命令确认
1
2
3
4
sudo snap install hello-world
hello-world 6.4 from Canonical✓ installed
$ hello-world
Hello World!
  1. 配置certbot&aliyun插件

sudo snap install –classic certbot

git clone https://github.com/tengattack/certbot-dns-aliyun
cd certbot-dns-aliyun
sudo python3 setup.py install
cd ..

https://ram.console.aliyun.com/users

1
2
dns_aliyun_access_key = 12345678
dns_aliyun_access_key_secret = 1234567890abcdef1234567890abcdef

chmod 600 ./aliyun_credentials.ini

sudo certbot certonly –authenticator=dns-aliyun –dns-aliyun-credentials=’/home/darwin/aliyun_credentials.ini’ -d “*.imas-music.top,imas-music.top”

sudo crontab -e

1
0 0 * * * certbot renew --quiet

树莓派配置内网静态ip

通过路由器配置

ddns

docker 安装 https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

https://github.com/sanjusss/aliyun-ddns

1
2
3
4
5
6
7
8
9
docker run -d --restart=always --net=host \
-e "AKID=[ALIYUN's AccessKey-ID]" \
-e "AKSCT=[ALIYUN's AccessKey-Secret]" \
-e "DOMAIN=ddns.aliyun.win" \
-e "REDO=30" \
-e "TTL=600" \
-e "TIMEZONE=8.0" \
-e "TYPE=A,AAAA" \
sanjusss/aliyun-ddns

dns

docker run -d –restart=always –net=host
-e “AKID=LTAI5tBBzeLfS2HtXVgzcWj2”
-e “AKSCT=ktwlEWwcFgntTLu08kbH4rr6RhxYl6”
-e “DOMAIN=www.imas-music.top"
-e “REDO=30”
-e “TTL=600”
-e “TIMEZONE=8.0”
-e “TYPE=A,AAAA”
sanjusss/aliyun-ddns

增加CNAME记录

@ - CNAME - www.xxxx.top

手动预转码

选择256k的aac

#!/bin/bash

Source directory

SRC_DIR=”SHINY COLORS”

Destination directory

DEST_DIR=”aac”

Function to convert various audio formats to AAC

convert_to_aac() {
local src=”$1”
local dest=”${DEST_DIR}${src#$SRC_DIR}”
local extension=”${src##*.}”
dest=”${dest%.$extension}.m4a”

Create destination directory

mkdir -p “$(dirname “$dest”)”

Convert to AAC

ffmpeg -hwaccel cuvid -i “$src” -c:v copy -c:a aac -b:a 256k “$dest”
}

Export function and destination directory for parallel processing

export -f convert_to_aac
export DEST_DIR

Find all supported audio files in source directory and convert them in parallel

1
find "$SRC_DIR" -type f \( -name "*.flac" -o -name "*.wav" -o -name "*.wv" -o -name "*.aiff" -o -name "*.m4a" \) -print0 | xargs -0 -n 1 -P 8 -I {} bash -c 'convert_to_aac "$@"' _ {}

ftp 设置

sudo apt-get install vsftpd

sudo umount -l /mnt/storage

sudo mount -o iocharset=utf8 /dev/sda1 /mnt/storage