Monday, February 18, 2008

Reading MP3 meta-data from ColdFusion

On friday I posted about how to read the meta-data for Swf (flash) file from ColdFusion and on the same note, I think it will be cool to read the meta-data for MP3 file as well. Here is the specification for MP3 header and it is fairly straight forward to read the header using the spec.

ColdFusion 8 knows how to read MP3 file and it can provide a set of meta-data for it. Though this functionality is not exposed via any tag or function, you can still use it. The meta-data it provides are

  • Bit Rate : in kbps
  • Frequency : in Hz
  • Play duration : in second
  • Version : integer
      • 0 - MPEG version 2.5
      • 1 - MPEG version 2
      • 3 - MPEG version 1
  • Copy Right : true/false
  • Channel mode : integer
      • 0 - Stereo
      • 1 - Joint Stereo
      • 2 - Dual Channel
      • 3 - Mono or Single Channel

Here is an example code.

<cfset mp3File = createObject("java", "coldfusion.util.MP3File").init("C:\music.mp3")>
<cfoutput>mp3File.getBitRate() : #mp3File.getBitRate()# kbps</cfoutput><br>
<cfoutput>mp3File.getFrequency() : #mp3File.getFrequency()# Hz</cfoutput><br>
<cfoutput>mp3File.getVersion() : #mp3File.getVersion()#</cfoutput><br>
<cfoutput>mp3File.getDuration() : #mp3File.getDuration()# Sec</cfoutput><br>
<cfoutput>mp3File.isCopyRighted() : #mp3File.isCopyRighted()#</cfoutput><br>
<cfoutput>mp3File.getChannelMode() : #mp3File.getChannelMode()#</cfoutput><br>

Though you can use the above mentioned class (coldfusion.util.MP3File) without any problem, you must keep it in mind that it is unsupported and *might* change in future. Just a disclaimer !!! :-)

This class does not get you the ID3V1 or ID3V2 meta-data, but it should be easy for you to do it. For ID3V1, all you need to do is to read the last 128 bytes of the mp3 file and you have all the information required. Reading ID3V2 tag is little more involved and there are a number of open source java libraries available (common one being jidlib and jaudiotagger) which can be used.


Michael Sharman said...

Nice that you can do this natively in CF8.

To read ID3 tags using one of the open source libraries, Ray did a couple of posts a little while ago.

Youtube lover said...

Thanks for the example code, had some trouble finding it. Now trying to get it to work.

Anonymous said...

When will adobe release CF7 patch for fontsize change if margin change on non standard papersize?

David Stamm said...

This is a really incredible discovery, Rupesh. Thanks for sharing it with us!

Also of interest, coldfusion.util.MP3File is a subclass of, so you can also call methods on the MP3 like lastModified() and isHidden().

Any idea what other goodies are in the coldfusion.util package?