Makefile: add pickrand
[cmccabe-bin] / audiorip.sh
1 #!/usr/bin/env bash
2
3 #
4 # audiorip
5 #
6 # Shell script to rip an audio CD-ROM.
7 #
8 # To use this, first create a directory for the mp3s, and cd to that
9 # directory. Then run this script. This script also saves lossless copies of
10 # the mp3s in a directory ending with [LL].
11 #
12 # One of these days I'm going to rewrite this in Ruby or some other language,
13 # and include more advanced features, like the ability to rip multiple
14 # CD-ROMs at once. For now, this script works, and is a good example of
15 # using getopts with bash.
16 #
17 # Colin McCabe
18 #
19
20 wav_to_mp3()
21 {
22     for i in *.wav; do
23         lame -q $2 -b $1 "$i"
24     done
25 }
26
27 usage() 
28 {
29     cat <<EOF
30 audiorip: a CD-ripping script
31 -h: this help message
32 -B: audiobook mode.
33 -d [cdrom device]
34 -s [start track]
35 -e [end track]
36 -N: don't run cdparanoia 
37 EOF
38 }
39
40 start_track=0
41 end_track=0
42 no_cdparanoia=0
43 cd_dev=/dev/cdrom
44 lame_bitrate=256
45 lame_q=2
46 flac=1
47 while getopts  "Bd:e:hNs:" flag
48 do
49     case $flag in
50     B)  lame_bitrate="96"
51         lame_q="2"
52         flac=0
53         ;;
54
55     d)  cd_dev=$OPTARG;;
56
57     e)  end_track=$OPTARG
58         if [ $end_track == 0 ]; then
59             echo "must give non-zero numeric argument";
60             exit 1
61         fi;;
62
63     h) usage
64         exit 0;;
65
66     N)  no_cdparanoia=1;;
67
68     s)  start_track=$OPTARG
69         if [ $start_track == 0 ]; then
70             echo "must give non-zero numeric argument";
71             exit 1
72         fi;;
73
74     *)
75         echo
76         usage
77         exit 1;;
78     esac
79     #echo "$flag" $OPTIND $OPTARG
80 done
81
82 ########### checks ########### 
83 if [ $UID -eq 0 ]; then
84     echo "DON'T run this as root! chmod the device file to yourself instead."
85     exit 1
86 fi
87
88 ########### directory stuff ########### 
89 base_dir=`pwd | sed 's|^\(.*\)/\([^/]*\)$|\1|'`
90 trailing_dir=`pwd | sed 's|^\(.*\)/\([^/]*\)$|\2|'`
91 lossless_dir="${base_dir}/${trailing_dir} [LL]"
92 echo "base_dir = \"${base_dir}\""
93 echo "lossless_dir = \"${lossless_dir}\""
94 [ $flac == 1 ] && mkdir -p "${lossless_dir}"
95
96 ############# cdparanoia ###############
97 if [ ${no_cdparanoia} -eq 0 ]; then
98     if [ ${start_track} -ne 0 ]; then
99         if [ ${end_track} -ne 0 ]; then
100             span="${start_track}-${end_track}"
101         else
102             span="${start_track}-"
103         fi
104     else
105         if [ ${end_track} -ne 0 ]; then
106             span="-${end_track}"
107         else
108             span=
109         fi
110     fi
111     nice -1 cdparanoia -B -d ${cd_dev} ${span}
112     if [ $? -ne 0 ]; then
113         echo "cdparanoia failed; aborting."
114         exit 1
115     fi
116 fi
117
118 ############# mp3 ###############
119 wav_to_mp3 $lame_bitrate $lame_q
120
121
122 ############# flac ###############
123 if [ $flac == 1 ]; then
124     mv *.wav "${lossless_dir}/"
125     cd "${lossless_dir}"
126     flac *.wav
127     flac -t *.flac && rm -f *.wav
128 else
129     rm -f *.wav
130     exit 0
131 fi