簡體   English   中英

如何在 Java 中獲取 mp3 文件的總時間?

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

如何在 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 文件的持續時間嗎?

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

}

這是我獲取文件 .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;

這里是MP3文件結構的詳細解釋

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

使用jave-1.0.1.jar庫。

  • 它通過了我對波形文件格式的測試,例如:wav-pcm8/16、wav-alaw、wav-ulaw、wav-gsm、wav-adpcm;
  • 它通過了我對一些 MP3 文件格式的測試:cbr vs vbr、立體聲 vs SingleChannel;
  • 它可以支持視頻格式,我還沒有測試過。

代碼示例:

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 格式它失敗了。 至於另一個 libaray jaudiotagger-2.0.3.jar ,它適用於 MP3 格式,但它只能支持 wav-pcm8/16。

我在這方面很老套,但我總是簡單地獲取 MP3 的規格,編寫一個搜索正確位的函數,然后找到它。

如果 Winamp 可以通過讀取 MP3 文件的比特流來確定這一點,那么我對嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM