Advertisement
Guest User

mp3ify

a guest
May 26th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 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 -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. python -c "
  38. import mutagen
  39. input = mutagen.File(\"$INPUT_FILE\", easy = True)
  40. output = mutagen.File(\"$OUTPUT_FILE\", easy = True)
  41. for tag in [ 'artist', 'album', 'tracknumber', 'date', 'genre', 'title', 'comment' ]:
  42. value = input.get(tag)
  43. if value: output[tag] = value[0]
  44. output.save(v1=2)"
  45. }
  46.  
  47.  
  48. function usage {
  49.  
  50. echo "mp3ify <input_dir> [<output_dir>]
  51.  
  52. Transforms structure <input_dir>/X/Y/.../Z into structure <output_dir>/X/Y/.../Z
  53. according to the following rules:
  54.  
  55. flac, ogg, m4a, ape, aac, mpc files will be encoded to mp3 preserving tags.
  56.  
  57. Everything else will be copied without modification.
  58.  
  59. Requires: mplayer, lame, mutagen.
  60. "
  61. exit 1
  62. }
  63.  
  64.  
  65. INPUT_DIR="$1"
  66. [ -d "$INPUT_DIR" ] || usage
  67. OUTPUT_DIR="${2:-$DEFAULT_OUTPUT_DIR}"
  68.  
  69. find "$INPUT_DIR" -name '*.*' | while read INPUT_FILE
  70. do
  71. INPUT_EXTENSION="${INPUT_FILE##*.}"
  72. INPUT_EXTENSION_LOWERCASE=`echo $INPUT_EXTENSION | tr "[A-Z]" "[a-z]"`
  73. OUTPUT_FILE="$OUTPUT_DIR/${INPUT_FILE#$INPUT_DIR}"
  74. mkdir -p "`dirname "$OUTPUT_FILE"`"
  75.  
  76.  
  77. case $INPUT_EXTENSION_LOWERCASE in
  78. flv|flac|m4a|ogg|ape|aac|mpc|mp4|wav)
  79. OUTPUT_FILE="${OUTPUT_FILE%.$INPUT_EXTENSION}.mp3"
  80. echo -n "Converting ${INPUT_FILE##*/}... "
  81. any_to_mp3
  82. ;;
  83. *)
  84. echo -n "Copying ${INPUT_FILE##*/}... "
  85. cp "$INPUT_FILE" "$OUTPUT_FILE"
  86. ;;
  87. esac
  88.  
  89. echo "done."
  90. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement