Compare commits

...

34 Commits

Author SHA1 Message Date
c2719ec081 Notify when folders are deleted. 2025-04-13 13:04:47 -07:00
bdc18dd727 Only check additonal band slots if the more important ones are empty. Fix bug in if statement, was checking wrong variable. 2025-04-13 11:47:33 -07:00
fa175639d8 Remove all the empty OLD folders once all refactoring is done. More changes to output spacing. 2025-04-13 11:42:56 -07:00
1fb3607165 Remove test code. 2025-04-13 11:38:07 -07:00
6c4d752e92 Fix removal of OLD if the file was converted. More changes to output. 2025-04-13 11:37:25 -07:00
7959609a33 Ensure spaces are handled correctly. 2025-04-13 11:27:57 -07:00
38aceb3220 Refactor program so that each converted file is instantly moved to the NEW directory struture. Start using mv for all operations and remove the non-MP3 originals after converted successfully. 2025-04-13 11:26:49 -07:00
a513995ad9 Fix double hyphen causing Year issue. Ensure space removal is done at the right point. Add a source description of where the year came from. 2025-04-13 10:59:36 -07:00
3ac99d17d3 Add description of what is echo'ing. 2025-04-03 15:02:57 -07:00
28b7575b4e Correct how the extension variable is used. Look in other fields for the Year. 2025-04-03 14:48:49 -07:00
778770c18b Use the variable in all applicable locations. 2025-04-03 11:51:27 -07:00
8d40f45732 Add missing value for variable EXT. 2025-04-03 11:36:14 -07:00
349e735c04 Skip conversion if file is still present from a previous run. 2025-03-19 13:08:10 -07:00
8b40fb3056 Create the interim folder and it subdirs for ffmpeg. 2025-03-19 13:04:38 -07:00
ea9bcddfdc Begin using INTERIM directory. 2025-03-19 12:48:22 -07:00
e7fabe6f35 Fix variable check conditional flag. 2025-03-19 12:44:50 -07:00
85e0a5be7d Change messaging. 2025-03-19 08:28:47 -07:00
ab58617815 Don't create the new file if it has no data. Also make the Track portion optional so that the files do not become hidden with a .. 2025-03-19 08:27:15 -07:00
9fcb460c96 Add a TODO. 2025-03-19 08:15:11 -07:00
94c387faba Force ffmpeg even if the file already exists. Sort output. Ignore folders when looking for files to convert.. 2025-03-19 08:04:09 -07:00
b4c5fa64cc Fix incorrect variable assignment. 2025-03-19 07:55:05 -07:00
da4462b46a Create the new directory structure for the file. 2025-03-19 07:54:28 -07:00
a4b9be077d Fix -s to be -n for checking if the variables have data. Show the error status of ffmpeg if it does not succeed. 2025-03-19 07:53:10 -07:00
0f83256e05 Pull the remaining needed information. 2025-03-19 07:42:43 -07:00
20161e48c5 Start on the actual parsing of the music data. 2025-03-19 05:53:15 -07:00
92ac167e0f Change output to left-lean better. 2025-03-13 06:05:00 -07:00
bbb97b81c7 Add system checks and folder creations. Make the conversion to mp3 more abstract. 2025-03-13 05:57:16 -07:00
92fe141508 Began working on a more full version which will handle the complete process of organizing new music into a library. 2025-03-13 05:38:45 -07:00
515bc753db Merge pull request 'Promote project to primary branch, tested great!' (#1) from dev into main
Reviewed-on: me/shell-music-add-track-to-filename#1
2025-03-13 05:32:42 -07:00
916bcc3afc Finalize the code. Still needs final testing. 2024-06-08 11:05:45 -07:00
5fe5e8106c Also remove double whitespace. 2024-06-07 19:18:30 -07:00
0a55a188bf Fixed bugs, working great now on full library. 2024-06-07 10:04:04 -07:00
6a0a12ad7a Got program into working order, still need to clean up. 2024-06-07 09:32:40 -07:00
1d85f3fd84 Create bash script based on other project. 2024-06-07 09:21:10 -07:00
2 changed files with 439 additions and 0 deletions

316
refactor_music_library.sh Executable file
View File

@ -0,0 +1,316 @@
#!/usr/bin/env bash
# 2025 Hyperling
# Clean up a music library. Improved version of rename_music_files.sh.
# Steps:
# 0) Ensure an OLD folder exists, it is what will be refactored.
# 1) Convert all non-mp3 files to mp3 and put them in an INTERIM folder.
# 2) Loop over all OLD and INTERIM's mp3 files and move them to NEW based on data.
# - Name the file as "${ZERO_LED_TRACK_NUMBER}. ${SONG_TITLE}
# - Name parent folder as "${YEAR} - ${ALBUM}" if both exist, otherwise just one or the other "${YEAR}${ALBUM}".
# - Name grandparent folder as "${ARTIST} - ${GENRE}" if both exist, otherwise "${ARTIST}${GENRE}".
# TODO list:
# - Organize tracks by Artist + Album folders.
# - Name parent folder as "$YEAR - $ALBUM" if both exist, otherwise just one or the other.
# - Name grandparent folder as ARTIST if it exists, otherwise GENRE.
## Facts and Variables ##
DIR="`pwd`"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
EXT="mp3"
FAIL=".$PROG.exit-error"
OLD="OLD"
NEW="NEW"
INT="INTERIM"
OLD_DIR="$DIR/$OLD"
NEW_DIR="$DIR/$NEW"
INT_DIR="$DIR/$INT"
cmd="mv"
## Checks and Setup ##
# System Packages #
echo "* Checking if necessary tools are installed."
function needed_progs {
cat <<- EOF
ffmpeg
exiftool
EOF
}
needed_progs | while read prog; do
if [[ -z "`which $prog`" ]]; then
echo "ERROR: $prog not found, please install."
touch $FAIL
fi
done
if [[ -e $FAIL ]]; then
rm -fv $FAIL
exit 1
fi
echo "** Tools are available."
# Old Directory #
echo "* Checking whether OLD directory exists."
if [[ "`pwd`" == *"/OLD" ]]; then
echo "** We are in the folder, moving up a directory."
cd ..
fi
if [[ ! -d "OLD" ]]; then
echo "ERROR: Could not find a folder named 'OLD'."
echo " Please create it and add your library there."
fi
echo "** Folder exists appropriately."
# Other Directories #
echo "* Ensuring working directories NEW and INTERIM exist."
mkdir -pv "$NEW_DIR" "$INT_DIR"
## Functions ##
# Transform Media #
function migrate_music {
file="$1"
command="$2"
if [[ -z "$file" ]]; then
echo "ERROR: No filename retrieved, cannot migrate."
return 1
fi
if [[ -z "$command" ]]; then
command="cp"
fi
echo "** $file **"
# Retrieve and clean the Title
title=""
title="`exiftool -Title "$file"`"
title="${title//Title /}"
title="${title// : /}"
title="${title//[^[:alnum:][:space:].]/}"
title="`echo $title`"
while [[ "$title" == *" "* ]]; do
title="${title// / }"
done
echo "*** Title=$title"
new_file="$title.$EXT"
# Retrieve and clean the Track Number
track=""
# Get raw value
track="`exiftool -Track "$file"`"
# Filter the header
track="${track//Track /}"
track="${track// : /}"
# Remove disk designations
track="${track%%/*}"
# Remove any whitespace before/after
track="`echo $track`"
# Add a leading 0 to single digits.
[[ ${#track} == 1 ]] && track="0$track"
echo "*** Track=$track"
if [[ -n "$track" ]]; then
new_file="$track. $new_file"
fi
# Add the Album and Year.
album=""
album="`exiftool -Album "$file"`"
album="${album//Album /}"
album="${album// : /}"
album="${album//[^[:alnum:][:space:].]/}"
album="`echo $album`"
while [[ "$album" == *" "* ]]; do
album="${album// / }"
done
echo "*** Album=$album"
year=""
year="`exiftool -Year "$file"`"
year="${year//Year /}"
year="${year// : /}"
year="${year//[^[:alnum:][:space:].]/}"
year="`echo $year`"
year_source="Year"
if [[ -z $year ]]; then
year=""
year="`exiftool -RecordingTime "$file"`"
year="${year//Recording Time /}"
year="${year// : /}"
year="${year//[^[:alnum:][:space:].]/}"
year="`echo $year`"
year_source="RecordingTime"
fi
if [[ -z $year ]]; then
year=""
year="`exiftool -DateTimeOriginal "$file"`"
year="${year//Date\/Time Original /}"
year="${year// : /}"
year="${year//[^[:alnum:][:space:].]/}"
year="`echo $year`"
year_source="DateTimeOriginal"
fi
while [[ "$year" == *" "* ]]; do
year="${year// / }"
done
echo "*** Year=$year ($year_source)"
parent=""
if [[ -n $year && -n $album ]]; then
parent="$year - $album"
else
parent="$year$album"
fi
new_file="$parent/$new_file"
# Add the Artist.
artist=""
artist="`exiftool -Artist "$file"`"
artist="${artist//Artist /}"
artist="${artist// : /}"
artist="${artist//[^[:alnum:][:space:].]/}"
artist="`echo $artist`"
while [[ "$artist" == *" "* ]]; do
artist="${artist// / }"
done
echo "*** Artist=$artist"
band=""
if [[ -z "$artist" ]]; then
band="`exiftool -Band "$file"`"
band="${band//Band /}"
band="${band// : /}"
band="${band//[^[:alnum:][:space:].]/}"
band="`echo $band`"
while [[ "$band" == *" "* ]]; do
band="${band// / }"
done
echo "*** Band=$band"
fi
album_artist=""
if [[ -z "$artist" && -z "$band" ]]; then
album_artist="`exiftool -Albumartist "$file"`"
album_artist="${album_artist//Albumartist /}"
album_artist="${album_artist// : /}"
album_artist="${album_artist//[^[:alnum:][:space:].]/}"
album_artist="`echo $album_artist`"
while [[ "$album_artist" == *" "* ]]; do
album_artist="${album_artist// / }"
done
echo "*** Albumartist=$album_artist"
fi
# Prefer Artist, then Band, then Albumartist
grandparent=""
if [[ -n $artist ]]; then
grandparent="$artist"
elif [[ -n $band ]]; then
grandparent="$band"
elif [[ -n $album_artist ]]; then
grandparent="$album_artist"
else
grandparent="Unknown"
fi
new_file="$grandparent/$new_file"
# Check that the file has proper data.
skip=false
if [[ -z "$grandparent" ]]; then
echo "*** WARNING: Could not find Artist information."
skip="true"
fi
if [[ -z "$parent" ]]; then
echo "*** WARNING: Could not find Album or Year information."
skip="true"
fi
if [[ -z "$track" && -z "$title" ]]; then
echo "*** WARNING: Could not find Track or Title information."
skip="true"
fi
if [[ "$skip" == "true" ]]; then
echo -e "*** Skipping, critical data missing, please add EXIF data.\n"
return 1
fi
# Create the new directories if they do not already exist.
mkdir -pv "$NEW_DIR/$grandparent/$parent"
# Move the file.
new_file="$NEW_DIR/$new_file"
$command -v "$file" "$new_file"
echo "*** Finished file!"
}
## Main ##
# Convert Media #
echo -e "\n* Ensure all files are mp3."
find "$OLD_DIR" -type f ! -name "*".$EXT | sort | while read non_mp3; do
echo "** Working on '$non_mp3'."
temp_file="`basename "$non_mp3"`"
temp_file="$INT_DIR/${temp_file//[^[:alnum:][:space:].]/}.$EXT"
echo "temp_file='$temp_file'"
status=""
if [[ ! -s "$temp_file" ]]; then
# Actual conversion.
ffmpeg -y \
-nostdin -hide_banner -loglevel quiet \
-i "$non_mp3" "$temp_file"
status="$?"
fi
if [[ $status != 0 ]]; then
echo "*** FAILED: Exited with status '$status'."
echo "**** To troubleshoot, try running this:"
echo "ffmpeg -y -i \"$non_mp3\" \"$temp_file\""
exit
fi
echo "*** Success!"
echo "*** Moving file to NEW."
migrate_music "$temp_file" $cmd
echo "** Removing original."
if [[ -d ~/TRASH ]]; then
mv -v "$non_mp3" ~/TRASH/
else
rm -v "$non_mp3"
fi
printf "\n"
done
echo "** Done converting any mp3s."
echo "** Done with INTERIM, deleting."
rm -rfv "$INT_DIR"
echo "* Copying OLD files to NEW."
find "$OLD_DIR" -name "*".$EXT | sort | while read file; do
migrate_music "$file" $cmd
printf "\n"
done
echo "** Done with OLD, deleting."
find "$OLD_DIR" -type d | sort -r | while read dir; do
rmdir -v "$dir"
done
exit 0

123
rename_music_files.sh Executable file
View File

@ -0,0 +1,123 @@
#!/bin/bash
# 2024-06-07 C.Greenwood
# Rename all files in a music library based on metadata.
# Uses the format "[Track]. [Title].mp3"
## Variables ##
PROG="`basename $0`"
#DIR="`dirname $0`"
DIR="`pwd`"
EXT=".mp3"
## Functions ##
function usage {
cat <<- EOF
Usage:
$PROG [-h] [-u]
Parameters:
-h : Help, display the usage and exit succesfully.
Place this file at the root of your music destined for a flash drive and run it without any parameters.
It will dive through all folders and convert your MP3's to have the Track# and Title in the filename.
This tool has a pre-requisite you should make sure you have installed:
- exiftool
Thanks for using $PROG!
EOF
exit $1
}
function error {
num_chars=$(( 7 + ${#1} ))
echo ""
printf '*%.0s' $(seq 1 $num_chars)
echo -e "\nERROR: $1"
printf '*%.0s' $(seq 1 $num_chars)
echo -e "\n"
usage $2
}
## Validations ##
# Check for parameters.
while getopts ":h" opt; do
case "$opt" in
h) usage 0
;;
*) error "Operator $OPTARG not recognized." 6
;;
esac
done
# Ensure critical tools are available.
if [[ ! `which exiftool` ]]; then
error "exiftool not found" 2
fi
## Main ##
# Loop through all files in and lower than the current directory.
time find $DIR -name "*${EXT}" | sort | while read file; do
echo -e "\n$file"
# Retrieve and clean the Track#
track=""
# Get raw value
track="`exiftool -Track "$file"`"
# Filter the header
track="${track//Track /}"
track="${track// : /}"
# Remove disk designations
track="${track%%/*}"
# Remove any whitespace before/after
track="`echo $track`"
# Add a leading 0 to single digits.
[[ ${#track} == 1 ]] && track="0$track"
echo "Track=$track"
# Retrieve and clean the Title
title=""
title="`exiftool -Title "$file"`"
title="${title//Title /}"
title="${title// : /}"
title="${title//[^[:alnum:][:space:].]/}"
title="`echo $title`"
while [[ "$title" == *" "* ]]; do
title="${title// / }"
done
echo "Title=$title"
# Create the new file with the correct filename.
new_file="`dirname "$file"`/$track. $title$EXT"
if [[ "$file" == "$new_file" ]]; then
echo "SKIP: Filename already correct! :)"
continue
fi
if [[ ! -z "$track" && ! -z "$title" ]]; then
echo "Creating '`basename "$new_file"`'."
mv -v "$file" "$new_file"
elif [[ -z "$track" && ! -z "$title" ]]; then
echo "No Track# found, leaving Title alone."
continue
else
echo "File does not have Track or Title metadata."
continue
fi
# Confirm the new file exists and remove the old file if so
if [[ -e "$new_file" ]]; then
echo "Success!"
else
error "$new_file was not created successfully." 5
fi
done
echo -e "\nProcess has completed. Enjoy having your songs named correctly!"
exit 0