Files
Server-scripts/quiethost/metadata_remove_genre.sh
T

103 lines
2.4 KiB
Bash
Raw Normal View History

2026-07-31 13:46:39 +02:00
#!/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
# Normalize genre names
s/\b(Sci[ -]?Fi|Science Fiction|SF)\b/SciFi/Ig
s/\bnonfiction\b/NonFiction/Ig
# Normalize separators (&, :, / -> ;)
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
printf "D "
kid3-cli \
-c "select-tag 1" \
-c "set genre \"$cleaned\"" \
-c "select-tag 2" \
-c "set genre \"$cleaned\"" \
-c save \
"$file" >/dev/null
((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"