use of org.jaudiotagger.audio.exceptions.CannotReadException in project MusicDNA by harjot-oberai.
the class Mp4StcoBox method debugShowStcoInfo.
public static void debugShowStcoInfo(RandomAccessFile raf) throws IOException, CannotReadException {
Mp4BoxHeader moovHeader = Mp4BoxHeader.seekWithinLevel(raf, Mp4AtomIdentifier.MOOV.getFieldName());
if (moovHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
ByteBuffer moovBuffer = ByteBuffer.allocate(moovHeader.getLength() - Mp4BoxHeader.HEADER_LENGTH);
raf.getChannel().read(moovBuffer);
moovBuffer.rewind();
//Level 2-Searching for "mvhd" somewhere within "moov", we make a slice after finding header
//so all getFields() methods will be relative to mvdh positions
Mp4BoxHeader boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.MVHD.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
ByteBuffer mvhdBuffer = moovBuffer.slice();
Mp4MvhdBox mvhd = new Mp4MvhdBox(boxHeader, mvhdBuffer);
mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength());
//Level 2-Searching for "trak" within "moov"
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.TRAK.getFieldName());
int endOfFirstTrackInBuffer = mvhdBuffer.position() + boxHeader.getDataLength();
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
//Level 3-Searching for "mdia" within "trak"
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.MDIA.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
//Level 4-Searching for "mdhd" within "mdia"
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.MDHD.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
//Level 4-Searching for "minf" within "mdia"
mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength());
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.MINF.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
//Level 5-Searching for "smhd" within "minf"
//Only an audio track would have a smhd frame
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.SMHD.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
mvhdBuffer.position(mvhdBuffer.position() + boxHeader.getDataLength());
//Level 5-Searching for "stbl within "minf"
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.STBL.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
//Level 6-Searching for "stco within "stbl"
boxHeader = Mp4BoxHeader.seekWithinLevel(mvhdBuffer, Mp4AtomIdentifier.STCO.getFieldName());
if (boxHeader == null) {
throw new CannotReadException("This file does not appear to be an audio file");
}
Mp4StcoBox stco = new Mp4StcoBox(boxHeader, mvhdBuffer);
stco.printAlloffsets();
}
use of org.jaudiotagger.audio.exceptions.CannotReadException in project MusicDNA by harjot-oberai.
the class Mp4TagReader method read.
/*
* The metadata is stored in the box under the hierachy moov.udta.meta.ilst
*
* There are gaps between these boxes
*/
public Mp4Tag read(RandomAccessFile raf) throws CannotReadException, IOException {
Mp4Tag tag = new Mp4Tag();
//Get to the facts everything we are interested in is within the moov box, so just load data from file
//once so no more file I/O needed
Mp4BoxHeader moovHeader = Mp4BoxHeader.seekWithinLevel(raf, Mp4AtomIdentifier.MOOV.getFieldName());
if (moovHeader == null) {
throw new CannotReadException(ErrorMessage.MP4_FILE_NOT_CONTAINER.getMsg());
}
ByteBuffer moovBuffer = ByteBuffer.allocate(moovHeader.getLength() - Mp4BoxHeader.HEADER_LENGTH);
raf.getChannel().read(moovBuffer);
moovBuffer.rewind();
//Level 2-Searching for "udta" within "moov"
Mp4BoxHeader boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.UDTA.getFieldName());
if (boxHeader != null) {
//Level 3-Searching for "meta" within udta
boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.META.getFieldName());
if (boxHeader == null) {
logger.warning(ErrorMessage.MP4_FILE_HAS_NO_METADATA.getMsg());
return tag;
}
Mp4MetaBox meta = new Mp4MetaBox(boxHeader, moovBuffer);
meta.processData();
//Level 4- Search for "ilst" within meta
boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.ILST.getFieldName());
//This file does not actually contain a tag
if (boxHeader == null) {
logger.warning(ErrorMessage.MP4_FILE_HAS_NO_METADATA.getMsg());
return tag;
}
} else {
//Level 2-Searching for "meta" not within udta
boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.META.getFieldName());
if (boxHeader == null) {
logger.warning(ErrorMessage.MP4_FILE_HAS_NO_METADATA.getMsg());
return tag;
}
Mp4MetaBox meta = new Mp4MetaBox(boxHeader, moovBuffer);
meta.processData();
//Level 3- Search for "ilst" within meta
boxHeader = Mp4BoxHeader.seekWithinLevel(moovBuffer, Mp4AtomIdentifier.ILST.getFieldName());
//This file does not actually contain a tag
if (boxHeader == null) {
logger.warning(ErrorMessage.MP4_FILE_HAS_NO_METADATA.getMsg());
return tag;
}
}
//Size of metadata (exclude the size of the ilst parentHeader), take a slice starting at
//metadata children to make things safer
int length = boxHeader.getLength() - Mp4BoxHeader.HEADER_LENGTH;
ByteBuffer metadataBuffer = moovBuffer.slice();
//Datalength is longer are there boxes after ilst at this level?
logger.config("headerlengthsays:" + length + "datalength:" + metadataBuffer.limit());
int read = 0;
logger.config("Started to read metadata fields at position is in metadata buffer:" + metadataBuffer.position());
while (read < length) {
//Read the boxHeader
boxHeader.update(metadataBuffer);
//Create the corresponding datafield from the id, and slice the buffer so position of main buffer
//wont get affected
logger.config("Next position is at:" + metadataBuffer.position());
createMp4Field(tag, boxHeader, metadataBuffer.slice());
//Move position in buffer to the start of the next parentHeader
metadataBuffer.position(metadataBuffer.position() + boxHeader.getDataLength());
read += boxHeader.getLength();
}
return tag;
}
use of org.jaudiotagger.audio.exceptions.CannotReadException in project MusicDNA by harjot-oberai.
the class WavInfoReader method read.
public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException {
// Reads wav header----------------------------------------
GenericAudioHeader info = new GenericAudioHeader();
if (raf.length() < 12) {
throw new CannotReadException("This is not a WAV File (<12 bytes)");
}
byte[] b = new byte[12];
raf.read(b);
WavRIFFHeader wh = new WavRIFFHeader(b);
if (wh.isValid()) {
b = new byte[34];
raf.read(b);
WavFormatHeader wfh = new WavFormatHeader(b);
if (wfh.isValid()) {
// Populates
// encodingInfo----------------------------------------------------
info.setPreciseLength(((float) raf.length() - (float) 36) / wfh.getBytesPerSecond());
info.setChannelNumber(wfh.getChannelNumber());
info.setSamplingRate(wfh.getSamplingRate());
info.setBitsPerSample(wfh.getBitsPerSample());
info.setEncodingType("WAV-RIFF " + wfh.getBitsPerSample() + " bits");
info.setExtraEncodingInfos("");
info.setBitrate(wfh.getBytesPerSecond() * 8 / 1000);
info.setVariableBitRate(false);
} else {
throw new CannotReadException("Wav Format Header not valid");
}
} else {
throw new CannotReadException("Wav RIFF Header not valid");
}
return info;
}
use of org.jaudiotagger.audio.exceptions.CannotReadException in project MusicDNA by harjot-oberai.
the class OggVorbisTagReader method convertToVorbisSetupHeaderPacketAndAdditionalPackets.
/**
* The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the
* file offset of the OggPage it belongs on, it probably won'timer be first packet, also returns any addditional
* packets that immediately follow the setup header in original file
* @param fileOffsetOfStartingOggPage
* @param raf
* @throws org.jaudiotagger.audio.exceptions.CannotReadException
* @throws java.io.IOException
* @return
*/
public byte[] convertToVorbisSetupHeaderPacketAndAdditionalPackets(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Seek to specified offset
raf.seek(fileOffsetOfStartingOggPage);
//Read Page
OggPageHeader setupPageHeader = OggPageHeader.read(raf);
//is setupheader
if (setupPageHeader.getPacketList().size() > 1) {
raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength());
}
//Now should be at start of next packet, check this is the vorbis setup header
byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
raf.read(b);
if (!isVorbisSetupHeader(b)) {
throw new CannotReadException("Unable to find setup header(2), unable to write ogg file");
}
//Go back to start of setupheader data
raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
//Read data
if (setupPageHeader.getPacketList().size() > 1) {
b = new byte[setupPageHeader.getPacketList().get(1).getLength()];
raf.read(b);
baos.write(b);
} else {
b = new byte[setupPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
}
//Return Data
if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) {
logger.config("Setupheader finishes on this page");
if (setupPageHeader.getPacketList().size() > 2) {
for (int i = 2; i < setupPageHeader.getPacketList().size(); i++) {
b = new byte[setupPageHeader.getPacketList().get(i).getLength()];
raf.read(b);
baos.write(b);
}
}
return baos.toByteArray();
}
//so carry on reading pages until we get to the end of comment
while (true) {
logger.config("Reading another page");
OggPageHeader nextPageHeader = OggPageHeader.read(raf);
b = new byte[nextPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
//on this page so thats all we need and we can return
if (nextPageHeader.getPacketList().size() > 1) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//There is only the Setupheader packet on page if it has completed on this page we can return
if (!nextPageHeader.isLastPacketIncomplete()) {
logger.config("Setupheader finish on Page because this packet is complete");
return baos.toByteArray();
}
}
}
use of org.jaudiotagger.audio.exceptions.CannotReadException in project MusicDNA by harjot-oberai.
the class OggVorbisTagReader method convertToVorbisSetupHeaderPacket.
/**
* The Vorbis Setup Header may span multiple(2) pages, athough it doesnt normally. We pass the start of the
* file offset of the OggPage it belongs on, it probably won'timer be first packet.
* @param fileOffsetOfStartingOggPage
* @param raf
* @throws org.jaudiotagger.audio.exceptions.CannotReadException
* @throws java.io.IOException
* @return
*/
public byte[] convertToVorbisSetupHeaderPacket(long fileOffsetOfStartingOggPage, RandomAccessFile raf) throws IOException, CannotReadException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Seek to specified offset
raf.seek(fileOffsetOfStartingOggPage);
//Read Page
OggPageHeader setupPageHeader = OggPageHeader.read(raf);
//is setupheader
if (setupPageHeader.getPacketList().size() > 1) {
raf.skipBytes(setupPageHeader.getPacketList().get(0).getLength());
}
//Now should be at start of next packet, check this is the vorbis setup header
byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
raf.read(b);
if (!isVorbisSetupHeader(b)) {
throw new CannotReadException("Unable to find setup header(2), unable to write ogg file");
}
//Go back to start of setupheader data
raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
//Read data
if (setupPageHeader.getPacketList().size() > 1) {
b = new byte[setupPageHeader.getPacketList().get(1).getLength()];
raf.read(b);
baos.write(b);
} else {
b = new byte[setupPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
}
//Return Data
if (!setupPageHeader.isLastPacketIncomplete() || setupPageHeader.getPacketList().size() > 2) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//so carry on reading pages until we get to the end of comment
while (true) {
logger.config("Reading another page");
OggPageHeader nextPageHeader = OggPageHeader.read(raf);
b = new byte[nextPageHeader.getPacketList().get(0).getLength()];
raf.read(b);
baos.write(b);
//on this page so thats all we need and we can return
if (nextPageHeader.getPacketList().size() > 1) {
logger.config("Setupheader finishes on this page");
return baos.toByteArray();
}
//There is only the Setupheader packet on page if it has completed on this page we can return
if (!nextPageHeader.isLastPacketIncomplete()) {
logger.config("Setupheader finish on Page because this packet is complete");
return baos.toByteArray();
}
}
}
Aggregations