如何在 Java 中获取 mp3 文件的总时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:57:57  来源:igfitidea点击:

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

javafileaudiomp3

提问by Tom Brito

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.

在提供的答案如何在Java中得到一个声音文件的总时间?适用于 wav 文件,但不适用于 mp3 文件。

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.

它们对 wav 文件给出了相同的正确结果,但对 mp3 文件给出了错误且不同的结果。

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

知道我该怎么做才能获得 mp3 文件的持续时间吗?

采纳答案by Tom Brito

Using MP3SPI:

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

}

回答by Zorf

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.

我在这方面很老套,但我总是简单地获取 MP3 的规格,编写一个搜索正确位的函数,然后找到它。

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

如果 Winamp 可以通过读取 MP3 文件的比特流来确定这一点,那么我可以吗?

And so can you, I believe in you mate!

你也可以,我相信你,伙计!

回答by Kasturi

Here is a detailed explanation of the MP3 File structure

这里是MP3文件结构的详细解释

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

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

回答by L?ng Minh

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

这是我获取文件 .mp3 总时间的方式,我使用的库是 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;

回答by davidu

Use jave-1.0.1.jarlibrary.

使用jave-1.0.1.jar图书馆。

  • 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.
  • 它通过了我对波形文件格式的测试,例如:wav-pcm8/16、wav-alaw、wav-ulaw、wav-gsm、wav-adpcm;
  • 它通过了我对一些 MP3 文件格式的测试:cbr vs vbr、立体声 vs SingleChannel;
  • 它可以支持视频格式,我还没有测试过。

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.

我试过Jlayer 1.0.1,但对于某些 MP3 格式它失败了。至于另一个 libaray jaudiotagger-2.0.3.jar,它适用于 MP3 格式,但它只能支持 wav-pcm8/16。