| Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next


Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

C-ORM: docs, API.

Last 100 entries

Re: Quick message - This link is broken; Adding Reverb To The Echo Chamber; Sox Audio Tools; Would This Have Been OK?; Honesty only important economically before institutions develop; Stegangraphy via PS4; OpenCL Mess; More Book Recommendations; Good Explanation of Difference Between Majority + Minority; Musical Chairs - Who's The Privileged White Guy; I can see straight men watching this conversation and laffing; When it's Actually a Source of Indignation and Disgust; Meta Thread Defending POC Causes POC To Close Account; Indigenous People Of Chile; Curry Recipe; Interesting Link On Marginality; A Nuclear Launch Ordered, 1962; More Book Recs (Better Person); It's Nuanced, And I Tried, So Back Off; Marx; The Negative Of Positive; Jenny Holzer Rocks; Huge Article on Cultural Evolution and More; "Ignoring language theory"; Negative Finger Counting; Week 12; Communication Via Telecomm Bids; Finding Suspects Via Relatives' DNA From Non-Crime Databases; Statistics and Information Theory; Ice OK in USA; On The Other Hand; (Current Understanding Of) Chilean Taxes / Contributions; M John Harrison; Playing Games on a Cloud GPU; China Gamifies Real Life; Can't Help Thinking It's Thoughtcrime; Mefi Quotes; Spray Painting Bike Frame; Weeks 10 + 11; Change: No Longer Possible To Merge Metadata; Books on Old Age; Health Tree Maps; MRA - Men's Rights Activists; Writing Good C++14; Risk Assessment - Fukushima; The Future of Advertising and Surveillance; Travelling With Betaferon; I think I know what I dislike so much about Metafilter; Weeks 8 + 9; More; Pastamore - Bad Italian in Vitacura; History Books; Iraq + The (UK) Governing Elite; Answering Some Hard Questions; Pinochet: The Dictator's Shadow; An Outsider's Guide To Julia Packages; Nobody gives a shit; Lepton Decay Irregularity; An Easier Way; Julia's BinDeps (aka How To Install Cairo); Good Example Of Good Police Work (And Anonymity Being Hard); Best Santiago Burgers; Also; Michael Emmerich (Vibrator Translator) Interview (Japanese Books); Clarice Lispector (Brazillian Writer); Books On Evolution; Looks like Ara (Modular Phone) is dead; Index - Translations From Chile; More Emotion in Chilean Wines; Week 7; Aeon Magazine (Science-ish); QM, Deutsch, Constructor Theory; Interesting Talk Transcripts; Interesting Suggestion Of Election Fraud; "Hard" Books; Articles or Papers on depolarizing the US; Textbook for "QM as complex probabilities"; SFO Get Libor Trader (14 years); Why Are There Still So Many Jobs?; Navier Stokes Incomplete; More on Benford; FBI Claimed Vandalism; Architectural Tessellation; Also: Go, Blake's 7; Delusions of Gender (book); Crypto AG DID work with NSA / GCHQ; UNUMS (Universal Number Format); MOOCs (Massive Open Online Courses); Interesting Looking Game; Euler's Theorem for Polynomials; Weeks 3-6; Reddit Comment; Differential Cryptanalysis For Dummies; Japanese Graphic Design; Books To Be Re-Read; And Today I Learned Bugs Need Clear Examples; Factoring a 67 bit prime in your head; Islamic Geometric Art; Useful Julia Backtraces from Tasks; Nothing, however, is lost with less discomfort than that which, when lost, cannot be missed; Article on Didion

© 2006-2015 Andrew Cooke (site) / post authors (content).

Various Scripts for ID3 and MP3 Processing

From: andrew cooke <andrew@...>

Date: Sun, 5 Aug 2012 12:20:17 -0400

This is for all the other Linux command line music loves...

Motivated by the purchase of an iPod Classic I've been cleaning up my music
collection, using these scripts.  The following are not all original - some
are based on other people's work - but I'm posting them all here in case they
prove useful to others.

First, it's important to note that many things assume a certain directory
structure.  Under some root - which I will call /music - are directories named
by artist (or "Various" for collections).  Below that are directories named by
album.  And below that are MP3 tracks.

Also, it may be worth describing my "workflow" when adding music.  First I use
kid3 to delete existing ID3 tags and get a "correct" set from musicbrainz or
similar (and add an image from google images).  Then I use easymp3 to rename
the files to the correct locations.  Those are both GUIs, so no scripts here
for that.

To load music to the iPod, the best solution I have found so far is to create
a parallel directory tree with the music I want, using soft links to link to
tracks, and then load that with gtkpod.  That code is in development, so isn't
included below.

Normalize gain


 find /music -name "*.mp3" -print0 | xargs -0 -n 1000 mp3gain -r -p -c

Very simple - this just adjusts each track independently (in a reversible way,
using metdata).

Note that, as with other scripts here, I use xargs to improve speed (if you
use --exec then the program has to restart for every track).  However, you may
find that some tracks crash mp3gain.  In that case, you need to run it for
each track individually (since with xargs if it crashes for one track it will
skip many others):


  find /music -name "*.mp3" -exec mp3gain -r -p -c \{} \;

Finding small directories

A directory with a small number of files may mean that you've accidentally
split an album with multiple artists into multiple directories.  I use the
following script to identify those (and to remove empty directories).


  for dir in `find /music -mindepth 2 -maxdepth 2`
      n=`ls -1 "$dir" | wc -l`
      if [[ $n -lt 4 ]]
	  if [[ ! -e "$dir/.small" ]]
	      if [[ $n -eq 0 ]]
		  rmdir "$dir"
		  echo $dir
		  ls "$dir"

  for dir in `find /music -mindepth 1 -maxdepth 1`
      n=`ls -1 "$dir" | wc -l`
      if [[ $n -eq 0 ]]
	  rmdir "$dir"

And, as you can see in the logic above, I use a ".small" marker file in
directories that I know are OK as they are (to avoid being warned again).  It
is added for all artists with this script:


  echo $artist

  for dir in `ls -1 "/music/$artist"`
      n=`ls -1 "/music/$artist/$dir" | wc -l`
      if [[ $n -lt 4 ]]
	  echo "/music/$artist/$dir"
	  touch "/music/$artist/$dir/.small"

Tagging compilations

iTunes (well, gtkpod, I guess) seems to infer albums from the combination of
album name and artist name.  This splits compilation albums.  To avoid this
you can add an additional ID3 tag (which I think means that only album name is

To do that I use this script (it takes an argument, which is the name of the
"artist" for various artists - I use "Various").


  echo $artist

  for dir in `ls -1 "/music/$artist"`
      pushd "/music/$artist/$dir"
      eyeD3 --no-tagging-time-frame --set-text-frame=TCMP:1 *.mp3

Cleaning ID3 tags

Some MP3 files come with lots of tags set.  Since I don't use them, and they
clutter displays in various programs, I delete them with this script (which
also deletes all v1 tags):

  # Script name: strip-tags.sh
  # Original Author: Ian of DarkStarShout Blog
  # Site: http://darkstarshout.blogspot.com/
  # Options slightly modified to liking of SavvyAdmin.com and acooke.org



  #Determine tags present:
  find . -iname "*.mp3" -print0 | xargs -0 -n 10000 eyeD3 --no-color -v >> $indexfile
  tagspresent=`sort -u $indexfile | awk -F\): '/^<.*$/ {print $1}' \
  | uniq | awk -F\)\> '{print $1}' | awk -F\( '{print $(NF)}' \
  | awk 'BEGIN {ORS=" "} {print $0}'`

  rm $indexfile

  #Determine tags to strip:
  tostrip=`echo -n $tagspresent $oktags $oktags \
  | awk 'BEGIN {RS=" "; ORS="\n"} {print $0}' | sort | uniq -u \
  | awk 'BEGIN {ORS=" "} {print $0}'`

  #Confirm action:
  echo The following tags have been found in the mp3s:
  echo $tagspresent
  echo These tags are to be stripped:
  echo $tostrip
  echo -n Press enter to confirm, or Ctrl+C to cancel...
  read dummy

  #Strip 'em
  stripstring=`echo $tostrip \
  | awk 'BEGIN {FS="\n"; RS=" "} {print "--set-text-frame=" $1 ": "}'`

  # First pass copies any v1.x tags to v2.3 and strips unwanted tag data.
  # Second pass removes v1.x tags, since I don't like to use them.
  # Without --no-tagging-time-frame, a new unwanted tag is added.  :-)

  find . -iname "*.mp3" -print0 | xargs -0 -n 10000 eyeD3 --no-tagging-time-frame $stripstring
  find . -iname "*.mp3" -print0 | xargs -0 -n 10000 eyeD3 --no-tagging-time-frame --remove-v1 

  echo "Script complete!"

A text catalogue of tracks

Yoo are shopping in some little second hand CD3 store and see an interesting
looking album.  But do you already have it?  This script generates a web page
that you can view in your browser and then print out (I print with 4 pages per
sheet to make it as compact as possible):


  from os import listdir
  from os.path import join

  MUSIC = '/music'
  CATALOGUE = '/catalogue.html'

  def clean(text):
      text = text.strip().lower()
      if text.startswith("the"): text = text[3:].strip()
      return text

  def artists(root):
      for artist in sorted(listdir(root), key=clean):
	  albums = sorted(listdir(join(root, artist)), key=clean)
	  yield "<strong>" + artist + "</strong>: " + ", ".join(albums)

  with open(CATALOGUE, 'w') as out:
      print("<! doctype html><meta charset='utf8'><div style='font-family: monospace;'>%s.</div>" % 
	    "; ".join(artists(MUSIC)), file=out)

Some music sources

If all that has made you hungry for some new sounds, here's a few places that
I've used:

  http://musopen.org/ - free classical music
  http://www.clubfonograma.com/2010/04/fonogramaticos.html - no longer 
  updted, but some great "iberoamerican pop" mixtapes
  http://www.portaldisc.com/ - contemporary chilean music
  http://archive.org/details/netlabels - free, virtual record labels


Comment on this post