Compare commits

...

21 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

View File

@ -20,11 +20,19 @@
DIR="`pwd`"
PROG="$(basename -- "${BASH_SOURCE[0]}")"
EXT="mp3"
FAIL=".$PROG.exit-error"
OLD="$DIR/OLD"
NEW="$DIR/NEW"
INT="$DIR/INTERIM"
OLD="OLD"
NEW="NEW"
INT="INTERIM"
OLD_DIR="$DIR/$OLD"
NEW_DIR="$DIR/$NEW"
INT_DIR="$DIR/$INT"
cmd="mv"
## Checks and Setup ##
@ -62,24 +70,11 @@ echo "** Folder exists appropriately."
# Other Directories #
echo "* Ensuring working directories NEW and INTERIM exist."
mkdir -pv "$NEW" "$INT"
mkdir -pv "$NEW_DIR" "$INT_DIR"
## Convert Media ##
## Functions ##
echo "* Ensure all files are mp3."
find "$OLD" -type f ! -name "*".mp3 | while read file; do
echo "** Working on '$file'."
ffmpeg -nostdin -hide_banner -loglevel quiet -i "$file" "$file".converted.mp3
status="$?"
if [[ $status != 0 ]]; then
echo "*** FAILED: Exited with status '$status'."
else
echo "*** Success!"
fi
done
echo "** Done converting any mp3s."
## Transform Media ##
# Transform Media #
function migrate_music {
file="$1"
@ -107,7 +102,7 @@ function migrate_music {
done
echo "*** Title=$title"
new_file="$title$EXT"
new_file="$title.$EXT"
# Retrieve and clean the Track Number
track=""
@ -124,7 +119,9 @@ function migrate_music {
[[ ${#track} == 1 ]] && track="0$track"
echo "*** Track=$track"
if [[ -n "$track" ]]; then
new_file="$track. $new_file"
fi
# Add the Album and Year.
album=""
@ -144,10 +141,29 @@ function migrate_music {
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"
echo "*** Year=$year ($year_source)"
parent=""
if [[ -n $year && -n $album ]]; then
@ -171,6 +187,7 @@ function migrate_music {
echo "*** Artist=$artist"
band=""
if [[ -z "$artist" ]]; then
band="`exiftool -Band "$file"`"
band="${band//Band /}"
band="${band// : /}"
@ -180,8 +197,10 @@ function migrate_music {
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// : /}"
@ -191,11 +210,12 @@ function migrate_music {
album_artist="${album_artist// / }"
done
echo "*** Albumartist=$album_artist"
fi
# Prefer Artist, then Band, then Albumartist
grandparent=""
if [[ -n $album ]]; then
grandparent="$album"
if [[ -n $artist ]]; then
grandparent="$artist"
elif [[ -n $band ]]; then
grandparent="$band"
elif [[ -n $album_artist ]]; then
@ -206,25 +226,91 @@ function migrate_music {
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/$grandparent/$parent"
mkdir -pv "$NEW_DIR/$grandparent/$parent"
# Move the file.
new_file="$NEW/$new_file"
new_file="$NEW_DIR/$new_file"
$command -v "$file" "$new_file"
echo "*** Finished file!"
}
echo "* Moving INTERIM files to NEW."
find "$INT" -name "*".mp3 | while read file; do
migrate_music "$file" mv
## 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"
rm -rfv "$INT_DIR"
echo "* Copying OLD files to NEW."
find "$OLD" -name "*".mp3 | while read file; do
migrate_music "$file" cp
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