This discussion is archived
3 Replies Latest reply: Apr 24, 2012 12:00 PM by 845492 RSS

How can I get information (playing time) of mp3 audio files?

845492 Newbie
Currently Being Moderated
I'm using the JavaZoom/Tritonus SPI implementation for decoding mp3 files. I have it installed correctly as I can open, decode, and play an mp3 file successfully. However, it's a terrible hack right now. I plan to simply read the mp3 file, convert it to WAV in memory, and then play/pause/seek with immediate response using the WAV format. In order to do this efficiently I need to know the size of the WAV format that will result from the decoding of the mp3 file. I am assuming that I can calculate this from the decoded format sample rate, sample size, channels, and length (in time). The first three I know as they are parameters for the target decoded format that I set. The last I cannot determine. I have found that using AudioInputStream.getFormat().properties() I can see two properties: vbr=false and bitrate=192000. On windows using Explorer I can see more information about the file than that, including length in mm:ss.

I can't figure out how to extract the play time length from the file. Can someone help? Thanks.

UPDATE:

I realized that I can at least generate an estimate of duration (time) using the encoded file length (in bytes) and the bitrate property (but not for variable bit rate I presume). That gets me close, but I'm still thinking there may be some metadata in the file that holds an exact value. I know my query for properties is not finding all the metadata in the file because when I view the file (in Explorer, or using Audacity or others) I can see there is artist, title, track, etc. metadata which I'm not seeing. Even using the method above to estimate duration, I need to find the length of the actual audio data, not including file/frame headers (I also presume this is not included in the bitrate value).

Edited by: ags - Updated
  • 1. Re: How can I get information (playing time) of mp3 audio files?
    810200 Newbie
    Currently Being Moderated
    I was doing something similar with ogg/vorbis files, unpacking them and storing the .wav onto the host. (The files were kind of large to commit to memory. But playback was quicker using SourceDataLines on the wav files directly rather than also having to decode them during playback.)

    If you have the sample size (i.e., the length in frames) and the number of bytes per frame, you can get an exact number of bytes by multiplying these numbers, yes?

    For example, if 16 bits encoding, stereo, then multiply the number of frames (sample size) * 2 bytes per sample * 2 channels per frame. The exact time can be determined from that by using the sample rate.

    AudioInputStream has a .getFrameLength method. The JavaZoom ogg/vorbis decoder I was using also had this method. I would guess the JavaZoom mp3 decoder also has a getFrameLength method.

    I wouldn't trust the info in the metadata. Isn't that often entered by the consumer? Also, I believe that the wav file format's header can vary in size (in part due to the metadata itself). It's better to rely on code that has already been built to parse wavs.
  • 2. Re: How can I get information (playing time) of mp3 audio files?
    845492 Newbie
    Currently Being Moderated
    Phil Freihofner wrote:
    If you have the sample size (i.e., the length in frames) and the number of bytes per frame, you can get an exact number of bytes by multiplying these numbers, yes?
    I dont' think that will work for MP3 files. I cannot find the length in frames, and there is no indication of bytes per frame. I don't think it's constant throughout the MP3 file.
    For example, if 16 bits encoding, stereo, then multiply the number of frames (sample size) * 2 bytes per sample * 2 channels per frame. The exact time can be determined from that by using the sample rate.
    That works for WAV files, but not MP3 (AFAIK)
    AudioInputStream has a .getFrameLength method. The JavaZoom ogg/vorbis decoder I was using also had this method. I would guess the JavaZoom mp3 decoder also has a getFrameLength method.
    Yes, it does support that method (I think it is required if the MP3SPI implementation is to suppor the required interfaces. However, that method "reliably" returns a value of -1 (as do getSampleSize() and getFrameSize()).
    I wouldn't trust the info in the metadata. Isn't that often entered by the consumer? Also, I believe that the wav file format's header can vary in size (in part due to the metadata itself). It's better to rely on code that has already been built to parse wavs.
    Absolutely agreed. That's why I'm looking for a more reliable method. I think I can calculate an estimate (but would prefer a precise value) of the decoded audio length as long as the bitrate property is always present and correct. I'm not sure if I can rely on that being true, though. I'm not sure if the bitrate property value itself is an estimate or precise value, or if it remains constant throughout the file. And I don't know if it includes the bits present in the frame headers or just the frame payload (audio).

    Still, I know I'm missing some data because I can see artist/title/track metadata when browsing in Explorer or WMP. I can't retrieve that as a property using the Java interface. I wonder if I have to read the last 128 bits of the file for that?

    UPDATE

    I finally realized that I was looking for properties of the audio file, not the audio stream. Using AudioFileFormat.properties() returns a Map of all the metadata that I see using Explorer, WMP, etc. There is a duration property available (for direct retrieval of the length in microseconds) and it is very close to what I can calculate using the mp3.framesize.bytes and mp3.length.frames property values (perhaps as a double-check).

    If the duration is always present, that seems most robust. I presume it would be accurate for variable bitrate files also. I don't think that I could calculate as above if the framesize varies throughout the file. I have no way to judge how reliably the duration property is actually present, or how accurate it is when it is present. However, unless there is some other field that is guaranteed to be available as specified in the mp3 format (which I haven't found, but would certainly welcome if someone would point me toward it) I think the only real choices are relying on the duration property or decoding the file fully and determiining the length for sure.

    Edit: added UPDATE info
  • 3. Re: How can I get information (playing time) of mp3 audio files?
    845492 Newbie
    Currently Being Moderated
    Use the AudioFileFormat.properties() method to find the duration value (in microseconds)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points