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.
 
 
 
 
 
 

197 lines
3.6 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. MP3TAG=$(which mid3v2) \
  83. || MP3TAG=$(which id3v2)
  84. if [ -z "${MP3TAG}" ]; then
  85. echo "error: not found '(m)id3v2'."
  86. exit 1
  87. fi
  88. # space seperated list of ID3 v1.1 tags
  89. # see http://id3lib.sourceforge.net/id3/idev1.html
  90. fields="TITLE ALBUM ARTIST YEAR COMMENT GENRE TRACKNUMBER"
  91. # fields' corresponding cueprint conversion characters
  92. # seperate alternates with a space
  93. TITLE='%t'
  94. ALBUM='%T'
  95. ARTIST='%p'
  96. YEAR=''
  97. COMMENT='%c'
  98. GENRE='%g'
  99. TRACKNUMBER='%n'
  100. for field in $fields; do
  101. value=""
  102. for conv in $(eval echo \$$field); do
  103. value=$($CUEPRINT -n $1 -t "$conv\n" "$cue_file")
  104. if [ -n "$value" ]; then
  105. break
  106. fi
  107. done
  108. if [ -n "$value" ]; then
  109. case $field in
  110. TITLE)
  111. $MP3TAG -t "$value" "$2"
  112. ;;
  113. ALBUM)
  114. $MP3TAG -A "$value" "$2"
  115. ;;
  116. ARTIST)
  117. $MP3TAG -a "$value" "$2"
  118. ;;
  119. YEAR)
  120. $MP3TAG -y "$value" "$2"
  121. ;;
  122. COMMENT)
  123. $MP3TAG -c "$value" "$2"
  124. ;;
  125. GENRE)
  126. $MP3TAG -g "$value" "$2"
  127. ;;
  128. TRACKNUMBER)
  129. $MP3TAG -T "$value" "$2"
  130. ;;
  131. esac
  132. fi
  133. done
  134. }
  135. main()
  136. {
  137. if [ $# -lt 1 ]; then
  138. usage
  139. exit
  140. fi
  141. cue_file=$1
  142. shift
  143. ntrack=$(cueprint -d '%N' "$cue_file")
  144. trackno=1
  145. if [ $# -ne $ntrack ]; then
  146. echo "warning: number of files does not match number of tracks"
  147. fi
  148. for file in "$@"; do
  149. case $file in
  150. *.[Ff][Ll][Aa][Cc])
  151. vorbis $trackno "$file"
  152. ;;
  153. *.[Oo][Gg][Gg])
  154. vorbis $trackno "$file"
  155. ;;
  156. *.[Mm][Pp]3)
  157. id3 $trackno "$file"
  158. ;;
  159. *.[Tt][Xx][Tt])
  160. vorbis $trackno "$file"
  161. ;;
  162. *)
  163. echo "$file: uknown file type"
  164. ;;
  165. esac
  166. trackno=$(($trackno + 1))
  167. done
  168. }
  169. main "$@"