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

C[omp]ute

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.

Last 100 entries

Half of Americans Think Climate Change Is a Sign of the Apocalypse; Saturday Surf Sessions With Juvenile Delinquents; Ssh, tty, stdout and stderr; Feathers falling in a vacuum; Santiago 30m Bike Route; Mapa de Ciclovias en Santiago; How Unreliable is UDP?; SE Santiago 20m Bike Route; Cameron's Rap; Configuring libxml with Eclipse; Reducing Combinatorial Complexity With Occam - AI; Sentidos Comunes (Chilean Online Magazine); Hilary Mantel: The Assassination of Margaret Thatcher - August 6th 1983; NSA Interceptng Gmail During Delivery; General IIR Filters; What's happening with Scala?; Interesting (But Largely Illegible) Typeface; Retiring Essentialism; Poorest in UK, Poorest in N Europe; I Want To Be A Redneck!; Reverse Racism; The Lost Art Of Nomography; IBM Data Center (Photo); Interesting Account Of Gamma Hack; The Most Interesting Audiophile In The World; How did the first world war actually end?; Ky - Restaurant Santiago; The Black Dork Lives!; The UN Requires Unaninmous Decisions; LPIR - Steganography in Practice; How I Am 6; Clear Explanation of Verizon / Level 3 / Netflix; Teenage Girls; Formalising NSA Attacks; Switching Brakes (Tektro Hydraulic); Naim NAP 100 (Power Amp); AKG 550 First Impressions; Facebook manipulates emotions (no really); Map Reduce "No Longer Used" At Google; Removing RAID metadata; New Bike (Good Bike Shop, Santiago Chile); Removing APE Tags in Linux; Compiling Python 3.0 With GCC 4.8; Maven is Amazing; Generating Docs from a GitHub Wiki; Modular Shelves; Bash Best Practices; Good Emergency Gasfiter (Santiago, Chile); Readings in Recent Architecture; Roger Casement; Integrated Information Theory (Or Not); Possibly undefined macro AC_ENABLE_SHARED; Update on Charges; Sunburst Visualisation; Spectral Embeddings (Distances -> Coordinates); Introduction to Causality; Filtering To Help Colour-Blindness; ASUS 1015E-DS02 Too; Ready Player One; Writing Clear, Fast Julia Code; List of LatAm Novels; Running (for women); Building a Jenkins Plugin and a Jar (for Command Line use); Headphone Test Recordings; Causal Consistency; The Quest for Randomness; Chat Wars; Real-life Financial Co Without ACID Database...; Flexible Muscle-Based Locomotion for Bipedal Creatures; SQL Performance Explained; The Little Manual of API Design; Multiple Word Sizes; CRC - Next Steps; FizzBuzz; Update on CRCs; Decent Links / Discussion Community; Automated Reasoning About LLVM Optimizations and Undefined Behavior; A Painless Guide To CRC Error Detection Algorithms; Tests in Julia; Dave Eggers: what's so funny about peace, love and Starship?; Cello - High Level C Programming; autoreconf needs tar; Will Self Goes To Heathrow; Top 5 BioInformatics Papers; Vasovagal Response; Good Food in Vina; Chilean Drug Criminals Use Subsitution Cipher; Adrenaline; Stiglitz on the Impact of Technology; Why Not; How I Am 5; Lenovo X240 OpenSuse 13.1; NSA and GCHQ - Psychological Trolls; Finite Fields in Julia (Defining Your Own Number Type); Julian Assange; Starting Qemu on OpenSuse; Noisy GAs/TMs; Venezuela; Reinstalling GRUB with EFI; Instructions For Disabling KDE Indexing; Evolving Speakers

© 2006-2013 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
--------------

 #!/bin/bash

 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):

  #!/bin/bash

  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).

  #!/bin/bash

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

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

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:

  #!/bin/bash

  artist=$1
  echo $artist

  IFS=$'\n'
  for dir in `ls -1 "/music/$artist"`
  do
      n=`ls -1 "/music/$artist/$dir" | wc -l`
      if [[ $n -lt 4 ]]
      then
	  echo "/music/$artist/$dir"
	  touch "/music/$artist/$dir/.small"
      fi
  done


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
used).

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

  #!/bin/bash

  artist=$1
  echo $artist

  IFS=$'\n'
  for dir in `ls -1 "/music/$artist"`
  do
      pushd "/music/$artist/$dir"
      eyeD3 --no-tagging-time-frame --set-text-frame=TCMP:1 *.mp3
      popd
  done


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):

  #!/bin/bash
  # 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

  oktags="TALB APIC TPE1 TIT2 TRCK TCMP TPOS"

  indexfile=`mktemp`

  #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
  echo The following tags have been found in the mp3s:
  echo $tagspresent
  echo These tags are to be stripped:
  echo $tostrip
  echo
  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):

  #!/usr/bin/python3

  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


Andrew

Comment on this post