繁体   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