繁体   English   中英

如何找到MP3音频剪辑的标题?

[英]How to find headers of MP3 audio clip?

我在这里试图解决的主要问题是找到以字节数组形式给出的MP3音频剪辑的长度。 为了找到长度,我需要找到剪辑中的frames 根据MP3规范 ,帧是一个字节,其中11个连续位设置为1; 例如,一个值为255的字节,后跟一个其3个最高有效位设置为1(十进制值224)的字节。 因此,从本质上讲,一旦找到一个字节值255,就在检查下一个字节是否为值224。但是,鉴于此标准,我无法在数组中找到任何帧头。 另外,参考文档还提到255之后的下一个字节有时可能会将其4 MSB设置为1。这样,我应该在255之后搜索值240。无论哪种情况,我都无法定位任何帧头。 请告知我是否正确执行此操作。

这是我的代码:

public class AudioUtils
{  
    public static int getAudioLength(byte[] audio, AudioFormat audioFormat, int samples, int sampleRate)
    {
        if (!audioFormat.getId().equals(AudioFormat.MP3))
            throw new UnsupportedOperationException(String.format("Audio length computation for audio of format %s is not supported", audioFormat.getFormat()));

        int frames = getNumberOfFrames(audio);
        return ((frames * samples) / sampleRate);
    }

    private static int getNumberOfFrames(byte[] audio)
    {
        int frames = 0;
        for (int i = 0; i < audio.length; i++)
        {
            byte b = audio[i];
            int byteInt = b & 0xff; // convert byte to int
            if (byteInt == 255)
            {
                // If there is another byte in the array
                if (i + 1 < audio.length && (audio[i + 1] & 0xff) == 224)
                {
                    // We have found an header. Increment the frames count.
                    frames++;
                }
            }
        }

        return frames;
    }
}

由于同步字只有11位,而您不知道其余5位的内容,因此您只需要比较第二个字节的前3位。 完成此操作的一种常用方法是使用二进制“&”运算符将要忽略的位设置为0(称为屏蔽),然后将要比较的常量中的相同位设置为0。

在下面的示例中,第二个字节被0xe0屏蔽,并与相同的值进行比较。

for (int i = 0; i < audio.length - 1; i++)
{
    if (audio[i] == 0xff && (audio[i+1] & 0xe0) == 0xe0)
    {
        frames++;
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM