Created
April 13, 2025 05:58
-
-
Save fuzzywalrus/b224f1ca6663e04471223d5e028b4166 to your computer and use it in GitHub Desktop.
Libation default downloads your books just by title but for sanity, this sh script will rename libation downloads to author - bookname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Libation library renamer | |
| # Final script to rename audiobook folders from "Title [ID]" to "Author - Title [ID]" | |
| # Correctly handles the TAG:artist=Stephen King format | |
| # brew install ffmpeg | |
| # usage /path/to/script/rename_audiobooks.sh /path/to/books | |
| # Check if ffprobe is installed | |
| if ! command -v ffprobe &> /dev/null; then | |
| echo "ffprobe not found. Please install ffmpeg:" | |
| echo "brew install ffmpeg" | |
| exit 1 | |
| fi | |
| # Directory containing your audiobook folders | |
| BOOKS_DIR="$1" | |
| if [ -z "$BOOKS_DIR" ]; then | |
| echo "Usage: $0 /path/to/Books" | |
| echo "Example: $0 \"/Users/$(whoami)/Music/Libation/test\"" | |
| exit 1 | |
| fi | |
| # Check if the directory exists | |
| if [ ! -d "$BOOKS_DIR" ]; then | |
| echo "Directory does not exist: $BOOKS_DIR" | |
| echo "Please provide the correct path to your audiobook folder." | |
| exit 1 | |
| fi | |
| # Print the absolute path we're working with | |
| BOOKS_DIR=$(cd "$BOOKS_DIR" && pwd) | |
| echo "Using directory: $BOOKS_DIR" | |
| # Change to the books directory | |
| cd "$BOOKS_DIR" | |
| # Log file for errors and skipped folders | |
| LOG_FILE="audiobook_rename_log.txt" | |
| > "$LOG_FILE" # Clear log file | |
| echo "Starting folder renaming process..." | |
| echo "Scanning in: $BOOKS_DIR" | |
| # Counter for stats | |
| processed=0 | |
| renamed=0 | |
| skipped=0 | |
| # Process each folder in the Books directory | |
| for folder in */; do | |
| # Remove trailing slash | |
| folder=${folder%/} | |
| # Skip if not a directory | |
| if [ ! -d "$folder" ]; then | |
| continue | |
| fi | |
| processed=$((processed + 1)) | |
| # Extract title and ID from folder name | |
| if [[ $folder =~ (.*)\[(.*)\] ]]; then | |
| title="${BASH_REMATCH[1]}" | |
| id="${BASH_REMATCH[2]}" | |
| # Remove trailing spaces from title | |
| title=$(echo "$title" | sed 's/ *$//') | |
| # Find .m4b file in the folder | |
| m4b_file=$(find "$folder" -name "*.m4b" -type f | head -n 1) | |
| if [ -n "$m4b_file" ]; then | |
| # Get metadata in the format we observed: TAG:artist=Stephen King | |
| metadata=$(ffprobe -v quiet -show_format "$m4b_file") | |
| # Extract author from TAG:artist= field | |
| author=$(echo "$metadata" | grep "TAG:artist=" | sed 's/TAG:artist=//g') | |
| # If no author found, try other fields | |
| if [ -z "$author" ]; then | |
| author=$(echo "$metadata" | grep "TAG:author=" | sed 's/TAG:author=//g') | |
| fi | |
| if [ -z "$author" ]; then | |
| author=$(echo "$metadata" | grep "TAG:album_artist=" | sed 's/TAG:album_artist=//g') | |
| fi | |
| # Debug: Log all metadata for problematic files | |
| if [ -z "$author" ]; then | |
| echo "Could not extract author from $m4b_file" >> "$LOG_FILE" | |
| echo "Full metadata:" >> "$LOG_FILE" | |
| echo "$metadata" >> "$LOG_FILE" | |
| echo "-----------------" >> "$LOG_FILE" | |
| skipped=$((skipped + 1)) | |
| continue | |
| fi | |
| # Create new folder name | |
| new_folder="$author - $title [$id]" | |
| # Check if new folder already exists | |
| if [ -d "$new_folder" ] && [ "$folder" != "$new_folder" ]; then | |
| echo "Cannot rename '$folder' to '$new_folder' - destination already exists" >> "$LOG_FILE" | |
| skipped=$((skipped + 1)) | |
| continue | |
| fi | |
| # Rename the folder | |
| if [ "$folder" != "$new_folder" ]; then | |
| echo "Renaming: $folder → $new_folder" | |
| mv "$folder" "$new_folder" | |
| renamed=$((renamed + 1)) | |
| else | |
| echo "Folder already has correct name: $folder" >> "$LOG_FILE" | |
| skipped=$((skipped + 1)) | |
| fi | |
| else | |
| echo "No .m4b file found in $folder" >> "$LOG_FILE" | |
| skipped=$((skipped + 1)) | |
| fi | |
| else | |
| echo "Folder name '$folder' does not match expected pattern 'Title [ID]'" >> "$LOG_FILE" | |
| skipped=$((skipped + 1)) | |
| fi | |
| done | |
| echo "Process completed." | |
| echo "Processed: $processed folders" | |
| echo "Renamed: $renamed folders" | |
| echo "Skipped: $skipped folders" | |
| echo "See $LOG_FILE for details on any skipped folders." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment