Java中的音频文件长度

时间:2016-03-06 02:29:46

标签: java audio mp3 audioformat

我遇到了根据mp3文件从mp3文件中解析数据的问题。

输出的第一部分是正确的,我有一个长达254秒的mp3文件,我从Github的mp3解析库mp3agic中获取了它的信息。

但是,关于帧长度和持续时间的信息的第二部分是不正确的。

Length of this mp3 is: 254 seconds
Bitrate: 320 kbps (CBR)
Sample rate: 44100 Hz
Has ID3v1 tag?: NO
Has ID3v2 tag?: YES
Has custom tag?: NO

framelength -1
framerate 38.28125
duration -271265.06

我用来获取帧长度,帧速率和持续时间的代码是:

File file = musicFile.getFileValue();

    this.audioStream.startMusicStream(file);

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

        System.out.println("framelength "+frameSize);
        System.out.println("framerate "+frameRate);
        System.out.println("duration "+durationInSeconds);

        this.setDurationLabel(durationInSeconds);
    } catch (Exception e) {
        e.printStackTrace();
    }

所有的拳头,为什么帧长和其他测量甚至是负的?那有什么意思?如何使用audioinputstream和audioformat中的信息准确计算mp3文件的持续时间?

2 个答案:

答案 0 :(得分:1)

对于帧长和持续时间,您可以使用:

long frameLen = audioInputStream.getFrameLength();
double durationInSeconds = (frameLen+0.0) / format.getFrameRate(); 

功能描述:

getFrameLength:Obtains the length of the stream, expressed in sample frames rather than bytes.Returns:the length in sample frames

getFrameRate:Obtains the frame rate in frames per second.Returns:the number of frames per second, or AudioSystem.NOT_SPECIFIED

参考文献:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/javax/sound/sampled/AudioInputStream.java#AudioInputStream.getFrameLength%28%29

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/sound/sampled/AudioFormat.java#AudioFormat.getFrameRate%28%29

答案 1 :(得分:1)

为我工作,感谢JAAD库http://jaadec.sourceforge.net/

import net.sourceforge.jaad.aac.Decoder;
import net.sourceforge.jaad.aac.SampleBuffer;
import net.sourceforge.jaad.adts.ADTSDemultiplexer;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import java.io.*;

public class AACHelper {

    public static AudioInputStream decodeAAC(File inputFile) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        AudioFormat audioFormat;

        try {
            final FileInputStream audioInputStream = new FileInputStream(inputFile);
            final ADTSDemultiplexer adts = new ADTSDemultiplexer(audioInputStream);
            final Decoder dec = new Decoder(adts.getDecoderSpecificInfo());

            final SampleBuffer buf = new SampleBuffer();
            byte[] b;
            while (true) {
                try {
                    b = adts.readNextFrame();
                }
                catch (Exception e) {
                    break;
                }

                dec.decodeFrame(b, buf);
                outputStream.write(buf.getData());
            }

            audioFormat = new AudioFormat(buf.getSampleRate(), buf.getBitsPerSample(), buf.getChannels(), true, buf.isBigEndian());
            LogUtils.i("FrameRate:     " + audioFormat.getFrameRate());
            LogUtils.i("FrameSize:     " + audioFormat.getFrameSize());
            LogUtils.i("SampleRate:    " + buf.getSampleRate());
            LogUtils.i("BitsPerSample: " + buf.getBitsPerSample());
            LogUtils.i("Bitrate:       " + buf.getBitrate());
            LogUtils.i("Length:        " + buf.getLength());

        } finally {
            // nop
        }

        byte[] outputStreamByteArray = outputStream.toByteArray();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStreamByteArray);
        AudioInputStream audioInputStream = new AudioInputStream(inputStream, audioFormat, outputStreamByteArray.length);

        long audioFileLength = audioInputStream.getFrameLength();
        int frameSize = audioFormat.getFrameSize();
        float frameRate = audioFormat.getFrameRate();
        double durationInSeconds = (audioFileLength / (frameSize * frameRate));
        LogUtils.i("Duration:      " + durationInSeconds);

        return audioInputStream;
    }

}