#!/bin/bash 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" # Parse flags MOVE_FILES=false BEETS_FLAG="" for arg in "$@"; do case $arg in --move) MOVE_FILES=true ;; --timid|--skip-beets) BEETS_FLAG="$arg" ;; *) echo "Unknown option: $arg" echo "Usage: $0 [--move] [--timid|--skip-beets]" exit 1 ;; esac done # Optional beets import case "$BEETS_FLAG" in --timid) echo "Running beet import --timid..." beet import "$IMPORT_DIR" --timid ;; --skip-beets) echo "Skipping beets import" ;; *) echo "Running beet import..." beet import "$IMPORT_DIR" ;; esac # Add Replay Gain information to files 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 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 COPY_CMD="cp" $MOVE_FILES && COPY_CMD="mv" # 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 artist_dir="$LIBRARY_DIR/$resolved_artist" if contains_non_latin "$album"; then resolved_album=$(find_matching_dir "$artist_dir" "$album") fi target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album" mkdir -p "$target_dir" $COPY_CMD "$file" "$target_dir/" echo "$COPY_CMD $file to $target_dir" done # Also move/copy associated image files (cover art & artist 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) if [[ -z "$first_flac" ]]; then echo "No FLAC file found in $dir_path to extract metadata from. Skipping image: $img_file" continue fi artist=$(metaflac --show-tag=ALBUMARTIST "$first_flac" | sed 's/^ALBUMARTIST=//') album=$(metaflac --show-tag=ALBUM "$first_flac" | sed 's/^ALBUM=//') resolved_artist="$artist" resolved_album="$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 target_dir="$LIBRARY_DIR/$resolved_artist/$resolved_album" mkdir -p "$target_dir" $COPY_CMD "$img_file" "$target_dir/" echo "$COPY_CMD $img_file to $target_dir" done echo "Import completed."