-
Notifications
You must be signed in to change notification settings - Fork 0
/
another-simple-renamer.sh
executable file
·324 lines (265 loc) · 11.4 KB
/
another-simple-renamer.sh
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# @package Another Simple Renamer
#
# @author Brett Shenk
# @version 0.8.1
# @license Creative Commons Attribution 4.0 https://choosealicense.com/licenses/cc-by-4.0/
# @copyright 2024
#
# @var [bool] $rename_status Rename video files or run a test run and log all effected files. [true, false]
# @var [array|string] $path The folder paths to be searched.
# @var [string] $main_match Logic for what folders will be included in the renaming process.
# @var [bool] $reset_perms Reset permissions for the videos in the folder paths. [true, false]
# @var [bool] $reset_owner Reset the owner for the videos in the folder paths. [true, false]
# Rename the file being processed? False for a test run.
rename_status=true
# Full file path to process.
declare -a path=(
'your-folder-path-here'
)
# To include all folders: "(.{1,})"
# Default folder regex: *[*]*
main_match="((.{1,})?\[.{1,}\](.{1,})?)"
# Reset file permissions? False to skip.
reset_perms=false
# Reset file owner? False to skip.
reset_owner=false
echo
# Loop over all paths in the array one at a time and change to it's directory.
for single_folder in "${path[@]}"
do
cd "$single_folder"
# Reset video permissions.
if $reset_perms; then
chmod 0777 -R ./*
fi
# Reset video owner.
if $reset_owner; then
sudo chown nobody:users /*
fi
# Folders matching the variable: $main_match
# By default, if missing, it's assumed to already be done.
find ./ -maxdepth 1 -type d -regextype posix-extended -regex "$main_match" -print0 |
while read -d $'\0' main_folder; do
# Skip main folder
if [[ "$main_folder" = "./" ]]; then
continue
fi
folder_char="./" # Character removal
result="${main_folder//$folder_char}"
folder_char1="\[*\] " # Character removal
result="${result//$folder_char1}"
folder_char2=" \[*\]" # Character removal
result="${result//$folder_char2}"
cd "$main_folder"
# Stuff for later
new_show_name="$result"
episode_numb=0
season_numb=0
check_movie_miniseries="NULL"
check_season="NULL"
leading_zero_fill (){
printf "%0$1d\\n" "$2"
}
# Get all video files in the main folder. Not seasons.
find ./ -maxdepth 1 -type f \( -iname '*.mp4' -o -iname '.mov' -o -iname '*.avi' -o -iname '*.wmv' -o -iname '*.flv' -o -iname '*.f4v' -o -iname '*.mkv' -o -iname '*.webm' -o -iname '*.m4v' -o -iname '*.3gp' -o -iname '*.3g2' -o -iname '*.ogv' -o -iname '*.vob' -o -iname '*.avchd' -o -iname '*.mpg' -o -iname '*.mpeg2' -o -iname '*.mxf' \) -print0 |
sort -z |
while read -r -d '' line; do
# s01e02.ext show name - S01E01.ext
# anything_s1e2.ext show name S01E01.ext anything_s01e02.ext
match_show1="([S|s][0-9]{1,}[E|e][0-9]{1,})"
# anything s1.e2.ext anything_s01.e02.ext
# anything_s01_e02.ext anything s01 e02.ext anything S01 E02.ext
match_show2="([S|s][0-9]{1,}[-|_|\.|\s][E|e][0-9]{1,})"
# Is the current file a movie or a tv show?
if [[ "$line" =~ "$match_show1" || "$line" =~ "$match_show2" ]]; then
is_tv_show=true
else
is_tv_show=false
fi
# All video file count.
video_count=$(find ./ -maxdepth 1 -type f -regextype posix-extended -regex '.*.(mp4|mov|avi|wmv|flv|f4v|mkv|webm|m4v|3gp|3g2|ogv|vob|avchd|mpg|mpeg2|mxf)' -printf x | wc -c)
# Only video trailers and such.
video_extra_count=$(find ./ -maxdepth 1 -type f -regextype posix-extended -regex '.*(-behindthescenes|-deleted|-featurette|-interview|-scene|-short|-other|(-|.|_)trailer|(-|.|_)sample|-clip|-deletedscene|-extra).(mp4|mov|avi|wmv|flv|f4v|mkv|webm|m4v|3gp|3g2|ogv|vob|avchd|mpg|mpeg2|mxf)' -printf x | wc -c)
video_count=$(expr $video_count - $video_extra_count)
# Catch for some vague valid syntax that's otherwise problematic to match.
if [[ $is_tv_show == false ]]; then
if [[ "$video_count" -gt "3" ]]; then
is_tv_show=true
fi
fi
# Assume show specials are named properly already.
if [[ "$line" == *"-behindthescenes"* || "$line" == *"-deleted"* || "$line" == *"-featurette"* || "$line" == *"-interview"* || "$line" == *"-scene"* || "$line" == *"-short"* || "$line" == *"-other"* || "$line" == *"-trailer"* || "$line" == *".trailer"* || "$line" == *"_trailer"* || "$line" == *"-sample"* || "$line" == *".sample"* || "$line" == *"_sample"* || "$line" == *"-clip"* || "$line" == *"-deletedscene"* || "$line" == *"-extra"* ]]; then
continue
fi
folder_char="./" # Character removal
line="${line//$folder_char}"
# Is this a tv show?
if $is_tv_show; then
((episode_numb++))
# Episode count leading zero fill.
if [[ "$video_count" -le "9999" ]]; then
episode_count=$(leading_zero_fill 4 "$episode_numb")
fi
if [[ "$video_count" -le "999" ]]; then
episode_count=$(leading_zero_fill 3 "$episode_numb")
fi
if [[ "$video_count" -le "99" ]]; then
episode_count=$(leading_zero_fill 2 "$episode_numb")
fi
file_ending=${line##*.}
new_name="$new_show_name"' - S01E'"$episode_count"."$file_ending";
else
file_ending=${line##*.}
new_name="$new_show_name"."$file_ending";
fi
# Does the current video need skipped?
activity_test=$(fuser -f "$line" 2>&1);
if [[ "$activity_test" ]]; then
echo "Video is currently in use. Skipping: " "$line"
check_movie_miniseries="false"
continue
fi
if [[ $check_movie_miniseries = "NULL" || $check_movie_miniseries = "true" ]]; then
check_movie_miniseries="true"
fi
# Confirm it doesn't exist and rename or echo it.
if [[ -f "$new_name" ]]; then
echo "File already exists with the new name. Skipped: " "$line"
else
if $rename_status; then
mv "$line" "$new_name"
else
echo "$line"
echo "$new_name"
fi
fi
# Rename main folder if..
# - Episode count is the same as the total video count
# - Video files are not currently in use
# - New folder name doesn't already exist
if [[ "$episode_numb" = "$video_count" && $check_movie_miniseries = "true" ]]; then
if [[ -d "$new_show_name" ]]; then
echo "Folder already exists with the new name. Skipped: " "$main_folder"
else
if $rename_status; then
cd "$single_folder"
mv "$main_folder" "$new_show_name"
cd "$new_show_name"
else
echo "$new_show_name"
echo
fi
fi
fi
done # end show search
# Does the folder contain sub folders labeled like: Season
find ./ -maxdepth 1 -type d -iname *'season'* -print0 |
sort -z |
while read -r -d '' season_folder; do
folder_char="./" # Character removal
clean_folder="${season_folder//$folder_char}"
# Season 0 and special folders are skipped.
if [[ "$clean_folder" == *"eason 0" || "$clean_folder" == *"eason 00" || "$clean_folder" == *"eason 000" || "$clean_folder" == *"eason 0000" ]]; then
continue
fi
# Get the current season folder number to use otherwise, start a new count.
season_match='[S|s]eason.([1-9]{1}[0]{3}|[1-9]{1}[0]{1,2}[1-9]{1,2}|[0]{1}[1-9]{1}[0]{1}|[1-9]{1,2}[0]{1,2}|[1-9]{1,4}|[0]{1,2}[1-9]{1})';
if [[ "$clean_folder" =~ $season_match ]]; then
season_count=${clean_folder#* }
season_count=$(echo ${season_count} | cut -d ' ' -f 1)
if [[ "$season_count" -le "9999" ]]; then
season_count_zero=$(leading_zero_fill 4 "$season_count")
fi
if [[ "$season_count" -le "999" ]]; then
season_count_zero=$(leading_zero_fill 3 "$season_count")
fi
if [[ "$season_count" -le "99" ]]; then
season_count_zero=$(leading_zero_fill 2 "$season_count")
fi
else
season_count_zero=$(leading_zero_fill 2 "$season_numb")
fi
total_season_count=$(find ./ -type d -regextype posix-extended -regex './[S|s]eason.([1-9]{1}[0]{3}|[1-9]{1}[0]{1,2}[1-9]{1,2}|[0]{1}[1-9]{1}[0]{1}|[1-9]{1,2}[0]{1,2}|[1-9]{1,4}|[0]{1,2}[1-9]{1})([[:space:]]\([0-9]{2,}\-[0-9]{2,}\))?([[:space:]]\([0-9]{4,}\))?' -printf x | wc -c)
((season_numb++))
cd "$season_folder"
# Get all video files in the season folder.
find ./ -maxdepth 1 -type f \( -iname '*.mp4' -o -iname '.mov' -o -iname '*.avi' -o -iname '*.wmv' -o -iname '*.flv' -o -iname '*.f4v' -o -iname '*.mkv' -o -iname '*.webm' -o -iname '*.m4v' -o -iname '*.3gp' -o -iname '*.3g2' -o -iname '*.ogv' -o -iname '*.vob' -o -iname '*.avchd' -o -iname '*.mpg' -o -iname '*.mpeg2' -o -iname '*.mxf' \) -print0 |
sort -z |
while read -r -d '' line; do
# All video file count.
video_count=$(find ./ -maxdepth 1 -type f -regextype posix-extended -regex '.*.(mp4|mov|avi|wmv|flv|f4v|mkv|webm|m4v|3gp|3g2|ogv|vob|avchd|mpg|mpeg2|mxf)' -printf x | wc -c)
# Only video trailers and such.
video_extra_count=$(find ./ -maxdepth 1 -type f -regextype posix-extended -regex '.*(-behindthescenes|-deleted|-featurette|-interview|-scene|-short|-other|(-|.|_)trailer|(-|.|_)sample|-clip|-deletedscene|-extra).(mp4|mov|avi|wmv|flv|f4v|mkv|webm|m4v|3gp|3g2|ogv|vob|avchd|mpg|mpeg2|mxf)' -printf x | wc -c)
video_count=$(expr $video_count - $video_extra_count)
# Assume show specials are named properly already.
if [[ "$line" == *"-behindthescenes"* || "$line" == *"-deleted"* || "$line" == *"-featurette"* || "$line" == *"-interview"* || "$line" == *"-scene"* || "$line" == *"-short"* || "$line" == *"-other"* || "$line" == *"-trailer"* || "$line" == *".trailer"* || "$line" == *"_trailer"* || "$line" == *"-sample"* || "$line" == *".sample"* || "$line" == *"_sample"* || "$line" == *"-clip"* || "$line" == *"-deletedscene"* || "$line" == *"-extra"* ]]; then
continue
fi
folder_char="./" # Character removal
line="${line//$folder_char}"
((episode_numb++))
# Episode count leading zero fill.
if [[ "$video_count" -le "9999" ]]; then
episode_count=$(leading_zero_fill 4 "$episode_numb")
fi
if [[ "$video_count" -le "999" ]]; then
episode_count=$(leading_zero_fill 3 "$episode_numb")
fi
if [[ "$video_count" -le "99" ]]; then
episode_count=$(leading_zero_fill 2 "$episode_numb")
fi
file_ending=${line##*.}
new_name="$new_show_name"' - S'"$season_count_zero"E"$episode_count"."$file_ending";
# Skip the current video?
activity_test=$(fuser -f "$line" 2>&1);
if [[ "$activity_test" ]]; then
echo "Video is currently in use. Skipping: " "$line"
check_season=false
continue
fi
if [[ $check_season = "NULL" || $check_season = true ]]; then
check_season=true
fi
# Confirm it doesn't exist and rename or echo it.
if [[ -f "$new_name" ]]; then
echo "File already exists with the new name. Skipped: " "$line"
else
if $rename_status; then
mv "$line" "$new_name"
else
echo "$line"
echo "$new_name"
fi
fi
# Rename main folder if..
# - Episode count is the same as the total video count
# - Video files are not currently in use
# - Last season in the series?
# - New folder name doesn't already exist
if [[ "$episode_numb" = "$video_count" && $check_season = "true" && "$total_season_count" = "$season_numb" ]]; then
if [[ -d "$new_show_name" ]]; then
echo "Folder already exists with the new name. Skipped: " "$main_folder"
else
if $rename_status; then
cd "$single_folder"
mv "$main_folder" "$new_show_name"
cd "$new_show_name"
cd "$season_folder"
else
echo "$new_show_name"
echo
fi
fi
fi
done # end show search
# Change back to the shows main folder.
cd ../
done #end season search
# Change back to root directory of the array.
cd ../
done
done
echo Done!
sleep 2
exit 1