This commit is contained in:
yutou 2020-11-23 18:31:09 +08:00
parent fa61b83665
commit 4f48d4f343

View File

@ -1,5 +1,6 @@
package com.yutou.tools.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.home.nas.Data.MusicData;
import com.yutou.tools.home.nas.MusicController;
@ -188,6 +189,8 @@ public class MusicTools {
|| file.getName().endsWith(".ini")
|| file.getName().endsWith(".png")
|| file.getName().endsWith(".torrent")
|| file.getName().endsWith(".log")
|| file.getName().endsWith(".mkv")
) {
return null;
}
@ -253,7 +256,9 @@ public class MusicTools {
}
return data;
} catch (CannotReadException e) {
MusicData data = new MusicData();
MusicData data = getMetadata_jthink(file);
if (data == null)
data = new MusicData();
data.setTitle(file.getName());
data.setFile(file);
return data;
@ -263,6 +268,74 @@ public class MusicTools {
return null;
}
public MusicData getMetadata_jthink(File file) {
try {
org.jaudiotagger.audio.AudioFile audioFile = org.jaudiotagger.audio.AudioFileIO.read(file);
org.jaudiotagger.tag.Tag tag = audioFile.getTag();
MusicData data = new MusicData();
try {
data.setAlbum(tag.getFirst(org.jaudiotagger.tag.FieldKey.ALBUM));
} catch (Exception ignored) {
}
try {
data.setArtist(tag.getFirst(org.jaudiotagger.tag.FieldKey.ARTIST));
} catch (Exception ignored) {
}
try {
data.setArtist_sort(tag.getFirst(org.jaudiotagger.tag.FieldKey.ARTIST_SORT));
} catch (Exception ignored) {
}
try {
data.setComment(tag.getFirst(org.jaudiotagger.tag.FieldKey.COMMENT));
} catch (Exception ignored) {
}
try {
data.setComposer(tag.getFirst(org.jaudiotagger.tag.FieldKey.COMPOSER));
} catch (Exception ignored) {
}
try {
data.setDisc_no(tag.getFirst(org.jaudiotagger.tag.FieldKey.DISC_NO));
} catch (Exception ignored) {
}
try {
if (StringUtils.isEmpty(tag.getFirst(org.jaudiotagger.tag.FieldKey.TITLE))) {
data.setTitle(file.getName());
} else {
data.setTitle(tag.getFirst(org.jaudiotagger.tag.FieldKey.TITLE));
}
} catch (Exception e) {
data.setTitle(file.getName());
}
try {
data.setTrack(tag.getFirst(org.jaudiotagger.tag.FieldKey.TRACK));
} catch (Exception ignored) {
}
try {
data.setYear(tag.getFirst(org.jaudiotagger.tag.FieldKey.YEAR));
} catch (Exception ignored) {
}
data.setFile(file);
org.jaudiotagger.audio.AudioHeader header = audioFile.getAudioHeader();
data.setBitRate(Integer.parseInt(header.getBitRate()));
data.setSampleRate(Integer.parseInt(header.getSampleRate()));
data.setNoOfSamples(header.getSampleRateAsNumber());
data.setChannelCount(Integer.parseInt(header.getChannels()));
data.setEncodingType(header.getEncodingType() + "");
data.setDurationAsDouble(header.getTrackLength());
data.setLossless(header.isLossless());
data.setVariableBitRate(header.isVariableBitRate());
try {
data.setMd5(header.getClass().getMethod("getMd5").invoke(header).toString());
} catch (Exception ignored) {
data.setMd5(Base64.getEncoder().encodeToString(file.getAbsolutePath().getBytes()));
}
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<MusicData> findOfTitle(String title) {
return find(title, FIND_TITLE);
@ -321,9 +394,10 @@ public class MusicTools {
}
public static void main(String[] args) throws Exception {
AudioFile audioFile = AudioFileIO.read(new File("C:\\Users\\admin\\Music\\34\\周杰伦 - 说好不哭with 五月天阿信).flac"));
File file = new File("Z:\\音乐\\秒速5厘米\\【秒速五厘米】OST-桜花抄.flac");
/*AudioFile audioFile = AudioFileIO.read(file);
AudioHeader header = audioFile.getAudioHeader();
System.out.println(JSONObject.toJSONString(header));
System.out.println(JSONObject.toJSONString(header));*/
}