use of org.jaudiotagger.audio.AudioFile 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;
}
use of org.jaudiotagger.audio.AudioFile in project Shuttle by timusus.
the class ShuttleApplication method repairMediaStoreYearFromTags.
@NonNull
private Completable repairMediaStoreYearFromTags() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return Completable.complete();
}
return DataManager.getInstance().getSongsObservable(value -> value.year < 1).first(Collections.emptyList()).map(songs -> Stream.of(songs).flatMap(song -> {
if (!TextUtils.isEmpty(song.path)) {
File file = new File(song.path);
if (file.exists()) {
try {
AudioFile audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag();
if (tag != null) {
String year = tag.getFirst(FieldKey.YEAR);
int yearInt = StringUtils.parseInt(year);
if (yearInt > 0) {
song.year = yearInt;
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Audio.Media.YEAR, yearInt);
return Stream.of(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, song.id)).withValues(contentValues).build());
}
}
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
}
}
}
return Stream.empty();
}).collect(Collectors.toCollection(ArrayList::new))).doOnSuccess(contentProviderOperations -> getContentResolver().applyBatch(MediaStore.AUTHORITY, contentProviderOperations)).flatMapCompletable(songs -> Completable.complete());
}
use of org.jaudiotagger.audio.AudioFile in project MusicDNA by harjot-oberai.
the class AudioFileReader method read.
/*
* Reads the given file, and return an AudioFile object containing the Tag
* and the encoding infos present in the file. If the file has no tag, an
* empty one is returned. If the encodinginfo is not valid , an exception is thrown.
*
* @param f The file to read
* @exception CannotReadException If anything went bad during the read of this file
*/
public AudioFile read(File f) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
if (logger.isLoggable(Level.CONFIG)) {
logger.config(ErrorMessage.GENERAL_READ.getMsg(f.getAbsolutePath()));
}
if (!f.canRead()) {
throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
}
if (f.length() <= MINIMUM_SIZE_FOR_VALID_AUDIO_FILE) {
throw new CannotReadException(ErrorMessage.GENERAL_READ_FAILED_FILE_TOO_SMALL.getMsg(f.getAbsolutePath()));
}
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(f, "r");
raf.seek(0);
GenericAudioHeader info = getEncodingInfo(raf);
raf.seek(0);
Tag tag = getTag(raf);
return new AudioFile(f, info, tag);
} catch (CannotReadException cre) {
throw cre;
} catch (Exception e) {
logger.log(Level.SEVERE, ErrorMessage.GENERAL_READ.getMsg(f.getAbsolutePath()), e);
throw new CannotReadException(f.getAbsolutePath() + ":" + e.getMessage(), e);
} finally {
try {
if (raf != null) {
raf.close();
}
} catch (Exception ex) {
logger.log(Level.WARNING, ErrorMessage.GENERAL_READ_FAILED_UNABLE_TO_CLOSE_RANDOM_ACCESS_FILE.getMsg(f.getAbsolutePath()));
}
}
}
use of org.jaudiotagger.audio.AudioFile in project MusicDNA by harjot-oberai.
the class AudioFileWriter method delete.
/**
* Delete the tag (if any) present in the given file
*
* @param af The file to process
* @throws CannotWriteException if anything went wrong
* @throws org.jaudiotagger.audio.exceptions.CannotReadException
*/
public void delete(AudioFile af) throws CannotReadException, CannotWriteException {
if (!af.getFile().canWrite()) {
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath()));
}
if (af.getFile().length() <= MINIMUM_FILESIZE) {
throw new CannotWriteException(ErrorMessage.GENERAL_DELETE_FAILED.getMsg(af.getFile().getPath()));
}
RandomAccessFile raf = null;
RandomAccessFile rafTemp = null;
File tempF = null;
// Will be set to true on VetoException, causing the finally block to
// discard the tempfile.
boolean revert = false;
try {
tempF = File.createTempFile(af.getFile().getName().replace('.', '_'), TEMP_FILENAME_SUFFIX, af.getFile().getParentFile());
rafTemp = new RandomAccessFile(tempF, WRITE_MODE);
raf = new RandomAccessFile(af.getFile(), WRITE_MODE);
raf.seek(0);
rafTemp.seek(0);
try {
if (this.modificationListener != null) {
this.modificationListener.fileWillBeModified(af, true);
}
deleteTag(raf, rafTemp);
if (this.modificationListener != null) {
this.modificationListener.fileModified(af, tempF);
}
} catch (ModifyVetoException veto) {
throw new CannotWriteException(veto);
}
} catch (Exception e) {
revert = true;
throw new CannotWriteException("\"" + af.getFile().getAbsolutePath() + "\" :" + e, e);
} finally {
// will be set to the remaining file.
File result = af.getFile();
try {
if (raf != null) {
raf.close();
}
if (rafTemp != null) {
rafTemp.close();
}
if (tempF.length() > 0 && !revert) {
boolean deleteResult = af.getFile().delete();
if (!deleteResult) {
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
boolean renameResult = tempF.renameTo(af.getFile());
if (!renameResult) {
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
throw new CannotWriteException(ErrorMessage.GENERAL_WRITE_FAILED_TO_RENAME_TO_ORIGINAL_FILE.getMsg(af.getFile().getPath(), tempF.getPath()));
}
result = tempF;
// If still exists we can now delete
if (tempF.exists()) {
if (!tempF.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath()));
}
}
} else {
// It was created but never used
if (!tempF.delete()) {
// Non critical failed deletion
logger.warning(ErrorMessage.GENERAL_WRITE_FAILED_TO_DELETE_TEMPORARY_FILE.getMsg(tempF.getPath()));
}
}
} catch (Exception ex) {
logger.severe("AudioFileWriter exception cleaning up delete:" + af.getFile().getPath() + " or" + tempF.getAbsolutePath() + ":" + ex);
}
// Notify listener
if (this.modificationListener != null) {
this.modificationListener.fileOperationFinished(result);
}
}
}
use of org.jaudiotagger.audio.AudioFile in project JamsMusicPlayer by psaravan.
the class WriteToM3UPlaylist method getSongTitle.
public String getSongTitle(String songFilePath) {
String songTitle = "Title";
//Try to get the song title from the ID3 tag.
File songFile = new File(songFilePath);
AudioFile audioFile = null;
Tag tag = null;
try {
audioFile = AudioFileIO.read(songFile);
} catch (CannotReadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TagException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReadOnlyFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tag = audioFile.getTag();
songTitle = tag.getFirst(FieldKey.TITLE);
//If the ID3 tag doesn't give us the info we need, just use the file name.
if (songTitle.equals("Title") || songTitle.isEmpty()) {
int indexOfLastSlash = songTitle.lastIndexOf("/") + 1;
int indexOfLastDot = songTitle.lastIndexOf(".");
songTitle = songTitle.substring(indexOfLastSlash, indexOfLastDot);
}
return songTitle;
}
Aggregations