扩展音乐参数

This commit is contained in:
yutou 2020-11-23 16:14:50 +08:00
parent cb0ccd24d2
commit fa61b83665
4 changed files with 42 additions and 12 deletions

View File

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>com.yutou</groupId> <groupId>com.yutou</groupId>
<artifactId>tools</artifactId> <artifactId>tools</artifactId>
<version>1.0.10.3</version> <version>1.0.10.4</version>
<name>tools</name> <name>tools</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>

View File

@ -12,7 +12,7 @@ import java.io.IOException;
public class ToolsApplication { public class ToolsApplication {
public static void main(String[] args) { 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); SpringApplication.run(ToolsApplication.class, args);
RedisTools.initRedisPoolSub(); RedisTools.initRedisPoolSub();
if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")) { if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")) {

View File

@ -11,6 +11,7 @@ import org.springframework.util.StringUtils;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Base64;
@Data @Data
public class MusicData { public class MusicData {
@ -27,6 +28,17 @@ public class MusicData {
private String lastDir;//上一个文件夹 private String lastDir;//上一个文件夹
private boolean isDir = false; 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) { public void setFile(File file) {
this.file = file; this.file = file;
this.isDir = file.isDirectory(); this.isDir = file.isDirectory();
@ -66,5 +78,4 @@ public class MusicData {
return Files.readAllBytes(Paths.get(img.getAbsolutePath())); return Files.readAllBytes(Paths.get(img.getAbsolutePath()));
} }
} }

View File

@ -1,13 +1,16 @@
package com.yutou.tools.utils; package com.yutou.tools.utils;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.home.nas.Data.MusicData; import com.yutou.tools.home.nas.Data.MusicData;
import com.yutou.tools.home.nas.MusicController; import com.yutou.tools.home.nas.MusicController;
import ealvatag.audio.AudioFile; import ealvatag.audio.AudioFile;
import ealvatag.audio.AudioFileIO; import ealvatag.audio.AudioFileIO;
import ealvatag.audio.AudioHeader;
import ealvatag.audio.exceptions.CannotReadException; import ealvatag.audio.exceptions.CannotReadException;
import ealvatag.tag.FieldKey; import ealvatag.tag.FieldKey;
import ealvatag.tag.NullTag; import ealvatag.tag.NullTag;
import ealvatag.tag.Tag; import ealvatag.tag.Tag;
import ealvatag.tag.flac.FlacTag;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.*; import java.io.*;
@ -36,13 +39,13 @@ public class MusicTools {
new Timer().schedule(new TimerTask() { new Timer().schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
String time=new SimpleDateFormat("HH:mm").format(new Date()); String time = new SimpleDateFormat("HH:mm").format(new Date());
if(time.equals("00:00")){ if (time.equals("00:00")) {
System.out.println("零点刷新列表"); System.out.println("零点刷新列表");
scanMusic(); scanMusic();
} }
} }
},0,10*1000); }, 0, 10 * 1000);
} }
public synchronized void scanMusic() { public synchronized void scanMusic() {
@ -103,13 +106,14 @@ public class MusicTools {
/** /**
* 获取指定目录下的音乐 * 获取指定目录下的音乐
*
* @param path 指定目录 * @param path 指定目录
* @param isDir 是否扫描目录下的所有文件false则仅为当前目录 * @param isDir 是否扫描目录下的所有文件false则仅为当前目录
* @return 音乐列表 * @return 音乐列表
*/ */
public List<MusicData> getPath(String path,boolean isDir) { public List<MusicData> getPath(String path, boolean isDir) {
List<MusicData> list = new ArrayList<>(); List<MusicData> list = new ArrayList<>();
if(isDir) { if (isDir) {
if (new File(path).isDirectory() && !path.equals(MusicController.defaultMusicPath)) { if (new File(path).isDirectory() && !path.equals(MusicController.defaultMusicPath)) {
for (String key : musicMap.keySet()) { for (String key : musicMap.keySet()) {
if (key.startsWith(path)) { if (key.startsWith(path)) {
@ -232,6 +236,21 @@ public class MusicTools {
} catch (Exception e) { } catch (Exception e) {
} }
data.setFile(file); 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; return data;
} catch (CannotReadException e) { } catch (CannotReadException e) {
MusicData data = new MusicData(); MusicData data = new MusicData();
@ -302,9 +321,9 @@ public class MusicTools {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
String base = "bXVzaWMuaHRtbDo2MyBaOlzpn7PkuZBc55m75bGx5bCR5aWzXFtzM11bRURd6Imy6YGV44GE44Gu57%2B8XDAyLiAwMiDoibLpgZXjgYTjga7nv7wgKOOBguOBiuOBhOODkOODvOOCuOODp%2BODsykud2F2Cg%3D%3D"; AudioFile audioFile = AudioFileIO.read(new File("C:\\Users\\admin\\Music\\34\\周杰伦 - 说好不哭with 五月天阿信).flac"));
byte[] bytes = getInstance().getMetadata(new File("Z:\\音乐\\BanG Dream!\\Poppin'on!\\B.O.F.flac")).readImage(); AudioHeader header = audioFile.getAudioHeader();
System.out.println(bytes.length); System.out.println(JSONObject.toJSONString(header));
} }