parent
ae6e57e2d1
commit
712373e582
Binary file not shown.
@ -1,14 +1,123 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
export LC_ALL=C.UTF-8
|
||||||
|
|
||||||
# usage: import_music.sh [opts]
|
# usage: import_music.sh [--move] [--timid|--skip-beets]
|
||||||
|
|
||||||
# This script will:
|
IMPORT_DIR="/mnt/media/slskd/downloads"
|
||||||
# - find unimported music in $IMPORT_DIR
|
LIBRARY_DIR="/mnt/media/Music"
|
||||||
# - use metaflac to add replay gain to all files
|
|
||||||
# - (optional w/ --musicbrainz) use MusicBrainz data to tag the files
|
|
||||||
# - match the artist with a known list of romanized japanese artists, and
|
|
||||||
# replace the artist tag with the native japanese artist name
|
|
||||||
# note: the album will still be placed under the romanized name folder
|
|
||||||
# - copy (or move with --move) the music to $MUSIC_DIR/Artist/Album
|
|
||||||
|
|
||||||
|
# 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")
|
||||||
|
artist=$(metaflac --show-tag=ALBUMARTIST "$dir_path"/*.flac 2>/dev/null | head -n1 | sed 's/^ALBUMARTIST=//')
|
||||||
|
album=$(metaflac --show-tag=ALBUM "$dir_path"/*.flac 2>/dev/null | head -n1 | 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."
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in new issue