-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcue2flac
More file actions
executable file
·198 lines (177 loc) · 5.27 KB
/
Copy pathcue2flac
File metadata and controls
executable file
·198 lines (177 loc) · 5.27 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
if [[ $# -ne 1 ]] ; then
echo 'Usage:'
echo -n "$(basename ${BASH_SOURCE[0]}) <path to CUE file>"
exit 1
fi
if ! which flac >/dev/null
then
echo 'flac was not found' >&2
exit 1
fi
set -e
readonly CUE="$1"
GENRE=
YEAR=
PERFORMER=
ALBUM=
FILE=
TRACKNUMBER=
TRACKTITLE=
INDEX=
READ_TRACK=
READ_PERFORMER=
READ_TITLE=
READ_FROM=
READ_UNTIL=
## function accepts 2 arguments:
## 1) prefix to be trimmed from the line;
## 2) the line read from the cue file;
## performs following operations:
## 1) cuts the prefix from the beginning of the line,
## including any whitespaces;
## 2) cuts all the trailing whitespaces;
## 3) if the remainder of the string starts with quotes
## then everything outside quotes is dropped and the
## content between the quotes is printed to stdout;
## 4) if the remainder of the string does NOT start with quotes
## then only first part of the string, to first space,
## is printed to stdout.
function sedTheLine {
local prefix="$1"
local line="$2"
line="$( echo "$line" | sed -e "s/^ *${prefix} *//" -e 's/ *$//' )"
if [[ $line == '"'* ]] ; then
echo "$line" | sed -e 's/^"//' -e 's/".*$//'
else
echo "$line" | awk '{ print $1 }'
fi
}
function resolveFile {
local input="$1"
case "${input,,}" in
*'.wav')
FILE="$input"
;;
*'.flac')
flac -d -f "$input" 1>/dev/null
FILE="$( echo "$input" | sed 's/.flac$/.wav/' )"
;;
*)
FILE="$( echo "$input" | sed 's/\.[^\.]*$/.wav/' )"
if [[ -f "$FILE" ]] ; then
rm -f "$FILE"
fi
if ! ffmpeg -i "$input" "$FILE" </dev/null
then
echo 'ffmpeg failed to decode the input file into wav' >&2
exit 1
fi
;;
esac
}
function cueTimeToFlacTime {
local input="$1"
local frame
local fraction
frame="$( echo "$input" | sed -e 's/^.*://' -e 's/^0//' )"
fraction=$(( $frame * 4 / 3 ))
if [[ $fraction -gt 99 ]] ; then
fraction=99
elif [[ $fraction -lt 10 ]] ; then
fraction="0$fraction"
fi
echo "${input:0:5}.$fraction"
}
function validate {
if [[ "$READ_TITLE" == *'"'* ]] || [[ "$READ_PERFORMER" == *'"'* ]] || [[ "$ALBUM" == *'"'* ]] ; then
echo 'Some of the titles contain double quotes, that is not yet supported' >&2
exit 1
fi
}
function encodeTrack {
validate
local command='flac -f --best'
## dropping illegal filename characters from title:
command="$command --output-name=\"$READ_TRACK - $( echo "$READ_TITLE" | tr -d "\`\!#\$%^&*{}<>;:?/'\"|=+\\\\" ).flac\""
## adding must-exist tags:
command="$command --tag=TRACKNUMBER=\"$READ_TRACK\" --tag=ARTIST=\"$READ_PERFORMER\" --tag=PERFORMER=\"$READ_PERFORMER\" --tag=TITLE=\"$READ_TITLE\" --tag=ALBUM=\"$ALBUM\""
## checking and adding optional tags:
if [[ -n "$GENRE" ]] ; then
command="$command --tag=GENRE=\"$GENRE\""
fi
if [[ -n "$YEAR" ]] ; then
command="$command --tag=DATE=\"$YEAR\""
fi
## adding skip:
command="$command --skip=\"$( cueTimeToFlacTime "$READ_FROM" )\""
## adding until if we have one:
if [[ -n "$READ_UNTIL" ]] ; then
command="$command --until=\"$( cueTimeToFlacTime "$READ_UNTIL" )\""
fi
## adding input file:
command="$command \"$FILE\""
echo -n "executing: '$command'"
eval "$command" 1>/dev/null
READ_TRACK=''
READ_TITLE=''
READ_PERFORMER=''
READ_FROM=''
READ_UNTIL=''
}
while read -r cueline || [[ -n "$cueline" ]] ; do
line="$( echo "$cueline" | tr -d '\r\n' )"
if [[ -z $line ]] ; then
continue
fi
case "$line" in
'REM'*)
case "$( echo "$line" | awk '{ print $2 }' )" in
'GENRE')
GENRE="$( sedTheLine 'REM *GENRE' "$line" )"
;;
'DATE')
YEAR="$( sedTheLine 'REM *DATE' "$line" )"
;;
esac
;;
'PERFORMER'*)
PERFORMER="$( sedTheLine 'PERFORMER' "$line" )"
;;
'TITLE'*)
if [[ -z $TRACKNUMBER ]] ; then
ALBUM="$( sedTheLine 'TITLE' "$line" )"
else
TRACKTITLE="$( sedTheLine 'TITLE' "$line" )"
fi
;;
'FILE'*)
resolveFile "$( sedTheLine 'FILE' "$line" )"
;;
'TRACK'*)
TRACKNUMBER="$( sedTheLine 'TRACK' "$line" )"
;;
'INDEX'*)
case "$( echo "$line" | awk '{ print $2 }' )" in
'00')
if [[ -n "$READ_TITLE" ]] ; then
READ_UNTIL="$( sedTheLine 'INDEX *00' "$line" )"
encodeTrack
fi
;;
'01')
if [[ -n "$READ_TITLE" ]] ; then
READ_UNTIL="$( sedTheLine 'INDEX *01' "$line" )"
encodeTrack
fi
READ_PERFORMER="$PERFORMER"
READ_TITLE="$TRACKTITLE"
READ_TRACK="$TRACKNUMBER"
READ_FROM="$( sedTheLine 'INDEX *01' "$line" )"
;;
esac
;;
esac
done < "$CUE"
## one last track left to encode:
encodeTrack