SHARE
TWEET

cue2flacs

julian_hughes May 1st, 2012 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. #  cue2flacs
  4. #  Copyright (C) 2012 Julian Hughes julianhughes<at>gmailDOTcom
  5. #
  6. #  This program is free software; you can redistribute it and/or modify
  7. #  it under the terms of the GNU General Public License as published by
  8. #  the Free Software Foundation; either version 3 of the License, or
  9. #  (at your option) any later version.
  10. #
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #
  16. #  You should have received a copy of the GNU General Public License
  17. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # DEPENDENCIES:
  20. # shntool ; cuetag ; flac ; iconv
  21. # and any other codecs required: for a list of supported codecs see
  22. # the shntool manpage
  23. #
  24. #set -x
  25. function usage ()
  26. {
  27.         printf "\n\nUSAGE:\
  28. \t$(basename "$0") /path/to/cue_file.cue\n
  29. \tUse find for recursion/multiple cue+image files:\n
  30. \tfind <path> -iname "*.cue" -execdir $(basename "$0") "{}" \+\n
  31. \tThe lossless file must be named exactly as stated in the cue\n\tfile.
  32. \n\t$(basename "$0") splits cue+lossless into individual flacs, named
  33. \tand tagged using the info in the cue.
  34. \tIf the cue sheet is non UTF-8 then a new UTF-8 cue is created.
  35. \tThis preserves diacritic marks and non-Latin characters.\n
  36. \t$(basename "$0") outputs to a new directory whose name is derived
  37. \tfrom the cue's first TITLE field.  No existing files are deleted
  38. \toverwritten or modified by this script.
  39. \tFor a list of supported codecs see the shntool manpage.\n
  40. \tThere are no user options or settings.\n
  41. \tI didn't include error checking so if it fails uncomment
  42. \t'set -x' and have a look for yourself.\n\n"
  43.  
  44. #show usage if script run without arguments
  45. if [ $# -lt 1 ]; then
  46.         usage
  47.         exit 1
  48. fi
  49.  
  50. for i in "$@" ; do
  51.  
  52. #find character encoding of cue. if not utf-8 then create new utf-8 cue
  53. UTFCUE="${i%.*}"_utf.cue
  54. CHARSET="$(file -bi "$i"|awk -F "=" '{print $2}')"
  55. if [ "$CHARSET" != utf-8 ]; then
  56.                 iconv -f "$CHARSET" -t utf8 "$i" -o "$UTFCUE"
  57.                 CUE="$UTFCUE"
  58.                 else
  59.                 CUE="$i"
  60. fi
  61.  
  62. #get lossless image file name from $CUE
  63. IMG="$(dirname "$i")"/"$(grep FILE "$CUE"|awk -F "\"" '{print $2}')"
  64. #get album title from $CUE
  65. OUTDIR="$(dirname "$i")"/"$(cueprint -d '%T\n' "$CUE")"
  66.  
  67. #quit if lossless image file doesn't exist
  68. if [ ! -e "$IMG" ]; then
  69.                 printf "\n\tSupported lossless file not found.  Check that cue \
  70. file references a valid file\n\n"
  71. exit
  72. fi
  73.  
  74. #create output directory
  75. if [ ! -d "$OUTDIR" ]; then
  76.                 mkdir "$OUTDIR"
  77. fi
  78.  
  79. #split image and set file names from cue: 'number - title'
  80. shnsplit -d "$OUTDIR" -f "$CUE" -t "%n - %t" -o flac "$IMG"
  81.  
  82. #remove unwanted pregap files (if any)
  83. if [ -e "$OUTDIR"/"00 - pregap.flac" ]; then
  84.                 rm "$OUTDIR"/"00 - pregap.flac"
  85. fi
  86.  
  87. #write tags from cue to new files
  88. cuetag "$CUE" "$OUTDIR"/*.flac
  89. done
  90. exit
RAW Paste Data
Top