forked from linux4ever07/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cue_time_subtract.sh
executable file
·252 lines (190 loc) · 5.38 KB
/
cue_time_subtract.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
#!/bin/bash
# This script is meant to get the length of all individual tracks in a
# CUE sheet.
if=$(readlink -f "$1")
if_bn=$(basename "$if")
if_bn_lc="${if_bn,,}"
if_name="${if_bn_lc%.[^.]*}"
if_dn=$(dirname "$if")
cue="$if"
# Creates a function called 'usage', which will print usage and quit.
usage () {
printf '\n%s\n\n' "Usage: $(basename "$0") [cue]"
exit
}
# If $if is not a real file, or it has the wrong extension, print usage
# and quit.
if [[ ! -f $if || ${if_bn_lc##*.} != 'cue' ]]; then
usage
fi
bin=$(find "$if_dn" -maxdepth 1 -type f -iname "${if_name}.bin" 2>&- | head -n 1)
declare -a format
format[0]='^[0-9]+$'
format[1]='([0-9]{2}):([0-9]{2}):([0-9]{2})'
format[2]='[0-9]{2}:[0-9]{2}:[0-9]{2}'
format[3]='^(FILE) (.*) (.*)$'
format[4]='^(TRACK) ([0-9]{2,}) (.*)$'
format[5]="^(PREGAP) (${format[2]})$"
format[6]="^(INDEX) ([0-9]{2,}) (${format[2]})$"
format[7]="^(POSTGAP) (${format[2]})$"
regex_blank='^[[:blank:]]*(.*)[[:blank:]]*$'
regex_path='^(.*[\\\/])'
declare -A cue_lines
declare -a frames
# Creates a function called 'read_cue', which will read the input CUE
# sheet, add full path to filenames listed in the CUE sheet, and create
# a new temporary CUE sheet in /dev/shm based on this.
read_cue () {
declare -a files not_found
track_n=0
handle_command () {
# If line is a file command...
if [[ $1 =~ ${format[3]} ]]; then
match=("${BASH_REMATCH[@]:1}")
track_n=$(( track_n + 1 ))
fn=$(tr -d '"' <<<"${match[1]}" | sed -E "s/${regex_path}//")
fn="${if_dn}/${fn}"
if [[ ! -f $fn ]]; then
not_found+=("$fn")
fi
if [[ $track_n -eq 1 ]]; then
if [[ -z $bin && -f $fn ]]; then
bin="$fn"
fi
fi
files+=("$fn")
string="${match[0]} \"${fn}\" ${match[2]}"
cue_lines["${track_n},filename"]="$fn"
cue_lines["${track_n},file_format"]="${match[2]}"
fi
# If line is a track command...
if [[ $1 =~ ${format[4]} ]]; then
match=("${BASH_REMATCH[@]:1}")
track_n="${match[1]#0}"
string="$1"
cue_lines["${track_n},track_number"]="${match[1]}"
cue_lines["${track_n},track_mode"]="${match[2]}"
fi
# If line is a pregap command...
if [[ $1 =~ ${format[5]} ]]; then
match=("${BASH_REMATCH[@]:1}")
string="$1"
cue_lines["${track_n},pregap"]="${match[1]}"
fi
# If line is an index command...
if [[ $1 =~ ${format[6]} ]]; then
match=("${BASH_REMATCH[@]:1}")
index_n="${match[1]#0}"
string="$1"
cue_lines["${track_n},index,${index_n}"]="${match[2]}"
fi
# If line is a postgap command...
if [[ $1 =~ ${format[7]} ]]; then
match=("${BASH_REMATCH[@]:1}")
string="$1"
cue_lines["${track_n},postgap"]="${match[1]}"
fi
}
mapfile -t cue_lines_if < <(tr -d '\r' <"$cue" | sed -E "s/${regex_blank}/\1/")
for (( i = 0; i < ${#cue_lines_if[@]}; i++ )); do
line="${cue_lines_if[${i}]}"
handle_command "$line"
done
if [[ ${#files[@]} -gt 1 ]]; then
cat <<MERGE
This CUE sheet contains multiple FILE commands!
MERGE
exit
fi
if [[ ${#not_found[@]} -gt 0 ]]; then
printf '\n%s\n\n' 'The files below were not found:'
printf '%s\n' "${not_found[@]}"
printf '\n'
fi
}
# Creates a function called 'get_frames', which will get the position of
# a track in the BIN file.
get_frames () {
track_n="$1"
declare index_0_ref index_1_ref index_ref frames_tmp
index_0_ref="cue_lines[${track_n},index,0]"
index_1_ref="cue_lines[${track_n},index,1]"
if [[ -n ${!index_0_ref} ]]; then
index_ref="$index_0_ref"
else
index_ref="$index_1_ref"
fi
if [[ -n ${!index_ref} ]]; then
frames_tmp=$(time_convert "${!index_ref}")
printf '%s' "$frames_tmp"
fi
}
# Creates a function called 'set_frames', which will get the length of
# all tracks in the BIN file (except the last one).
set_frames () {
i=0
declare frames_this frames_next size frames_total
while true; do
i=$(( i + 1 ))
j=$(( i + 1 ))
frames_this=$(get_frames "$i")
frames_next=$(get_frames "$j")
if [[ -n $frames_next ]]; then
frames["${i}"]=$(( frames_next - frames_this ))
else
if [[ -n $bin ]]; then
size=$(stat -c '%s' "$bin")
frames_total=$(( size / 2352 ))
frames["${i}"]=$(( frames_total - frames_this ))
else
frames["${i}"]=0
fi
break
fi
done
}
# Creates a function called 'time_convert', which converts track length
# back and forth between the time (mm:ss:ff) format and frames /
# sectors.
time_convert () {
time="$1"
m=0
s=0
f=0
# If argument is in the mm:ss:ff format...
if [[ $time =~ ${format[1]} ]]; then
m="${BASH_REMATCH[1]#0}"
s="${BASH_REMATCH[2]#0}"
f="${BASH_REMATCH[3]#0}"
# Converting minutes and seconds to frames, and adding all the numbers
# together.
m=$(( m * 60 * 75 ))
s=$(( s * 75 ))
time=$(( m + s + f ))
# If argument is in the frame format...
elif [[ $time =~ ${format[0]} ]]; then
f="$time"
# While $f (frames) is equal to (or greater than) 75, clear the $f
# variable and add 1 to the $s (seconds) variable.
while [[ $f -ge 75 ]]; do
s=$(( s + 1 ))
f=$(( f - 75 ))
done
# While $s (seconds) is equal to (or greater than) 60, clear the $s
# variable and add 1 to the $m (minutes) variable.
while [[ $s -ge 60 ]]; do
m=$(( m + 1 ))
s=$(( s - 60 ))
done
time=$(printf '%02d:%02d:%02d' "$m" "$s" "$f")
fi
printf '%s' "$time"
}
read_cue
set_frames
last=$(( ${#frames[@]} + 1 ))
printf '\n'
for (( i = 1; i < last; i++ )); do
printf 'Track %02d) %s , frames: %s\n' "$i" "$(time_convert "${frames[${i}]}")" "${frames[${i}]}"
done
printf '\n'