Search in sources :

Example 1 with Mp4FieldKey

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

the class Mp4Tag method getFields.

/**
 * Maps the generic key to the mp4 key and return the list of values for this field
 *
 * @param genericKey
 */
@SuppressWarnings({ "JavaDoc" })
public List<TagField> getFields(FieldKey genericKey) throws KeyNotFoundException {
    if (genericKey == null) {
        throw new KeyNotFoundException();
    }
    Mp4FieldKey mp4FieldKey = tagFieldToMp4Field.get(genericKey);
    List<TagField> list = getFields(mp4FieldKey.getFieldName());
    List<TagField> filteredList = new ArrayList<TagField>();
    if (genericKey == FieldKey.KEY) {
        if (list.size() == 0) {
            list = getFields(KEY_OLD.getFieldName());
        }
        return list;
    } else if (genericKey == FieldKey.GENRE) {
        if (list.size() == 0) {
            list = getFields(GENRE_CUSTOM.getFieldName());
        }
        return list;
    } else if (genericKey == FieldKey.TRACK) {
        for (TagField next : list) {
            Mp4TrackField trackField = (Mp4TrackField) next;
            if (trackField.getTrackNo() > 0) {
                filteredList.add(next);
            }
        }
        return filteredList;
    } else if (genericKey == FieldKey.TRACK_TOTAL) {
        for (TagField next : list) {
            Mp4TrackField trackField = (Mp4TrackField) next;
            if (trackField.getTrackTotal() > 0) {
                filteredList.add(next);
            }
        }
        return filteredList;
    } else if (genericKey == FieldKey.DISC_NO) {
        for (TagField next : list) {
            Mp4DiscNoField discNoField = (Mp4DiscNoField) next;
            if (discNoField.getDiscNo() > 0) {
                filteredList.add(next);
            }
        }
        return filteredList;
    } else if (genericKey == FieldKey.DISC_TOTAL) {
        for (TagField next : list) {
            Mp4DiscNoField discNoField = (Mp4DiscNoField) next;
            if (discNoField.getDiscTotal() > 0) {
                filteredList.add(next);
            }
        }
        return filteredList;
    } else {
        return list;
    }
}
Also used : ArrayList(java.util.ArrayList) Mp4FieldKey(org.jaudiotagger.tag.mp4.Mp4FieldKey)

Example 2 with Mp4FieldKey

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

the class Mp4TagReader method createMp4Field.

/**
 * Process the field and add to the tag
 *
 * Note:In the case of coverart MP4 holds all the coverart within individual dataitems all within
 * a single covr atom, we will add separate mp4field for each image.
 *
 * @param tag
 * @param header
 * @param raw
 * @return
 * @throws UnsupportedEncodingException
 */
private void createMp4Field(Mp4Tag tag, Mp4BoxHeader header, ByteBuffer raw) throws UnsupportedEncodingException {
    // Header with no data #JAUDIOTAGGER-463
    if (header.getDataLength() == 0) {
    // Just Ignore
    } else // Reverse Dns Atom
    if (header.getId().equals(Mp4TagReverseDnsField.IDENTIFIER)) {
        // 
        try {
            TagField field = new Mp4TagReverseDnsField(header, raw);
            tag.addField(field);
        } catch (Exception e) {
            logger.warning(ErrorMessage.MP4_UNABLE_READ_REVERSE_DNS_FIELD.getMsg(e.getMessage()));
            TagField field = new Mp4TagRawBinaryField(header, raw);
            tag.addField(field);
        }
    } else // Normal Parent with Data atom
    {
        int currentPos = raw.position();
        boolean isDataIdentifier = Utils.getString(raw, Mp4BoxHeader.IDENTIFIER_POS, Mp4BoxHeader.IDENTIFIER_LENGTH, "ISO-8859-1").equals(Mp4DataBox.IDENTIFIER);
        raw.position(currentPos);
        if (isDataIdentifier) {
            // Need this to decide what type of Field to create
            int type = Utils.getIntBE(raw, Mp4DataBox.TYPE_POS_INCLUDING_HEADER, Mp4DataBox.TYPE_POS_INCLUDING_HEADER + Mp4DataBox.TYPE_LENGTH - 1);
            Mp4FieldType fieldType = Mp4FieldType.getFieldType(type);
            logger.config("Box Type id:" + header.getId() + ":type:" + fieldType);
            // Special handling for some specific identifiers otherwise just base on class id
            if (header.getId().equals(Mp4FieldKey.TRACK.getFieldName())) {
                TagField field = new Mp4TrackField(header.getId(), raw);
                tag.addField(field);
            } else if (header.getId().equals(Mp4FieldKey.DISCNUMBER.getFieldName())) {
                TagField field = new Mp4DiscNoField(header.getId(), raw);
                tag.addField(field);
            } else if (header.getId().equals(Mp4FieldKey.GENRE.getFieldName())) {
                TagField field = new Mp4GenreField(header.getId(), raw);
                tag.addField(field);
            } else if (header.getId().equals(Mp4FieldKey.ARTWORK.getFieldName()) || Mp4FieldType.isCoverArtType(fieldType)) {
                int processedDataSize = 0;
                int imageCount = 0;
                // The loop should run for each image (each data atom)
                while (processedDataSize < header.getDataLength()) {
                    // for each subimage (if there are more than one image)
                    if (imageCount > 0) {
                        type = Utils.getIntBE(raw, processedDataSize + Mp4DataBox.TYPE_POS_INCLUDING_HEADER, processedDataSize + Mp4DataBox.TYPE_POS_INCLUDING_HEADER + Mp4DataBox.TYPE_LENGTH - 1);
                        fieldType = Mp4FieldType.getFieldType(type);
                    }
                    Mp4TagCoverField field = new Mp4TagCoverField(raw, fieldType);
                    tag.addField(field);
                    processedDataSize += field.getDataAndHeaderSize();
                    imageCount++;
                }
            } else if (fieldType == Mp4FieldType.TEXT) {
                TagField field = new Mp4TagTextField(header.getId(), raw);
                tag.addField(field);
            } else if (fieldType == Mp4FieldType.IMPLICIT) {
                TagField field = new Mp4TagTextNumberField(header.getId(), raw);
                tag.addField(field);
            } else if (fieldType == Mp4FieldType.INTEGER) {
                TagField field = new Mp4TagByteField(header.getId(), raw);
                tag.addField(field);
            } else {
                boolean existingId = false;
                for (Mp4FieldKey key : Mp4FieldKey.values()) {
                    if (key.getFieldName().equals(header.getId())) {
                        // The parentHeader is a known id but its field type is not one of the expected types so
                        // this field is invalid. i.e I received a file with the TMPO set to 15 (Oxf) when it should
                        // be 21 (ox15) so looks like somebody got their decimal and hex numbering confused
                        // So in this case best to ignore this field and just write a warning
                        existingId = true;
                        logger.warning("Known Field:" + header.getId() + " with invalid field type of:" + type + " is ignored");
                        break;
                    }
                }
                // Unknown field id with unknown type so just create as binary
                if (!existingId) {
                    logger.warning("UnKnown Field:" + header.getId() + " with invalid field type of:" + type + " created as binary");
                    TagField field = new Mp4TagBinaryField(header.getId(), raw);
                    tag.addField(field);
                }
            }
        } else // Special Cases
        {
            // copy parent and child as is without modification
            if (header.getId().equals(Mp4NonStandardFieldKey.AAPR.getFieldName())) {
                TagField field = new Mp4TagRawBinaryField(header, raw);
                tag.addField(field);
            } else // Default case
            {
                TagField field = new Mp4TagRawBinaryField(header, raw);
                tag.addField(field);
            }
        }
    }
}
Also used : TagField(org.jaudiotagger.tag.TagField) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mp4FieldKey(org.jaudiotagger.tag.mp4.Mp4FieldKey)

Aggregations

Mp4FieldKey (org.jaudiotagger.tag.mp4.Mp4FieldKey)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)1 TagField (org.jaudiotagger.tag.TagField)1