tags:

views:

205

answers:

3

The answers provided in How do I get a sound file’s total time in Java? work well for wav files, but not for mp3 files.

They are (given a file):

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();  

and:

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));

They give the same correct result for wav files, but wrong and different results for mp3 files.

Any idea what do I have to do to get the mp3 file's duration?

+1  A: 

I'm old-fashioned in this, but I always simply get the specs for MP3, write a function that searches the right bits, and find it out.

If Winamp can determine this by reading the bitstream of an MP3 file, then so can I right?

And so can you, I believe in you mate!

Lajla
I think people came to SO looking for answers, not for words of faith, but thanks anyway...
Tom Brito
By the way, I know nothing about handle mp3 files, do you have an example or article link about what you are saying?
Tom Brito
Well, this is how built-in functions do it. MP3 is a specification, which means that at some index of an MP3 file in some format the length should be written. Say the length is at the 129th byte, you just load the file as a stream, go to that part and read it out.Below, some one posted a link of the MP3 file structure.
Lajla
In the TAGV2 part, it says that `Size of TAG is encoded into 4 Bytes. But not to be so easy, the most significant bit in each Byte is set to 0 and ignored. Only remaining 7 bits are used. The reason is to avoid mismatch with audio frame header which has the first synchro Byte FF).` That means when I find a FF is the end of TAGV2 and begin of audio frames?
Tom Brito
The audio length isn't written explicitly in a field in the MP3 format.
indiv
A: 

Here is a detailed explanation of the MP3 File structure

http://www.autohotkey.com/forum/topic29420.html

Kasturi
You and I baby, we're a team.
Lajla
Direct link to the page (better formated): http://www.multiweb.cz/twoinches/MP3inside.htm(I'm reading, thanks!)
Tom Brito
+2  A: 

Using MP3SPI:

private static void getDurationWithMp3Spi(File file) throws UnsupportedAudioFileException, IOException {

    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
    if (fileFormat instanceof TAudioFileFormat) {
        Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
        String key = "duration";
        Long microseconds = (Long) properties.get(key);
        int mili = (int) (microseconds / 1000);
        int sec = (mili / 1000) % 60;
        int min = (mili / 1000) / 60;
        System.out.println("time = " + min + ":" + sec);
    } else {
        throw new UnsupportedAudioFileException();
    }

}
Tom Brito