Advertisement
Guest User

mp3ify.sh

a guest
Dec 18th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script that will convert a lot of audio formats to MP3 via the commandline.
  4. # Dependencies: MPlayer, mutagen and lame (It should accept every format that both mutagen and Mplayer accept).
  5.  
  6. # mp3ify <input_dir> [<output_dir>] transforms structure <input_dir>/X/Y/.../Z into structure <output_dir>/X/Y/.../Z according to the following rules:
  7.  
  8. #    * flac, ogg, m4a, ape, aac, mpc files will be encoded to mp3 preserving tags.
  9.  
  10. #    * Everything else will be copied without modification.
  11.  
  12. # Default output_dir is /tmp/mp3ify.
  13.  
  14. # lame settings fine-tunning is done editing the configuration variables here below.
  15.  
  16.  
  17. DEFAULT_OUTPUT_DIR=/tmp/mp3ify
  18. QUALITY=2
  19. VBR_QUALITY=4
  20. MIN_BITRATE=64
  21. MAX_BITRATE=256
  22. SAMPLE_FREQ=44.1
  23.  
  24.  
  25. function any_to_mp3 {
  26.  
  27.   PIPE=`mktemp -u -t mp3ify.pipe.XXXXXX`
  28.   mkfifo "$PIPE"
  29.  
  30.   mplayer -v -nocorrect-pts -vo null -vc null -ao pcm:fast:file="$PIPE" "$INPUT_FILE" -noconsolecontrols > /dev/null 2>&1 &
  31.  
  32.   lame -m j -q $QUALITY -v -V $VBR_QUALITY -b $MIN_BITRATE \
  33.        -B $MAX_BITRATE -s $SAMPLE_FREQ "$PIPE" "$OUTPUT_FILE" > /dev/null 2>&1
  34.  
  35.   rm "$PIPE"
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. if [ "$INPUT_EXTENSION_LOWERCASE" != "3gp" ]; then
  43. python -c "
  44. import mutagen
  45. input = mutagen.File(\"$INPUT_FILE\", easy = True)
  46. output = mutagen.File(\"$OUTPUT_FILE\", easy = True)
  47. for tag in [ 'artist', 'album', 'tracknumber', 'date', 'genre', 'title', 'comment' ]:
  48.  value = input.get(tag)
  49.  if value: output[tag] = value[0]
  50.  output.save(v1=2)"
  51. fi
  52. }
  53.  
  54.  
  55. function usage {
  56.  
  57.   echo "mp3ify <input_dir> [<output_dir>]
  58.  
  59.  Transforms structure <input_dir>/X/Y/.../Z into structure <output_dir>/X/Y/.../Z
  60.  according to the following rules:
  61.  
  62.    flac, ogg, m4a, ape, aac, mpc, 3gp files will be encoded to mp3 preserving tags.
  63.  
  64.    Everything else will be copied without modification.
  65.  
  66.  Requires: mplayer, lame, mutagen.
  67. "
  68.   exit 1
  69. }
  70.  
  71.  
  72. INPUT_DIR="$1"
  73. [ -d "$INPUT_DIR" ] || usage
  74. OUTPUT_DIR="${2:-$DEFAULT_OUTPUT_DIR}"
  75.  
  76. find "$INPUT_DIR" -name '*.*' | while read INPUT_FILE
  77. do
  78.     INPUT_EXTENSION="${INPUT_FILE##*.}"
  79.     INPUT_EXTENSION_LOWERCASE=`echo $INPUT_EXTENSION | tr "[A-Z]" "[a-z]"`
  80.     OUTPUT_FILE="$OUTPUT_DIR/${INPUT_FILE#$INPUT_DIR}"
  81.     mkdir -p "`dirname "$OUTPUT_FILE"`"
  82.  
  83.  
  84.     case $INPUT_EXTENSION_LOWERCASE in
  85.     flv|flac|m4a|ogg|ape|aac|mpc|mp4|wav|3gp|mpg|mpeg|avi)
  86.     OUTPUT_FILE="${OUTPUT_FILE%.$INPUT_EXTENSION}.mp3"
  87.         echo -n "Converting ${INPUT_FILE##*/}... "
  88.         any_to_mp3
  89.     ;;
  90.     *)
  91.     echo -n "Copying ${INPUT_FILE##*/}... "
  92.         cp "$INPUT_FILE" "$OUTPUT_FILE"
  93.     ;;
  94.   esac
  95.  
  96.   echo "done."
  97. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement