From fa61b83665da438e3a2cda196e2e710a9c702819 Mon Sep 17 00:00:00 2001 From: yutou Date: Mon, 23 Nov 2020 16:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E9=9F=B3=E4=B9=90=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../com/yutou/tools/ToolsApplication.java | 2 +- .../yutou/tools/home/nas/Data/MusicData.java | 13 ++++++- .../com/yutou/tools/utils/MusicTools.java | 37 ++++++++++++++----- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index a94aa50..476fc8d 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.yutou tools - 1.0.10.3 + 1.0.10.4 tools Demo project for Spring Boot diff --git a/src/main/java/com/yutou/tools/ToolsApplication.java b/src/main/java/com/yutou/tools/ToolsApplication.java index 8b3e6f0..fd3478c 100644 --- a/src/main/java/com/yutou/tools/ToolsApplication.java +++ b/src/main/java/com/yutou/tools/ToolsApplication.java @@ -12,7 +12,7 @@ import java.io.IOException; public class ToolsApplication { public static void main(String[] args) { - System.out.println("当前版本号:1.0.10.3"); + System.out.println("当前版本号:1.0.10.4"); SpringApplication.run(ToolsApplication.class, args); RedisTools.initRedisPoolSub(); if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")) { diff --git a/src/main/java/com/yutou/tools/home/nas/Data/MusicData.java b/src/main/java/com/yutou/tools/home/nas/Data/MusicData.java index 2ec66b5..4e2abb6 100644 --- a/src/main/java/com/yutou/tools/home/nas/Data/MusicData.java +++ b/src/main/java/com/yutou/tools/home/nas/Data/MusicData.java @@ -11,6 +11,7 @@ import org.springframework.util.StringUtils; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Base64; @Data public class MusicData { @@ -27,6 +28,17 @@ public class MusicData { private String lastDir;//上一个文件夹 private boolean isDir = false; + private int bitRate;//比特率 + private int sampleRate;//采样率 + private long noOfSamples;//采样数 + private int channelCount;//声道 + private String encodingType;//解码类型 + private double durationAsDouble;//持续时长 + private boolean lossless;//无损 + private boolean variableBitRate;//固定码率 + private String md5;//确保是同一个文件 + + public void setFile(File file) { this.file = file; this.isDir = file.isDirectory(); @@ -66,5 +78,4 @@ public class MusicData { return Files.readAllBytes(Paths.get(img.getAbsolutePath())); } - } diff --git a/src/main/java/com/yutou/tools/utils/MusicTools.java b/src/main/java/com/yutou/tools/utils/MusicTools.java index 4fd7679..813389b 100644 --- a/src/main/java/com/yutou/tools/utils/MusicTools.java +++ b/src/main/java/com/yutou/tools/utils/MusicTools.java @@ -1,13 +1,16 @@ package com.yutou.tools.utils; +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.*; @@ -36,13 +39,13 @@ public class MusicTools { new Timer().schedule(new TimerTask() { @Override public void run() { - String time=new SimpleDateFormat("HH:mm").format(new Date()); - if(time.equals("00:00")){ + String time = new SimpleDateFormat("HH:mm").format(new Date()); + if (time.equals("00:00")) { System.out.println("零点刷新列表"); scanMusic(); } } - },0,10*1000); + }, 0, 10 * 1000); } public synchronized void scanMusic() { @@ -103,13 +106,14 @@ public class MusicTools { /** * 获取指定目录下的音乐 - * @param path 指定目录 + * + * @param path 指定目录 * @param isDir 是否扫描目录下的所有文件,false则仅为当前目录 * @return 音乐列表 */ - public List getPath(String path,boolean isDir) { + public List getPath(String path, boolean isDir) { List list = new ArrayList<>(); - if(isDir) { + if (isDir) { if (new File(path).isDirectory() && !path.equals(MusicController.defaultMusicPath)) { for (String key : musicMap.keySet()) { if (key.startsWith(path)) { @@ -232,6 +236,21 @@ public class MusicTools { } 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 = new MusicData(); @@ -302,9 +321,9 @@ public class MusicTools { } public static void main(String[] args) throws Exception { - String base = "bXVzaWMuaHRtbDo2MyBaOlzpn7PkuZBc55m75bGx5bCR5aWzXFtzM11bRURd6Imy6YGV44GE44Gu57%2B8XDAyLiAwMiDoibLpgZXjgYTjga7nv7wgKOOBguOBiuOBhOODkOODvOOCuOODp%2BODsykud2F2Cg%3D%3D"; - byte[] bytes = getInstance().getMetadata(new File("Z:\\音乐\\BanG Dream!\\Poppin'on!\\B.O.F.flac")).readImage(); - System.out.println(bytes.length); + AudioFile audioFile = AudioFileIO.read(new File("C:\\Users\\admin\\Music\\34\\周杰伦 - 说好不哭(with 五月天阿信).flac")); + AudioHeader header = audioFile.getAudioHeader(); + System.out.println(JSONObject.toJSONString(header)); }