-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2m4a
executable file
·373 lines (291 loc) · 7.28 KB
/
2m4a
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/bin/bash
# convert "any" audioformat to mp3
#
# sports some meta data converting too. It's rudimentary for now.
# Development 2006 by Andreas Engl <[email protected]>
#
# 2mp3 is based on audio convert 0.3.1:
# by 2005 linasoft
#
# Requires: mplayer, faad, kde, mppdec, oggdec, mac, id3tag
#TODO: + real tagging solution
# + move all decoding to mplayer
PN=`basename "$0"` # Program name
VER='0.2'
temp_file="audiodump.wav"
graceful_exit()
{
clean_up
exit 1
}
clean_up()
{
rm -f "$temp_file"
}
get_metatags ()
{
#echo "get_metatags() called"
if (is_ogg "$file")
then
# artist_name=`ogginfo "$file" | grep artist | cut -d \= -f 2`
# album_name=`ogginfo "$file" | grep album | cut -d \= -f 2`
# song_name=`ogginfo "$file" | grep title | cut -d \= -f 2`
# track_number=`ogginfo "$file" | grep tracknumber | cut -d \= -f 2`
echo "ogg tags not supported yet"
fi
if (is_flac "$file")
then
artist_name=`metaflac --show-tag=artist "$file" | cut -d \= -f 2`
album_name=`metaflac --show-tag=album "$file" | cut -d \= -f 2`
song_name=`metaflac --show-tag=title "$file" | cut -d \= -f 2`
track_number=`metaflac --show-tag=tracknumber "$file" | cut -d \= -f 2`
fi
if (is_aac "$file")
then
artist_name=`faad -i "$file" 2>&1 | awk '/artist/ { print substr($0, match($0, /:/) + 2 ) }'`
album_name=`faad -i "$file" 2>&1 | awk '/album/ { print substr($0, match($0, /:/) + 2 ) }'`
song_name=`faad -i "$file" 2>&1 | awk '/title/ { print substr($0, match($0, /:/) + 2 ) }'`
track_number=`faad -i "$file" 2>&1 | awk '/track/ { print substr($0, match($0, /:/) + 2 ) }'`
fi
}
set_mp3_fields ()
{
#echo "set_mp3_fields() called "
if [ "$artist_name" ]
then
mp3_fields=(-a"$artist_name")
fi
if [ "$album_name" ]
then
mp3_fields=("${mp3_fields[@]}" -A"$album_name")
fi
if [ "$song_name" ]
then
mp3_fields=("${mp3_fields[@]}" -s"$song_name")
fi
if [ "$track_number" ]
then
mp3_fields=("${mp3_fields[@]}" -t"$track_number")
fi
}
# determine audio file type, silently
is_ogg ()
{
file -b "$file" | grep 'Vorbis' &> /dev/null && echo $file | grep -i '\.ogg$' &> /dev/null
}
is_mpc ()
{
file -b "$file" | grep 'Musepack' &> /dev/null && echo $file | grep -i '\.mpc$' &> /dev/null
}
is_flac ()
{
file -b "$file" | grep 'FLAC' &> /dev/null || echo $file | grep -i '\.flac$' &> /dev/null
}
is_mac ()
{
file -b "$file" | grep 'data' &> /dev/null && echo $file | grep -i '\.ape$' &> /dev/null
}
is_aac ()
{
file -b "$file" | grep 'AAC' &> /dev/null && echo $file | grep -i '\.aac$' &> /dev/null
}
is_wav ()
{
file -b "$file" | grep 'WAVE' &> /dev/null && echo $file | grep -i '\.wav$' &> /dev/null
}
is_wma ()
{
file -b "$file" | grep 'Microsoft' &> /dev/null && echo $file | grep -i '\.wma$' &> /dev/null
}
is_m4a ()
{
file -b "$file" | grep "MPEG v4 system" &> /dev/null || echo $file | grep -i '\.m4a$' &> /dev/null
}
is_mp4 ()
{
file -b "$file" | grep "MPEG v4 system" &> /dev/null && echo $file | grep -i '\.mp4$' &> /dev/null
}
is_mp3 ()
{
file -b "$file" | grep 'MP§' &> /dev/null || echo $file | grep -i '\.mp3$' &> /dev/null
}
is_ape ()
{
file -b "$file" | grep "Monkey's Audio" &> /dev/null || echo $file | grep -i '\.ape$' &> /dev/null
}
######## decoding
ogg_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.ogg}
#oggdec "$file" -o "$temp_file"
mplayer -ao pcm "$file"
#echo -e "ogg_decode() song:" $song
}
mpc_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.mpc}
mplayer -ao pcm "$file"
#echo -e "mpc_decode() song:" $song
}
flac_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.flac}
#flac -d -f -F --no-verify "$file" -o "$temp_file" 2>&1
mplayer -ao pcm "$file"
#echo -e "flac_decode() song:" $song
}
mac_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.wav}
mac "$file" "$temp_file" -d
#echo -e "mac_decode() song:" $song
}
ape_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.ape}
mac "$file" "$temp_file" -d
}
aac_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.aac}
faad -o "$temp_file" "$file"
#echo -e "aac_decode() song:" $song
}
wma_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.wma}
mplayer -ao pcm "$file"
#echo -e "wma_decode() song:" $song
}
m4a_decode ()
{
echo -e "\t decoding file:" $file
song=${file%.m4a}
mplayer -ao pcm "$file"
#echo -e "m4a_decode() song:" $song
}
mp4_decode ()
{
echo -e "\t + decoding file:" $file
song=${file%.mp4}
mplayer -ao pcm "$file"
#echo -e "mp4_decode() song:" $song
}
mp3_decode ()
{
echo -e "\t + decoding file:" $file
song=${file%.mp3}
mplayer -ao pcm "$file"
#echo -e "mp3_decode() song:" $song
}
####################################################################
# encode to mp3
mp3_encode ()
{
echo -e "\t + encoding file:" $song ".mp3"
if !( lame --preset fast extreme "$temp_file" "$song".mp3 )
then
exit -1
fi
#echo "mp3_encode() called on Song:" $song
}
convert_audio_file ()
{
# decode the given audioformat
if (is_ogg "$file")
then
ogg_decode
fi
if (is_mpc "$file")
then
mpc_decode
fi
if (is_flac "$file")
then
flac_decode
fi
if (is_mac "$file")
then
mac_decode
fi
if (is_aac "$file")
then
aac_decode
fi
if (is_wma "$file")
then
wma_decode
fi
if (is_m4a "$file")
then
m4a_decode
fi
if (is_mp4 "$file")
then
mp4_decode
fi
if (is_ape "$file")
then
ape_decode
fi
if (is_mp3 "$file")
then
mp3_decode
fi
# encode in mp3
mp3_encode
# prepare mp3 fields and write them the new mp3 file
#set_mp3_fields
#id3tag "${mp3_fields[@]}" "$song".mp3
# delete original file
mp3file="$song.mp3"
dir=$(echo ${PWD##*/})
i=`ls -l "$mp3file" | awk '{print $5}'`
k=`ls -l "$file" | awk '{print $5}'`
#echo "k" $k "i" $i
help=$(echo $i/$k|bc -l)
#echo "help" $help
percentage=$(echo $help*100|bc -l)
percentage=${percentage%.*}
#echo "percentage" $percentage
if [ 40 -lt $percentage ] # if new file is at least 40% of the original it should be kosher
then
echo -e "\t + " $file "succesfully converted."
if !(is_mp3 "$file")
then
kfmclient move "$file" trash:/ &> /dev/null
fi # don't forget to trash your trashbin
fi
}
# main
echo -e " == " $PN "Version:" $VER "by" $AUT " == "
trap "graceful_exit" SIGINT SIGTERM # graceful exit upon signal
if [ $# -eq 0 ]; then
echo -e "You attempted to convert nothing to mp3... Try with a file or an expression like *.wma"
exit 1
fi
echo -e "\t + " $# "files found. Starting process at:" $(date '+%Y.%m.%d:%H:%M')
for file in "$@"; do
if !(is_ogg "$file") && !(is_mpc "$file") && !(is_flac "$file") && !(is_mac "$file") && \
!(is_aac "$file") && !(is_wav "$file") && !(is_wma "$file") && !(is_mp4 "$file") && !(is_mp3 "$file") && !(is_m4a "$file") && !(is_ape "$file")
then
echo "Couldn't find a supported format."
echo "processed file: $file"
echo -e "\n"
echo "Supported formats are:"
echo -e " m4a The MP3 Replacement \t \n mpc \t Musepack \n flac \t Free Lossless Audio Codec\n ape \t Monkey's Audio \n aac \t Apple Audio Codec\n wma \t Windows Media Codec\n ogg \t Ogg Vorbis \n wav \t Wave\n"
exit 1
fi
get_metatags "$file"
convert_audio_file "$file"
clean_up
done
echo -e "\t + finished at: " $(date '+%Y.%m.%d:%H:%M')
echo -e "\t + Don't forget to empty your trashbin."