Files
Server-scripts/audiobook_metadata/metadata_remove_genre.sh
T
2026-09-14 12:43:38 +02:00

102 lines
2.4 KiB
Bash

#!/usr/bin/env bash
LIBRARY="$1"
MODE="${2:-}"
REPORT="${3:-/home/lucas/audiobook_genre_cleanup.txt}"
if [[ -z "$LIBRARY" ]]; then
echo "Usage: $0 <library_path> [--delete] [report_file]"
exit 1
fi
mapfile -t FILES < <(
find "$LIBRARY" -type f \( \
-iname "*.mp3" -o \
-iname "*.m4b" -o \
-iname "*.m4a" \
\)
)
TOTAL=${#FILES[@]}
PROCESSED=0
MATCHES=0
MODIFIED=0
{
echo "Audiobook Genre Cleanup Report"
echo "Generated: $(date)"
echo "Library: $LIBRARY"
echo "Total files: $TOTAL"
echo
} > "$REPORT"
for file in "${FILES[@]}"; do
((PROCESSED++))
if (( PROCESSED % 100 == 0 )); then
echo "Progress: $PROCESSED/$TOTAL files processed"
fi
genre=$(kid3-cli -c "get genre" "$file" 2>/dev/null)
[[ -z "$genre" ]] && continue
cleaned=$(printf '%s\n' "$genre" | sed -E '
s/[Aa]udio[[:space:]]*[Bb]ooks?//g
s/\b(Hörbücher?|Hoerbücher?|Hoerbuecher?|Hörspiele?|Hoerspiele?)\b//Ig
# Normalize genre names
s/\b(Sci[ -]?Fi|Science Fiction|SF)\b/SciFi/Ig
s/\bnonfiction\b/NonFiction/Ig
# Normalize separators (&, :, / -> ;)
s/\b(Sci[ -]?Fi|Science Fiction|SF)\b/Science Fiction/Ig
s/[[:space:]]*[,&:\/][[:space:]]*/;/g
# Normalize commas
s/[[:space:]]*;[[:space:]]*/; /g
# Trim leading/trailing punctuation and whitespace
s/^[[:space:],;:-]+//
s/[[:space:],;-]+$//
')
# s/.*//;
if [[ "$genre" != "$cleaned" ]]; then
((MATCHES++))
status="MATCH"
if [[ "$MODE" == "--delete" ]]; then
kid3-cli \
-c "tag 2" \
-c "set genre \"$cleaned\"" \
-c save \
"$file"
((MODIFIED++))
status="UPDATED"
fi
printf '[%d/%d] [%s] %s | OLD="%s" | NEW="%s"\n' \
"$PROCESSED" "$TOTAL" "$status" "$file" "$genre" "$cleaned"
printf '[%d/%d] [%s] %s | OLD="%s" | NEW="%s"\n' \
"$PROCESSED" "$TOTAL" "$status" "$file" "$genre" "$cleaned" >> "$REPORT"
fi
done
{
echo
echo "========== SUMMARY =========="
echo "Files scanned : $TOTAL"
echo "Matches found : $MATCHES"
echo "Files updated : $MODIFIED"
} >> "$REPORT"
echo
echo "========== SUMMARY =========="
echo "Files scanned : $TOTAL"
echo "Matches found : $MATCHES"
echo "Files updated : $MODIFIED"
echo "Report written to: $REPORT"