如何获取音频文件的时长?

3

我想要获取音频文件的总时间(持续时间),单位为秒。音频文件可以是任何格式,例如 (.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

https://dev59.com/43A75IYBdhLWcg3w8-LZ - Afsun Khammadli
2个回答

1

如果是库,请提供一个获取音频文件持续时间的示例程序。 - programminglover
@programminglover:- 你编写的程序是正确的。你只需要将像jl.jar mp3spi.jar和tritonus_share.jar这样的一些jar文件添加到我的类路径中即可。 - Rahul Tripathi
иҜ·еҸӮйҳ…Java Sound Wikiдёӯзҡ„жңҚеҠЎжҸҗдҫӣзЁӢеәҸжҺҘеҸЈе’ҢMP3и§Јз Ғж”ҜжҢҒйғЁеҲҶгҖӮ JMFеҢ…еҗ«дәҶдёҖдёӘMP3и§Јз ҒеҷЁгҖӮеңЁиҝҗиЎҢж—¶зұ»и·Ҝеҫ„дёӯеҢ…еҗ«mp3plugin.jar(еҗҚз§°пјҹ)пјҢ然еҗҺжӮЁе°ұеҸҜд»ҘејҖе§ӢдҪҝз”ЁдәҶгҖӮжҲ‘жӣҫе°Ҷе…¶з”ЁдәҺеҹәдәҺJava Soundзҡ„MP3зӮ№е”ұжңәгҖӮ - Andrew Thompson

0
音频文件的持续时间通常作为AudioFileFormat的属性提供。来自文档

The following table lists some common properties that should be used in implementations:

Audio File Format Properties

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

这段代码可以从持续时间属性或帧长和帧速率中获取微秒级别的持续时间。 后者仅适用于像WAVE或AIFF这样的无压缩格式

由于当前JVM不支持mp3,您需要安装mp3编解码器。这可以通过服务提供商实现服务提供商接口(SPI)来完成。 mp3的纯Java SPI实现是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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接