How do I get a mp3 file's total time in Java?

情到浓时终转凉″ 提交于 2019-12-18 11:26:53

问题


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:


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();
    }

}



回答2:


Here is the way I get the total time of a file .mp3, I'm using the library is Jlayer 1.0.1

    Header h= null;
    FileInputStream file = null;
    try {
        file = new FileInputStream(filename);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
    }
    bitstream = new  Bitstream(file);
    try {
        h = bitstream.readFrame();

    } catch (BitstreamException ex) {
        Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
    }
    int size = h.calculate_framesize();
    float ms_per_frame = h.ms_per_frame();
    int maxSize = h.max_number_of_frames(10000);
    float t = h.total_ms(size);
    long tn = 0;
    try {
        tn = file.getChannel().size();
    } catch (IOException ex) {
        Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
    }
    //System.out.println("Chanel: " + file.getChannel().size());
    int min = h.min_number_of_frames(500);
    return h.total_ms((int) tn)/1000;



回答3:


Here is a detailed explanation of the MP3 File structure

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




回答4:


Use jave-1.0.1.jar library.

  • It passed my test on wave file formats such as: wav-pcm8/16, wav-alaw, wav-ulaw, wav-gsm, wav-adpcm;
  • It passed my test on some MP3 file formats: cbr vs vbr, stereo vs SingleChannel;
  • It can support video formats, which I haven't tested on.

Code sample:

File source = new File("C:\\22.mp3");
Encoder encoder = new Encoder();
try {
    MultimediaInfo mi = encoder.getInfo(source);
    long ls = mi.getDuration();
    System.out.println("duration(sec) = "+  ls/1000);
} catch (Exception e) {
    e.printStackTrace();
}

I've tried Jlayer 1.0.1, but it failed for some MP3 format. As for another libaray jaudiotagger-2.0.3.jar, it works fine for MP3 formats, but it can only support wav-pcm8/16.




回答5:


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!



来源:https://stackoverflow.com/questions/3046669/how-do-i-get-a-mp3-files-total-time-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!