Andrew Cooke | Contents | Latest | RSS | 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

Choochoo Training Diary

Last 100 entries

Surprise Paradox; [Books] Good Author List; [Computing] Efficient queries with grouping in Postgres; [Computing] Automatic Wake (Linux); [Computing] AWS CDK Aspects in Go; [Bike] Adidas Gravel Shoes; [Computing, Horror] Biological Chips; [Books] Weird Lit Recs; [Covid] Extended SIR Models; [Art] York-based Printmaker; [Physics] Quantum Transitions are not Instantaneous; [Computing] AI and Drum Machines; [Computing] Probabilities, Stopping Times, Martingales; bpftrace Intro Article; [Computing] Starlab Systems - Linux Laptops; [Computing] Extended Berkeley Packet Filter; [Green] Mainspring Linear Generator; Better Approach; Rummikub Solver; Chilean Poetry; Felicitations - Empowerment Grant; [Bike] Fixing Spyre Brakes (That Need Constant Adjustment); [Computing, Music] Raspberry Pi Media (Audio) Streamer; [Computing] Amazing Hack To Embed DSL In Python; [Bike] Ruta Del Condor (El Alfalfal); [Bike] Estimating Power On Climbs; [Computing] Applying Azure B2C Authentication To Function Apps; [Bike] Gearing On The Back Of An Envelope; [Computing] Okular and Postscript in OpenSuse; There's a fix!; [Computing] Fail2Ban on OpenSuse Leap 15.3 (NFTables); [Cycling, Computing] Power Calculation and Brakes; [Hardware, Computing] Amazing Pockit Computer; Bullying; How I Am - 3 Years Post Accident, 8+ Years With MS; [USA Politics] In America's Uncivil War Republicans Are The Aggressors; [Programming] Selenium and Python; Better Walking Data; [Bike] How Fast Before Walking More Efficient Than Cycling?; [COVID] Coronavirus And Cycling; [Programming] Docker on OpenSuse; Cadence v Speed; [Bike] Gearing For Real Cyclists; [Programming] React plotting - visx; [Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life

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