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; import ealvatag.audio.AudioFile; import ealvatag.audio.AudioFileIO; import ealvatag.audio.AudioHeader; import ealvatag.audio.exceptions.CannotReadException; import ealvatag.tag.FieldKey; import ealvatag.tag.NullTag; import ealvatag.tag.Tag; import ealvatag.tag.flac.FlacTag; import org.springframework.util.StringUtils; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; public class MusicTools { public static final int FIND_TITLE = 1; public static final int FIND_ARTIST = 2; private static MusicTools tools; private String musicPath = "/media/yutou/4t/public/音乐"; private final List musicList = new ArrayList<>(); private HashMap> musicMap = new HashMap>(); private boolean isScan = false; public static MusicTools getInstance() { if (tools == null) { tools = new MusicTools(); } return tools; } private MusicTools() { scanMusic(); new Timer().schedule(new TimerTask() { @Override public void run() { String time = new SimpleDateFormat("HH:mm").format(new Date()); if (time.equals("00:00")) { System.out.println("零点刷新列表"); scanMusic(); } } }, 0, 10 * 1000); } public synchronized void scanMusic() { if (isScan) { return; } System.out.println("执行扫描"); musicList.clear(); musicMap.clear(); new Thread(() -> { isScan = true; scan(new File(musicPath)); isScan = false; System.out.println("扫描完成"); }).start(); } public List scan(File path, boolean isScanDir) { if (isScanDir) { scan(path); return musicList; } else { if (path.exists() && path.isDirectory()) { List list = new ArrayList<>(); for (File file : path.listFiles()) { list.add(getMetadata(file)); } return list; } } return null; } private void scan(File path) { if (path.isFile()) { add(path); } else if (path.isDirectory()) { for (File file : Objects.requireNonNull(path.listFiles())) { if (file.isDirectory()) { scan(file); } else { add(file); } } } } public void getPathOrDir(String path, List list) { File files = new File(path); for (File file : files.listFiles()) { if (file.isFile()) { list.add(getMetadata(file)); } else { getPathOrDir(file.getAbsolutePath(), list); } } } /** * 获取指定目录下的音乐 * * @param path 指定目录 * @param isDir 是否扫描目录下的所有文件,false则仅为当前目录 * @return 音乐列表 */ public List getPath(String path, boolean isDir) { List list = new ArrayList<>(); if (isDir) { if (new File(path).isDirectory() && !path.equals(MusicController.defaultMusicPath)) { for (String key : musicMap.keySet()) { if (key.startsWith(path)) { list.addAll(musicMap.get(key)); } } return list; } } if (musicMap.containsKey(path)) { MusicData tmp = musicMap.get(path).isEmpty() ? null : musicMap.get(path).get(0); if (tmp != null) { if (!tmp.getFile().getParent().equals(MusicController.defaultMusicPath)) { MusicData t2 = new MusicData(); t2.setTitle("返回"); t2.setFile(new File(tmp.getLastDir())); list.add(t2); } } getDirList(path, list); list.addAll(musicMap.get(path)); return list; } else { if (path.contains(MusicController.defaultMusicPath)) { MusicData t2 = new MusicData(); t2.setTitle("返回"); t2.setFile(new File(path).getParentFile()); if (!path.equals(MusicController.defaultMusicPath)) { list.add(t2); } getDirList(path, list); return list; } } return new ArrayList<>(); } private void getDirList(String path, List list) { File file = new File(path); for (File listFile : file.listFiles()) { if (listFile.isDirectory()) { MusicData data = new MusicData(); data.setTitle(listFile.getName()); data.setFile(listFile); list.add(data); } } } private void add(File file) { MusicData data = getMetadata(file); if (data != null) { musicList.add(data); String path = file.getAbsolutePath().replace(File.separator + file.getName(), ""); List list; if (musicMap.containsKey(path)) { list = musicMap.get(path); } else { list = new ArrayList<>(); } list.add(data); musicMap.put(path, list); } } public MusicData getMetadata(File file) { try { if (file.getName().endsWith(".lrc") || file.getName().endsWith(".jpg") || file.getName().endsWith(".ini") || file.getName().endsWith(".png") || file.getName().endsWith(".torrent") || file.getName().endsWith(".log") || file.getName().endsWith(".mkv") || file.getName().endsWith(".dff") || file.getName().endsWith(".cue") ) { return null; } AudioFile audioFile = AudioFileIO.read(file); Tag tag = audioFile.getTag().or(NullTag.INSTANCE); MusicData data = new MusicData(); try { data.setAlbum(tag.getFirst(FieldKey.ALBUM)); } catch (Exception e) { } try { data.setArtist(tag.getFirst(FieldKey.ARTIST)); } catch (Exception e) { } try { data.setArtist_sort(tag.getFirst(FieldKey.ARTIST_SORT)); } catch (Exception e) { } try { data.setComment(tag.getFirst(FieldKey.COMMENT)); } catch (Exception e) { } try { data.setComposer(tag.getFirst(FieldKey.COMPOSER)); } catch (Exception e) { } try { data.setDisc_no(tag.getFirst(FieldKey.DISC_NO)); } catch (Exception e) { } try { if (StringUtils.isEmpty(tag.getFirst(FieldKey.TITLE))) { data.setTitle(file.getName()); } else { data.setTitle(tag.getFirst(FieldKey.TITLE)); } } catch (Exception e) { data.setTitle(file.getName()); } try { data.setTrack(tag.getFirst(FieldKey.TRACK)); } catch (Exception e) { } try { data.setYear(tag.getFirst(FieldKey.YEAR)); } catch (Exception e) { } data.setFile(file); AudioHeader header = audioFile.getAudioHeader(); data.setBitRate(header.getBitRate()); data.setSampleRate(header.getSampleRate()); data.setNoOfSamples(header.getNoOfSamples()); data.setChannelCount(header.getChannelCount()); data.setEncodingType(header.getEncodingType()); data.setDurationAsDouble(header.getDurationAsDouble()); 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 (CannotReadException e) { MusicData data = getMetadata_jthink(file); if (data == null) data = new MusicData(); data.setTitle(file.getName()); data.setFile(file); return data; } catch (Exception e) { e.printStackTrace(); } 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 findOfTitle(String title) { return find(title, FIND_TITLE); } public List findOfArtist(String by) { return find(by, FIND_ARTIST); } public List getMusicList() { return musicList; } public int getLength() { return tools.musicList.size(); } public boolean isScan() { return isScan; } private List find(String title, int type) { List list = new ArrayList<>(); for (MusicData data : musicList) { if (data.getTitle().contains(title)) { list.add(data); } } return list; } public String getMusicPath() { return musicPath; } public void setMusicPath(String musicPath) { this.musicPath = musicPath; } private void saveImage(MusicData data) { try { File file = new File(data.getTitle() + ".jpg"); if (!file.exists()) { if (!file.createNewFile()) { return; } } OutputStream outputStream = new FileOutputStream(file); outputStream.write(data.readImage()); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { File file = new File("Z:\\音乐\\总之就是非常酸\\ED\\カノエラナ - 月と星空\\カノエラナ.wav"); System.out.println(new MusicTools().getMetadata(file)); } }