Advertisement
wallyweek

flac2mp3

Mar 12th, 2018
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. LAMEOPTS="-b 320 --quiet"
  4. OGGOPTS="-b 320 --quiet"
  5.  
  6. while getopts ":s:f:e:" opt; do
  7.   case $opt in
  8.     f)
  9.       FILE="$OPTARG"
  10.       ;;
  11.     s)
  12.       SPLIT="$OPTARG"
  13.       ;;
  14.     e)
  15.       FMT="$OPTARG"
  16.       ;;
  17.     \?)
  18.       echo "Invalid option: -$OPTARG" >&2
  19.       exit 1
  20.       ;;
  21.     :)
  22.       echo "Option -$OPTARG requires an argument." >&2
  23.       exit 1
  24.       ;;
  25.   esac
  26. done
  27.  
  28. # Set default format to mp3
  29. case ${FMT:="mp3"} in
  30.     mp3|MP3|ogg|OGG)
  31.     ;;
  32.     *)
  33.         echo "Unknown format $FMT" >&2
  34.         exit 1
  35.     ;;
  36. esac
  37.  
  38. # Check both files
  39. if [ ! -f "$FILE" -o ! -f "$SPLIT" ]
  40. then
  41.     echo "You must specify correct flac file and CUE/TOC file." >&2
  42.     exit 1
  43. fi
  44.  
  45. #Get number of tracks
  46. NUMTRACKS=`cueprint -d '%N' "${SPLIT}"`
  47.  
  48. for i in `seq 1 $NUMTRACKS`
  49. do
  50.     # Clear previous obtained variables
  51.     unset PERFORMER
  52.     unset ALBUM
  53.     unset TITLE
  54.    
  55.     # Set performer, album, track title
  56.     PERFORMER=`cueprint -n $i -t '%p' "${SPLIT}"`
  57.     ALBUM=`cueprint -d '%T' "${SPLIT}"`
  58.     TITLE=`cueprint -n $i -t '%t' "${SPLIT}"`
  59.    
  60.     ## Check perfomer, album and track title
  61.     if [ -z "$PERFORMER" ]
  62.     then
  63.         echo "Track $i: Can not obtain performer from cue, set it to 'Unknown Artist'" >&2
  64.         PERFORMER="Unknown Artist"
  65.     fi
  66.    
  67.     if [ -z "$ALBUM" ]
  68.     then
  69.         echo "Track $i: Can not obtain album from cue, set it to 'Unknown Album'" >&2
  70.         ALBUM="Unknown Album"
  71.     fi
  72.    
  73.     if [ -z "$TITLE" ]
  74.     then
  75.         echo "Track $i: Can not obtain track from cue, set it to 'Track $i'" >&2
  76.         TITLE="Track $i"
  77.     fi
  78.    
  79.     ## End
  80.    
  81.     # Split and encoding files
  82.     echo "Encoding track $i/$NUMTRACKS."
  83.     if [ "$FMT" = "mp3" -o "$FMT" = "MP3" ]
  84.     then        
  85.         cuebreakpoints "$SPLIT"| shnsplit -q -o "cust ext=mp3 lame  $LAMEOPTS - %f" \
  86. -x $i -O always -a "" -z " - $TITLE" "$FILE"
  87.         OUTPUT=`printf "%.2d - ${TITLE}.mp3" $i`
  88.         id3v2 -T $i -a "$PERFORMER" -A "$ALBUM" -t "$TITLE" "$OUTPUT"
  89.     fi
  90.    
  91.     if [ "$FMT" = "ogg" -o "$FMT" = "OGG" ]
  92.     then
  93.         cuebreakpoints "$SPLIT"|shnsplit -q -o "cust ext=ogg oggenc $OGGOPTS -o %f -" \
  94. -x $i -O always -a "" -z " - $TITLE" "$FILE"
  95.         OUTPUT=`printf "%.2d - ${TITLE}.ogg" $i`
  96.         echo -e "TRACKNUMBER=${i}\nARTIST=${PERFORMER}\nPERFORMER=${PERFORMER}" \
  97.                              "\nALBUM=${ALBUM}\nTITLE=${TITLE}\n"|vorbiscomment "$OUTPUT"
  98.     fi    
  99.        
  100. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement