feat: add existing library lyric backfill script

This commit is contained in:
2026-07-20 20:32:28 +08:00
parent 0ba6297596
commit f5033c7020
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
set -euo pipefail
LIBRARY_DIR="${1:-/vol3/1000/音视频/MusicWork/Library}"
BASE_URL="${MANGTOOL_LYRICS_BASE_URL:-http://192.168.10.159:28883}"
AUTH="${MANGTOOL_LYRICS_AUTH:-}"
[[ -d "$LIBRARY_DIR" ]] || { echo "Library 不存在: $LIBRARY_DIR" >&2; exit 1; }
export BASE_URL AUTH
find "$LIBRARY_DIR" -type f \( -iname '*.mp3' -o -iname '*.flac' -o -iname '*.m4a' -o -iname '*.aac' -o -iname '*.ogg' -o -iname '*.opus' -o -iname '*.wma' \) -print0 |
while IFS= read -r -d '' audio; do
lrc="${audio%.*}.lrc"; [[ -s "$lrc" ]] && { echo "SKIP $audio"; continue; }
meta=$(ffprobe -v error -show_entries format_tags=title,artist -of json "$audio" 2>/dev/null || true)
result=$(AUDIO_META="$meta" python3 - <<'PY'
import json,os,sys,urllib.parse,urllib.request
m=json.loads(os.environ.get('AUDIO_META','{}')).get('format',{}).get('tags',{}); t=m.get('title','').strip(); a=m.get('artist','').strip()
if not t or not a: sys.exit(0)
req=urllib.request.Request(os.environ['BASE_URL'].rstrip('/')+'/lrc?'+urllib.parse.urlencode({'title':t,'artist':a}))
if os.environ.get('AUTH'): req.add_header('Authorization',os.environ['AUTH'])
try:
with urllib.request.urlopen(req,timeout=15) as r: rows=json.load(r)
except Exception: sys.exit(0)
def n(x): return ''.join(c.lower() for c in x if c.isalnum())
best=None
for x in rows if isinstance(rows,list) else []:
l=x.get('lyrics','').strip(); ct=x.get('title',''); ca=x.get('artist','')
if not l: continue
s=(45 if n(t)==n(ct) else 25 if n(t) in n(ct) or n(ct) in n(t) else 0)+(45 if n(a)==n(ca) else 20 if n(a) in n(ca) or n(ca) in n(a) else -40)
s+=10 if '[' in l and ':' in l else 0
if s>=60 and (best is None or s>best[0]): best=(s,l)
if best: sys.stdout.write(best[1])
PY
)
[[ -n "$result" ]] || { echo "MISS $audio"; continue; }
printf '%s\n' "$result" > "${lrc}.tmp" && mv -f "${lrc}.tmp" "$lrc"; echo "OK $lrc"
done