Unattended import on docker

I’m looking for some help to get my beets docker installation to auto-detect and import new music files and folders in my /downloads to my /music folder. I want new tracks from exisiting albums to merge with that album.

I saw that drop2beets only works with singles, but I download mostly albums. So with the help of ChatGPT I set up a script that checks recursively for each new track and runs an beets import command.

I’m not only failing miserably at importing (Track not found) but also at merging (Album already exists). I would really appreciate some help with:

  • the recommended way for an unattended auto-import of new music
  • the rcemmended yaml options to achieve this.

Thank you!

Here now my stump script and my yaml:

Import-Script:
#!/bin/sh

DOWNLOADS_DIR=“/mnt/media/media/nicotine-downs/downloads”
BEETS_DOCKER_CONTAINER=“beets” # Replace with your Docker container name
LOG_FILE=“/home/tom/beets/beets_watcher.log” # Log file location

Function to write log entries

log() {
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) - $1” >> “$LOG_FILE”
}

inotifywait -mr -e create -e moved_to --format ‘%w%f’ “$DOWNLOADS_DIR” | while read NEW_FILE
do
if [ -f “$NEW_FILE” ]; then
log “New file detected: $NEW_FILE”

    # Small delay to ensure file is fully written
    sleep 1

    # Replace the bare-metal path with the container path, assuming they are mounted correctly
    CONTAINER_PATH="${NEW_FILE/$DOWNLOADS_DIR/\/downloads}"

    log "Attempting to import file from container path: $CONTAINER_PATH"

    # Check if the file exists inside the Docker container
    docker exec "$BEETS_DOCKER_CONTAINER" test -f "$CONTAINER_PATH"
    if [ $? -eq 0 ]; then
        docker exec "$BEETS_DOCKER_CONTAINER" beet import "$CONTAINER_PATH" >> "$LOG_FILE" 2>&1
        if [ $? -eq 0 ]; then
            log "Import complete for: $CONTAINER_PATH"
        else
            log "Error importing: $CONTAINER_PATH"
        fi
    else
        log "File not found in Docker container: $CONTAINER_PATH"
    fi
fi

done

Yaml:
plugins: fetchart embedart convert scrub replaygain lastgenre chroma web duplicates
directory: /music
library: /config/musiclibrary.blb
art_filename: albumart
threaded: yes
original_date: no
per_disc_numbering: no

duplicates:
merge: yes
action: remove # This removes the lower quality files
tiebreak:
items: [bitrate, samplerate] # Prioritize higher bitrate and sample rate
full: true # This ensures the full comparison is made

import:
write: yes
copy: no
move: yes
resume: yes
quiet: yes
incremental: no
incremental_skip_later: no
quiet_fallback: asis
timid: no
log: /config/beet.log

convert:
auto: no
ffmpeg: /usr/bin/ffmpeg
opts: -ab 320k -ac 2 -ar 48000
max_bitrate: 320
threads: 1

paths:
default: $albumartist/$album%aunique{}/$track - $title
singleton: Non-Album/$artist - $title
comp: Compilations/$album%aunique{}/$track - $title
albumtype_soundtrack: Soundtracks/$album/$track $title

match:
strong_rec_thresh: 0.16
medium_rec_thresh: 0.25
rec_gap_thresh: 0.25

lastgenre:
auto: yes
source: album

embedart:
auto: yes

fetchart:
auto: yes
art_filename: cover.jpg
minwidth: 500

replaygain:
auto: no

scrub:
auto: yes

replace:
‘^.’: _
‘[\x00-\x1f]’: _
‘[<>:"?*|]’: _
‘[\xE8-\xEB]’: e
‘[\xEC-\xEF]’: i
‘[\xE2-\xE6]’: a
‘[\xF2-\xF6]’: o
‘[\xF8]’: o
‘.$’: _
‘\s+$’: ‘’

web:
host: 0.0.0.0
port: 8337

the:
a: yes
the: yes