chill_dohc

Recursive file manipulation

May 7th, 2011
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #
  3. # Notes:
  4. #
  5. # For each flac file in the current directory and its subdirectories,
  6. # convert it to an mp3, importing the flac's tags. If the flac has
  7. # Vorbis tags, use them. Otherwise look for id3v2 tags and use them
  8. # if found. If the flac has no tags, log that fact and skip
  9. # processing the file.
  10. #
  11. # This script traverses directory structures using a mechanism adapted
  12. # from Steve Parker's traverse2.sh script.
  13. #
  14. # I assume that the commands ls, rm, sed, grep, pwd, wc and mkdir exist
  15. # and can be called by those names. But let's make sure this system has
  16. # the necessary third-party software installed, and find the binaries.
  17. # Complain and exit if any one is not found.
  18.  
  19. METAFLAC=`which metaflac`
  20. FLAC=`which flac`
  21. LAME=`which lame`
  22. ID3INFO=`which id3info`
  23.  
  24. # Exit if we are missing anything:
  25. if [ -z $METAFLAC ]; then
  26. echo "metaflac not found! Exiting."
  27. exit 1
  28. fi
  29.  
  30. if [ -z $FLAC ]; then
  31. echo "flac not found! Exiting."
  32. exit 1
  33. fi
  34.  
  35. if [ -z $LAME ]; then
  36. echo "lame not found! Exiting."
  37. exit 1
  38. fi
  39.  
  40. if [ -z $ID3INFO ]; then
  41. echo "id3info not found! Exiting."
  42. exit 1
  43. fi
  44.  
  45.  
  46. # Where to log errors?
  47. LOGFILE=~/flac_err_log
  48.  
  49. # Where to put the mp3s:
  50. # Create that directory if necessary
  51. MP3ROOT=/media/mp3 # change this to whatever you like
  52. if [ -d $MP3ROOT ]; then
  53. # then we're OK
  54. else
  55. mkdir $MP3ROOT
  56. fi
  57.  
  58. # Parameters for lame - will affect sound quality and file size.
  59. #LAME_PARAMS="-V 0 -h" # variable bitrate, best quality
  60.  
  61. # From Steve Parker, only slightly modified:
  62. traverse()
  63. {
  64. # Traverse a directory
  65.  
  66. ls "$1" | while read i
  67. do
  68. if [ -d "$1/$i" ]; then
  69. THISDIR=$1/$i
  70. # Calling this as a subshell means that when the called
  71. # function changes directory, it will not affect our
  72. # current working directory
  73.  
  74. if [ -d $MP3ROOT/$THISDIR ]; then
  75. # directory exists, leave it be
  76. echo "$MP3ROOT/$THISDIR already exists, not created."
  77. else
  78. mkdir $MP3ROOT/$THISDIR
  79. echo "Copying $THISDIR to $MP3ROOT/$THISDIR"
  80. fi
  81.  
  82. traverse "$1/$i" `expr $2 + 1`
  83. else
  84. FILE=$1/$i
  85. FLACFILE=`echo $FILE | grep -i flac$`
  86.  
  87. # Ditch the empty lines:
  88. if [ -z $FLACFILE ]; then
  89. # do nothing!
  90. else
  91. # echo $FLACFILE
  92.  
  93. MP3="$MP3ROOT/$THISDIR/${i%.flac}.mp3"
  94.  
  95. # Look for Vorbis tags:
  96. NUMVTAGS=`$METAFLAC --export-tags-to=- $FLACFILE | wc -l`
  97. # Create temporary file with tags in shell variable assignment
  98. # form, depending on whether we found Vorbis or id3v2 tags
  99. # Any 'slash' characters must be removed now, so they don't cause
  100. # trouble later. Don't convert quotes yet.
  101.  
  102. if [ $NUMVTAGS -gt 0 ]; then # found Vorbis tags
  103.  
  104. # title...
  105. TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE`
  106. SLASHFOUND=`echo $TITLETAG | grep \/ | wc -l`
  107. QUOTEFOUND=`echo $TITLETAG | grep \" | wc -l`
  108.  
  109. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  110. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  111. TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
  112. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  113. else # slashes but no quotes
  114. TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
  115. sed 's/\//\-/g'`
  116. fi
  117. else # no slashes found
  118. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  119. TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE |\
  120. sed s/\"/\'/ | sed s/\"/\'/`
  121. else # no quotes, no slashes
  122. TITLETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TITLE`
  123. fi
  124. fi
  125.  
  126. # artist...
  127. ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST`
  128. SLASHFOUND=`echo $ARTISTTAG | grep \/ | wc -l`
  129. QUOTEFOUND=`echo $ARTISTTAG | grep \" | wc -l`
  130.  
  131. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  132. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  133. ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
  134. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  135. else # slashes but no quotes
  136. ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
  137. sed 's/\//\-/g'`
  138. fi
  139. else # no slashes found
  140. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  141. ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST |\
  142. sed s/\"/\'/ | sed s/\"/\'/`
  143. else # no quotes, no slashes
  144. ARTISTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ARTIST`
  145. fi
  146. fi
  147.  
  148. # album...
  149. ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM`
  150. SLASHFOUND=`echo $ALBUMTAG | grep \/ | wc -l`
  151. QUOTEFOUND=`echo $ALBUMTAG | grep \" | wc -l`
  152.  
  153. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  154. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  155. ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
  156. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  157. else # slashes but no quotes
  158. ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
  159. sed 's/\//\-/g'`
  160. fi
  161. else # no slashes found
  162. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  163. ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM |\
  164. sed s/\"/\'/ | sed s/\"/\'/`
  165. else # no quotes, no slashes
  166. ALBUMTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep ALBUM`
  167. fi
  168. fi
  169.  
  170.  
  171. # date, or year...
  172. DATETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep DATE`
  173. # track number...
  174. TRACKNOTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TRACKNUMBER`
  175. # number of tracks...
  176. TRACKTOTTAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep TRACKTOTAL`
  177.  
  178.  
  179. # genre... as of this writing, the official tag strings might
  180. # have spaces...
  181. # y'know what? Let's skip it. Too much trouble, and usually wrong.
  182. # GENRETAG=`$METAFLAC --export-tags-to=- $FLACFILE | grep GENRE`
  183. # SPACEFOUND=`echo $GENRETAG | grep \/ | wc -l`
  184. # if [ $SPACEFOUND -gt 0 ]; then # spaces found
  185. # GENRETAG=`METAFLAC --export-tags-to=- $FLACFILE | grep GENRE |\
  186. # sed s/\ /\_/g`
  187. # fi
  188.  
  189. else # No Vorbis tags. Look for id3v2 tags
  190.  
  191. # title...
  192. TITLETAG=`$ID3INFO $FLACFILE | grep TIT2`
  193. SLASHFOUND=`echo $TITLETAG | grep \/ | wc -l`
  194. QUOTEFOUND=`echo $TITLETAG | grep \" | wc -l`
  195.  
  196. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  197. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  198. TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
  199. sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
  200. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  201. else # slashes but no quotes
  202. TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
  203. sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
  204. sed 's/\//\-/g'`
  205. fi
  206. else # no slashes found
  207. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  208. TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
  209. sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/ |\
  210. sed s/\"/\'/ | sed s/\"/\'/`
  211. else # no quotes, no slashes
  212. TITLETAG=`$ID3INFO $FLACFILE | grep TIT2 |\
  213. sed s/"=== TIT2 (Title\/songname\/content description): "/"TITLE="/`
  214. fi
  215. fi
  216.  
  217. # artist...
  218. ARTISTTAG=`id3info $FLACFILE | grep TPE1 | \
  219. sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/`
  220. SLASHFOUND=`echo $ARTISTTAG | grep \/ | wc -l`
  221. QUOTEFOUND=`echo $ARTISTTAG | grep \" | wc -l`
  222.  
  223. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  224. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  225. ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
  226. sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
  227. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  228. else # slashes but no quotes
  229. ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
  230. sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
  231. sed 's/\//\-/g'`
  232. fi
  233. else # no slashes found
  234. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  235. ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
  236. sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/ |\
  237. sed s/\"/\'/ | sed s/\"/\'/`
  238. else # no quotes, no slashes
  239. ARTISTTAG=`$ID3INFO $FLACFILE | grep TPE1 |\
  240. sed s/"=== TPE1 (Lead performer(s)\/Soloist(s)): "/"ARTIST="/`
  241. fi
  242. fi
  243.  
  244. # album...
  245. ALBUMTAG=`id3info $FLACFILE | grep TALB | \
  246. sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/`
  247. SLASHFOUND=`echo $ALBUMTAG | grep \/ | wc -l`
  248. QUOTEFOUND=`echo $ALBUMTAG | grep \" | wc -l`
  249.  
  250. if [ $SLASHFOUND -gt 0 ]; then # slashes found
  251. if [ $QUOTEFOUND -gt 0 ]; then # found slashes and quotes
  252. ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
  253. sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |\
  254. sed 's/\//\-/g' | sed s/\"/\'/ | sed s/\"/\'/`
  255. else # slashes but no quotes
  256. ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
  257. sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |\
  258. sed 's/\//\-/g'`
  259. fi
  260. else # no slashes found
  261. if [ $QUOTEFOUND -gt 0 ]; then # quotes but no slashes
  262. ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
  263. sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/ |
  264. sed s/\"/\'/ | sed s/\"/\'/`
  265. else # no quotes, no slashes
  266. ALBUMTAG=`$ID3INFO $FLACFILE | grep TALB |\
  267. sed s/"=== TALB (Album\/Movie\/Show title): "/"ALBUM="/`
  268. fi
  269. fi
  270.  
  271.  
  272. # date, or year...
  273. DATETAG=`$ID3INFO $FLACFILE | grep TYER | \
  274. sed s/"=== TYER (Year): "/"DATE="/`
  275. # track number...
  276. TRACKNOTAG=`$ID3INFO $FLACFILE | grep TRCK | \
  277. sed s/"=== TRCK (Track number\/Position in set): "/"TRACKNUMBER="/`
  278. # number of tracks on this album...
  279. TRACKTOTTAG=`$ID3INFO $FLACFILE | grep TCON | \
  280. sed s/"=== TCON (Content type): "/"TRACKTOTAL="/ |\
  281. sed s/\(// | sed s/\)//`
  282. # skip the genre
  283. fi
  284.  
  285. # Create the temorary file!
  286. echo $TITLETAG | sed 's/=\(.*\)/="\1"/' > tmpfile
  287. echo $ARTISTTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  288. echo $ALBUMTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  289. echo $DATETAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  290. echo $TRACKNOTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  291. echo $TRACKTOTTAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  292. # echo $GENRETAG | sed 's/=\(.*\)/="\1"/' >> tmpfile
  293.  
  294. # Make sure tags exist:
  295. if [ -s tmpfile ]; then # regular file, nonzero size
  296. # Include the temporary file:
  297. . ./tmpfile
  298. # Delete the temporary file since we're done with it:
  299. rm tmpfile
  300.  
  301. # if [ -z ${GENRE} ]; then # in some of my files, this field is empty
  302. # GENRE="Other"
  303. # fi
  304.  
  305. # vvv commented during script debugging vvv
  306. if [ -f $MP3 ]; then
  307. # mp3 already exists; don't overwrite
  308. if [ -f $LOGFILE ]; then # if the log file exists, append
  309. echo "$THISDIR/$MP3" >> $LOGFILE
  310. else # file does not exist; create
  311. echo "The following mp3 files already exist and were not created:" \
  312. > $LOGFILE
  313. echo "" >> $LOGFILE
  314. echo "$MP3ROOT/$MP3" >> $LOGFILE
  315. fi
  316. else
  317. echo "Creating ${MP3}..."
  318. # Create mp3 with tags:
  319. $FLAC -cd "$FLACFILE" | $LAME -V 0 -h \
  320. --tt "$TITLE" \
  321. --tn "$TRACKNUMBER" \
  322. --ty "$DATE" \
  323. --ta "$ARTIST" \
  324. --tl "$ALBUM" \
  325. --add-id3v2 \
  326. - "$MP3"
  327. fi
  328. # ^^^ commented during script debugging ^^^
  329. # vvv use only for debugging vvv
  330. # echo $GENRE
  331. # --tg "$GENRE" \
  332. # ^^^ use only for debugging ^^^
  333.  
  334. else
  335. if [ -f $LOGFILE ]; then # if the log file exists, append
  336. echo "$THISDIR/$FLACFILE" >> $LOGFILE
  337. else # file does not exist; create
  338. echo "No tags found for the following:" > $LOGFILE
  339. echo "" >> $LOGFILE
  340. echo "$THISDIR/$FLACFILE" >> $LOGFILE
  341. fi
  342. fi
  343. fi
  344. fi
  345. done
  346. }
  347.  
  348. traverse . 0
RAW Paste Data