1#!/bin/sh 
2
3############################################################################
4#   
5#  Run the LAME encoder on multiple files, with option to delete .wav files
6#  after encoding.  "mlame -?" will give instructions.
7#
8#  Robert Hegemann
9#  modified on request: Frank Klemm <pfk@uni-jena.de>
10#
11############################################################################
12
13# encoder path to use
14mp3coder="lame"
15mp3analyzer="mlame_corr"
16
17# default options to use
18options_low="-h -d -mj -b 128"
19options_high="-h -d -mj -V 1 -b 112 -B 320"
20options=$options_high
21
22# remove source?
23removesource=false
24
25# force overwrite of destination
26testoverwrite=true
27
28# waiting after error report n seconds
29errordelay=1 
30
31helptext="\n\
32This script runs the LAME mp3 encoder on multiple files: \n\n\
33    $0 [options] <file 1> ... <file n>\n\
34\n\
35  options:\n\
36    -?                  this help text\n\
37    -r                  remove files after encoding\n\
38    -f                  force overwrite of destination if exists\n\
39    -l                  low quality settings\n\
40    -h                  high quality settings\n\
41    -o \"<lame options>\" overrides script default options
42\n\
43  example:\n\
44    $0  -r  -f  -o \"-v -V 0 -b 112\" a*.wav z*.aif g*.mp?\n\
45\n\
46"
47
48#   process command-line options
49#   this could be extended to fake the 
50#   commandline interface of the mp3encoder
51
52while getopts ":o:r:h:l:f" optn; do
53    case $optn in
54    o ) options=$OPTARG 	# replace default options
55	echo New lame options are \'$options\'
56        ;; 
57    r ) removesource=true
58        echo Removing source files after successfully converting
59	;;
60    f ) testoverwrite=false
61        echo Force overwriting existing destination files
62	;;
63    h ) options=$options_high
64        ;;
65    l ) options=$options_low
66        ;;
67    \? ) printf "$helptext"
68	sleep $errordelay
69        exit 1  
70        ;;
71    esac
72done
73shift $(($OPTIND - 1))
74
75# no files remaining?
76
77if [ "$1" = "" ]; then
78    printf "$helptext"
79    sleep $errordelay
80    exit 1  
81fi
82
83#   process input-files
84
85for src in "$@"; do
86
87    case $src in
88    *[.][wW][aA][vV]  )
89        dst=${src%[.][wW][aA][vV]}.mp3
90        if [ -f "$src" ]; then
91            if [ $testoverwrite = true -a -f "$dst" ]; then
92                echo \'$dst\' already exists, skipping
93		sleep $errordelay
94            elif $mp3coder $options `$mp3analyzer "$src"` "$src" "$dst"; then
95                if [ $removesource = true ]; then
96                    rm -f "$src"
97                fi
98            else
99                echo converting of \'$src\' to \'$dst\' failed
100		sleep $errordelay
101            fi
102        else
103            echo No source file \'$src\' found.
104	    sleep $errordelay
105        fi
106        ;;
107
108    *[.][aA][iI][fF]  )
109        dst=${src%[.][aA][iI][fF]}.mp3
110        if [ -f "$src" ]; then
111            if [ $testoverwrite = true -a -f "$dst" ]; then
112                echo \'$dst\' already exists, skipping
113		sleep $errordelay
114            elif $mp3coder $options "$src" "$dst"; then
115                if [ $removesource = true ]; then
116                    rm -f "$src"
117                fi
118            else
119                echo converting of \'$src\' to \'$dst\' failed
120		sleep $errordelay
121            fi
122        else
123            echo No source file \'$src\' found.
124	    sleep $errordelay
125        fi
126        ;;
127
128    *[.][aA][iI][fF][fF] )
129        dst=${src%[.][aA][iI][fF][fF]}.mp3
130        if [ -f "$src" ]; then
131            if [ $testoverwrite = true -a -f "$dst" ]; then
132                echo \'$dst\' already exists, skipping
133		sleep $errordelay
134            elif $mp3coder $options "$src" "$dst"; then
135                if [ $removesource = true ]; then
136                    rm -f "$src"
137                fi
138            else
139                echo converting of \'$src\' to \'$dst\' failed
140		sleep $errordelay
141            fi
142        else
143            echo No source file \'$src\' found.
144	    sleep $errordelay
145        fi
146        ;;
147
148    *[.][mM][pP][gG12]  )
149        dst=${src%[.][mM][pP][gG12]}.mp3
150        if [ -f "$src" ]; then
151            if [ $testoverwrite = true -a -f "$dst" ]; then
152                echo \'$dst\' already exists, skipping
153		sleep $errordelay
154            elif $mp3coder $options "$src" "$dst"; then
155                if [ $removesource = true ]; then
156                    rm -f "$src"
157                fi
158            else
159                echo converting of \'$src\' to \'$dst\' failed
160		sleep $errordelay
161            fi
162        else
163            echo No source file \'$src\' found.
164	    sleep $errordelay
165        fi
166        ;;
167
168    *[.][mM][pP]3 )
169        dst=${src%[.][mM][pP]3}-new-converted-file.${src##*.}
170        if [ -f "$src" ]; then
171            if [ $testoverwrite = true -a -f "$dst" ]; then
172                echo \'$dst\' already exists, skipping
173		sleep $errordelay
174            elif $mp3coder $options "$src" "$dst"; then
175                if [ $removesource = true ]; then
176                    mv -f "$dst" "$src"
177                fi
178            else
179                echo converting of \'$src\' to \'$dst\' failed
180		sleep $errordelay
181            fi
182        else
183            echo No source file \'$src\' found.
184	    sleep $errordelay
185        fi
186        ;;
187
188    * ) # the rest
189        echo warning: File extention \'.${src##*.}\' not supported
190        sleep $errordelay
191        ;;
192
193    esac
194
195done
196