Search in sources :

Example 1 with AudioHeader

use of org.jaudiotagger.audio.AudioHeader in project musiccabinet by hakko.

the class AudioTagService method updateMetadata.

public void updateMetadata(File file) {
    String extension = getExtension(file.getFilename()).toUpperCase();
    if (!ALLOWED_EXTENSIONS.contains(extension)) {
        return;
    }
    MetaData metaData = new MetaData();
    metaData.setMediaType(Mediatype.valueOf(extension));
    try {
        AudioFile audioFile = AudioFileIO.read(new java.io.File(file.getDirectory(), file.getFilename()));
        Tag tag = audioFile.getTag();
        if (tag != null) {
            metaData.setArtist(getTagField(tag, ARTIST));
            metaData.setArtistSort(getTagField(tag, ARTIST_SORT));
            metaData.setAlbumArtist(toAlbumArtist(tag));
            metaData.setAlbumArtistSort(getTagField(tag, ALBUM_ARTIST_SORT));
            metaData.setAlbum(toAlbum(getTagField(tag, ALBUM)));
            metaData.setTitle(getTagField(tag, TITLE));
            metaData.setYear(getTagField(tag, YEAR));
            metaData.setGenre(toGenre(getTagField(tag, GENRE)));
            metaData.setLyrics(getTagField(tag, LYRICS));
            metaData.setComposer(getTagField(tag, COMPOSER));
            metaData.setDiscNr(toFirstNumber(getTagField(tag, DISC_NO)));
            metaData.setDiscNrs(toShort(getTagField(tag, DISC_TOTAL)));
            metaData.setTrackNr(toFirstNumber(getTagField(tag, TRACK)));
            metaData.setTrackNrs(toShort(getTagField(tag, TRACK_TOTAL)));
            metaData.setCoverArtEmbedded(tag.getFirstArtwork() != null);
        }
        AudioHeader audioHeader = audioFile.getAudioHeader();
        if (audioHeader != null) {
            metaData.setVbr(audioHeader.isVariableBitRate());
            metaData.setBitrate((short) audioHeader.getBitRateAsNumber());
            metaData.setDuration((short) audioHeader.getTrackLength());
        }
        file.setMetaData(metaData);
    } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | RuntimeException e) {
        // AudioFileIO has been seen to throw NumberFormatException
        LOG.warn("Could not read metadata of file " + file.getFilename() + " from " + file.getDirectory(), e);
    }
}
Also used : AudioHeader(org.jaudiotagger.audio.AudioHeader) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) IOException(java.io.IOException) AudioFile(org.jaudiotagger.audio.AudioFile) TagException(org.jaudiotagger.tag.TagException) MetaData(com.github.hakko.musiccabinet.domain.model.library.MetaData) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) FlacTag(org.jaudiotagger.tag.flac.FlacTag) Tag(org.jaudiotagger.tag.Tag) AbstractID3v2Tag(org.jaudiotagger.tag.id3.AbstractID3v2Tag)

Example 2 with AudioHeader

use of org.jaudiotagger.audio.AudioHeader in project libresonic by Libresonic.

the class JaudiotaggerParser method getRawMetaData.

/**
     * Parses meta data for the given music file. No guessing or reformatting is done.
     *
     *
     * @param file The music file to parse.
     * @return Meta data for the file.
     */
@Override
public MetaData getRawMetaData(File file) {
    MetaData metaData = new MetaData();
    try {
        AudioFile audioFile = AudioFileIO.read(file);
        Tag tag = audioFile.getTag();
        if (tag != null) {
            metaData.setAlbumName(getTagField(tag, FieldKey.ALBUM));
            metaData.setTitle(getTagField(tag, FieldKey.TITLE));
            metaData.setYear(parseYear(getTagField(tag, FieldKey.YEAR)));
            metaData.setGenre(mapGenre(getTagField(tag, FieldKey.GENRE)));
            metaData.setDiscNumber(parseInteger(getTagField(tag, FieldKey.DISC_NO)));
            metaData.setTrackNumber(parseTrackNumber(getTagField(tag, FieldKey.TRACK)));
            String songArtist = getTagField(tag, FieldKey.ARTIST);
            String albumArtist = getTagField(tag, FieldKey.ALBUM_ARTIST);
            metaData.setArtist(StringUtils.isBlank(songArtist) ? albumArtist : songArtist);
            metaData.setAlbumArtist(StringUtils.isBlank(albumArtist) ? songArtist : albumArtist);
        }
        AudioHeader audioHeader = audioFile.getAudioHeader();
        if (audioHeader != null) {
            metaData.setVariableBitRate(audioHeader.isVariableBitRate());
            metaData.setBitRate((int) audioHeader.getBitRateAsNumber());
            metaData.setDurationSeconds(audioHeader.getTrackLength());
        }
    } catch (Throwable x) {
        LOG.warn("Error when parsing tags in " + file, x);
    }
    return metaData;
}
Also used : AudioFile(org.jaudiotagger.audio.AudioFile) AudioHeader(org.jaudiotagger.audio.AudioHeader) Tag(org.jaudiotagger.tag.Tag)

Aggregations

AudioFile (org.jaudiotagger.audio.AudioFile)2 AudioHeader (org.jaudiotagger.audio.AudioHeader)2 Tag (org.jaudiotagger.tag.Tag)2 MetaData (com.github.hakko.musiccabinet.domain.model.library.MetaData)1 IOException (java.io.IOException)1 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)1 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)1 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)1 TagException (org.jaudiotagger.tag.TagException)1 FlacTag (org.jaudiotagger.tag.flac.FlacTag)1 AbstractID3v2Tag (org.jaudiotagger.tag.id3.AbstractID3v2Tag)1