aboutsummaryrefslogtreecommitdiff
path: root/lib/liblame/misc/mlame
blob: 23beab5f79e6b3e9e99fd80642a83362afa8c9b3 (plain)
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
#!/bin/sh 

############################################################################
#   
#  Run the LAME encoder on multiple files, with option to delete .wav files
#  after encoding.  "mlame -?" will give instructions.
#
#  Robert Hegemann
#  modified on request: Frank Klemm <pfk@uni-jena.de>
#
############################################################################

# encoder path to use
mp3coder="lame"
mp3analyzer="mlame_corr"

# default options to use
options_low="-h -d -mj -b 128"
options_high="-h -d -mj -V 1 -b 112 -B 320"
options=$options_high

# remove source?
removesource=false

# force overwrite of destination
testoverwrite=true

# waiting after error report n seconds
errordelay=1 

helptext="\n\
This script runs the LAME mp3 encoder on multiple files: \n\n\
    $0 [options] <file 1> ... <file n>\n\
\n\
  options:\n\
    -?                  this help text\n\
    -r                  remove files after encoding\n\
    -f                  force overwrite of destination if exists\n\
    -l                  low quality settings\n\
    -h                  high quality settings\n\
    -o \"<lame options>\" overrides script default options
\n\
  example:\n\
    $0  -r  -f  -o \"-v -V 0 -b 112\" a*.wav z*.aif g*.mp?\n\
\n\
"

#   process command-line options
#   this could be extended to fake the 
#   commandline interface of the mp3encoder

while getopts ":o:r:h:l:f" optn; do
    case $optn in
    o ) options=$OPTARG 	# replace default options
	echo New lame options are \'$options\'
        ;; 
    r ) removesource=true
        echo Removing source files after successfully converting
	;;
    f ) testoverwrite=false
        echo Force overwriting existing destination files
	;;
    h ) options=$options_high
        ;;
    l ) options=$options_low
        ;;
    \? ) printf "$helptext"
	sleep $errordelay
        exit 1  
        ;;
    esac
done
shift $(($OPTIND - 1))

# no files remaining?

if [ "$1" = "" ]; then
    printf "$helptext"
    sleep $errordelay
    exit 1  
fi

#   process input-files

for src in "$@"; do

    case $src in
    *[.][wW][aA][vV]  )
        dst=${src%[.][wW][aA][vV]}.mp3
        if [ -f "$src" ]; then
            if [ $testoverwrite = true -a -f "$dst" ]; then
                echo \'$dst\' already exists, skipping
		sleep $errordelay
            elif $mp3coder $options `$mp3analyzer "$src"` "$src" "$dst"; then
                if [ $removesource = true ]; then
                    rm -f "$src"
                fi
            else
                echo converting of \'$src\' to \'$dst\' failed
		sleep $errordelay
            fi
        else
            echo No source file \'$src\' found.
	    sleep $errordelay
        fi
        ;;

    *[.][aA][iI][fF]  )
        dst=${src%[.][aA][iI][fF]}.mp3
        if [ -f "$src" ]; then
            if [ $testoverwrite = true -a -f "$dst" ]; then
                echo \'$dst\' already exists, skipping
		sleep $errordelay
            elif $mp3coder $options "$src" "$dst"; then
                if [ $removesource = true ]; then
                    rm -f "$src"
                fi
            else
                echo converting of \'$src\' to \'$dst\' failed
		sleep $errordelay
            fi
        else
            echo No source file \'$src\' found.
	    sleep $errordelay
        fi
        ;;

    *[.][aA][iI][fF][fF] )
        dst=${src%[.][aA][iI][fF][fF]}.mp3
        if [ -f "$src" ]; then
            if [ $testoverwrite = true -a -f "$dst" ]; then
                echo \'$dst\' already exists, skipping
		sleep $errordelay
            elif $mp3coder $options "$src" "$dst"; then
                if [ $removesource = true ]; then
                    rm -f "$src"
                fi
            else
                echo converting of \'$src\' to \'$dst\' failed
		sleep $errordelay
            fi
        else
            echo No source file \'$src\' found.
	    sleep $errordelay
        fi
        ;;

    *[.][mM][pP][gG12]  )
        dst=${src%[.][mM][pP][gG12]}.mp3
        if [ -f "$src" ]; then
            if [ $testoverwrite = true -a -f "$dst" ]; then
                echo \'$dst\' already exists, skipping
		sleep $errordelay
            elif $mp3coder $options "$src" "$dst"; then
                if [ $removesource = true ]; then
                    rm -f "$src"
                fi
            else
                echo converting of \'$src\' to \'$dst\' failed
		sleep $errordelay
            fi
        else
            echo No source file \'$src\' found.
	    sleep $errordelay
        fi
        ;;

    *[.][mM][pP]3 )
        dst=${src%[.][mM][pP]3}-new-converted-file.${src##*.}
        if [ -f "$src" ]; then
            if [ $testoverwrite = true -a -f "$dst" ]; then
                echo \'$dst\' already exists, skipping
		sleep $errordelay
            elif $mp3coder $options "$src" "$dst"; then
                if [ $removesource = true ]; then
                    mv -f "$dst" "$src"
                fi
            else
                echo converting of \'$src\' to \'$dst\' failed
		sleep $errordelay
            fi
        else
            echo No source file \'$src\' found.
	    sleep $errordelay
        fi
        ;;

    * ) # the rest
        echo warning: File extention \'.${src##*.}\' not supported
        sleep $errordelay
        ;;

    esac

done