Getting audio duration in a MP3 file

I’m using a java library that Ray Camden introduced us to about 17 years ago and I use it reliably to read the metadata of mp3 files added to our radio station library so i can load the metadata into our database without needing to laboriously retype everything. However I have never been able to figure out how to use Lucee to find out the duration of the song its examining. I know it’s possible because every app that plays music can figure out how long the song is. It must be in the metadata or file data somewhere. I’ve read up everything i can find and all i’ve ended up with is a brain that’s hurting because it’s all confusing and no one seems to be able to describe to me how I can find out the duration of a song.

Does anyone have an answer to this? I can examine the metadata and get the songname, album it’s from, music genre, year it was released, songwriters, all that good stuff. But not duration. It’s maddening because I know the answer is staring me in the face somewhere, if i only knew where!

The java library I’m using is called Java ID3 Tag Library
https://javamusictag.sourceforge.net/ but there are others that have much the same information.

Cheers
Mike Kear
Windsor, NSW, Australia

To be clear, are you saying that this Java library, when used with Adobe ColdFusion, provides you with the duration, but when used with Lucee, does not? Or are you just saying the library will read metadata, but not duration on any CF engine?

I’m not sure duration would be part of the ID3 tag, but I assume if you Google how to get the duration of an MP3 with Java, you could transfer that method to CF.

Thank you for your input.

I confess that I am a total novice when dealing directly with java. So here I am in a foreign corner of the Lucee world, dealing with java, which I am struggling to understand in the first place. Back when I was using ACF, I followed Ray Camden’s blog post and it all worked. And the method "getDuration()’ gave me the number of seconds the audio ran. Brilliant!! I never needed to know how it worked, it just did. However when i moved this code all over to Lucee, with a little tinkering about I got Lucee to read the mp3 files metaradata and that worked well. Except it didnt like the getDuration() method So I guess ACF was figuring out the duration itself rather than the java library. “So never mind, I’l just go find another library,” I thought. HAH! Now I’m so confused i dont know which way to go, how to work out the duration and not only that every time i google it, i get another set of the same sort of non-answers along the lines of “I use this library and it’s soo easy!” (might be easy for you mate, but it’s not for me!) But none of them have a duration method, nor an explanation of how duration is calculated. As I said, it must be simple once you know how, because every player app calculates it.

In the mean time I am programmatically getting all the details of my library music for the database (which makes adding 10-20 albums to the library fast and accurate) except for duration which I have to type in song by song. I’d like to do it programmatically.

Cheers
Mike Kear
Windsor, NSW, Australia

Can you explain what you mean? What code did you run? What error did you receive?

That’s not possible. When you call a Java method on a Java library, the CF engine isn’t doing anything but returning that value.

I think in order to help you, we need a reproducible test case you can provide us that we can run to see what you’re seeing.

Fair enough. I’ll come back in a short while with usable test files for you

Cheers
Mike Kear

1 Like

I’d try a Tika (maintained apache library) approach to retrieve mp3 tag data . Tika is already loaded in Lucee (i think for zip functionality)
You may need to load another Tika version besides.

Here is a nice post from @Peter who retrieved video data with tika. I’d do ti that way.

https://lucee.daemonite.io/t/how-to-retrieve-video-dimensions-with-lucee-coldfusion/9230

it’s an executable, but MediaInfo is pretty good for this

1 Like

I’m still worrying away at this annoying problem. What i’m attempting to do ( and nearly finished!) is read some MP3 files of songs to be added to our radio station library. They are just songs that artists and/or record labels send us for broadcast. Properly tagged MP3 files are perfect for broadcast on FM - they have the same specs as the transmission specs.

I have been able to read the MP3 files and extract into a struct most of the variables of each MP3, for example song title, artistname, albumname, songwriter etc to add into the database we use to control our programming, and display on your car’s dash the name etc of the song playing - all that kind of stuff. The only data I havent been able to get this routine to find is the duration of the mp3 file. Believe it or not it isn’t actually part of the mp3 metadata. We need that for figuring out what songs will fit into the time leading up to the next commiitment (news link, ad break etc. ) At the moment, I’m adding the duratoin manually by typing it into a form while the files are loaded into Windows Media player. When you’re librarying 10 albums a day it isnt very efficient to do that for only one item of data per song.

Duration might not be part of the metadata but any playing device instantly knows how long the song is, because they all display it. Even the most basic ones. Also when you play an audio file on a web page, using the HTML5 AUDIO tag the duration is shown there SOMEWHERE too.

So how are they getting the duration? OR more imporantly, how can i do it from Lucee in a way that lets me add the duration to the song’s struct so I can load that automatically into the database with everything else?

**What I’ve tried so far: **

The java library I’m using is called Java ID3 Tag Library
https://javamusictag.sourceforge.net/ but there are others. This gives me everything I need - except duration of the MP3. .

Flollowing suggestions in this thread, I’ve looked at Apache Tika but that seems to be doing almost anything BUT what I’m looking for. I guess I’m confused about that too.

I’'ve seen reference in stackoverflow to AudioFileFormat which apparently has a property called Long duration but i’m confused about how that can help me too - i cant see where to find out anything about AudioFileFormat. Not even uncle Google seems to now much about it.

I guess this is all a result of not getting a degree in computer science but when i was at uni in 1970 such things weren’t available.

Can anyone help me clear away some of the fog so i can see the Duration? Please?

Cheers
Mike Kear
Windsor, NSW, Australia

Hi, i was able to extract mp3 duration with Tika. I didn’t post it here because I thought @Zackster approach with the exe file is way better and easier. I’ve added the instruction and snippet when I experimented with Tika to my own documentation here (just search for MP3):

1 Like

Thank you Andreas. What is xmpDM about? (I see in your script you have the expression “xmpDM:duration” ) I dont think I’ve seen that in travailing this issue before.

Cheers,
Mike Kear

This is part of the namespace Tika provides for the metadata (XMP Dynamic Media schema). See the Tikas XMPDM Interface Specification here:XMPDM (Apache Tika 2.4.0 API)

Thanks Andreas/ It’s late here in Australia now I’ll have a look at that tomorrow.

Cheers
Mke Kear

1 Like