-
Notifications
You must be signed in to change notification settings - Fork 0
/
wapasplit
executable file
·204 lines (156 loc) · 3.96 KB
/
wapasplit
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
#!/bin/sh
# wapasplit
#
# Split TV episodes in current directory into subdirectories according to
# their metadata checksum.
#
# Requires: mkvmerge (from the mkvtoolnix package), rsync.
#
# This script is in the public domain and was originally written by
# Dennis Murczak <https://github.com/lortordermur/>
#### GLOBALS ####
ext="mkv"
#copycmd="cp -ip"
copycmd="rsync -ahP --append-verify"
movecmd="mv -i"
fileopcmd=
#### FUNCTIONS ####
usage () {
echo "Usage: $(basename $0) [-s | --scanonly] [fileext]"
echo "
Splits all video files ending in .fileext found in the current directory into
subdirectories, based on their metadata checksum. fileext defaults to 'mkv'.
If the -s or --scanonly option is given, only the checksum scan is carried
out and a summary of which directories would be created is displayed.
Rsync is used for incremental copying, so partial runs can be resumed.
An exit status of 0 is returned on an error-free run, 1 in case of a file-
related problem or failure of an external command, 2 if the metadata
consistency check (with -s) is unsuccessful.
"
exit 0
}
commandexists () {
command -v "$1" > /dev/null || { echo "Required command '$1' not found! $2" && echo && exit 1; }
}
filesexist () {
files=$(ls *.$ext) 2> /dev/null
if [ ! "$files" ]; then
echo "No matching files found for *.$ext! Exiting."
echo
echo "For usage information, try -h or --help."
echo
exit 1
fi
}
countfiles () {
ls -l *.$ext | wc -l
}
makeMKVck () {
echo $(
mkvmerge --identify --identification-format json "$1" |
grep -E '"codec": |"number": |"language": |"type": ' |
cksum |
cut -f 1 -d " "
)
}
scanheaders () {
local mkvck=
local chklist=
local uniqlist=
local uniqcnt=
local numfiles=$(countfiles)
echo "Starting metadata checksumming of all .$ext files."
echo
for f in *.$ext; do
echo "Obtaining metadata: $f"
mkvck=$(makeMKVck "$f")
[ "$?" -ne "0" ] && { echo "Not a supported video file: $f" && echo && exit 1; }
chklist=$(echo $chklist; echo $mkvck)
echo "Checksum is $mkvck"
done
[ "$numfiles" -eq "1" ] && { echo "Single file scanned." && echo && return; }
uniqlist=$( { for i in $chklist; do
echo $i
done; } | sort | uniq )
echo
uniqcnt=$(echo $uniqlist | wc -w)
if [ $uniqcnt -eq "1" ]; then
echo "All files have a consistent metadata checksum: $uniqlist."
echo "No splitting necessary. Exiting."
echo
exit 0
else
echo "$uniqcnt unique metadata checksums found:"
echo $uniqlist
echo
[ "$uniqcnt" -gt "1" ] && return 2
fi
}
processfiles () {
local mkvck=
local dirname=
local numfiles=$(countfiles)
local c="0"
for f in *.$ext; do
c=$(($c+1))
echo "Processing ($c/$numfiles): $f"
echo
mkvmerge --identify "$f"
mkvck=$(makeMKVck "$f")
[ "$?" -ne "0" ] && { echo "There was a problem with mkvmerge. Aborting." && echo && exit 1; }
dirname="__"$mkvck"__"
echo "Copying/moving to subdirectory '$dirname'"
mkdir -p $dirname
$fileopcmd "$f" $dirname/
[ "$?" -ne "0" ] && { echo "Error on copy/move. Aborting." && echo && exit 1; }
echo
done
echo "All done!"
echo
exit 0
}
#### MAIN ####
commandexists "mkvmerge" "Please install mkvtoolnix."
commandexists "rsync" "Rsync is required for incremental copying, please install it."
if [ "$1" ]; then
case $1 in
"--help" | "-h" )
usage
;;
"--scanonly" | "-s")
[ "$2" ] && ext="$2"
filesexist
scanheaders
ret=$?
echo "These would be the directories created in a normal run."
echo
exit $ret
;;
*)
ext="$1"
;;
esac
fi
filesexist
scanheaders
echo "Would you like to copy or move the files to subdirectories now [C/m/q/?]?"
read -r ans
case $ans in
"c" | "C" | "")
fileopcmd=$copycmd
break
;;
"m" | "M" | "")
fileopcmd=$movecmd
break
;;
"?")
usage
;;
*)
echo "Aborting."
echo
exit 0
;;
esac
processfiles