blob: 1be09399a8d2351b6457c82e3ffbbccedd8d9959 (
plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/bin/sh
# qndl-artist — 아티스트 폴더/메타데이터 통일 헬퍼
export LC_ALL="${LC_ALL:-C.UTF-8}"
MUSIC="${XDG_MUSIC_DIR:-$HOME/Music}"
ALIASES="${QNDL_ALIASES:-${XDG_DOTFILES_DIR:-$HOME/.dotfiles}/global/Music/artist-aliases.tsv}"
# 이름 → 표준명. 맵 정확일치 → 기존 폴더 대소문자무시 매칭 → 원본.
cmd_normalize() {
_name="$1"
if [ -f "$ALIASES" ]; then
_c="$(awk -F'\t' -v n="$_name" '/^#/ || NF < 2 { next } $1 == n { print $2; exit }' "$ALIASES")"
[ -n "$_c" ] && { printf '%s\n' "$_c"; return 0; }
fi
if [ -d "$MUSIC" ]; then
_m="$(find "$MUSIC" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null |
awk -v n="$_name" 'tolower($0) == tolower(n) { print; exit }')"
[ -n "$_m" ] && { printf '%s\n' "$_m"; return 0; }
fi
printf '%s\n' "$_name"
}
# mp3 1개를 표준 폴더로 이동(필요시) + album_artist 설정.
cmd_apply() {
_fp="$1"; _canon="$2"
[ -f "$_fp" ] || { printf 'apply: no such file: %s\n' "$_fp" >&2; return 1; }
case "$_fp" in
"$MUSIC"/*) : ;;
*) printf 'apply: not under %s: %s\n' "$MUSIC" "$_fp" >&2; return 1 ;;
esac
_rel="${_fp#"$MUSIC"/}"
_artist_seg="${_rel%%/*}"
_subpath="${_rel#*/}" # album/.../title.mp3
if [ "$_artist_seg" != "$_canon" ]; then
_destdir="$MUSIC/$_canon/$(dirname "$_subpath")"
_dest="$_destdir/$(basename "$_fp")"
if [ -e "$_dest" ]; then
printf 'apply: skip (exists): %s\n' "$_dest" >&2
return 0
fi
mkdir -p "$_destdir"
mv "$_fp" "$_dest" || return 1
# 빈 원본 앨범/아티스트 폴더 정리 (MUSIC 루트는 지우지 않음)
rmdir "$MUSIC/$_artist_seg/$(dirname "$_subpath")" 2>/dev/null || true
rmdir "$MUSIC/$_artist_seg" 2>/dev/null || true
_fp="$_dest"
fi
_tmp="$(dirname "$_fp")/.qndl-tag-$$.mp3"
if ffmpeg -v error -y -i "$_fp" -map 0 -c copy -metadata album_artist="$_canon" "$_tmp" 2>/dev/null; then
mv "$_tmp" "$_fp" || { rm -f "$_tmp"; return 1; }
else
rm -f "$_tmp"
printf 'apply: tag failed: %s\n' "$_fp" >&2
return 1
fi
}
_sub="${1:-}"
[ $# -gt 0 ] && shift
case "$_sub" in
normalize) cmd_normalize "$@" ;;
apply) cmd_apply "$@" ;;
*) printf 'usage: qndl-artist {normalize|apply|apply-download|merge} ...\n' >&2; exit 2 ;;
esac
|