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 -d [cdrom device]
33 -s [start track]
34 -e [end track]
35 -N: don't run cdparanoia 
36 EOF
37 }
38
39 start_track=0
40 end_track=0
41 no_cdparanoia=0
42 cd_dev=/dev/cdrom
43 while getopts  "d:e:hNs:" flag
44 do
45     case $flag in
46     d)  cd_dev=$OPTARG;;
47
48     e)  end_track=$OPTARG
49         if [ $end_track == 0 ]; then
50             echo "must give non-zero numeric argument";
51             exit 1
52         fi;;
53
54     h) usage
55         exit 0;;
56
57     N)  no_cdparanoia=1;;
58
59     s)  start_track=$OPTARG
60         if [ $start_track == 0 ]; then
61             echo "must give non-zero numeric argument";
62             exit 1
63         fi;;
64
65     *)
66         echo
67         usage
68         exit 1;;
69     esac
70     #echo "$flag" $OPTIND $OPTARG
71 done
72
73 ########### checks ########### 
74 if [ $UID -eq 0 ]; then
75     echo "DON'T run this as root! chmod the device file to yourself instead."
76     exit 1
77 fi
78
79 ########### directory stuff ########### 
80 base_dir=`pwd | sed 's|^\(.*\)/\([^/]*\)$|\1|'`
81 trailing_dir=`pwd | sed 's|^\(.*\)/\([^/]*\)$|\2|'`
82 lossless_dir="${base_dir}/${trailing_dir} [LL]"
83 echo "base_dir = \"${base_dir}\""
84 echo "lossless_dir = \"${lossless_dir}\""
85 mkdir -p "${lossless_dir}"
86
87 ############# cdparanoia ###############
88 if [ ${no_cdparanoia} -eq 0 ]; then
89     if [ ${start_track} -ne 0 ]; then
90         if [ ${end_track} -ne 0 ]; then
91             span="${start_track}-${end_track}"
92         else
93             span="${start_track}-"
94         fi
95     else
96         if [ ${end_track} -ne 0 ]; then
97             span="-${end_track}"
98         else
99             span=
100         fi
101     fi
102     nice -1 cdparanoia -B -d ${cd_dev} ${span}
103     if [ $? -ne 0 ]; then
104         echo "cdparanoia failed; aborting."
105         exit 1
106     fi
107 fi
108
109 ############# mp3 ###############
110 wav_to_mp3 256 2
111
112 ############# flac ###############
113 mv *.wav "${lossless_dir}/"
114 cd "${lossless_dir}"
115 flac *.wav
116 flac -t *.flac && rm -f *.wav