|
|
|
|
@ -4,7 +4,7 @@ export LC_ALL=C.UTF-8
|
|
|
|
|
# usage: import_music.sh [--move] [--timid|--skip-beets]
|
|
|
|
|
|
|
|
|
|
IMPORT_DIR="/mnt/media/slskd/downloads"
|
|
|
|
|
LIBRARY_DIR="/mnt/media/Music"
|
|
|
|
|
LIBRARY_DIR="/mnt/media/MusicTest"
|
|
|
|
|
|
|
|
|
|
# Parse flags
|
|
|
|
|
MOVE_FILES=false
|
|
|
|
|
@ -26,6 +26,17 @@ for arg in "$@"; do
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Build original metadata map
|
|
|
|
|
declare -A ORIGINAL_ARTIST_MAP
|
|
|
|
|
declare -A ORIGINAL_ALBUM_MAP
|
|
|
|
|
|
|
|
|
|
while IFS= read -r file; do
|
|
|
|
|
orig_artist=$(metaflac --show-tag=ALBUMARTIST "$file" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
orig_album=$(metaflac --show-tag=ALBUM "$file" | sed 's/^ALBUM=//')
|
|
|
|
|
ORIGINAL_ARTIST_MAP["$file"]="$orig_artist"
|
|
|
|
|
ORIGINAL_ALBUM_MAP["$file"]="$orig_album"
|
|
|
|
|
done < <(find "$IMPORT_DIR" -type f -name "*.flac")
|
|
|
|
|
|
|
|
|
|
# Optional beets import
|
|
|
|
|
case "$BEETS_FLAG" in
|
|
|
|
|
--timid)
|
|
|
|
|
@ -41,52 +52,27 @@ case "$BEETS_FLAG" in
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
# Add Replay Gain information to files
|
|
|
|
|
# Add Replay Gain information
|
|
|
|
|
echo "Adding Replay Gain information to files..."
|
|
|
|
|
find "$IMPORT_DIR" -type f -iname "*.flac" -exec dirname {} \; | sort -u | while read -r album_path; do
|
|
|
|
|
echo "Processing album for ReplayGain: $album_path"
|
|
|
|
|
metaflac --add-replay-gain "$album_path"/*.flac
|
|
|
|
|
echo "Processing album for ReplayGain: $album_path"
|
|
|
|
|
metaflac --add-replay-gain "$album_path"/*.flac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Function to detect non-Latin characters
|
|
|
|
|
contains_non_latin() {
|
|
|
|
|
echo "$1" | grep -P "[^\x00-\x7F]" > /dev/null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Function to find best match directory in LIBRARY_DIR containing a given substring
|
|
|
|
|
find_matching_dir() {
|
|
|
|
|
local search_root="$1"
|
|
|
|
|
local pattern="$2"
|
|
|
|
|
match=$(find "$search_root" -mindepth 1 -maxdepth 1 -type d -print | grep -iF "$pattern" | head -n 1)
|
|
|
|
|
|
|
|
|
|
if [[ -n "$match" ]]; then
|
|
|
|
|
basename "$match"
|
|
|
|
|
else
|
|
|
|
|
echo "$pattern"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Choose copy or move command
|
|
|
|
|
# Choose copy or move
|
|
|
|
|
COPY_CMD="cp"
|
|
|
|
|
$MOVE_FILES && COPY_CMD="mv"
|
|
|
|
|
|
|
|
|
|
# Process FLAC files
|
|
|
|
|
# === Process FLAC files ===
|
|
|
|
|
find "$IMPORT_DIR" -type f -name "*.flac" | while read -r file; do
|
|
|
|
|
artist=$(metaflac --show-tag=ALBUMARTIST "$file" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
album=$(metaflac --show-tag=ALBUM "$file" | sed 's/^ALBUM=//')
|
|
|
|
|
|
|
|
|
|
resolved_artist="$artist"
|
|
|
|
|
resolved_album="$album"
|
|
|
|
|
|
|
|
|
|
if contains_non_latin "$artist"; then
|
|
|
|
|
resolved_artist=$(find_matching_dir "$LIBRARY_DIR" "$artist")
|
|
|
|
|
fi
|
|
|
|
|
original_artist="${ORIGINAL_ARTIST_MAP["$file"]}"
|
|
|
|
|
original_album="${ORIGINAL_ALBUM_MAP["$file"]}"
|
|
|
|
|
|
|
|
|
|
artist_dir="$LIBRARY_DIR/$resolved_artist"
|
|
|
|
|
updated_artist=$(metaflac --show-tag=ALBUMARTIST "$file" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
updated_album=$(metaflac --show-tag=ALBUM "$file" | sed 's/^ALBUM=//')
|
|
|
|
|
|
|
|
|
|
if contains_non_latin "$album"; then
|
|
|
|
|
resolved_album=$(find_matching_dir "$artist_dir" "$album")
|
|
|
|
|
fi
|
|
|
|
|
[[ "$original_artist" != "$updated_artist" ]] && resolved_artist="$original_artist ($updated_artist)" || resolved_artist="$original_artist"
|
|
|
|
|
[[ "$original_album" != "$updated_album" ]] && resolved_album="$original_album ($updated_album)" || resolved_album="$original_album"
|
|
|
|
|
|
|
|
|
|
target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album"
|
|
|
|
|
mkdir -p "$target_dir"
|
|
|
|
|
@ -94,7 +80,7 @@ find "$IMPORT_DIR" -type f -name "*.flac" | while read -r file; do
|
|
|
|
|
echo "$COPY_CMD $file to $target_dir"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Also move/copy associated image files (cover art & artist images)
|
|
|
|
|
# === Process album cover images ===
|
|
|
|
|
find "$IMPORT_DIR" -type f \( -iname "*.jpg" -o -iname "*.png" \) | while read -r img_file; do
|
|
|
|
|
dir_path=$(dirname "$img_file")
|
|
|
|
|
first_flac=$(find "$dir_path" -maxdepth 1 -type f -iname "*.flac" | head -n 1)
|
|
|
|
|
@ -104,21 +90,14 @@ find "$IMPORT_DIR" -type f \( -iname "*.jpg" -o -iname "*.png" \) | while read -
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
artist=$(metaflac --show-tag=ALBUMARTIST "$first_flac" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
album=$(metaflac --show-tag=ALBUM "$first_flac" | sed 's/^ALBUM=//')
|
|
|
|
|
original_artist="${ORIGINAL_ARTIST_MAP["$first_flac"]}"
|
|
|
|
|
original_album="${ORIGINAL_ALBUM_MAP["$first_flac"]}"
|
|
|
|
|
|
|
|
|
|
resolved_artist="$artist"
|
|
|
|
|
resolved_album="$album"
|
|
|
|
|
updated_artist=$(metaflac --show-tag=ALBUMARTIST "$first_flac" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
updated_album=$(metaflac --show-tag=ALBUM "$first_flac" | sed 's/^ALBUM=//')
|
|
|
|
|
|
|
|
|
|
if contains_non_latin "$artist"; then
|
|
|
|
|
resolved_artist=$(find_matching_dir "$LIBRARY_DIR" "$artist")
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
artist_dir="$LIBRARY_DIR/$resolved_artist"
|
|
|
|
|
|
|
|
|
|
if contains_non_latin "$album"; then
|
|
|
|
|
resolved_album=$(find_matching_dir "$artist_dir" "$album")
|
|
|
|
|
fi
|
|
|
|
|
[[ "$original_artist" != "$updated_artist" ]] && resolved_artist="$original_artist ($updated_artist)" || resolved_artist="$original_artist"
|
|
|
|
|
[[ "$original_album" != "$updated_album" ]] && resolved_album="$original_album ($updated_album)" || resolved_album="$original_album"
|
|
|
|
|
|
|
|
|
|
target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album"
|
|
|
|
|
mkdir -p "$target_dir"
|
|
|
|
|
@ -126,4 +105,24 @@ find "$IMPORT_DIR" -type f \( -iname "*.jpg" -o -iname "*.png" \) | while read -
|
|
|
|
|
echo "$COPY_CMD $img_file to $target_dir"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# === Process artist images (e.g. artist.jpg) ===
|
|
|
|
|
find "$IMPORT_DIR" -mindepth 2 -maxdepth 2 -type f \( -iname "artist.jpg" -o -iname "artist.png" \) | while read -r artist_img; do
|
|
|
|
|
artist_dir_path=$(dirname "$artist_img")
|
|
|
|
|
artist_name=$(basename "$artist_dir_path")
|
|
|
|
|
|
|
|
|
|
flac_file=$(find "$artist_dir_path" -type f -iname "*.flac" | head -n 1)
|
|
|
|
|
if [[ -n "$flac_file" ]]; then
|
|
|
|
|
updated_artist=$(metaflac --show-tag=ALBUMARTIST "$flac_file" | sed 's/^ALBUMARTIST=//')
|
|
|
|
|
[[ "$artist_name" != "$updated_artist" ]] && resolved_artist="$artist_name ($updated_artist)" || resolved_artist="$artist_name"
|
|
|
|
|
else
|
|
|
|
|
resolved_artist="$artist_name"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
target_dir="$LIBRARY_DIR/$resolved_artist"
|
|
|
|
|
mkdir -p "$target_dir"
|
|
|
|
|
$COPY_CMD "$artist_img" "$target_dir/"
|
|
|
|
|
echo "$COPY_CMD $artist_img to $target_dir"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
echo "Import completed."
|
|
|
|
|
|
|
|
|
|
|