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);
}
}
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;
}
Aggregations