Compare commits
23 Commits
0f83256e05
...
main
Author | SHA1 | Date | |
---|---|---|---|
c2719ec081 | |||
bdc18dd727 | |||
fa175639d8 | |||
1fb3607165 | |||
6c4d752e92 | |||
7959609a33 | |||
38aceb3220 | |||
a513995ad9 | |||
3ac99d17d3 | |||
28b7575b4e | |||
778770c18b | |||
8d40f45732 | |||
349e735c04 | |||
8b40fb3056 | |||
ea9bcddfdc | |||
e7fabe6f35 | |||
85e0a5be7d | |||
ab58617815 | |||
9fcb460c96 | |||
94c387faba | |||
b4c5fa64cc | |||
da4462b46a | |||
a4b9be077d |
@ -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
|
||||||
|
Reference in New Issue
Block a user