You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

90 lines
2.1 KiB

  1. #!/usr/bin/env bash
  2. DEFAULT_BASEDIR=$(pwd)
  3. DEFAULT_CONVERTER="ffmpeg"
  4. FLACDIR="flac"
  5. MP3DIR="mp3"
  6. while [[ $# -gt 0 ]]; do
  7. opt="$1"
  8. shift;
  9. case "$opt" in
  10. "-d"|"--dir")
  11. BASEDIR="$1"
  12. shift;
  13. ;;
  14. "-c"|"--converter")
  15. CONVERTER="$1"
  16. shift;
  17. ;;
  18. "-h"|"--help")
  19. usage
  20. ;;
  21. esac
  22. done
  23. if [ -z ${BASEDIR} ]; then
  24. echo "No Basedir parameter given."
  25. BASEDIR="${DEFAULT_BASEDIR}"
  26. fi
  27. if [ -z ${CONVERTER} ]; then
  28. CONVERTER="${DEFAULT_CONVERTER}"
  29. fi
  30. usage() {
  31. cat<<EOF
  32. Usage: $0 [-d BASEDIR] [-c CONVERTER]
  33. -d, --dir BASEDIR The root directory where to start to convert recursively.
  34. -c, --converter CONVERTER The converter to use. Choices are "ffmpeg" or "libav"
  35. -h, --help This usage dialog
  36. EOF
  37. exit
  38. }
  39. convert_to_mp3() {
  40. NUM_FILES=$(find . -type f -iname '*.flac' | wc -l)
  41. CURRENT=${NUM_FILES}
  42. IFS=$'\n'; for FLACFILE in $(find ${BASEDIR}/${FLACDIR}/ -type f -iname '*.flac'); do
  43. SRCPATH=${FLACFILE}
  44. DSTPATH=$(echo ${FLACFILE} | sed "s|^${BASEDIR}/${FLACDIR}/|${BASEDIR}/${MP3DIR}/|g;s|\.flac$|\.mp3|g")
  45. SRCDIR=$(dirname "${SRCPATH}")
  46. DSTDIR=$(dirname "${DSTPATH}")
  47. if [ ! -d "${DSTDIR}" ]; then
  48. echo "Creating ${DSTDIR}."
  49. mkdir -p "${DSTDIR}"
  50. fi
  51. if [ ! -e "${DSTDIR}/folder.jpg" ]; then
  52. echo "Copying cover art into ${DSTDIR}."
  53. cp "${SRCDIR}/folder.jpg" "${DSTDIR}/folder.jpg"
  54. fi
  55. if [ ! -e "${DSTPATH}" ]; then
  56. echo "Converting file ${CURRENT} of ${NUM_FILES}: $(basename $FLACFILE) -> $(basename $DSTPATH)"
  57. if [ ${CONVERTER} == "libav" ]; then
  58. # Convert using libav
  59. avconv -i "${SRCPATH}" -c:a libmp3lame -b:a 192k -map_metadata 0:g:0 "${DSTPATH}"
  60. elif [ ${CONVERTER} == "ffmpeg" ]; then
  61. # Convert using ffmpeg
  62. ffmpeg -i "${SRCPATH}" -ab 192k -vcodec copy -map_metadata 0:g:0 -hide_banner -v 0 "${DSTPATH}"
  63. fi
  64. fi
  65. let CURRENT-=1
  66. done
  67. }
  68. echo "Using ${BASEDIR} as basedir"
  69. echo " Therefore we are converting from ${BASEDIR}/${FLACDIR} to ${BASEDIR}/${MP3DIR}"
  70. read -p "Press any key to continue... (CTRL+C to abort)" -n1 -s
  71. cd ${BASEDIR}
  72. convert_to_mp3
  73. # vim: set ts=2 sw=2 tw=0 noet :