代码之家  ›  专栏  ›  技术社区  ›  Student

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

  •  14
  • Student  · 技术社区  · 16 年前

    提供的答案 How do I get a sound file’s total time in Java? 适用于wav文件,但不适用于MP3文件。

    它们(给出一个文件):

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

    还有:

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

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

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

    5 回复  |  直到 8 年前
        1
  •  14
  •   Student    16 年前

    使用 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
  •  4
  •   Hitesh Sahu    9 年前

    这是我获取文件总时间的方法。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;
    
        3
  •  2
  •   Kasturi    16 年前

    以下是MP3文件结构的详细说明

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

        4
  •  0
  •   davidu    8 年前

    使用 jave-1.0.1.jar 图书馆。

    • 它通过了我对wav文件格式的测试,如:wav-pcm8/16, wav-alaw、wav-ulaw、wav-gsm、wav-adpcm;
    • 它通过了我对一些MP3文件格式的测试:CBR与VBR,立体声与 单通道;
    • 它可以支持视频格式,我还没有测试过。

    代码示例:

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

    我试过了 Jlayer 1.0.1 ,但某些MP3格式失败。至于另一个诽谤案 jaudiotagger-2.0.3.jar 对于MP3格式,它可以正常工作,但它只能支持wav-pcm8/16。

        5
  •  -2
  •   Zorf    16 年前

    我很老套,但我总是简单地得到MP3的规格,写一个搜索正确位的函数,然后找到它。

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

    你也可以,我相信你!