Parsing ID3 tags in Bash and renaming MP3s

January 29, 2011 2 comments

(Subtitle: Using Bash when I probably shouldn’t…)

First, let’s define a bunch of useful functions.  We’ll later use these functions to extract the relevant sections of our ID3 tags.  I’m going to assume you are using your interpreter directly, so lets copy & paste the following lines straight into your terminal:

function tag() { tail -c 128 "$1" | head -c 3; }
function title() { tail -c 125 "$1" | head -c 30 | rtrim; }
function artist() { tail -c 95 "$1" | head -c 30 | rtrim; }
function album() { tail -c 65 "$1" | head -c 30 | rtrim; }
function year() { tail -c 35 "$1" | head -c 4 | rtrim; }
function comment() { tail -c 31 "$1" | head -c 28 | rtrim; }
function track() { tail -c 3 "$1" | hexdump -ve '/1 "%02i "' \
                   | awk '{ if ($1 == 0 && $2 != 0) print $2" " }'; }
function genre() { tail -c 1 "$1" | hexdump -ve '/1 "%03i"'; }

function rtrim() { sed 's/\([^ ]*\) *$/\1/'; }
function filt() { tr -d '/*?'; }

Now lets say the current directory is full of MP3s that we would like to move to an appropriate folder and give a more appropriate filename (in my case, I had my corrupt old iPod from which I was attempting to extract all the music I could).  The structure I’m aiming for is:


Making use of our functions defined above, this for loop ought to do the trick:

for file in *.mp3; do [ "$(tag "$file")" == "TAG" ] && \
    (  artist="$(artist "$file" | filt)"; \
       album="$(album "$file" | filt)"; \
       title="$(title "$file" | filt)"; \
       track="$(track "$file" | filt)"; \
       dir="$artist - $album"; \
       newfile="$track$artist - $title.mp3"; \
       mkdir -p "$dir"; \
       mv "$file" "$dir/$newfile"); \

And indeed this “one liner” did a reasonable job on my MP3 collection, YMMV.

Of course this only works for ID3v1 tags and doesn’t support “extended” v1 tags.

I can’t imagine ID3v2 possibly being parsed in bash, but I’d like to be proved wrong 🙂

BBC Open Sources MHEG+ Toolkit

August 20, 2010 23 comments

MHEG Player showing the Freesat Red Button homepage

Some very good news for all the MHEG developers out there, the BBC have this week open sourced the MHEG+ toolkit and it can be downloaded over at the MHEG+ SourceForge page.

The release comes with little in the way of tutorials and so it might all seem a little daunting at first. But having used (and worked on) the toolkit for a couple of years I think it is worth the initial effort and recommend you give it a try.

I intend to write a full getting started guide to help new users over on the MHEG+ SourceForge forum, but for now here are some code examples and an ultra-quick guide to getting the toolkit up & running…

Code Examples

There is a simple “Hello World” MHEG+ application downloadable from the MHEG+ subversion repository.

This blog also has two examples of MHEG+ code, see these posts #1, #2.

Ultra-quick MHEG+ getting started guide

The toolkit contains a compiler and an emulator. Currently both are available for download as a separate Java .jar files. You will need Java installed to use them.

To use the emulator/debugger, download the file named MhegPlus.MhegPlayer-1.0.1.jar and issue the command:

java -jar MhegPlus.MhegPlayer-1.0.1.jar

This will launch the Mheg Player GUI.

To use the compiler first download the file named MhegPlus.Compiler-1.0.1.jar. The compiler can invoked with the following command, but you will need to pass command-line options to get anything meaningful out of it:

java -jar MhegPlus.Compiler-1.0.1.jar

You can get help on the various compiler options by passing –help, e.g.:

java -jar MhegPlus.Compiler-1.0.1.jar --help
usage: mhegplus
-p,--output-app output application file? (optional)
-d,--output-dir output directory (optional)
-P,--output-app-sans-extension output application file as a or startup
-l,--log-level log level
-a,--asn1 select ASN.1 output (default)
-f,--application application file
-h,--help print this help
-t,--text select text output
-v,--version report version number

Happy MHEG+ing!

Working on a new HTML5 game engine for iPhone

February 24, 2010 2 comments

I’ve just completed the first stage of development on a new 2D HTML5 game engine for iPhone.

It is a complete re-write of the engine used in my iPhone platform game and it achieves a much higher and more consistent frame rate. I’m currently very pleased to be getting a consistent 17-18 FPS running full screen on an iPhone 3G.

It is a scrolling tile-based engine, meaning it should suitable for most 2D games. I’d like to use it to create another platformer, or maybe a shoot ’em up this time would be cool, but the engine could be used for other game types too.

Artist Wanted!

I need your help! to turn it into a finished game. This is a call out to any artists who would like to get involved, please drop me an email with examples of your work. My address can be found on the about page.

I intend to release 2 versions of the game:-

  • a demo version that runs in Mobile Safari but has no sound and few levels, and
  • a full version that can be purchased from the app store.

Revenues made from the app store version would be shared with the artist, so there might even be a few dollars in it for you!

HTML5 Platform Game for iPhone

February 4, 2010 37 comments

I’ve spent today porting my HTML 5 platformer, mentioned in an earlier post, to run on iPhone/iPod Touch.

The game uses the HTML 5 Canvas element and a few iPhone-specific HTML/JS extensions. It should still run on most desktop and mobile web browsers however.

I’ve tested the game on an iPhone 3G and 3Gs. The 3Gs performs as well as a desktop PC which is some feat! The 3G on the other had is a little sluggish, but the game is still playable; if you’re using a 3G I recommend playing the game with your phone upright (portrait) for best performance, since in this mode the game aims to draw to a smaller area of the display.

It is also possible to play the game full screen, that is without the Safari address bar & toolbar, by adding the game to your home screen.

According to Liam Green-Hughes on Twitter, the game also runs fine on the Nokia N900, running Firefox.

See my earlier post for the history of the game, or to play the desktop version. As before I invite you to take a look at the source code of the game. Simply hit your browser’s “view source” button to read the code.

Click the Play link below (ideally on your iPhone!) and be sure to leave a comment with your thoughts.


Or go to this address on your iPhone:

A Platform Game Written in HTML5

September 11, 2009 117 comments

Click to play

Nearly 15 years ago, when I was all of 15 years old, I wrote this little platform game which I have recently ported to HTML5/Javascript.

It’s a kind of Mario clone and at the time I named it “Fred Jones in Adventureland”… hmm, not a great name.

The themes of the game are intended to be tongue-in-cheek however. Hence the ridiculous title and deliberately unsubtle references to other games in the genre.

And it is somewhat incomplete; the player has unlimited lives and unfortunately there are only 5 levels. But as it stands it is playable and hopefully a little bit of fun. There are a couple of surprises along the way too.

Browser Compatibility

If you’re using Microsoft Internet Explorer 7 or 8 the game will run dreadfully slow. (Update: The game now supports the Google Chrome Frame plug-in. If you are using Internet Explorer I recommend you install it.)

Internet Explorer 6 and earlier, Firefox 1.x and I believe Opera 8 and previous will not run the game at all. This is because these older browsers do not support HTML5 (arguably, no version of Internet Explorer supports HTML5, however the game can run with the assistance of 3rd-party plug-ins and other workarounds). Please upgrade or switch to a different browser.


Left Arrow: move left
Right Arrow: move right
Spacebar: jump

iPhone Version

See HTML5 Platform Game for iPhone


Click the link above to play the game.

Technical Details

This is a port of the original game to HTML5/Javascript. This means it now runs in any modern web browser.

Unlike most web games the game does not use Flash. The HTML5 Canvas is used to draw the visual elements.

All the code for the game is all available to read if you hit “view source” in your browser and I invite you to take a look.

I was pleasantly surprised by the performance of Canvas drawing operations and javascript run-times in most modern browsers. However Internet Explorer users apparently will have to wait for version 10 before such features will grace the browser.

The game currently has no sound but this could be added using the HTML5 <audio> element.

I’m on BBC News online!

March 26, 2009 3 comments

If you visit the BBC News Technology Homepage at the moment you’ll find a link to my Red Button Arcade blog post in the Features, Views and Analysis section.

Must have been a slow news day…


My BBC Red Button games blog post is up…

March 23, 2009 8 comments

My BBC Red Button Aracde blog post is live on the BBC Red Button blog.  Follow the link to read about the challenges of writing simple games on interactive television platforms.

The post includes a video and screenshots of the games I’ve written during my “10% time” at the BBC.  This is time we are given — half a day each week — to work on our own projects and try to innovate.

The games are written in the MHEG programming language, a subject I often discuss on this blog.

Update: The post is now linked to from the front page of the BBC News Technology section.