[raspberry-vi] Off Topic: Automating metaflac and sox.

  • From: Jeffery Mewtamer <mewtamer@xxxxxxxxx>
  • To: lug <lug@xxxxxxxxxxxxxx>, raspberry-vi@xxxxxxxxxxxxx
  • Date: Tue, 2 Aug 2016 22:24:45 +0000

Okay, so I've started amassing a collection of Audiobooks and Audio
Dramas in AudioCD format. Compared to Music CD, ripping these to flac
on average requires more post-processing due to things like multiple
discs, incomplete, inconsistant, or missing CDDB data, or
chapters/episodes being split across multiple tracks or discs.

I like having my Audiobooks and Audio Dramas organized as follows:
1 file per Chapter/Episode.
Each file named with chapter/episode number and title if
chapters/episodes are titled or Chapter XX.flac/Episode XX.flac if
they aren't titled.
All the Chapters/Episodes of the same book/program in a directory
named for the book/program.
All Books/Programs by the same Author or in the same franchise in a
directory named for the Author or Franchise.
And with the following file tags:
Artist=Author's Name/Franchise Name as appropriate
Album=Book Tile/Program title
Track= Chapter/Episode Number
Title = Chapter/Episode title if there is one Chapter XX/Episode XX otherwise.

I've been using sox to concatenate tracks belonging to the same
chapter/episode and metaflac for fixing file tags, but I would like to
use scripts to better automate this process after I've gotten files
either sorted into a directory per chapter/episode or into the final
preferred filenames.

For sox, I'd like to create a script that, when executed, will descend
into each subdirectory of the working directory, use sox to
concatenate the flac files found there and spit out the result in the
working directory with a filename that matches the subdirectory whose
contents were concatenated. This problem has a similar structure to
ones I've used a for file in *.ext loop, but I've never done such a
loop where the parameters were directories rather than files with a
given filename extension.

Here is the contents of my untested massSox.sh:

# /bin/bash
for dir in *; do
cd "$dir"
sox * ../"$dir".flac &
cd ../
done

With metaflac, I'd like to create a script that will:
remove all tags for every flac file in the working directory.
set Artist and Album to specified values(if these can be given as
command line parameters or take from Album=../ and Artist=../../ that
would be great).
For each flac file with a filename of the form XX Chapter Title.flac,
it sets track=XX and title=Chapter Title.
for every flace file with a name of the form Chapter XX.flac, it sets
track=XX and Title=Chapter XX with similar for filenames of the form
Episode XX.flac

currently, I've got two scripts for tagging, one to handle the XX
Chapter Title.flac case that requires using ls -1 > filelist.txt to
generate a list of chapter titles and then doing a lot of cutting and
pasting to populate a list of nearly identical commands. The other
tagging script handles the Chapter XX.flac case and only requires that
I add hardcoded Artist and Album values and that there's enough lines
to handle the number of input files. I haven't encountered the Episode
XX.flac case yet, but I can convert tagNoChapterTitles.sh to
tagNoEpisodeTitles.sh with a simple sed command. I would think there
should be a way to do string manipulation of filenames to create a
loop, but my scripting knowledge is not that advanced.

Here are my skeletal tag.sh followed by my tagNoChapterTitles.sh:

# /bin/bash
metaflac --remove-all-tags *.flac
metaflac --set-tag=ARTIST="" --set-tag=ALBUM="" *.flac
metaflac --set-tag=TRACK=01 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=02 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=03 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=04 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=05 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=06 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=07 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=08 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=09 --set-tag=TITLE="" ""
metaflac --set-tag=TRACK=10 --set-tag=TITLE="" ""

# /bin/bash
metaflac --remove-all-tags *.flac
metaflac --set-tag=ARTIST="" --set-tag=ALBUM="" *.flac
metaflac --set-tag=TRACK=01 --set-tag=TITLE="Chapter 01" "Chapter 01.flac"
metaflac --set-tag=TRACK=02 --set-tag=TITLE="Chapter 02" "Chapter 02.flac"
metaflac --set-tag=TRACK=03 --set-tag=TITLE="Chapter 03" "Chapter 03.flac"
metaflac --set-tag=TRACK=04 --set-tag=TITLE="Chapter 04" "Chapter 04.flac"
metaflac --set-tag=TRACK=05 --set-tag=TITLE="Chapter 05" "Chapter 05.flac"
metaflac --set-tag=TRACK=06 --set-tag=TITLE="Chapter 06" "Chapter 06.flac"
metaflac --set-tag=TRACK=07 --set-tag=TITLE="Chapter 07" "Chapter 07.flac"
metaflac --set-tag=TRACK=08 --set-tag=TITLE="Chapter 08" "Chapter 08.flac"
metaflac --set-tag=TRACK=09 --set-tag=TITLE="Chapter 09" "Chapter 09.flac"
metaflac --set-tag=TRACK=10 --set-tag=TITLE="Chapter 10" "Chapter 10.flac"
metaflac --set-tag=TRACK=11 --set-tag=TITLE="Chapter 11" "Chapter 11.flac"
metaflac --set-tag=TRACK=12 --set-tag=TITLE="Chapter 12" "Chapter 12.flac"
metaflac --set-tag=TRACK=13 --set-tag=TITLE="Chapter 13" "Chapter 13.flac"
metaflac --set-tag=TRACK=14 --set-tag=TITLE="Chapter 14" "Chapter 14.flac"
metaflac --set-tag=TRACK=15 --set-tag=TITLE="Chapter 15" "Chapter 15.flac"
metaflac --set-tag=TRACK=16 --set-tag=TITLE="Chapter 16" "Chapter 16.flac"
metaflac --set-tag=TRACK=17 --set-tag=TITLE="Chapter 17" "Chapter 17.flac"
metaflac --set-tag=TRACK=18 --set-tag=TITLE="Chapter 18" "Chapter 18.flac"
metaflac --set-tag=TRACK=19 --set-tag=TITLE="Chapter 19" "Chapter 19.flac"
metaflac --set-tag=TRACK=20 --set-tag=TITLE="Chapter 20" "Chapter 20.flac"
metaflac --set-tag=TRACK=21 --set-tag=TITLE="Chapter 21" "Chapter 21.flac"
metaflac --set-tag=TRACK=22 --set-tag=TITLE="Chapter 22" "Chapter 22.flac"
metaflac --set-tag=TRACK=23 --set-tag=TITLE="Chapter 23" "Chapter 23.flac"
metaflac --set-tag=TRACK=24 --set-tag=TITLE="Chapter 24" "Chapter 24.flac"
metaflac --set-tag=TRACK=25 --set-tag=TITLE="Chapter 25" "Chapter 25.flac"
metaflac --set-tag=TRACK=26 --set-tag=TITLE="Chapter 26" "Chapter 26.flac"
metaflac --set-tag=TRACK=27 --set-tag=TITLE="Chapter 27" "Chapter 27.flac"
metaflac --set-tag=TRACK=28 --set-tag=TITLE="Chapter 28" "Chapter 28.flac"
metaflac --set-tag=TRACK=29 --set-tag=TITLE="Chapter 29" "Chapter 29.flac"
metaflac --set-tag=TRACK=30 --set-tag=TITLE="Chapter 30" "Chapter 30.flac"
metaflac --set-tag=TRACK=31 --set-tag=TITLE="Chapter 31" "Chapter 31.flac"
metaflac --set-tag=TRACK=32 --set-tag=TITLE="Chapter 32" "Chapter 32.flac"
metaflac --set-tag=TRACK=33 --set-tag=TITLE="Chapter 33" "Chapter 33.flac"
metaflac --set-tag=TRACK=34 --set-tag=TITLE="Chapter 34" "Chapter 34.flac"
metaflac --set-tag=TRACK=35 --set-tag=TITLE="Chapter 35" "Chapter 35.flac"
metaflac --set-tag=TRACK=36 --set-tag=TITLE="Chapter 36" "Chapter 36.flac"
metaflac --set-tag=TRACK=37 --set-tag=TITLE="Chapter 37" "Chapter 37.flac"
metaflac --set-tag=TRACK=38 --set-tag=TITLE="Chapter 38" "Chapter 38.flac"
metaflac --set-tag=TRACK=39 --set-tag=TITLE="Chapter 39" "Chapter 39.flac"
metaflac --set-tag=TRACK=40 --set-tag=TITLE="Chapter 40" "Chapter 40.flac"

Any help that could be provided in improving my scripts would be
greatly appreciated, and I'm open to suggestions for alternatives to
metaflac and sox for doing these jobs.
=========================================================== 
The raspberry-vi mailing list 
Archives: //www.freelists.org/archives/raspberry-vi
Administrative contact: <mike.ray@xxxxxxxxxxxxxx>
-----------------------------------------------------------
Raspberry Pi and the Raspberry Pi logo are trademarks of the Raspberry Pi 
Foundation.

This list is not affiliated to the Raspberry Pi Foundation and the views and 
attitudes expressed by the subscribers to this list do not reflect those of the 
Foundation.

Mike Ray, list creator, January 2013

Other related posts:

  • » [raspberry-vi] Off Topic: Automating metaflac and sox. - Jeffery Mewtamer