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.
 
 
 
 
 
 

223 lines
4.1 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. trackno=$1; shift
  27. file="$1"; shift
  28. fields="$@"
  29. # FLAC tagging
  30. # --remove-all-tags overwrites existing comments
  31. METAFLAC="metaflac --no-utf8-convert --remove-all-tags --import-tags-from=-"
  32. # Ogg Vorbis tagging
  33. # -w overwrites existing comments
  34. # -a appends to existing comments
  35. VORBISCOMMENT="vorbiscomment -w -c -"
  36. # VC text file format
  37. # TODO: this also outputs to stdout
  38. TXTFILE="tee"
  39. case "$file" in
  40. *.[Ff][Ll][Aa][Cc])
  41. VORBISTAG=$METAFLAC
  42. ;;
  43. *.[Oo][Gg][Gg])
  44. VORBISTAG=$VORBISCOMMENT
  45. ;;
  46. *.[Tt][Xx][Tt])
  47. VORBISTAG=$TXTFILE
  48. ;;
  49. esac
  50. # space separated list of recommended standard field names
  51. # see http://www.xiph.org/ogg/vorbis/doc/v-comment.html
  52. # TRACKTOTAL is not in the Xiph recommendation, but is in common use
  53. [ -n "$fields" ] ||
  54. fields='TITLE VERSION ALBUM TRACKNUMBER TRACKTOTAL ARTIST PERFORMER COPYRIGHT LICENSE ORGANIZATION DESCRIPTION GENRE DATE LOCATION CONTACT ISRC'
  55. # fields' corresponding cueprint conversion characters
  56. # separate alternates with a space
  57. TITLE='%t'
  58. VERSION=''
  59. ALBUM='%T'
  60. TRACKNUMBER='%02n'
  61. TRACKTOTAL='%02N'
  62. ARTIST='%c %p'
  63. PERFORMER='%p'
  64. COPYRIGHT=''
  65. LICENSE=''
  66. ORGANIZATION=''
  67. DESCRIPTION='%m'
  68. GENRE='%g'
  69. DATE=''
  70. LOCATION=''
  71. CONTACT=''
  72. ISRC='%i %u'
  73. (for field in $fields; do
  74. case "$field" in
  75. (*=*) echo "$field";;
  76. (*)
  77. value=""
  78. for conv in $(eval echo \$$field); do
  79. value=$($CUEPRINT -n $trackno -t "$conv\n" "$cue_file")
  80. if [ -n "$value" ]; then
  81. echo "$field=$value"
  82. break
  83. fi
  84. done
  85. ;;
  86. esac
  87. done) | $VORBISTAG "$file"
  88. }
  89. id3()
  90. {
  91. MP3TAG=$(which mid3v2) \
  92. || MP3TAG=$(which id3v2)
  93. if [ -z "${MP3TAG}" ]; then
  94. echo "error: not found '(m)id3v2'."
  95. exit 1
  96. fi
  97. # space separated list of ID3 v1.1 tags
  98. # see http://id3lib.sourceforge.net/id3/idev1.html
  99. fields="TITLE ALBUM ARTIST YEAR COMMENT GENRE TRACKNUMBER"
  100. # fields' corresponding cueprint conversion characters
  101. # separate alternates with a space
  102. TITLE='%t'
  103. ALBUM='%T'
  104. ARTIST='%p'
  105. YEAR=''
  106. COMMENT='%c'
  107. GENRE='%g'
  108. TRACKNUMBER='%n'
  109. for field in $fields; do
  110. case "$field" in
  111. *=*) value="${field#*=}";;
  112. *)
  113. value=""
  114. for conv in $(eval echo \$$field); do
  115. value=$($CUEPRINT -n $1 -t "$conv\n" "$cue_file")
  116. if [ -n "$value" ]; then
  117. break
  118. fi
  119. done
  120. ;;
  121. esac
  122. if [ -n "$value" ]; then
  123. case $field in
  124. TITLE)
  125. $MP3TAG -t "$value" "$2"
  126. ;;
  127. ALBUM)
  128. $MP3TAG -A "$value" "$2"
  129. ;;
  130. ARTIST)
  131. $MP3TAG -a "$value" "$2"
  132. ;;
  133. YEAR)
  134. $MP3TAG -y "$value" "$2"
  135. ;;
  136. COMMENT)
  137. $MP3TAG -c "$value" "$2"
  138. ;;
  139. GENRE)
  140. $MP3TAG -g "$value" "$2"
  141. ;;
  142. TRACKNUMBER)
  143. $MP3TAG -T "$value" "$2"
  144. ;;
  145. esac
  146. fi
  147. done
  148. }
  149. main()
  150. {
  151. if [ $# -lt 1 ]; then
  152. usage
  153. exit
  154. fi
  155. sed '1s/^\xEF\xBB\xBF//' < $1 > temp-cuetag-sh.cue
  156. cue_file=temp-cuetag-sh.cue
  157. shift
  158. ntrack=$(cueprint -d '%N' "$cue_file")
  159. trackno=1
  160. NUM_FILES=0 FIELDS=
  161. for arg in "$@"; do
  162. case "$arg" in
  163. *.*) NUM_FILES=$(expr $NUM_FILES + 1);;
  164. *) FIELDS="$FIELDS $arg";;
  165. esac
  166. done
  167. if [ $NUM_FILES -ne $ntrack ]; then
  168. echo "warning: number of files does not match number of tracks"
  169. fi
  170. for file in "$@"; do
  171. case $file in
  172. *.[Ff][Ll][Aa][Cc])
  173. vorbis $trackno "$file" $FIELDS
  174. ;;
  175. *.[Oo][Gg][Gg])
  176. vorbis $trackno "$file" $FIELDS
  177. ;;
  178. *.[Mm][Pp]3)
  179. id3 $trackno "$file" $FIELDS
  180. ;;
  181. *.[Tt][Xx][Tt])
  182. vorbis $trackno "$file"
  183. ;;
  184. *.*)
  185. echo "$file: uknown file type"
  186. ;;
  187. esac
  188. trackno=$(($trackno + 1))
  189. done
  190. rm -f temp-cuetag-sh.cue
  191. }
  192. main "$@"