GSRip

GSRip Git Source Tree

Root/gsrip.sh

1#!/bin/bash
2
3# GSRip is a wav/mp3/ogg command line Audio CD ripper
4# Copyright 2010 Grégory Soutadé
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19set -e
20
21CDDA2WAV=icedax
22LAME=lame
23OGGENC=oggenc
24EDITORS="emacs vim gedit kate"
25
26tmp=
27
28function _exit()
29{
30 cd /
31 [ ! -z "$tmp" ] && /bin/rm -rf $tmp
32}
33
34function usage()
35{
36 echo "gsrip : rip an audio CD into wav/mp3/ogg (Grégory Soutadé <soutade@gmail.com>)"
37 echo
38 echo "usage : gsrip.sh [options]"
39 echo
40 echo "Options :"
41 echo "-a|--auto"
42 echo "\tSelect the first entry from cddb server"
43 echo ""
44 echo "-b|--bitrate <bitrate>"
45 echo "mp3 bitrate (32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320), default : 128"
46 echo ""
47 echo "-d|--dest <directory>"
48 echo "mp3 destination, default : /tmp "
49 echo ""
50 echo "-D|--dev <device>"
51 echo "CDROM device, default : /dev/cdrom"
52 echo ""
53 echo "-e|--extract-only"
54 echo "Only extract wav"
55 echo ""
56 echo "-f|--file <file>"
57 echo "Use a .cddb file instead of retrieving from cddb server"
58 echo ""
59 echo "-h|--help"
60 echo "This message"
61 echo ""
62 echo "-o|--overwrite"
63 echo "Overwrite files if existing, default is skipping"
64 echo ""
65 echo "-p|--pattern <pattern>"
66 echo "Output filename pattern"
67 echo "%g, %a, %l, %n, %t, %y replaced by genre, artist, album, track number, title, and year"
68 echo "default is \"%n - %t\""
69 echo ""
70 echo "-r|--resume <path>"
71 echo "Don't extract wav files from CD, just encode. Wav files must be into <path>"
72 echo "This option can be combined with -f"
73 echo ""
74 echo "--wav"
75 echo "Keep .wav files"
76 echo ""
77 echo "--mp3"
78 echo "Encode wav into mp3"
79 echo ""
80 echo "--ogg"
81 echo "Encode wav into ogg"
82
83 exit
84}
85
86function isAbsolute()
87{
88 echo "$1" | grep "^/" > /dev/null
89
90 return $?
91}
92
93trap "_exit" 0
94
95auto=0
96bitrate=128
97bitrates="32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320"
98dev="/dev/cdrom"
99dest="/tmp"
100extract_only=0
101audio_cddb="audio.cddb"
102retrieve_infos=1
103overwrite=0
104pattern="%n - %t"
105resume=0
106keep_wav=0
107encode_mp3=0
108encode_ogg=0
109
110[ $# = 0 ] && usage
111
112while getopts ":ab:d:D:ef:hkop:r:-:" arg ; do
113 if [ "$arg" = "-" ] ; then
114arg="${OPTARG%%=*}"
115OPTARG="${OPTARG#*=}"
116 fi
117 case $arg in
118a|auto)
119 auto=1
120 ;;
121b|bitrate)
122 bitrate=$OPTARG
123 echo $bitrates | grep $bitrate
124 if [ ! $? ] ; then
125echo "Invalid bitrate $bitrate"
126echo "valid bitrates are $bitrates"
127exit 1
128 fi
129 ;;
130d|dest)
131 dest="$OPTARG"
132 isAbsolute $dest || dest=$PWD/$dest
133 ;;
134D|dev)
135 dev="$OPTARG"
136 ;;
137e|extract-only)
138 extract_only=1
139 ;;
140f|file)
141 audio_cddb="$OPTARG"
142 isAbsolute $audio_cddb || audio_cddb=$PWD/$audio_cddb
143 retrieve_infos=0
144 ;;
145h|"help")
146 usage
147 ;;
148o|overwrite)
149 overwrite=1
150 ;;
151p|pattern)
152 pattern="$OPTARG"
153 ;;
154r|resume)
155 resume=1
156 cd "$OPTARG"
157 retrieve_infos=0
158 ;;
159wav)
160 keep_wav=1
161 ;;
162mp3)
163 encode_mp3=1
164 if [ "`which $LAME`" == "" ] ; then
165echo "$LAME not found, cannot continue"
166exit 1
167 fi
168 ;;
169ogg)
170 encode_ogg=1
171 if [ "`which $OGGENC`" == "" ] ; then
172echo "$OGGENC not found, cannot continue"
173exit 1
174 fi
175 ;;
176":")
177 echo "Invalid argument: -$OPTARG need argument" >&2
178 usage
179 ;;
180"\?")
181 echo "Invalid option: -$OPTARG" >&2
182 usage
183 ;;
184 esac
185done
186
187if [ $keep_wav = 0 -a $encode_mp3 = 0 -a $encode_ogg = 0 ] ; then
188 echo "At least one of --wav --mp3 --ogg option must be set"
189 exit 1
190fi
191
192if [ $resume = 0 ] ; then
193 if [ "`which $CDDA2WAV`" == "" ] ; then
194echo "$CDDA2WAV not found, cannot continue"
195exit 1
196 fi
197
198 tmp=$(mktemp)
199 /bin/rm -f $tmp
200 /bin/mkdir -p $tmp
201 cd $tmp
202fi
203
204if [ $retrieve_infos = 0 ] ; then
205 if [ ! -f "$audio_cddb" ] ; then
206 echo "$audio_cddb not found"
207 exit
208 fi
209 cat "$audio_cddb"
210 echo
211 echo "Is this correct ? (y[es], n[o], e[xit], c[opy])"
212 echo -n "> "
213 read response
214 if [ "$response" = "c" -o "$response" = "copy" ] ; then
215 echo /bin/cp audio.cddb /tmp
216 /bin/cp "$audio_cddb" /tmp
217 exit
218 fi
219 [ ! -z "$response" -a $response != "y" -a $response = "yes" ] && exit
220else
221 if [ $auto = 1 ] ; then
222 $CDDA2WAV -D "$dev" -L 1 -J -v title
223 else
224# Info query
225while [ 1 ] ; do
226 if [ "$response" = "e" -o "$response" = "edit" ] ; then
227 cat "$audio_cddb"
228 else
229 $CDDA2WAV -D "$dev" -L 0 -J -v title
230 fi
231 echo
232 echo "Is this correct ? (y[es], n[o], q[uit], c[opy], e[dit])"
233 echo -n "> "
234 read response
235 if [ "$response" = "c" -o "$response" = "copy" ] ; then
236 echo /bin/cp audio.cddb /tmp
237 /bin/cp "$audio_cddb" /tmp
238 exit
239 fi
240 if [ "$response" = "e" -o "$response" = "edit" ] ; then
241 for editor in $EDITORS ; do
242 if [ "`which $editor`" != "" ] ; then
243 $editor "$audio_cddb"
244 break
245 fi
246 done
247 fi
248 [ -z "$response" -o "$response" = "y" -o "$response" = "yes" ] && break
249 [ "$response" = "q" -o "$response" = "quit" ] && exit
250done
251 fi
252fi
253
254# Read CD infos
255band="Unknown"
256album="Album"
257genre="unknown"
258year=""
259i=0
260if [ ! -f "$audio_cddb" ] ; then
261 while [ -d $dest/$band/$album ] ; do
262i=$((i + 1))
263album="album$i"
264 done
265else
266 infos=`cat "$audio_cddb" | grep DTITLE | cut -d= -f2`
267 band=$(echo $infos | cut -d/ -f1)
268 album=$(echo $infos | cut -d/ -f2)
269 genre=`cat "$audio_cddb" | grep DGENRE | cut -d= -f2`
270 year=`cat "$audio_cddb" | grep DYEAR | cut -d= -f2`
271fi
272
273# Suppress spaces
274band=$(echo $band | sed "s;/;-;g")
275album=$(echo $album | sed "s;/;-;g")
276genre=$(echo $genre | sed "s;/;-;g")
277year=$(echo $year | sed "s;/;-;g")
278
279if [ $resume = 0 ] ; then
280# Extract wav
281 tracks=""
282 track=""
283 tracknum=1
284 while [ 1 ] ;
285 do
286if [ $tracknum -lt 10 ] ; then
287 track="audio_0$tracknum"
288else
289 track="audio_$tracknum"
290fi
291[ ! -f "$track.inf" ] && break
292tracks="$tracks $track.wav"
293tracknum=$((tracknum + 1))
294 done;
295
296 $CDDA2WAV -B -D $dev $tracks
297fi
298
299# Convert into mp3
300dir="$dest/$band/$album"
301/bin/mkdir -p "$dir"
302
303if [ $extract_only = 1 ] ; then
304 cp *.wav $dir
305 exit
306fi
307
308i=0
309for track_wav in *.wav ; do
310
311 echo $track_wav | grep audio_ >/dev/null || continue
312
313 i=$(echo $track_wav | sed -r s/audio_0*\([0-9]*\).wav/\\1/g)
314 i=$((i - 1))
315 title=`cat "$audio_cddb" | grep TTITLE$i= | cut -d= -f2`
316
317 i=$((i + 1))
318
319 if [ $i -lt 10 ] ; then
320prefix="0$i"
321 else
322prefix=$i
323 fi
324
325 [ -z "$title" ] && title="Audio Track $prefix"
326
327 title=$(echo $title | sed "s;/;-;g")
328
329 filename=$pattern
330
331 filename=$(echo $filename | sed "s/%g/$genre/g")
332 filename=$(echo $filename | sed "s/%a/$album/g")
333 filename=$(echo $filename | sed "s/%l/$band/g")
334 filename=$(echo $filename | sed "s/%n/$prefix/g")
335 filename=$(echo $filename | sed "s/%y/$year/g")
336 filename=$(echo $filename | sed "s/%t/$title/g")
337
338 new_file="$dir/$filename"
339
340 if [ $encode_mp3 = 1 ] ; then
341if [ ! -f "$new_file.mp3" -o $overwrite = 1 ] ; then
342 $LAME -b $bitrate -h --tt "$title" --ta "$band" --tl "$album" --ty "$year" --tn $i --tg "$genre" --add-id3v2 $track_wav "$new_file.mp3"
343else
344 echo "$new_file.mp3 already exists, skipping"
345fi
346 fi
347
348 if [ $encode_ogg = 1 ] ; then
349if [ ! -f "$new_file.ogg" -o $overwrite = 1 ] ; then
350 $OGGENC -b $bitrate -q 10 -t "$title" -a "$band" --album "$album" -d "$year" -N $i -G "$genre" $track_wav -o "$new_file.mp3"
351else
352 echo "$new_file.ogg already exists, skipping"
353fi
354 fi
355
356 if [ $keep_wav = 1 ] ; then
357if [ ! -f "$new_file.wav" -o $overwrite = 1 ] ; then
358 cp $track_wav "$new_file.wav"
359else
360 echo "$new_file.wav already exists, skipping"
361fi
362 else
363 /bin/rm -f $track_wav
364
365 fi
366done
367
368echo "CD extracted into $dest/$dir"
369exit

Archive Download this file

Branches