簡體   English   中英

如何獲取音頻文件的持續時間?

[英]How to get duration of audio file?

我想以秒為單位獲取音頻文件的持續時間(總時間)。 音頻文件可以是任何格式的 IE(.mp3、.wav、.wma 等)。

 try {
    File file = new File("C:/Users/Administrator/AppData/Local/Temp/" + 
        "01 - Ab Tere Bin Jee Lenge Hum-(MyMp3Singer.com).mp3");
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
    AudioFormat format = audioInputStream.getFormat();
    long frames = audioInputStream.getFrameLength();
    double durationInSeconds = (frames + 0.0) / format.getFrameRate();
    System.out.println("duration in seconds"+durationInSeconds);
    AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
    Map<String, Object> properties = audioFileFormat.properties();
    System.out.println("properties"+properties.size()+"empy::"+properties.isEmpty());
    for(String key: properties.keySet()){
        System.out.println(key  +" :: "+ properties.get(key).toString());
    }
} catch (Exception e) {
    System.out.println("Exception:::::" + e.getMessage());
}

我嘗試使用上面的代碼來做到這一點,但它給出了:

Exception:::::could not get audio input stream from input file

您可能缺少 MP3 編解碼器。 您可能想嘗試Tritonus:開源 Java 聲音

還要檢查相關線程: MP3 play using Java Sound and Sun MP3 plugin

音頻文件的持續時間通常作為AudioFileFormat的屬性提供。 文檔

下表列出了一些應在實現中使用的常見屬性:

音頻文件格式屬性

+-------------+------------+-----------------------------------------------+ |Property key | Value type | Description | |-------------|------------|-----------------------------------------------| | "duration" | Long | playback duration of the file in microseconds | | "author" | String | name of the author of this file | | "title" | String | title of this file | | "copyright" | String | copyright message | | "date" | Date | date of the recording or release | | "comment" | String | an arbitrary text | +-------------+------------+-----------------------------------------------+

注意should ,這意味着它們是可選的。

另請注意, duration微秒為單位,而不是毫秒!

因此,要在 Java 中獲取音頻文件的持續時間,您可以調用:

final String DURATION = "duration";
Long duration = (Long)audioFileFormat.properties().get(DURATION);

// and because duration is optional, use a fallback
// for uncompressed formats like AIFF and WAVE
if (duration == null
    && audioFileFormat.getFormat().getEncoding() == PCM_SIGNED
    // make sure we actually have a frame length
    && audioFileFormat.getFrameLength() != NOT_SPECIFIED
    // make sure we actually have a frame rate
    && audioFileFormat.getFormat().getFrameRate() != NOT_SPECIFIED
    // check if this is WAVE or AIFF, other uncompressed formats may work as well
    && (audioFileFormat.getType() == WAVE || audioFileFormat.getType() == AIFF)) {

    duration = (long) (audioFileFormat.getFrameLength() / audioFileFormat.getFormat().getFrameRate() * 1000L * 1000L);
}

此代碼段將從持續duration屬性或幀長度和幀速率中獲取以微秒為單位的duration 后者僅對 WAVE 或 AIFF 等未壓縮格式有用

由於當前的 JVM 不支持 mp3,因此您需要安裝一個 mp3 編解碼器。 這以實現服務提供者接口 (SPI)服務提供者的形式出現。 用於 mp3 的 SPI 的純 Java 實現是 Tritonus 庫。 有關 Maven 的使用,請參見此處的示例。 請注意,該庫顯然很久以前就被廢棄了。 使用CoreAudio for Mac 的更新包是CASampledSP 另一個基於FFmpeg並為 Mac 和 Windows 編寫的庫是FFSampledSP (完全披露:我是這兩個庫的作者)。

獲取持續時間的另一種非常規方式是使用 JavaFX,因為它支持開箱即用的 mp3:

import java.io.File;
import javafx.scene.media.Media;
import javafx.util.Duration;

final Media media = new Media(new File("somefile.mp3").toURI().toString());
final Duration duration = media.duration();

暫無
暫無

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

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