-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvidname-cleanup
More file actions
executable file
·171 lines (145 loc) · 3.88 KB
/
vidname-cleanup
File metadata and controls
executable file
·171 lines (145 loc) · 3.88 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
#!/bin/bash
# SPDX-FileCopyrightText: 2015 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
source "${ROOT}"/langs
! check_command grep sed && exit 3
#
# Functions
#
function get_language() {
local res=
local lang
for lang in "${!ISO_639_2[@]}"; do
if [[ "${1,,}" = "${ISO_639_2[${lang}],,}" ]]; then
res="${1}"
break
fi
done
# if language not found, try to parse three-character strings as a fallback.
if [ -z "${res}" ]; then
for lang in "${!ISO_639_2[@]}"; do
if [[ "${1,,}" = "${lang,,}" ]]; then
res="${1^^}"
break
fi
done
fi
# If language not found, try to parse two-character strings as a fallback.
if [ -z "${res}" ]; then
for lang in "${!ISO_639_1[@]}"; do
if [[ "${1,,}" = "${lang,,}" ]]; then
res="${1^^}"
break
fi
done
fi
echo "${res}"
}
function cleanup_filename() {
local name
local extension
local lang
local newname
name="${1%.*}"
extension="${1##*.}"
newname=$(echo "${name}" |
sed \
-e "s/_\|\./\ /g" \
-e "s/^\[[^]]*\]\ \?//g" \
-e "s/\ \[[^()]*\]$//g" \
-e "s/\ \[[^()]*\]\ //g" \
-e "s/^([^()]*)\ \?//g" \
-e "s/\ ([^()[0-9]\{4\}]*)//g" \
-e "s/\(DVD\|HDTV\|BluRay\|WEB-DL\|BRRip\|DVDRip\|HDRip\).*//g" \
-e "s/\(x264\|x265\|h264\|h265\|AV1\).*//g" \
-e "s/\(720\|1080\|480\|576\)p.*//g" \
-e "s/\ *(\?\(19\|20\)\([0-9]\{2\}\))\?\ *$/ (\1\2)/g" \
-e "s/\b\(s[0-9]\+e[0-9]\+\)/\U\1/g" \
-e "s/\(S[0-9]\+E[0-9]\+\)[- ]/\1 /g" \
-e "s/\ \ \+/\ /g" \
-e "s/\ *$//g" \
-e "s/^\ *//g")
if [[ "${name}" = "${extension}" ]]; then
echo "${newname}"
else
lang="$(echo "${name}" | sed -e "s/_\|\./\ /g" | grep -o '[^\ ?!?]*$')"
lang="$(get_language "${lang}")"
if [ -z "${lang}" ]; then
echo "${newname}.${extension}"
else
local tmp
tmp="$(echo "${newname}" | grep -o '[^\ ?!?]*$')"
if [[ "${lang,,}" = "${tmp,,}" ]]; then
echo "${newname}.${extension}"
else
echo "${newname} ${lang}.${extension}"
fi
fi
fi
}
function scan_files() {
local infile
local outfile
for infile in *; do
if [ -f "${infile}" ]; then
if [[ "$(file -b --mime-type "${infile}")" =~ video|text|audio|subrip ]]; then
outfile=$(cleanup_filename "${infile}")
if [[ "${infile}" = "${outfile}" ]]; then
show_listitem "${outfile@Q} unchanged..." >&2
continue
else
show_listitem "${infile@Q} -> ${outfile@Q}" >&2
fi
INFILES+=("${infile}")
OUTFILES+=("${outfile}")
fi
fi
done
}
function print_files() {
local idx
for idx in "${!INFILES[@]}"; do
echo "${OUTFILES[${idx}]}"
done
}
function rename_files() {
local idx
for idx in "${!INFILES[@]}"; do
mv -v "${INFILES[${idx}]}" "${OUTFILES[${idx}]}"
done
}
#
# Main
#
INFILES=()
OUTFILES=()
scan_files
if [ "${#INFILES[@]}" -eq 0 ]; then
show_info "No files found. Exiting..."
exit
fi
# print_files
REPLY=$(ask_question "Is this good? (y/N)")
if [[ "${REPLY}" =~ ^(y|Y) ]]; then
rename_files
show_success "Done!"
else
echo "Fine. Be that way."
fi