java - Getting 1 second sample using Xuggler -
i have issues regarding xuggler api,
i want create little class, once mp3 file opened, gets raw bytes it, but, how many? have 1 second sample original file.
this current code:
public byte[] getsamples(int ibytesqtty){ byte[] rawbytes = null; /** go correct packet */ while (this._inputcontainer.readnextpacket(this._packet) >= 0){ system.out.println(this._packet.getduration()); /** once have packet, let's see if belongs audio stream */ if (this._packet.getstreamindex() == this._iaudiostreamid){ iaudiosamples samples = iaudiosamples.make(ibytesqtty, this._audiocoder.getchannels()); //system.out.println(">> " + samples.tostring()); /** because packet can contain multiple set of samples (frames of samples). may need call * decode audio multiple times @ different offsets in 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 */ do{}while(false); /** wtf? **/ } } return rawbytes; /** return null @ point */ }
any hint appreciated, in advance.
edit:
actually little piece of code, works me, not in cases
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.)");
i assume mp3 file it's internally converted regular wav file, per each second have 1411kbps rate. said, doesn't work in cases.
Comments
Post a Comment