USAGE="usage: $0 [-d delimiter [-o {t|a}]] [-t {m|f|o}] [-p path] [-e encoder] [-c cddb-entry] [-h]\n" USAGE="${USAGE} -d :\tA single character delimiting trackname from\n" USAGE="${USAGE}\tartistname with sampler cds\n\n" USAGE="${USAGE} -o :\tspecifies if theres the track or the artist first\n" USAGE="${USAGE}\tMust be:\n" USAGE="${USAGE}\t t for titel first or\n" USAGE="${USAGE}\t a for artist first\n" USAGE="${USAGE}\tIf there is none specified t is assumed\n" USAGE="${USAGE} -t :\tyou can specify either m for encoding to mp3\n" USAGE="${USAGE}\tusing lame, or f for making lossless encoding using flac,\n" USAGE="${USAGE}\tor o for making ogg/vorbis, or w for uncompressed wav.\n" USAGE="${USAGE}\tOmitting this results in encoding to flac.\n" USAGE="${USAGE}\tmp3s will be placed under a subfolder mp3 and flacs\n" USAGE="${USAGE}\tunder a subfolder flac\n" USAGE="${USAGE} -p :\tspecifies the path to save the encoded data.\n" USAGE="${USAGE}\tDefaults to the users home directory.\n" USAGE="${USAGE} -e :\tspecifies the mp3 encoder to use.\n" USAGE="${USAGE}\tvalid encoders are actually: lame,bladeenc\n" USAGE="${USAGE}\tDefaults to lame.\n" USAGE="${USAGE} -c :\tspecifies the CDDB-Entry to use.\n" USAGE="${USAGE}\tsometimes there is more than 1 entry for a cd in cddb.\n" USAGE="${USAGE}\tThen you can specify wich one to use by this option.\n" USAGE="${USAGE}\tYou can checkout all entries using cddb.pl.\n" USAGE="${USAGE} -h :\tShows this help." PATH="$PATH:." HAS_DELIMITER=0 TA_ORDER="t" ENCODING_DIR=$HOME ENCODING_TYPE="f" ENCODER="lame" CDDB_ENTRY=1 while getopts d:o:t:p:he:c: option do case $option in d) if [ ${#OPTARG} -gt 1 ] then echo -e "$USAGE" exit 1 fi HAS_DELIMITER=1 TA_DELIMITER=$OPTARG ;; o) if [ \( "${OPTARG}" != "t" -a "${OPTARG}" != "a" \) -o \ ${HAS_DELIMITER} -eq 0 ] then echo -e "$USAGE" exit 1 fi TA_ORDER=$OPTARG ;; t) if [ ${OPTARG} != "m" -a ${OPTARG} != "f" -a ${OPTARG} != "o" -a \ ${OPTARG} != "w" ] then echo -e "$USAGE" exit 1 fi ENCODING_TYPE=$OPTARG ;; p) if [ ! \( -d ${OPTARG} \) ] then echo -e "$USAGE" exit 1 fi ENCODING_DIR=$OPTARG ;; e) if [ -z ${OPTARG} ] then echo -e "$USAGE" exit 1 fi ENCODER=$OPTARG ;; c) if [ ${OPTARG} -lt 1 ] then echo -e "$USAGE" exit 1 fi CDDB_ENTRY=$OPTARG ;; [h\?]) echo -e "$USAGE" exit 1 ;; esac done test "$ENCODING_TYPE" = "m" && ENCODING_DIR="$ENCODING_DIR/mp3" test "$ENCODING_TYPE" = "f" && ENCODING_DIR="$ENCODING_DIR/flac" test "$ENCODING_TYPE" = "o" && ENCODING_DIR="$ENCODING_DIR/ogg" test "$ENCODING_TYPE" = "w" && ENCODING_DIR="$ENCODING_DIR/wav" indices[0]="album" indices[1]="artist" indices[2]="genre" indices[3]="year" indices[4]="comment" i=1 max_track=30 while [ $i -le $max_track ]; do indices[`expr \( $i \- 1 \) \* 2 \+ 5`]="`echo $i | \ sed 's/^\([0-9]\)$/0\1/g;\ s/\(.*\)/track-\1/g'`" indices[`expr \( $i \- 1 \) \* 2 \+ 6`]="`echo $i | \ sed 's/^\([0-9]\)$/0\1/g;\ s/\(.*\)/tartist-\1/g'`" i=`expr $i \+ 1` done comment="" function idxof() { str=$1 idx=0 for i in "${indices[@]}"; do test "$i" == "$str" && echo $idx && return 0 idx=`expr $idx \+ 1` done echo -1 return 0 } title_pat="/^[^:]*: *(.*[^ ]).*/" artist_pat="//" if [ $HAS_DELIMITER -ne 0 ] then if [ $TA_ORDER == "t" ] then title_pat="/^[^:]*: *([^\\${TA_DELIMITER}]*[^ \\${TA_DELIMITER}]).*/" artist_pat="/^[^:]*: *[^\\${TA_DELIMITER}]*\\${TA_DELIMITER} *(.*[^ ]).*/" else artist_pat="/^[^:]*: *([^\\${TA_DELIMITER}]*[^ \\${TA_DELIMITER}]).*/" title_pat="/^[^:]*: *[^\\${TA_DELIMITER}]*\\${TA_DELIMITER} *(.*[^ ]).*/" fi fi echo "Preparing...." echo -n "getting cddb info..." eval "`cddb.pl -c /dev/cdrom1 -e $CDDB_ENTRY | sed 's/ \([0-9]\):/ 0\1:/g;s/track /track-/g' |\ awk -v has_delimiter=$HAS_DELIMITER \ '/artist/||/title/||/genre/||/year/ { \ cddb[gensub(/^([^:]*):.*/, \"\\\\1\", \"1\")]=\ gensub(/^[^:]*: *(.*[^ ]).*/, \"\\\\1\", \"g\")\ } \ /trackno/ {\ trackno=int(gensub(/^trackno: *([0-9]*).*/, \"\\\\1\", \"1\"))\ }\ /track-/ {\ cddb[gensub(/^([^:]*):.*/, \"\\\\1\", \"1\")]=\ gensub('\"$title_pat\"', \"\\\\1\", \"1\");\ if(has_delimiter==0) \ cddb[gensub(/^track([^:]*):.*/, \"tarti\\\\1\", \"1\")]=\ cddb[\"artist\"]; \ else \ cddb[gensub(/^track([^:]*):.*/, \"tarti\\\\1\", \"1\")]=\ gensub('\"$artist_pat\"', \"\\\\1\", \"1\");\ } \ END {\ cddb_str=\"cddb=(\\\"\" cddb[\"title\"] \"\\\" \\\"\" \ cddb[\"artist\"] \"\\\" \\\"\" \ cddb[\"genre\"] \"\\\" \\\"\" \ cddb[\"year\"] \"\\\" \\\"'$comment'\\\"\"; \ for(i=1; i<=trackno; i++) {\ cddb_str=cddb_str \" \\\"\" \ cddb[\"track-\" sprintf(\"%02s\", i)] \"\\\"\";\ cddb_str=cddb_str \" \\\"\" \ cddb[\"tarti-\" sprintf(\"%02s\", i)] \"\\\"\"\ }\ print cddb_str \")\"\ }'`" echo "done" echo -n "getting CD info..." cddainfo=`cdda2wav -H -D/dev/cdrom1 -J -v summary,toc,sectors 2>&1` echo "done" track_num=`echo "$cddainfo" | \ sed '/tracks/{s/^.*tracks:\([0-9]*\).*$/\1/g;p};d'` cd_time=`echo "$cddainfo" | \ sed '/tracks/{s/^.*time \([0-9:\.]*\).*$/\1/g;p};d'` cddbid=`echo "$cddainfo" | \ sed '/CDDB/{s/^.*discid: *\(0x[a-z0-9]*[^ ]\).*/\1/g;p};d'` cdindex=`echo "$cddainfo" | \ sed '/CDINDEX/{s/^.*discid: *\([a-zA-Z0-9_\.]*-\).*/\1/g;p};d'` cdtext=`echo "$cddainfo" | sed '/CD-Text/{s/^[^:]*: *\(.*[^ ]\).*/\1/g s/&/\&\;/g;s/\"/\"\;/g s//\>\;/g s/\o47/\&apos\;/g p};d'` cdextra=`echo "$cddainfo" | sed '/CD-Extra/{s/^[^:]*: *\(.*[^ ]\).*/\1/g s/&/\&\;/g;s/\"/\"\;/g s//\>\;/g s/\o47/\&apos\;/g p};d'` # cddb[`idxof "artist"`]="Blue Öyster Cult" ENCODING_DIR="${ENCODING_DIR}/${cddb[`idxof "artist"`]}/" ENCODING_DIR="${ENCODING_DIR}${cddb[`idxof "album"`]}/" mkdirhier "$ENCODING_DIR" echo "Output directory is: $ENCODING_DIR" xmlfile="${ENCODING_DIR}cdinfo.xml" # some special chars within xml must be converted to special syntax # & => & # " => " # ' => ' # < => < # > => > xmlartist=`echo ${cddb[\`idxof "artist"\`]} | \ sed 's/&/\&\;/g s//\>\;/g s/\"/\"\;/g s/\o47/\&apos\;/g'` xmlalbum=`echo ${cddb[\`idxof "album"\`]} | \ sed 's/&/\&\;/g s//\>\;/g s/\"/\"\;/g s/\o47/\&apos\;/g'` echo "" > "${xmlfile}" echo "" >> "${xmlfile}" echo "" >> "${xmlfile}" echo " $cdindex" >> "${xmlfile}" echo " $cddbid" >> "${xmlfile}" echo " $cdtext" >> "${xmlfile}" echo " $cdextra" >> "${xmlfile}" echo " ${xmlartist}" >> "${xmlfile}" echo " ${xmlalbum}" >> "${xmlfile}" echo " ${cddb[`idxof "year"`]}" >> "${xmlfile}" echo " ${cddb[`idxof "genre"`]}" >> "${xmlfile}" echo " $cd_time" >> "${xmlfile}" echo " $track_num" >> "${xmlfile}" echo " " >> "${xmlfile}" i=1; while [ $i -le $track_num ] do j=`expr $i \+ 1` tracknr=`awk 'BEGIN{printf("%02d\n",'$i')}'` track=${cddb[`idxof "track-$tracknr"`]} tartist=${cddb[`idxof "tartist-$tracknr"`]} xmltrack=`echo ${track} | sed 's/&/\&\;/g s//\>\;/g s/\"/\"\;/g s/\o47/\&apos\;/g'` xmltartist=`echo ${tartist} | sed 's/&/\&\;/g s//\>\;/g s/\"/\"\;/g s/\o47/\&apos\;/g'` tracklen=`echo "$cddainfo" | \ sed '/ '"$i"'\.([0-9 ]*:/{s/.*'"$i"'\.( *\([0-9:\.]*\)).*/\1/g;p};d'` trackstart=`echo "$cddainfo" | \ sed '/ '"$i"'\.([0-9 ]*)/{s/.*'"$i"'\.( *\([0-9]*\)).*/\1/g;p};d'` test $i -eq $track_num && \ trackend=`echo "$cddainfo" | \ sed '/ lead-out([0-9 ]*)/{s/.*lead-out( *\([0-9]*\)).*/\1/g;p};d'` || \ trackend=`echo "$cddainfo" | \ sed '/ '"$j"'\.([0-9 ]*)/{s/.*'"$j"'\.( *\([0-9]*\)).*/\1/g;p};d'` test -z "$trackstart" -o -z "$trackend" && echo "Disk Error" && exit 1 echo " " >> "${xmlfile}" echo " $i" >> "${xmlfile}" echo " $tracklen" >> "${xmlfile}" echo " $trackstart" >> "${xmlfile}" echo " `expr $trackend \- $trackstart`" >> "${xmlfile}" echo " $xmltartist" >> "${xmlfile}" echo " $xmltrack" >> "${xmlfile}" echo " " >> "${xmlfile}" echo " " >> "${xmlfile}" # outfile="${ENCODING_DIR}The Wall/Disk2/" outfile="${ENCODING_DIR}${tracknr}-${track}" test "$ENCODING_TYPE" = "f" && \ outstr="track-$tracknr => ${tracknr}-${track}.flac" test "$ENCODING_TYPE" = "m" && \ outstr="track-$tracknr => ${tracknr}-${track}.mp3" test "$ENCODING_TYPE" = "o" && \ outstr="track-$tracknr => ${tracknr}-${track}.ogg" test "$ENCODING_TYPE" = "w" && \ outstr="track-$tracknr => ${tracknr}-${track}.wav" if [ "$ENCODING_TYPE" = "f" ] then bash -c "exec 2>&1;\ cdda2wav -H -D0,4,0 -t$i - | \ flac -c \ -T artist=\"${tartist}\" -T album=\"${cddb[`idxof "album"`]}\" \ -T genre=\"${cddb[`idxof "genre"`]}\" -T title=\"${track}\" \ -T tracknumber=\"`expr ${tracknr} \+ 0`\" \ -T tracknum=\"`expr ${tracknr} \+ 0`\" -T date=\"${cddb[`idxof "year"`]}\" \ -T comment=\"${cddb[`idxof "comment"`]}\" \ -o \"${outfile}.flac\" - 2>/dev/null" | rton | \ awk '/ [0-9 ][0-9]%/ && percent!=$5{percent=$5;printf("\r'"$outstr"': %s", percent)}END{print "\r'"$outstr"': 100%"}' fi if [ "$ENCODING_TYPE" = "m" -a "$ENCODER" = "lame" ] then bash -c "exec 2>&1;\ cdda2wav -H -D0,4,0 -t$i - | \ lame -h \ --ta \"${tartist}\" --tl \"${cddb[`idxof "album"`]}\" \ --tg \"${cddb[`idxof "genre"`]}\" --tt \"${track}\" \ --tn \"`expr ${tracknr} \+ 0`\" --ty \"${cddb[`idxof "year"`]}\" \ --tc \"${cddb[`idxof "comment"`]}\" \ - \"${outfile}.mp3\" 2>/dev/null" | rton | \ awk '/ [0-9 ][0-9]%/ && percent!=$5{percent=$5;printf("\r'"$outstr"': %s", percent)}END{print "\r'"$outstr"': 100%"}' fi if [ "$ENCODING_TYPE" = "m" -a "$ENCODER" = "bladeenc" ] then bash -c "exec 2>&1;\ cdda2wav -H -D0,4,0 -t$i - | \ bladeenc -progress=0 STDIN \"${outfile}.mp3\" 2>/dev/null" | rton | \ awk '/ [0-9 ][0-9]%/ && percent!=$5{percent=$5;printf("\r'"$outstr"': %s", percent)}END{print "\r'"$outstr"': 100%"}' fi if [ "$ENCODING_TYPE" = "o" ] then bash -c "exec 2>&1;\ cdda2wav -H -D0,4,0 -t$i - | \ oggenc -a \"${tartist}\" -l \"${cddb[`idxof "album"`]}\" \ -G \"${cddb[`idxof "genre"`]}\" -t \"${track}\" \ -N \"`expr ${tracknr} \+ 0`\" -d \"${cddb[`idxof "year"`]}\" \ -c \"${cddb[`idxof "comment"`]}\" \ -o \"${outfile}.ogg\" - 2>/dev/null" | rton | \ awk '/ [0-9 ][0-9]%/ && percent!=$5{percent=$5;printf("\r'"$outstr"': %s", percent)}END{print "\r'"$outstr"': 100%"}' fi if [ "$ENCODING_TYPE" = "w" ] then bash -c "exec 2>&1;\ cdda2wav -H -D0,4,0 -t$i - > \"${outfile}.wav\"" | rton | \ awk '/ [0-9 ][0-9]%/ && percent!=$5{percent=$5;printf("\r'"$outstr"': %s", percent)}END{print "\r'"$outstr"': 100%"}' fi i=`expr $i + 1` done echo "" >> "${xmlfile}"