Compare commits

..

23 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

View File

@ -20,11 +20,19 @@
DIR="`pwd`" DIR="`pwd`"
PROG="$(basename -- "${BASH_SOURCE[0]}")" PROG="$(basename -- "${BASH_SOURCE[0]}")"
EXT="mp3"
FAIL=".$PROG.exit-error" FAIL=".$PROG.exit-error"
OLD="$DIR/OLD" OLD="OLD"
NEW="$DIR/NEW" NEW="NEW"
INT="$DIR/INTERIM" INT="INTERIM"
OLD_DIR="$DIR/$OLD"
NEW_DIR="$DIR/$NEW"
INT_DIR="$DIR/$INT"
cmd="mv"
## Checks and Setup ## ## Checks and Setup ##
@ -62,23 +70,17 @@ echo "** Folder exists appropriately."
# Other Directories # # Other Directories #
echo "* Ensuring working directories NEW and INTERIM exist." echo "* Ensuring working directories NEW and INTERIM exist."
mkdir -pv "$NEW" "$INT" mkdir -pv "$NEW_DIR" "$INT_DIR"
## Convert Media ## ## Functions ##
echo "* Convert all files to mp3." # Transform Media #
find "$OLD" ! -name "*".mp3 | while read file; do
ffmpeg -nostdin -hide_banner -loglevel quiet -i "$file" "$file".converted.mp3
done
echo "** Done converting mp3s."
## Transform Media ##
function migrate_music { function migrate_music {
file="$1" file="$1"
command="$2" command="$2"
if [[ -z $file ]]; then if [[ -z "$file" ]]; then
echo "ERROR: No filename retrieved, cannot migrate." echo "ERROR: No filename retrieved, cannot migrate."
return 1 return 1
fi fi
@ -100,7 +102,7 @@ function migrate_music {
done done
echo "*** Title=$title" echo "*** Title=$title"
new_file="$title$EXT" new_file="$title.$EXT"
# Retrieve and clean the Track Number # Retrieve and clean the Track Number
track="" track=""
@ -117,7 +119,9 @@ function migrate_music {
[[ ${#track} == 1 ]] && track="0$track" [[ ${#track} == 1 ]] && track="0$track"
echo "*** Track=$track" echo "*** Track=$track"
new_file="$track. $new_file" if [[ -n "$track" ]]; then
new_file="$track. $new_file"
fi
# Add the Album and Year. # Add the Album and Year.
album="" album=""
@ -137,13 +141,32 @@ function migrate_music {
year="${year// : /}" year="${year// : /}"
year="${year//[^[:alnum:][:space:].]/}" year="${year//[^[:alnum:][:space:].]/}"
year="`echo $year`" 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 while [[ "$year" == *" "* ]]; do
year="${year// / }" year="${year// / }"
done done
echo "*** Year=$year" echo "*** Year=$year ($year_source)"
parent="" parent=""
if [[ -s $year && -s $album ]]; then if [[ -n $year && -n $album ]]; then
parent="$year - $album" parent="$year - $album"
else else
parent="$year$album" parent="$year$album"
@ -164,34 +187,38 @@ function migrate_music {
echo "*** Artist=$artist" echo "*** Artist=$artist"
band="" band=""
band="`exiftool -Band "$file"`" if [[ -z "$artist" ]]; then
band="${band//Band /}" band="`exiftool -Band "$file"`"
band="${band// : /}" band="${band//Band /}"
band="${band//[^[:alnum:][:space:].]/}" band="${band// : /}"
band="`echo $band`" band="${band//[^[:alnum:][:space:].]/}"
while [[ "$band" == *" "* ]]; do band="`echo $band`"
band="${band// / }" while [[ "$band" == *" "* ]]; do
done band="${band// / }"
echo "*** Band=$band" done
echo "*** Band=$band"
fi
album_artist="" album_artist=""
album_artist="`exiftool -Albumartist "$file"`" if [[ -z "$artist" && -z "$band" ]]; then
album_artist="${album_artist//Albumartist /}" album_artist="`exiftool -Albumartist "$file"`"
album_artist="${album_artist// : /}" album_artist="${album_artist//Albumartist /}"
album_artist="${album_artist//[^[:alnum:][:space:].]/}" album_artist="${album_artist// : /}"
album_artist="`echo $album_artist`" album_artist="${album_artist//[^[:alnum:][:space:].]/}"
while [[ "$album_artist" == *" "* ]]; do album_artist="`echo $album_artist`"
album_artist="${album_artist// / }" while [[ "$album_artist" == *" "* ]]; do
done album_artist="${album_artist// / }"
echo "*** Albumartist=$album_artist" done
echo "*** Albumartist=$album_artist"
fi
# Prefer Artist, then Band, then Albumartist # Prefer Artist, then Band, then Albumartist
grandparent="" grandparent=""
if [[ -s $album ]]; then if [[ -n $artist ]]; then
grandparent="$album" grandparent="$artist"
elif [[ -s $band ]]; then elif [[ -n $band ]]; then
grandparent="$band" grandparent="$band"
elif [[ -s $album_artist ]]; then elif [[ -n $album_artist ]]; then
grandparent="$album_artist" grandparent="$album_artist"
else else
grandparent="Unknown" grandparent="Unknown"
@ -199,22 +226,91 @@ function migrate_music {
new_file="$grandparent/$new_file" 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. # Move the file.
new_file="$NEW/$new_file" new_file="$NEW_DIR/$new_file"
$command -v "$file" "$new_file" $command -v "$file" "$new_file"
echo "*** Finished file!" echo "*** Finished file!"
} }
echo "* Moving INTERIM files to NEW." ## Main ##
find "$INT" -name "*".mp3 | while read file; do
migrate_music "$file" mv # 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 done
echo "** Done converting any mp3s."
echo "** Done with INTERIM, deleting." echo "** Done with INTERIM, deleting."
rm -rfv "$INT" rm -rfv "$INT_DIR"
echo "* Copying OLD files to NEW." echo "* Copying OLD files to NEW."
find "$OLD" -name "*".mp3 | while read file; do find "$OLD_DIR" -name "*".$EXT | sort | while read file; do
migrate_music "$file" cp migrate_music "$file" $cmd
printf "\n"
done done
echo "** Done with OLD, deleting."
find "$OLD_DIR" -type d | sort -r | while read dir; do
rmdir -v "$dir"
done
exit 0