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.
 
 
 
 
 
 

192 line
3.5 KiB

  1. #! /bin/sh
  2. # cuetag.sh - tag files based on cue/toc file information
  3. # uses cueprint output
  4. # usage: cuetag.sh <cuefile|tocfile> [file]...
  5. CUEPRINT=cueprint
  6. cue_file=""
  7. # print usage instructions
  8. usage()
  9. {
  10. echo "usage: cuetag.sh <cuefile|tocfile> [file]..."
  11. echo
  12. echo "cuetag.sh adds tags to files based on cue or toc information"
  13. echo
  14. echo "Supported formats (format extension, format name, tagging utility):"
  15. echo "ogg, Ogg Vorbis, vorbiscomment"
  16. echo "flac, FLAC, metaflac"
  17. echo "mp3, MP3, mp3info"
  18. echo "txt, Vorbis Comment Text File, tee"
  19. echo
  20. echo "cuetag.sh uses cueprint, which must be in your path"
  21. }
  22. # Vorbis Comments
  23. # for FLAC and Ogg Vorbis files
  24. vorbis()
  25. {
  26. # FLAC tagging
  27. # --remove-vc-all overwrites existing comments
  28. METAFLAC="metaflac --remove-all-tags --import-tags-from=-"
  29. # Ogg Vorbis tagging
  30. # -w overwrites existing comments
  31. # -a appends to existing comments
  32. VORBISCOMMENT="vorbiscomment -w -c -"
  33. # VC text file format
  34. # TODO: this also outputs to stdout
  35. TXTFILE="tee"
  36. case "$2" in
  37. *.[Ff][Ll][Aa][Cc])
  38. VORBISTAG=$METAFLAC
  39. ;;
  40. *.[Oo][Gg][Gg])
  41. VORBISTAG=$VORBISCOMMENT
  42. ;;
  43. *.[Tt][Xx][Tt])
  44. VORBISTAG=$TXTFILE
  45. ;;
  46. esac
  47. # space seperated list of recomended stardard field names
  48. # see http://www.xiph.org/ogg/vorbis/doc/v-comment.html
  49. # TRACKTOTAL is not in the Xiph recomendation, but is in common use
  50. fields='TITLE VERSION ALBUM TRACKNUMBER TRACKTOTAL ARTIST PERFORMER COPYRIGHT LICENSE ORGANIZATION DESCRIPTION GENRE DATE LOCATION CONTACT ISRC'
  51. # fields' corresponding cueprint conversion characters
  52. # seperate alternates with a space
  53. TITLE='%t'
  54. VERSION=''
  55. ALBUM='%T'
  56. TRACKNUMBER='%n'
  57. TRACKTOTAL='%N'
  58. ARTIST='%c %p'
  59. PERFORMER='%p'
  60. COPYRIGHT=''
  61. LICENSE=''
  62. ORGANIZATION=''
  63. DESCRIPTION='%m'
  64. GENRE='%g'
  65. DATE=''
  66. LOCATION=''
  67. CONTACT=''
  68. ISRC='%i %u'
  69. (for field in $fields; do
  70. value=""
  71. for conv in $(eval echo \$$field); do
  72. value=$($CUEPRINT -n $1 -t "$conv\n" "$cue_file")
  73. if [ -n "$value" ]; then
  74. echo "$field=$value"
  75. break
  76. fi
  77. done
  78. done) | $VORBISTAG "$2"
  79. }
  80. id3()
  81. {
  82. MP3INFO=mp3info
  83. # space seperated list of ID3 v1.1 tags
  84. # see http://id3lib.sourceforge.net/id3/idev1.html
  85. fields="TITLE ALBUM ARTIST YEAR COMMENT GENRE TRACKNUMBER"
  86. # fields' corresponding cueprint conversion characters
  87. # seperate alternates with a space
  88. TITLE='%t'
  89. ALBUM='%T'
  90. ARTIST='%p'
  91. YEAR=''
  92. COMMENT='%c'
  93. GENRE='%g'
  94. TRACKNUMBER='%n'
  95. for field in $fields; do
  96. value=""
  97. for conv in $(eval echo \$$field); do
  98. value=$($CUEPRINT -n $1 -t "$conv\n" "$cue_file")
  99. if [ -n "$value" ]; then
  100. break
  101. fi
  102. done
  103. if [ -n "$value" ]; then
  104. case $field in
  105. TITLE)
  106. $MP3INFO -t "$value" "$2"
  107. ;;
  108. ALBUM)
  109. $MP3INFO -l "$value" "$2"
  110. ;;
  111. ARTIST)
  112. $MP3INFO -a "$value" "$2"
  113. ;;
  114. YEAR)
  115. $MP3INFO -y "$value" "$2"
  116. ;;
  117. COMMENT)
  118. $MP3INFO -c "$value" "$2"
  119. ;;
  120. GENRE)
  121. $MP3INFO -g "$value" "$2"
  122. ;;
  123. TRACKNUMBER)
  124. $MP3INFO -n "$value" "$2"
  125. ;;
  126. esac
  127. fi
  128. done
  129. }
  130. main()
  131. {
  132. if [ $# -lt 1 ]; then
  133. usage
  134. exit
  135. fi
  136. cue_file=$1
  137. shift
  138. ntrack=$(cueprint -d '%N' "$cue_file")
  139. trackno=1
  140. if [ $# -ne $ntrack ]; then
  141. echo "warning: number of files does not match number of tracks"
  142. fi
  143. for file in "$@"; do
  144. case $file in
  145. *.[Ff][Ll][Aa][Cc])
  146. vorbis $trackno "$file"
  147. ;;
  148. *.[Oo][Gg][Gg])
  149. vorbis $trackno "$file"
  150. ;;
  151. *.[Mm][Pp]3)
  152. id3 $trackno "$file"
  153. ;;
  154. *.[Tt][Xx][Tt])
  155. vorbis $trackno "$file"
  156. ;;
  157. *)
  158. echo "$file: uknown file type"
  159. ;;
  160. esac
  161. trackno=$(($trackno + 1))
  162. done
  163. }
  164. main "$@"