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`"
|
||||
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,23 +70,17 @@ 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 "* Convert all files to mp3."
|
||||
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 ##
|
||||
# Transform Media #
|
||||
|
||||
function migrate_music {
|
||||
file="$1"
|
||||
command="$2"
|
||||
|
||||
if [[ -z $file ]]; then
|
||||
if [[ -z "$file" ]]; then
|
||||
echo "ERROR: No filename retrieved, cannot migrate."
|
||||
return 1
|
||||
fi
|
||||
@ -100,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=""
|
||||
@ -117,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=""
|
||||
@ -137,13 +141,32 @@ 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 [[ -s $year && -s $album ]]; then
|
||||
if [[ -n $year && -n $album ]]; then
|
||||
parent="$year - $album"
|
||||
else
|
||||
parent="$year$album"
|
||||
@ -164,6 +187,7 @@ function migrate_music {
|
||||
echo "*** Artist=$artist"
|
||||
|
||||
band=""
|
||||
if [[ -z "$artist" ]]; then
|
||||
band="`exiftool -Band "$file"`"
|
||||
band="${band//Band /}"
|
||||
band="${band// : /}"
|
||||
@ -173,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// : /}"
|
||||
@ -184,14 +210,15 @@ function migrate_music {
|
||||
album_artist="${album_artist// / }"
|
||||
done
|
||||
echo "*** Albumartist=$album_artist"
|
||||
fi
|
||||
|
||||
# Prefer Artist, then Band, then Albumartist
|
||||
grandparent=""
|
||||
if [[ -s $album ]]; then
|
||||
grandparent="$album"
|
||||
elif [[ -s $band ]]; then
|
||||
if [[ -n $artist ]]; then
|
||||
grandparent="$artist"
|
||||
elif [[ -n $band ]]; then
|
||||
grandparent="$band"
|
||||
elif [[ -s $album_artist ]]; then
|
||||
elif [[ -n $album_artist ]]; then
|
||||
grandparent="$album_artist"
|
||||
else
|
||||
grandparent="Unknown"
|
||||
@ -199,22 +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_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
|
||||
|
Reference in New Issue
Block a user