Java 使用Xuggler获取1秒样本
我对Xuggler API有一些问题 我想创建一个小类,一旦打开mp3文件,它就会从中获取原始字节,但是,有多少个?我想从原始文件中获得1秒的样本 这是我当前的代码:Java 使用Xuggler获取1秒样本,java,audio,xuggler,Java,Audio,Xuggler,我对Xuggler API有一些问题 我想创建一个小类,一旦打开mp3文件,它就会从中获取原始字节,但是,有多少个?我想从原始文件中获得1秒的样本 这是我当前的代码: public byte[] getSamples(int iBytesQtty){ byte[] rawBytes = null; /** Go to the correct packet */ while (this._inputContainer.readNextPacket(this._packet
public byte[] getSamples(int iBytesQtty){
byte[] rawBytes = null;
/** Go to the correct packet */
while (this._inputContainer.readNextPacket(this._packet) >= 0){
System.out.println(this._packet.getDuration());
/** Once we have a packet, let's see if it belongs to the audio stream */
if (this._packet.getStreamIndex() == this._iAudioStreamId){
IAudioSamples samples = IAudioSamples.make(iBytesQtty, this._audioCoder.getChannels());
//System.out.println(">> " + samples.toString());
/** Because a packet can contain multiple set of samples (frames of samples). We may need to call
* decode audio multiple times at different offsets in the packet's data */
int iCurrentOffset = 0;
while(iCurrentOffset < this._packet.getSize()){
int iBytesDecoded = this._audioCoder.decodeAudio(samples, this._packet, iCurrentOffset);
iCurrentOffset += iBytesDecoded;
if (samples.isComplete()){
rawBytes = samples.getData().getByteArray(0, samples.getSize());
}
}
return rawBytes;
}
else{
/** Otherwise drop it */
do{}while(false); /** WTF? **/
}
}
return rawBytes; /** This will return null at this point */
}
我假设mp3文件实际上在内部转换为常规WAV文件,因此每秒的速率为1411kbps。但正如我所说,它并不是在所有情况下都能工作。从中获取原始字节,但是,有多少?我想要1秒的示例“无法判断1秒可变比特率MP3需要多少字节。所以基本上你需要先把它解析回声音。事实上,我认为通过从mp3文件解码它,它可以在mp3和字节值之间进行转换(我认为它们实际上是wav)。虽然,应该有办法做到这一点,但您指的是“首先将其解析回声音”,请您更具体一点。如果mp3文件是CBR?
Mp3File song = new Mp3File("sample.mp3");
byte[] buff = null;
int totalBytes = 0;
int iNumBytes = 1024;
while( (buff = song.getSamples(iNumBytes)) != null ){
totalBytes += buff.length;
// System.out.println("Length buff: " + buff.length + " TOTAL: " + totalBytes);
}
System.out.println("Bytes: " + totalBytes + " (" + totalBytes/1024 + " Kb.)");
float lengthSecs = (float) (totalBytes / ((1411*1000)/8));
float lengthMins = lengthSecs/60;
System.out.println("song duration: " + lengthMins + " mins. (" + lengthSecs + " secs.)");