-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortAudio
More file actions
executable file
·177 lines (148 loc) · 4.68 KB
/
sortAudio
File metadata and controls
executable file
·177 lines (148 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
# Check if it is a directory, file or invalid
isDir() {
if [[ -d $1 ]]; then
# This is a directory
return 1
elif [[ -f $1 ]]; then
# This is a file
return 2
else
# This is invalid
return 0
fi
}
# Handles logic according to the file type
handleType() {
# If it is a directory, then go through the directory to check for files
# or directories
if [ "$1" -eq 1 ]; then
for file in "$2"/*; do
isDir "$file"
TYPE=$?
handleType "$TYPE" "$file" "$ROOT" "$ARTIST"
done
# If it is a file, check if it is an image
elif [ "$1" -eq 2 ]; then
MimeType=$(file "$2" --mime-type -b)
# If it is an audio, print its information
if [[ "$MimeType" == audio/* ]]; then
AlbumTag=("$(ffprobe -v quiet -show_entries format_tags=album -of default=nw=1:nk=1 "$2")")
ArtistTag=("$(ffprobe -v quiet -show_entries format_tags=album_artist -of default=nw=1:nk=1 "$2")")
DiscsTag=("$(ffprobe -v quiet -show_entries format_tags=totaldiscs -of default=nw=1:nk=1 "$2")")
DiscTag=("$(ffprobe -v quiet -show_entries format_tags=disc -of default=nw=1:nk=1 "$2")")
if [ "${AlbumTag[0]}" == '' ]; then
AlbumTag=("$(ffprobe -v quiet -show_entries stream_tags=album -of default=nw=1:nk=1 "$2")")
ArtistTag=("$(ffprobe -v quiet -show_entries stream_tags=album_artist -of default=nw=1:nk=1 "$2")")
DiscsTag=("$(ffprobe -v quiet -show_entries stream_tags=totaldiscs -of default=nw=1:nk=1 "$2")")
DiscTag=("$(ffprobe -v quiet -show_entries stream_tags=disc -of default=nw=1:nk=1 "$2")")
fi
if [ "${ArtistTag[0]}" == '' ]; then
ArtistTag=("$(ffprobe -v quiet -show_entries format_tags=artist -of default=nw=1:nk=1 "$2")")
if [ "${ArtistTag[0]}" == '' ]; then
ArtistTag=("$(ffprobe -v quiet -show_entries stream_tags=artist -of default=nw=1:nk=1 "$2")")
fi
fi
# To prevent the creation of additional folders,
# I'll replace all "/" with "_"
AlbumTag="${AlbumTag[0]//\//_}"
ArtistTag="${ArtistTag[0]//\//_}"
if [[ "$(basename "$(dirname "$2")" | tr '[:upper:]' '[:lower:]')" == disc* ]]; then
ALBUMFOLDER="$(basename "$(dirname "$(dirname "$2")")")"
ARTISTFOLDER="$(basename "$(dirname "$(dirname "$(dirname "$2")")")")"
DISCFOLDER="$(basename "$(dirname "$2")")"
else
ALBUMFOLDER="$(basename "$(dirname "$2")")"
ARTISTFOLDER="$(basename "$(dirname "$(dirname "$2")")")"
DISCFOLDER=""
fi
# echo "$ALBUMFOLDER"
# echo "$ARTISTFOLDER"
# echo "$DISCFOLDER"
FOLDER=""
if [[
"$ALBUMFOLDER" != "${AlbumTag[0]}" ||
("$ARTIST" = true && "$ARTISTFOLDER" != "${ArtistTag[0]}") ||
("$ARTIST" = false && "$ARTISTFOLDER" == "${ArtistTag[0]}") ||
("$DISCS" = true && "$DISCFOLDER" != "DISC ${DiscTag[0]}") ||
("$DISCS" = false && "$DISCFOLDER" == "DISC ${DiscTag[0]}") ]]; then
if [ "$ROOT" = true ]; then
isDir "$DEST"
TYPE=$?
if [ $TYPE -eq 1 ]; then
FOLDER="$DEST"
else
FOLDER="$(dirname "$DEST")"
fi
else
FOLDER="$(dirname "$2")"
fi
if [ "$ARTIST" = true ]; then
FOLDER="$FOLDER"/"${ArtistTag[0]}"
fi
FOLDER="$FOLDER"/"${AlbumTag[0]}"
if [ "$DISCS" = true ] && [ "${DiscsTag[0]}" -gt 1 ]; then
FOLDER="$FOLDER"/"DISC ${DiscTag[0]}"
fi
fi
if [ "$FOLDER" != "" ]; then
mkdir -pv "$FOLDER"
chown "$SUDO_USER":"$SUDO_USER" "$FOLDER"
mv "$2" "$FOLDER" >/dev/null 2>&1
fi
fi
fi
}
if [ "$1" == '' ]; then
DEST=$(pwd)
else
DEST=$1
fi
echo -e "${BLUE}"
read -n 1 -rp "Sort files to root path? (N for sorting to the file's current folder) [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
ROOT=true
else
ROOT=false
fi
echo -e "${BLUE}"
read -n 1 -rp "Sort files into artist folders as well? (N for only into album folders) [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
ARTIST=true
else
ARTIST=false
fi
echo -e "${BLUE}"
read -n 1 -rp "Separate discs from albums into separate folders? [Y/n] " prompt
# Convert input to lowercase
prompt=${prompt,,}
#Add extra \n if not empty due to "enter" inserting a newline
if [[ -n "$prompt" ]]; then
printf "\n"
fi
if [ "$prompt" == y ] || [ -z "$prompt" ]; then
DISCS=true
else
DISCS=false
fi
isDir "$DEST"
TYPE=$?
echo -e "\n${BLUE}"Sorting through "$DEST"..."${NC}\n"
handleType "$TYPE" "$DEST" "$ROOT" "$ARTIST" "$DISCS"
echo -e "\n${GREEN}"Sorting finished."${NC}"