-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmp3rip
executable file
·478 lines (393 loc) · 12.6 KB
/
mp3rip
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/sh
##
## Rips audio tracks and encodes them to mp3
## Copyright (c) 2003-2005 by Tomekk (tomekk/AT/oswiecim.eu.org)
## Copyright (c) 2005,2006 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## 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 <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
set -e
echo 'mp3rip v0.5 (c) Tomekk & mina86'
echo
LOWER_CHARS=abcdefghijklmnopqrstuvwxyz
UPPER_CHARS=ABCDEFGHIJKLMNOPQRSTUVWXYZ
##
## Usage
##
usage () {
cat <<EOF
usage: ${0##*/} [<options>] [ [--] <tracks> | -a | --ask | --list ]
${0##*/} --setup
<options>:
-h --help Prints this help screan and exits
-V --version Prints version number and exits
-H --method-help Prints help about methods and exits
-s --setup Runs setup
--config Synonym of --setup
-v<verbosity> Verbosity level (0 to 5) [2]
-v Synonym of -v3
-vv Synonym of -v4
-d --device=<device> Uses <device> as cdrom [/dev/cdrom]
'search' makes cdparanoia search for a drive
-m --method=<method> Method to use when encoding [vbr]
-b --bitrate=<br> Bitrate used in CBR/ABR method [128]
-b --min-bitrate=<br> Mimimum bitrate used in VBR/ABR method [32]
-B --max-bitrate=<br> Maximum bitarte used in VBR/ABR method [320]
-O --options=<opt> Passes <opt> to lame when in user method
-q --quality=<q> Quality to encode with (0-best; 9-worse) [5]
-p --pipe Use pipes -- may be a bit faster (implies -v1)
-k --keep-wav Doesn't remove wav files after encoding
--cd=<dir> Changes directory to <dir> (creates if missing)
-l --lame=<lame> Path to lame [auto]
-L --lame-opts=<opt> Aditional lame options [none]
-c --cdparanoia=<cdp> Path to cdparanoia [auto]
-C --cdparanoia-opts=<opt> Aditional cdparanoia options [none]
-a --ask Prompts user to enter track numbers to rip
-t --list List all audio tracks
<tracks> Track numbers to rip [all]
EOF
}
##
## Description of methods
##
method_help () {
cat <<EOF
There are fallowing methods available:
* cbr (constant bitrate)
Encodes MP3 with a constant bitrate specified using -b option.
* vbr (variable bitrate)
The bitrate of each fragments of MP3 is adjusted automaticaly so
that higher bitrates are used only when needed (ie. a few seconds of
silence are encoded using minimal bitrate to save some space).
* vbr-old (variable bitrate, old routine)
Works just like vbr but uses older, slower and most tested VBR algorithm.
* abr (average bitrate)
Similar to VBR but makes the average bitrate of MP3 file to be
exactly the bitrate specified by --bitrate option.
Note: Do not use -b option with abr method since it is ambigious.
Use --bitrate and --min-bitrate instead.
* user
You have to specify options used to encode the WAV file by yourself.
* rip-only
When this method is selected WAV files won't be encoded to MP3s.
EOF
}
##
## Messages
##
error () {
__EC=$1
shift
echo "${0##*/}: $*"
exit $__EC
}
##
## Init variables and read config
##
VERBOSITY=2
RUN_SETUP=
TRACKS=all
DEV=/dev/cdrom
METHOD=vbr
LAME=auto
CDPARANOIA=auto
LAME_OPTS=
CDPARANOIA_OPTS=
BR=128
MIN_BR=32
MAX_BR=320
QUALITY=5
OPTIONS=
PIPE=
KEEP_WAV=
CD=
if [ -e ~/.mp3-opt ]; then
. ~/.mp3-opt
KEEP_WAV=$(printf "%s" "$KEEP_WAV" | tr $UPPER_CHARS $LOWER_CHARS)
if [ "X$KEEP_WAV" != "Xy" ] && [ "X$KEEP_WAV" != "Xyes" ]
then KEEP_WAV=; else KEEP_WAV=y; fi
PIPE=$(printf "%s" "$PIPE" | tr $UPPER_CHARS $LOWER_CHARS)
if [ "X$PIPE" != "Xy" ] && [ "X$PIPE" != "Xyes" ]
then PIPE=; else PIPE=y; fi
fi
##
## Parse arguments
##
while [ $# -ne 0 ] && [ "X$1" != X-- ]; do
case "$1" in
(-h|--help) usage ; exit 0; ;;
(-V|--version) exit 0; ;;
(-H|--method-help) method_help ; exit 0; ;;
(-s|--setup|--config) RUN_SETUP=y ; ;;
(-v[0-5]) VERBOSITY="${1#-v}" ; ;;
(-v) VERBOSITY=3 ; ;;
(-vv) VERBOSITY=4 ; ;;
(-d) DEVICE="$2" ; shift ; ;;
(-d*) DEVICE="${1#-d}" ; ;;
(--device=*) DEVICE="${1#*=}" ; ;;
(-m) METHOD="$2" ; shift ; ;;
(-m*) METHOD="${1#-m}" ; ;;
(--method=*) METHOD="${1#*=}" ; ;;
(-b) BR="$2"; MIN_BR="$2" ; shift ; ;;
(-b*) BR="${1#-b}"; MIN_BR="${1#-b}"; ;;
(--bitrate=*) BR="${1#*=}" ; ;;
(--min-bitrate=*) MIN_BR="${1#*=}" ; ;;
(--max-bitrate=*) MAX_BR="${1#*=}" ; ;;
(-o) OPTIONS="$2" ; shift ; ;;
(-o*) OPTIONS="${1#-o}" ; ;;
(--options=*) OPTIONS="${1#*=}" ; ;;
(-q) QUALITY="$2" ; shift ; ;;
(-q*) QUALITY="${1#-q}" ; ;;
(--quality=*) QUALITY="${1#*=}" ; ;;
(-p|--pipe) PIPE=y ; ;;
(-k|--keep-wav) KEEP_WAV=y ; ;;
(--cd=*) CD="${1#*=}" ; ;;
(-l) LAME="$2" ; shift ; ;;
(-l*) LAME="${1#-l}" ; ;;
(--lame=*) LAME="${1#*=}" ; ;;
(-L) LAME_OPTS="$2" ; shift ; ;;
(-L*) LAME_OPTS="${1#-L}" ; ;;
(--lame-opts=*) LAME_OPTS="${1#*=}" ; ;;
(-c) CDPARANOIA="$2" ; shift ; ;;
(-c*) CDPARANOIA="${1#-c}" ; ;;
(--cdparanoia=*) CDPARANOIA="${1#*=}" ; ;;
(-c) CDPARANOIA_OPTS="$2" ; shift ; ;;
(-c*) CDPARANOIA_OPTS="${1#-C}" ; ;;
(--cdparanoia-opts=*) CDPARANOIA_OPTS="${1#*=}" ; ;;
(-a|--ask) TRACKS=ask ; ;;
(-t|--list) TRACKS=list ; ;;
(-*) error 1 "Invalid argument: '$1'"; ;;
([0-9][0-9]) break; ;;
([0-9]) break; ;;
esac
shift
done
if [ "X$1" = X-- ]; then
shift;
fi
if [ "X$TRACKS" = Xall ] && [ $# -ne 0 ]; then
TRACKS=args
fi
##
## Run setup
##
if [ "$RUN_SETUP" ]; then
echo '=== Entering setup'
## Function for asking question
ask () {
__NAME="$1"; shift
__VALUE=$(eval echo \"\$$__NAME\")
if [ "X$2" = Xbool ]; then
if [ -z "$__VALUE" ]; then __VALUE=no; else __VALUE=yes; fi
fi
if [ "X$2" != Xnodefault ]; then
printf "%s [%s]: " "$1" "$__VALUE"
if read __ANS && [ -n "$__ANS" ]; then __VALUE="$__ANS"; fi
else
printf "%s (%s): " "$1" "$__VALUE"
if read __ANS; then __VALUE="$__ANS"; fi
fi
printf "\n# %s\n%s='%s'\n" "$1" "$__NAME" \
$(printf "%s" "$__VALUE" | sed -e "s/'/'\\\\''/") >>"$TMP_FILE"
if [ "X$2" = Xbool ]; then
__VALUE=$(printf "%s" "$__VALUE" | tr $UPPER_CHARS $LOWER_CHARS)
if [ "X$__VALUE" != "Xy" ] && [ "X$__VALUE" != "Xyes" ]
then __VALUE=; else __VALUE=y; fi
fi
eval $__NAME=\"\$__VALUE\"
}
## Temp file
if which mktemp >/dev/null 2>&1; then
TMP_FILE=$(mktemp)
elif which tempfile >/dev/null 2>&1; then
TMP_FILE=$(tempfile)
else
echo unable to create temporary file, \
mktemp or tempfile required >&2
exit 1
fi
## Config header
cat <<EOF >$TMP_FILE
##
## mp3rip Configuration File
##
## This file has been generated automaticly.
## To enter setup run mp3rip --setup
##
EOF
## Questions
ask VERBOSITY 'Default verbosity level (0 to 5)'
ask DEV 'CD-Rom Device'
ask METHOD 'Method (see --method-help for more info)'
ask BR 'Bitrate (used in cbr and abr methods)'
ask MIN_BR 'Mimimum bitrate (used in vbr and abr methods)'
ask MAX_BR 'Mimimum bitrate (used in vbr and abr methods)'
ask QUALITY 'Encoding quality (0 - best, 9 - worse)'
ask OPTIONS 'Options to use in user method'
ask LAME 'Lame executable to use'
ask LAME_OPTS 'Aditional arguments to lame' nodefault
ask CDPARANOIA 'Cdparanoia executable to use'
ask CDPARANOIA_OPTS 'Aditional arguments to cdparanoia' nodefault
ask PIPE 'Do you want to use pipes' bool
ask KEEP_WAV 'Do you want to keep wav files' bool
## Save?
DONE=
while [ -z "$DONE" ]; do
printf 'Do you want to save the options (show to show config file) [yes]: '
if ! read __ANS || [ -z "$__ANS" ]; then __ANS=yes; fi
__ANS=$(echo "$__ANS" | tr $UPPER_CHARS $LOWER_CHARS)
DONE=y
if [ "X$__ANS" = Xshow ]; then
printf '\n%s\n' '---<cut here>---'
cat <"$TMP_FILE"
printf '%s\n\n' '---<cut here>---'
DONE=
elif [ "X$__ANS" = Xy ] || [ "X$__ANS" = Xyes ]; then
echo 'Saving options'
cat <"$TMP_FILE" >~/.mp3riprc
fi
done
rm -f "$TMP_FILE"
## Continue
printf 'Do you want to rip CD now [no]: '
read __ANS
__ANS=$(echo "$__ANS" | tr $UPPER_CHARS $LOWER_CHARS)
if [ "X$__ANS" != Xy ] && [ "X$__ANS" != Xyes ]; then
exit 0;
fi
if [ "X$TRACKS" = Xall ]; then
TRACKS=ask
fi
fi
##
## Things concerning verbosity
##
if [ -n "$PIPE" ] && [ $VERBOSITY -gt 1 ]; then VERBOSITY=1; fi
if [ $VERBOSITY -gt 2 ]; then V=-v; else V=; fi
msg () {
if [ $VERBOSITY -ge $1 ]; then
shift
echo "$*"
fi
}
##
## Change directory
##
if [ -n "$CD" ]; then
echo "=== Entering directory $CD"
mkdir -p $V -- "$CD"
cd "$CD"
fi
##
## Prepare cdparanoia
##
if [ "X$CDPARANOIA" = Xauto ]; then CDPARANOIA=cdparanoia; fi
if ! CDPARANOIA=$(which "$CDPARANOIA" 2>/dev/null); then
error 2 Could not fine cdparanoia executable
fi
msg 4 Cdparanoia executable: $CDPARANOIA
CDP="$CDPARANOIA $CDPARANOIA_OPTS"
if [ "X$DEV" = Xsearch ]; then
CDP="$CDP -s"
else
CDP="$CDP -d $DEV"
fi
if [ $VERBOSITY -eq 0 ]; then CDP="$CDP -q"
elif [ $VERBOSITY -ge 4 ]; then CDP="$CDP --verbose"
fi
msg 3 Cdparanoia command: $CDP
##
## List tracks (if --list given)
##
if [ "X$TRACKS" = Xlist ]; then
echo === Listing tracks
msg 5 Running: $CDP -Q
$CDP -Q
exit $?
fi
##
## Get list of tracks to rip
##
if [ "X$TRACKS" = Xask ]; then
set --
printf 'Enter track numbers to rip (^D or q ends): '
while read NUM; do
set -- "$@" $NUM
if printf " %s " $NUM | grep -iF ' q ' >/dev/null 2>&1; then
break
fi
done
elif [ "X$TRACKS" = Xall ]; then
set -- $($CDP -Q 2>&1 | sed -ne \
's/^ *\([1-9][0-9]*\)\. *[1-9][0-9]*.*$/\1/ p')
msg 2 === $# tracks found
fi
##
## Prepare lame
##
if [ "X$LAME" = Xauto ]; then LAME=lame; fi
if ! LAME=$(which "$LAME" 2>/dev/null); then
error 2 Could not fine lame executable
fi
msg 4 Lame executable: $LAME
LM="$LAME $LAME_OPTS"
case "$METHOD" in
(cbr) LM="$LM --cbr -q $QUALITY -b $BR" ; ;;
(vbr) LM="$LM --vbr-new -V $QUALITY -b $MIN_BR -B $MAX_BR"; ;;
(vbr-old) LM="$LM --vbr-old -V $QUALITY -b $MIN_BR -B $MAX_BR"; ;;
(abr) LM="$LM --abr $BR -b $MIN_BR -B $MAX_BR"; ;;
(user) LM="$LM $OPTIONS" ; ;;
(rip-only) LM=; KEEP_WAV=y; PIPE= ; ;;
esac
if [ $VERBOSITY -eq 0 ]; then LM="$LM --silent"
elif [ $VERBOSITY -eq 1 ]; then LM="$LM --nohist"
elif [ $VERBOSITY -eq 5 ]; then LM="$LM --verbose"
fi
msg 3 Lame command: $LM
##
## Do the job
##
ALL=$#
DONE=1
KEEP_WAV="$KEEP_WAV$PIPE"
while [ -n "$1" ] && [ "X$1" != Xq ] && [ "X$1" != XQ ]; do
## Check arg
MATCH=$(expr "$1" : '[0-9]*')
if [ $MATCH -ne ${#1} ]; then
echo "${0##*/}: '$1' is not a number, ignoring" >&2
shift
conitnue;
fi
## Rip
NUM=$(printf %02d "$1")
printf "=== Ripping track %2d (%2d/%2d)\n" $1 $DONE $ALL
if [ -n "$PIPE" ]; then
$CDP "$1" - | $LM - "track$NUM.mp3"
else
$CDP "$1" "track$NUM.wav"
if [ -n "$LM" ]; then
echo "=== Encoding track %2d (%2d/%2d)" $1 $DONE $ALL
$LM "track$NUM.wav" "track$NUM.mp3"
fi
fi
## Remove wav
if [ -z "$KEEP_WAV" ]; then
rm -f $V "track$NUM.wav"
fi
DONE=$((DONE + 1 ))
shift
done