| 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

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; Changing Salt Size in Simple Crypt 3.0.0; Logarithmic Map (Moved); More Info; Words Found in Voynich Manuscript; An Inventory Of 3D Space-Filling Curves; Foxes Using Magnetic Fields To Hunt; 5 Rounds RC5 No Rotation; JP Morgan and Madoff; Ori - Secure, Distributed File System; Physical Unclonable Functions (PUFs); Prejudice on Reddit; Recursion OK; Optimizing Julia Code; Cash Handouts in Brazil; Couple Nice Music Videos; It Also Works!; Adaptive Plaintext; It Works!; RC5 Without Rotation (2); 8 Years...; Attack Against Encrypted Linux Disks; Pushing Back On NSA At IETF; Summary of Experimental Ethics; Very Good Talk On Security, Snowden; Locusts are Grasshoppers!; Vagrant (OpenSuse and IDEs); Interesting Take On Mandela's Context; Haskell Cabal O(n^2) / O(n) Fix; How I Am 4; Chilean Charity Supporting Women; Doing SSH right; Festival of Urban Intervention; Neat Idea - Wormholes Provide Entanglement; And a Link....; Simple Encryption for Python 2.7; OpenSuse 13.1 Is Better!; Little Gain...; More Details on Technofull Damage; Palmrest Cracked Too....; Tecnofull (Lenovo Support) Is Fucking Useless; The Neuroscientist Who Discovered He Was a Psychopath; Interpolating Polynomials; Bottlehead Crack as Pre-amp; Ooops K702!; Bottlehead Crack, AKG K701; Breaking RC5 Without Rotation; Great post thank you; Big Balls of Mud; Phabricator - Tools for working together; Amazing Julia RC5 Code Parameterized By Word Size; Chi-Square Can Be Two-Sided; Why Do Brits Accept Surveillance?; Statistics Done Wrong; Mesas Trape from Bravo; European Report on Crypto Primitives and Protocols; Interesting Omissions; Oryx And Crake (Margaret Atwood); Music and Theory; My Arduino Programs; Elliptic Curve Crypto; Re: Licensing Interpreted Code; Licensing Interpreted Code; ASUS 1015E-DS03 OpenSuse 12.3 SSD

© 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