This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
blog/_posts/2022-10-16-koel.md
2023-06-03 15:58:09 +08:00

193 lines
8.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
layout : post
title : "Koel:一种叫不停的鸟儿"
subtitle : "Just cannot stop"
date : 2022-10-16 12:30:23
author : "Manford Fan"
catalog : false
header-img : "img/post-bg-universe.jpg"
tags :
- Music
- Server
- Koel
---
其实一直都在探寻自建音乐库方案,目前实现形式有很多了,从最开始的[mStream](https://blog.rustle.cc/2022/05/15/mstream/),到后来的通过[cloudreve](https://blog.rustle.cc/2022/06/12/cloudreve/)的webdav挂载到Fireball等APP最近测试的[Navidrome](https://blog.rustle.cc/2022/10/02/navidrome/),以及本篇的主题[Koel](https://blog.rustle.cc/2022/10/16/koel/),感觉差不多可以给这个音乐库主题画上一个句号了。[Navidrome](https://www.navidrome.org/)做的已经相当好了,如果不考虑颜值,我更愿意用它,因为相比于[Koel](https://koel.dev/)前者配置更简单更易用但是显然Koel的颜值抵消了它配置复杂性带来的困扰以至于我还是把它搞定了。
## 一、Koel简介
如下是Koel的开发者描述这款应用的来源很有意思再一次证明了生产力来源于需求。
![koel_intro](/img/posts/koel_intro.png 'koel_intro')
## 二、配置安装
这一部分当然是主要参考网站主页https://docs.koel.dev/这里详尽的罗列了安装Koel的三种方法。安装之前需要一些必要条件和依赖这里就不罗列了可以参考网站主页其实就是phpopensslmysql or mariaDB如果是源码安装的话还需要nodejsgitcomposer等等的一些东西。我自己用的是第一种因为对于我而言这种方式升级方便容易维护菜是原罪.gif
#### 1. 使用预编译文件
先到[release](https://github.com/koel/koel/releases)页面下载最新的文件将文件解压放到你想安装的目录然后执行如下命令安装好之后第一件事是修改密码详见第三部分调优测试中的第四点——有用的命令行工具还有就是koel目录的所有者需要设定成`www-data`
```bash
php artisan koel:init --no-assets # Populate necessary configurations during the process
php artisan serve
```
![koel_install](/img/posts/koel_install.png 'koel_install')
#### 2. 使用源码编译
我曾尝试使用这种方法来编译安装但是对php不是很熟悉期间出了很多报错所以就作罢如下是官方的安装命令
```bash
cd <KOEL_ROOT_DIR>
git clone https://github.com/koel/koel.git .
git checkout latest # Check out the latest version at https://github.com/koel/koel/releases
composer install
php artisan koel:init # Populate necessary configurations during the process
php artisan serve
```
#### 3. 使用docker安装部署
之前也有提到过,我比较喜欢源码安装,再不行就预编译好的,不太喜欢封装(自洽的解释是,对我不透明,我不喜欢 ——笑说的好像我看得懂源码似的但却是源码安装的可配置性更强暂时不想研究docker怎么玩仅仅是mark一下最后放一张安装好可以服务的web页面吧~
Koel has an official Docker image: [koel/docker](https://github.com/koel/docker)
![koel_show](/img/posts/koel_show.png 'koel_show')
## 三、调优测试
#### 1. 调整php.ini文件相关参数
如果之前有用过php相关的应用应该是已经修改了的可以在`/ect/php`目录下搜索该文件,基本上是`/etc/php/8.0/cli/php.ini``/etc/php/8.0/fpm/php.ini`这两个文件,修改如下:
```php
memory_limit = 1024M
post_max_size = 4096M
upload_max_filesize = 4096M
max_execution_time = 3600;
max_input_time = 3600;
```
#### 2. 配置文件`.env`
修改Koel根目录下的`.env`文件这个文件很重要包含了所有的配置选项当出现在web ui上修改了媒体文件存储路径的时候就可以在这个文件中修改如下是比较重要的一些配置项是一定要设置且一定要设置对的尤其是数据库相关`SCOUT_DRIVER`以及`FFMPEG_PATH`
```php
DB_CONNECTION=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
MEDIA_PATH=/opt/media/music
APP_MAX_SCAN_TIME=600
MEMORY_LIMIT=
STREAMING_METHOD=php
SCOUT_DRIVER=database
FFMPEG_PATH=/usr/bin/ffmpeg
```
#### 3. 监控媒体文件的变化
虽然官方提供了使用[`inotify`工具](https://docs.koel.dev/watch.html)监控媒体文件夹内容变动的方法,个人感觉不是那么好用,所以自己写了一个监控脚本,搭配`cron`服务完美实现当有媒体文件发生变更仅包括数量的增减以及文件名的变化当然也可以做md5值校验等更精细化的对比但是没必要1分钟内完成媒体文件扫描。
```bash
[[ ! -e /tmp/files_now ]] && touch /tmp/files_now
[[ ! -e /tmp/files_pre_60s ]] && touch /tmp/files_pre_60s
success_flg=1
ols -aR | grep -E "*.(mp3|flac|opus|aac|ogg|m4a)" | sort
ls -aR /opt/media/music/ | grep -E "*.(mp3|flac|opus|aac|ogg|m4a)" | sort > /tmp/files_now
diff /tmp/files_now /tmp/files_pre_60s >> now_pre.diff
if [[ $? -ne 0 ]]; then
chown -R www-data:www-data /opt/meida/music
for i in `seq 20`; do
php /opt/source-code/koel/artisan koel:sync > /dev/null
if [[ $? -eq 0 ]]; then
php /opt/source-code/koel/artisan koel:sync >> /tmp/now_pre.diff
success_flg=0
break
fi
sleep 2
done
[[ success_flg -eq 1 ]] && echo 'Failed scanning the media dir, need processing that by hand.' >> /tmp/now_pre.diff
echo -e "Happening @ $(date)\n" >> /tmp/now_pre.diff
fi
cp /tmp/files_now /tmp/files_pre_60s
## crontab内容
* * * * * /usr/bin/bash /path/to/scripts/koel_update.sh
```
#### 4. 有用的命令行工具
部署好所有服务之后,页面上的功能基本已经够用,但是正如上面第三点的需求,在系统下,还是需要命令的帮助,可以使用`php artisan list`来获取所有可用的命令,其中如下命令是经常用到的:
```bash
list List commands
serve Serve the application on the PHP development server
koel:admin:change-password Change a user's password
koel:init Install or upgrade Koel
koel:prune Remove empty artists and albums
koel:sync Sync songs found in configured directory against the database.
# 使用方法:
php artisan + 如上命令
```
#### 5. 修改用户头像
因为程序服务是国外的程序员开发的,头像他好像是使用了一个在线的网站服务,可以随机的分配一个头像,但是这个网站国内无法访问,只能自己动手,把相关的链接替换成一个可以访问的地址。文件是`/app/Models/User.php`
```text
替换 fn () => sprintf('https://www.gravatar.com/avatar/%s?s=192&d=robohash', md5($this->email))
为 fn () => sprintf('https://rustle.cc/assets/img/logo.jpg', md5($this->email))
```
#### 6. manifst.json 404问题
出于职业习惯,每当部署好一个服务之后,我都会习惯性的看下所有情况下访问网页的状态码,发现服务找不到这个文件。去根[public]目录下一番探索之后返现了该文件的模板修改好之后就OK了。
#### 7. nginx 配置
安装包的根目录下提供了一个nginx的示例配置文件修改一下就能用如果需要支持flac文件格式的音频还需要额外配置一下响应头部。
```nginx
# 仅是配置支持flac文件的部分其余部分参考源码
location /media/ {
internal;
# alias $upstream_http_x_media_root;
alias /opt/media/music;
if ($request_filename ~ "^.*\/(.+\.(flac))$"){
set $fname $1;
add_header Content-Disposition 'inline; filename="$fname"';
add_header Content-Type 'audio/mpeg';
}
access_log logs/koel.access.log main;
#error_log /var/log/nginx/koel.error.log;
}
```
## 四、遗留问题 || 需要改进
关于待改进以及目前的bug最好的地方是去看GitHub上的[Issue](https://github.com/koel/koel/issues)基本上能遇到的都能在那里找到不想当吸血鬼但是还是期望开发者能修目前存在的各种bug。
1. 移动端支持的不好需要一个免费的APP
2. 移动网页端播放体验很不好
3. 歌词不能滚动跟随滚动
4. 缺少在线削刮器
5. 没有定时关闭功能
6. 下一首歌曲预取不完整的替代方案设置mp3文件缓存 -- 发现有预取...
7. Content-disposition响应头内容如果是中文会乱码开发者也没有意向修改...
## 五、参考文档
- [Koel官方主页](https://koel.dev/)
- [Keol Github](https://github.com/koel/koel)