use of org.libresonic.player.service.metadata.MetaDataParser in project libresonic by Libresonic.
the class PodcastService method updateTags.
private void updateTags(File file, PodcastEpisode episode) {
try {
MediaFile mediaFile = mediaFileService.getMediaFile(file, false);
if (StringUtils.isNotBlank(episode.getTitle())) {
MetaDataParser parser = metaDataParserFactory.getParser(file);
if (!parser.isEditingSupported()) {
return;
}
MetaData metaData = parser.getRawMetaData(file);
metaData.setTitle(episode.getTitle());
parser.setMetaData(mediaFile, metaData);
mediaFileService.refreshMediaFile(mediaFile);
}
} catch (Exception x) {
LOG.warn("Failed to update tags for podcast " + episode.getUrl(), x);
}
}
use of org.libresonic.player.service.metadata.MetaDataParser in project libresonic by Libresonic.
the class EditTagsController method createSong.
private Song createSong(MediaFile file, int index) {
MetaDataParser parser = metaDataParserFactory.getParser(file.getFile());
Song song = new Song();
song.setId(file.getId());
song.setFileName(FilenameUtils.getBaseName(file.getPath()));
song.setTrack(file.getTrackNumber());
song.setSuggestedTrack(index + 1);
song.setTitle(file.getTitle());
song.setSuggestedTitle(parser.guessTitle(file.getFile()));
song.setArtist(file.getArtist());
song.setAlbum(file.getAlbumName());
song.setYear(file.getYear());
song.setGenre(file.getGenre());
return song;
}
use of org.libresonic.player.service.metadata.MetaDataParser in project libresonic by Libresonic.
the class TagService method setTags.
/**
* Updated tags for a given music file.
*
* @param id The ID of the music file.
* @param track The track number.
* @param artist The artist name.
* @param album The album name.
* @param title The song title.
* @param year The release year.
* @param genre The musical genre.
* @return "UPDATED" if the new tags were updated, "SKIPPED" if no update was necessary.
* Otherwise the error message is returned.
*/
public String setTags(int id, String track, String artist, String album, String title, String year, String genre) {
track = StringUtils.trimToNull(track);
artist = StringUtils.trimToNull(artist);
album = StringUtils.trimToNull(album);
title = StringUtils.trimToNull(title);
year = StringUtils.trimToNull(year);
genre = StringUtils.trimToNull(genre);
Integer trackNumber = null;
if (track != null) {
try {
trackNumber = new Integer(track);
} catch (NumberFormatException x) {
LOG.warn("Illegal track number: " + track, x);
}
}
Integer yearNumber = null;
if (year != null) {
try {
yearNumber = new Integer(year);
} catch (NumberFormatException x) {
LOG.warn("Illegal year: " + year, x);
}
}
try {
MediaFile file = mediaFileService.getMediaFile(id);
MetaDataParser parser = metaDataParserFactory.getParser(file.getFile());
if (!parser.isEditingSupported()) {
return "Tag editing of " + FilenameUtils.getExtension(file.getPath()) + " files is not supported.";
}
if (StringUtils.equals(artist, file.getArtist()) && StringUtils.equals(album, file.getAlbumName()) && StringUtils.equals(title, file.getTitle()) && ObjectUtils.equals(yearNumber, file.getYear()) && StringUtils.equals(genre, file.getGenre()) && ObjectUtils.equals(trackNumber, file.getTrackNumber())) {
return "SKIPPED";
}
MetaData newMetaData = parser.getMetaData(file.getFile());
// Note: album artist is intentionally set, as it is not user-changeable.
newMetaData.setArtist(artist);
newMetaData.setAlbumName(album);
newMetaData.setTitle(title);
newMetaData.setYear(yearNumber);
newMetaData.setGenre(genre);
newMetaData.setTrackNumber(trackNumber);
parser.setMetaData(file, newMetaData);
mediaFileService.refreshMediaFile(file);
mediaFileService.refreshMediaFile(mediaFileService.getParentOf(file));
return "UPDATED";
} catch (Exception x) {
LOG.warn("Failed to update tags for " + id, x);
return x.getMessage();
}
}
use of org.libresonic.player.service.metadata.MetaDataParser in project libresonic by Libresonic.
the class MediaFileService method createMediaFile.
private MediaFile createMediaFile(File file) {
MediaFile existingFile = mediaFileDao.getMediaFile(file.getPath());
MediaFile mediaFile = new MediaFile();
Date lastModified = new Date(FileUtil.lastModified(file));
mediaFile.setPath(file.getPath());
mediaFile.setFolder(securityService.getRootFolderForFile(file));
mediaFile.setParentPath(file.getParent());
mediaFile.setChanged(lastModified);
mediaFile.setLastScanned(new Date());
mediaFile.setPlayCount(existingFile == null ? 0 : existingFile.getPlayCount());
mediaFile.setLastPlayed(existingFile == null ? null : existingFile.getLastPlayed());
mediaFile.setComment(existingFile == null ? null : existingFile.getComment());
mediaFile.setChildrenLastUpdated(new Date(0));
mediaFile.setCreated(lastModified);
mediaFile.setMediaType(DIRECTORY);
mediaFile.setPresent(true);
if (file.isFile()) {
MetaDataParser parser = metaDataParserFactory.getParser(file);
if (parser != null) {
MetaData metaData = parser.getMetaData(file);
mediaFile.setArtist(metaData.getArtist());
mediaFile.setAlbumArtist(metaData.getAlbumArtist());
mediaFile.setAlbumName(metaData.getAlbumName());
mediaFile.setTitle(metaData.getTitle());
mediaFile.setDiscNumber(metaData.getDiscNumber());
mediaFile.setTrackNumber(metaData.getTrackNumber());
mediaFile.setGenre(metaData.getGenre());
mediaFile.setYear(metaData.getYear());
mediaFile.setDurationSeconds(metaData.getDurationSeconds());
mediaFile.setBitRate(metaData.getBitRate());
mediaFile.setVariableBitRate(metaData.getVariableBitRate());
mediaFile.setHeight(metaData.getHeight());
mediaFile.setWidth(metaData.getWidth());
}
String format = StringUtils.trimToNull(StringUtils.lowerCase(FilenameUtils.getExtension(mediaFile.getPath())));
mediaFile.setFormat(format);
mediaFile.setFileSize(FileUtil.length(file));
mediaFile.setMediaType(getMediaType(mediaFile));
} else {
// Is this an album?
if (!isRoot(mediaFile)) {
File[] children = FileUtil.listFiles(file);
File firstChild = null;
for (File child : filterMediaFiles(children)) {
if (FileUtil.isFile(child)) {
firstChild = child;
break;
}
}
if (firstChild != null) {
mediaFile.setMediaType(ALBUM);
// Guess artist/album name, year and genre.
MetaDataParser parser = metaDataParserFactory.getParser(firstChild);
if (parser != null) {
MetaData metaData = parser.getMetaData(firstChild);
mediaFile.setArtist(metaData.getAlbumArtist());
mediaFile.setAlbumName(metaData.getAlbumName());
mediaFile.setYear(metaData.getYear());
mediaFile.setGenre(metaData.getGenre());
}
// Look for cover art.
try {
File coverArt = findCoverArt(children);
if (coverArt != null) {
mediaFile.setCoverArtPath(coverArt.getPath());
}
} catch (IOException x) {
LOG.error("Failed to find cover art.", x);
}
} else {
mediaFile.setArtist(file.getName());
}
}
}
return mediaFile;
}
Aggregations