Search in sources :

Example 6 with TagField

use of org.jaudiotagger.tag.TagField in project MusicDNA by harjot-oberai.

the class Mp4TagCreator method convert.

/**
     * Convert tagdata to rawdata ready for writing to file
     *
     * @param tag
     * @param padding TODO padding parameter currently ignored
     * @return
     * @throws UnsupportedEncodingException
     */
public ByteBuffer convert(Tag tag, int padding) throws UnsupportedEncodingException {
    try {
        //Add metadata raw content
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Iterator<TagField> it = tag.getFields();
        boolean processedArtwork = false;
        while (it.hasNext()) {
            TagField frame = it.next();
            //To ensure order is maintained dont process artwork until iterator hits it.
            if (frame instanceof Mp4TagCoverField) {
                if (processedArtwork) {
                //ignore
                } else {
                    processedArtwork = true;
                    //Because each artwork image is held within the tag as a separate field, but when
                    //they are written they are all held under a single covr box we need to do some checks
                    //and special processing here if we have any artwork image (this code only necessary
                    //if we have more than 1 but do it anyway even if only have 1 image)
                    ByteArrayOutputStream covrDataBaos = new ByteArrayOutputStream();
                    try {
                        for (TagField artwork : tag.getFields(FieldKey.COVER_ART)) {
                            covrDataBaos.write(((Mp4TagField) artwork).getRawContentDataOnly());
                        }
                    } catch (KeyNotFoundException knfe) {
                        //This cannot happen
                        throw new RuntimeException("Unable to find COVERART Key");
                    }
                    //Now create the parent Data
                    byte[] data = covrDataBaos.toByteArray();
                    baos.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + data.length));
                    baos.write(Utils.getDefaultBytes(Mp4FieldKey.ARTWORK.getFieldName(), "ISO-8859-1"));
                    baos.write(data);
                }
            } else {
                baos.write(frame.getRawContent());
            }
        }
        //Wrap into ilst box
        ByteArrayOutputStream ilst = new ByteArrayOutputStream();
        ilst.write(Utils.getSizeBEInt32(Mp4BoxHeader.HEADER_LENGTH + baos.size()));
        ilst.write(Utils.getDefaultBytes(Mp4AtomIdentifier.ILST.getFieldName(), "ISO-8859-1"));
        ilst.write(baos.toByteArray());
        //Put into ByteBuffer
        ByteBuffer buf = ByteBuffer.wrap(ilst.toByteArray());
        buf.rewind();
        return buf;
    } catch (IOException ioe) {
        //Should never happen as not writing to file at this point
        throw new RuntimeException(ioe);
    }
}
Also used : Mp4TagCoverField(org.jaudiotagger.tag.mp4.field.Mp4TagCoverField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) TagField(org.jaudiotagger.tag.TagField) KeyNotFoundException(org.jaudiotagger.tag.KeyNotFoundException)

Aggregations

TagField (org.jaudiotagger.tag.TagField)6 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ByteBuffer (java.nio.ByteBuffer)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)1 KeyNotFoundException (org.jaudiotagger.tag.KeyNotFoundException)1 Mp4FieldKey (org.jaudiotagger.tag.mp4.Mp4FieldKey)1 Mp4TagCoverField (org.jaudiotagger.tag.mp4.field.Mp4TagCoverField)1