Build An MP3 Catalogue System With Perl – Basics

My mp3 collection was increasing and I wanted to build a catalogue for the same. There are various steps in having even a simple catalogue system. In this post and a few posts that will follow, I will be explaining how to write such a system using perl as the programming language.

MP3 Format and ID3 Tags

MP3 is an audio coding format for digital audio. The audio data in this file is in a compressed format. The compression is a lossy compression, meaning the sound quality is not very clear. In spite of being a lossy format, it is one of the most popular format for audio streaming and storage. The mp3 file has built in bibliographic information such as title, artist, album. This information is stored in a field inside the file known as ID3 tag. Using this information, the MP3 players are able to display the Song Title, Album name and Artist name(s) etc.

There are couple of versions of these ID3 tags in use. ID3v1 (1.1 being the last in version 1 series) and ID3v2 (ID3v2.4 being the latest version).

Perl MP3::Tag Module

The MP3::Tag module of perl can be used to read and write both the versions of the ID3 tag. Here are few of the sample perl programs to do that. This will help in understanding the usage of the modules before we proceed to the next steps.

Below are couple of examples showing how to read ID3v1 and ID3v2 tags.

#!/usr/bin/perl
#
# id3v1_read.pl
#
use 5.010;
use warnings;
use strict;
use MP3::Tag;

# set filename of MP3 track
my $filename = "your_mp3_file";

# create new MP3-Tag object
my $mp3 = MP3::Tag->new($filename);

# get tag information
$mp3->get_tags();

# check to see if an ID3v1 tag exists
# if it does, print track information
if (exists $mp3->{ID3v1}) {
  #$mp3->{ID3v1}->remove_tag();exit;

  say "Filename: $filename";
  say "Artist: " . $mp3->{ID3v1}->artist;
  say "Title: " . $mp3->{ID3v1}->title;
  say "Album: " . $mp3->{ID3v1}->album;
  say "Year: ". $mp3->{ID3v1}->year;
  say "Genre: " . $mp3->{ID3v1}->genre;
} else {
  say "$filename: ID3v1 tag not found";
}

# clean up
$mp3->close();

ID3v2 tags are a bit more complex as they allow a lot more information to be stored in the MP3 file such as the album artwork etc. If you run the following script on one of your MP3 files it will print all the ID3v2 information to the screen. I have used the getc() function in order to allow you to observe the output and press <Enter> to proceed to the next set of key-value pair. After couple of keystrokes you will see there are lot of junk characters printed. These junk characters are nothing but the album artwork and following the junk characters is the MIME type of the artwork. In my case the MIME types were all “image/jpeg”.

#!/usr/bin/perl
#
# id3v2_read.pl
#
use 5.010;
use warnings;
use strict;
use MP3::Tag;

# set filename of MP3 track
my $filename = "mp3_file_name;

# create new MP3-Tag object
my $mp3 = MP3::Tag->new($filename);

# get tag information
$mp3->get_tags();

# check to see if an ID3v2 tag exists
# if it does, print track information
if (exists $mp3->{ID3v2}) {
  # get a list of frames as a hash reference
  my $frames = $mp3->{ID3v2}->get_frame_ids();

  # iterate over the hash, process each frame
  foreach my $frame (keys %$frames) {
    # for each frame get a key-value pair of content-description
    my ($value, $desc) = $mp3->{ID3v2}->get_frame($frame);
    if (defined($desc) and length $desc) {
      say "$frame $desc: "; 
    } else {
      say "$frame :";
    }
    # sometimes the value is itself a hash reference containing more values
    # deal with that here
    if (ref $value eq "HASH") {
      while (my ($k, $v) = each (%$value)) {
        say "\n     - $k: $v";
      }
    } else {
      say "$value";
    }
    # allows to view each iteration
    getc(STDIN);
  }
} else {
  say "$filename: ID3v2 tag not found";
}

# clean up
$mp3->close();

Next Steps

Most of the current MP3 players read ID3v2 tags. It will be good to understand the structure of the ID3v2 tags, using one of the links I provided above. This will help you prepare for understanding the further articles in this series. In next part we will see how to extract desired information quickly and how to extract the artwork data from the MP3 file. Stay tuned for more.

Posted in FLOSS, Programming, Tips/Code Snippets | Tagged , , | Leave a comment

Einstein’s Universe

Einstein's Universe Book CoverAn excellent book written for ordinary people who want to understand about Einstein’s ideas on universe. Written in a very simple language and no mathematics. The author has successfully explained the complex concept of Special Theory of Relativity and General Theory of Relativity.

However, the book only covers Einstein’s view on the Universe and as it is clearly known that Universe can be better explained together with Relativity and Quantum Theory, the universe can not be explained. Einstein never accepted the quantum theory and hence this book does not mention much about quantum theory.

In close to four decades since this book was published first time (first published in 1979), a lot has transpired in the field of quantum theory including the very recent discovery at CERN of God particles (Bosons traveling faster than light) (experiments are still going on).

Nevertheless this book is still worth a read for every Relativity fan.

Posted in Book Reviews | Tagged | Leave a comment

As The Crow Flies

As The Crow Flies Book CoverCharlie Trumper, a young boy from East End of London, inherits his grandfather’s barrow and then there is no stopping for him. He ended up setting a retail empire and served as a second Chairman and Life President of the Trumper empire. The story takes the reader through Charlie’s childhood memories, the two world wars and the emergence of vibrant and colorful 60′s and 70′s. All this and the enmity with the Trentham family almost caused Charlie his empire.

I enjoyed this book thoroughly and lived the entire 75 years with Charlie, such is the writing of Jeffrey Archer. This book also gave a glimpse of life in britain during this 75 year period starting from 1900, especially the increasing prices. This will be quite evident when the reader reads what Charlie and his grandfather used to shout while selling fruits and vegetables and what Charlie was shouting at the age of 75. What? Shouting to sell fruits and vegetables …… and Lord Sir Charlie Trumper of Whitechappel ……… Read on this book dear reader to find out this and a lot of other suspense revealing themselves.

Posted in Book Reviews | Tagged , | Leave a comment

Last Man Standing

Last Man Standing Book CoverWeb London is the only survivor of the Charlie group of FBIs Hostage Rescue Team which was all wiped out in a drug raid. The story revolves around what happens in Web’s life after that incident. Determined to find the culprit, Web was struggling with personal psychological problems and also suspicions from his own FBI, he decided to take professional help from a psychiatrist. In the process lot of interesting events unfolded including Web’s previous visits to another psychiatrist and his undergoing hypnosis.

The climax was somewhat unexpected and the actual culprit came as a surprise to me. However, I feel that it may have been presented in a better way to gain some interest from the user. Lots of different scenarios keep on switching very fast and often the traction from the main plot was lost sometime. Some of the characters perhaps were not necessary as I was not able to understand the role they played and significant number of pages were wasted on them.

Posted in Book Reviews | Tagged , , | Leave a comment

Bankerupt

Bankerupt Book CoverThis book the author has focused on gun crimes in the USA along with the usual banking theme. Two plots simultaneously runs and then they merge with each other towards the end. The author has also mentioned the problem of stashing black money in swiss banks by Indians and the role Indian investment bankers played in that.

Overall a good read, but not the very best by the author.

Posted in Book Reviews | Tagged , , | Leave a comment