mp3me.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/sh
  2. # mp3me
  3. # author: lotus
  4. # https://git.zerohack.xyz/lotus
  5. # license: BSD 3-clause
  6. #
  7. # A portable shell script for converting audio files to mp3.
  8. # It works on directories of music in parallel.
  9. #
  10. # Dependencies: ffmpeg, gnu parallel
  11. #
  12. VERSION="0.1"
  13. # default bitrate and jobs
  14. BITRATE=320000
  15. JOBS=4
  16. EXT[0]="flac"
  17. EXT[1]="m4a"
  18. EXT[2]="ogg"
  19. EXT[3]="wav"
  20. EXT[4]="aac"
  21. # check if the file provided is a has a valid music extension
  22. # cuts off extension and compares it to the ext array items
  23. fun_is_music () {
  24. local FILE_EXT=$(echo $1 | cut -d"." -f 2)
  25. for E in "${EXT[@]}" ; do
  26. if [ $E = $FILE_EXT ] ; then
  27. return 0
  28. fi
  29. done
  30. return 1
  31. }
  32. # echoes out all the valid music files in a directory
  33. fun_echo_files () {
  34. for FILE in $(ls $1) ; do
  35. if fun_is_music $FILE ; then
  36. echo $FILE
  37. fi
  38. done
  39. }
  40. # take user input as kbps and return bps
  41. fun_get_bitrate () {
  42. if [ $1 > 0 ] && [ $1 < 321 ] ; then
  43. BITRATE=$(($1*1000))
  44. return 0
  45. fi
  46. return 1
  47. }
  48. # parse the user input using getopts
  49. fun_parse_options () {
  50. }
  51. # call this function to start the script
  52. fun_run () {
  53. JOBS=$1
  54. fun_echo_files $2 | parallel -j $JOBS ffmpeg -i $2/{} -b:a $BITRATE $2/{.}.mp3
  55. }
  56. # run when ctrl-c is pressed
  57. fun_cleanup () {
  58. echo "caught sigint, exiting.."
  59. exit 0
  60. }
  61. # print help/version info
  62. fun_print_info () {
  63. echo "mp3me - parallel mp3 converter script"
  64. fun_print_ver
  65. echo "license: bsd 3-clause"
  66. echo "author: lotus"
  67. fun_print_usage
  68. }
  69. # print usage info
  70. fun_print_usage () {
  71. echo "-- usage: --------------------"
  72. echo " mp3me -j 8 ./mymusicdir"
  73. echo " mp3me -h # shows this help message"
  74. echo " mp3me -v # show the version info"
  75. }
  76. # print version info
  77. fun_print_ver () {
  78. echo "version: $VERSION"
  79. }
  80. # print error message
  81. fun_error () {
  82. echo "Please use one of the following flags:"
  83. fun_print_usage
  84. exit 1
  85. }
  86. #fun_parse_options
  87. while getopts 'b:j:i:' OPT ; do
  88. case "$OPT" in
  89. b) echo "bitrate";;
  90. j) echo "jobs";;
  91. i) echo "input files";;
  92. [?]) fun_error();;
  93. esac
  94. done
  95. shift $OPTIND-1
  96. # parse what arguments were passed in
  97. # call a corresponding function
  98. #trap 'fun_cleanup' INT # catch a ctrl-c
  99. #case "$1" in
  100. # -h) shift; fun_print_info "$@" ;;
  101. # -v) shift; fun_print_ver "$@" ;;
  102. # -j) shift; fun_run "$@" ;;
  103. # *) fun_error "$@" ;;
  104. #esac
  105. exit 0