更换元数据读取库

This commit is contained in:
yutou 2020-10-23 17:55:56 +08:00
parent b0460fbe14
commit 180346963c
6 changed files with 91 additions and 44 deletions

View File

@ -96,6 +96,11 @@
<artifactId>jaudiotagger</artifactId> <artifactId>jaudiotagger</artifactId>
<version>2.2.3</version> <version>2.2.3</version>
</dependency> </dependency>
<dependency>
<groupId>com.ealva</groupId>
<artifactId>ealvatag</artifactId>
<version>0.4.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -11,8 +11,7 @@ public class ToolsApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ToolsApplication.class, args); SpringApplication.run(ToolsApplication.class, args);
RedisTools.initRedisPoolSub(); RedisTools.initRedisPoolSub();
MusicTools.getInstance().setMusicPath("Z:\\音乐\\Aimer - ARIA STRINGS"); MusicTools.getInstance().setMusicPath("C:\\Users\\admin\\Music\\");
MusicTools.getInstance().scanMusic();
} }
} }

View File

@ -1,13 +1,11 @@
package com.yutou.tools.home.nas.Data; package com.yutou.tools.home.nas.Data;
import ealvatag.audio.AudioFile;
import ealvatag.audio.AudioFileIO;
import ealvatag.tag.NullTag;
import ealvatag.tag.Tag;
import ealvatag.tag.images.NullArtwork;
import lombok.Data; import lombok.Data;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
@ -29,11 +27,12 @@ public class MusicData {
public byte[] readImage() throws Exception { public byte[] readImage() throws Exception {
AudioFile audioFile = null; AudioFile audioFile = null;
audioFile = AudioFileIO.read(file); audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag(); Tag tag = audioFile.getTag().or(NullTag.INSTANCE);
if (tag.getFirstArtwork() == null) { byte[] bytes=tag.getFirstArtwork().or(NullArtwork.INSTANCE).getBinaryData();
if (bytes.length==0) {
return readImageFile(); return readImageFile();
} }
return tag.getFirstArtwork().getBinaryData(); return bytes;
} }
private byte[] readImageFile() throws Exception { private byte[] readImageFile() throws Exception {

View File

@ -43,15 +43,17 @@ public class MusicController {
if(StringUtils.isEmpty(path)){ if(StringUtils.isEmpty(path)){
return getAllMusicList(); return getAllMusicList();
} }
path=path.replace(defaultMusicPath+File.separator,""); if(path.equals("root")){
return getAllMusicList();
}
//path=path.replace(defaultMusicPath+File.separator,"");
JSONObject json=new JSONObject(); JSONObject json=new JSONObject();
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
json.put("code", 1); json.put("code", 1);
MusicTools tools = MusicTools.getInstance(); MusicTools tools = MusicTools.getInstance();
tools.setMusicPath(defaultMusicPath+File.separator+path);
data.put("scan", tools.isScan()); data.put("scan", tools.isScan());
data.put("size", tools.getLength()); data.put("size", tools.getLength());
data.put("list", JSONArray.toJSON(tools.getMusicPath())); data.put("list", JSONArray.toJSON(tools.getPath(path)));
json.put("data", data); json.put("data", data);
return json.toJSONString(); return json.toJSONString();

View File

@ -1,18 +1,16 @@
package com.yutou.tools.utils; package com.yutou.tools.utils;
import com.yutou.tools.home.nas.Data.MusicData; import com.yutou.tools.home.nas.Data.MusicData;
import org.jaudiotagger.audio.AudioFile; import ealvatag.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO; import ealvatag.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException; import ealvatag.audio.exceptions.CannotReadException;
import org.jaudiotagger.tag.FieldKey; import ealvatag.tag.FieldKey;
import org.jaudiotagger.tag.Tag; import ealvatag.tag.NullTag;
import ealvatag.tag.Tag;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.*;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
public class MusicTools { public class MusicTools {
public static final int FIND_TITLE = 1; public static final int FIND_TITLE = 1;
@ -21,6 +19,7 @@ public class MusicTools {
private static MusicTools tools; private static MusicTools tools;
private String musicPath = "C:\\Users\\admin\\Music"; private String musicPath = "C:\\Users\\admin\\Music";
private final List<MusicData> musicList = new ArrayList<>(); private final List<MusicData> musicList = new ArrayList<>();
private HashMap<String,List<MusicData>> musicMap=new HashMap<String,List<MusicData>>();
private boolean isScan = false; private boolean isScan = false;
public static MusicTools getInstance() { public static MusicTools getInstance() {
@ -29,8 +28,14 @@ public class MusicTools {
} }
return tools; return tools;
} }
private MusicTools(){
scanMusic();
}
public synchronized void scanMusic() { public synchronized void scanMusic() {
if(isScan){
return;
}
System.out.println("执行扫描"); System.out.println("执行扫描");
musicList.clear(); musicList.clear();
new Thread(() -> { new Thread(() -> {
@ -70,11 +75,26 @@ public class MusicTools {
} }
} }
} }
public List<MusicData> getPath(String path){
if(musicMap.containsKey(path)){
return musicMap.get(path);
}
return new ArrayList<>();
}
private void add(File file) { private void add(File file) {
MusicData data = getMetadata(file); MusicData data = getMetadata(file);
if (data != null) { if (data != null) {
musicList.add(data); musicList.add(data);
String path=file.getAbsolutePath().replace(file.getName(),"");
List<MusicData> list;
if(musicMap.containsKey(path)){
list=musicMap.get(path);
}else{
list=new ArrayList<>();
}
list.add(data);
musicMap.put(path,list);
} }
} }
@ -84,7 +104,7 @@ public class MusicTools {
return null; return null;
} }
AudioFile audioFile = AudioFileIO.read(file); AudioFile audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag(); Tag tag = audioFile.getTag().or(NullTag.INSTANCE);
MusicData data = new MusicData(); MusicData data = new MusicData();
try { try {
data.setAlbum(tag.getFirst(FieldKey.ALBUM)); data.setAlbum(tag.getFirst(FieldKey.ALBUM));
@ -197,9 +217,10 @@ public class MusicTools {
} }
} }
public static void main(String[] args) { public static void main(String[] args) throws Exception{
String base = "Wjpc6Z-z5LmQXOOAkOmbqOWuruWkqeOAkU1PUkEg5YWo5aWXMTHlvKBIaXJlc-S4k-i-kVxbMjAyMDA5MDJdIOmbqOWuruWkqSAzcmTjgqLjg6vjg5Djg6DjgIxQYWludCBpdCwgQkxVReOAjVs5NmtIejI0Yml0XVtGTEFDXVwwMDMtRGVmaWFuY2UuZmxhYw=="; String base = "bXVzaWMuaHRtbDo2MyBaOlzpn7PkuZBc55m75bGx5bCR5aWzXFtzM11bRURd6Imy6YGV44GE44Gu57%2B8XDAyLiAwMiDoibLpgZXjgYTjga7nv7wgKOOBguOBiuOBhOODkOODvOOCuOODp%2BODsykud2F2Cg%3D%3D";
System.out.println(new String(Base64.getDecoder().decode(base))); byte[] bytes=getInstance().getMetadata(new File("Z:\\音乐\\BanG Dream!\\Poppin'on!\\B.O.F.flac")).readImage();
System.out.println(bytes.length);
} }

View File

@ -11,6 +11,7 @@
<body> <body>
<div class="layui-layout layui-layout-admin"> <div class="layui-layout layui-layout-admin">
<div id="header"></div> <div id="header"></div>
<div class="layui-body" style="left: 200px;"> <div class="layui-body" style="left: 200px;">
<div id="side"></div> <div id="side"></div>
@ -19,20 +20,29 @@
<img src="/assets/defaultPlayImg.jpg" id="img" style="height: 200px; width: 200px"/> <img src="/assets/defaultPlayImg.jpg" id="img" style="height: 200px; width: 200px"/>
<div style="display:inline-block;"> <div style="display:inline-block;">
<div id="title">title</div> <div id="title">标题:</div>
<div id="artist">by</div> <div id="artist">艺术家:</div>
<div id="album">album</div> <div id="album">专辑:</div>
<div id="composer">composer</div> <div id="composer">作曲家</div>
</div> </div>
<div id="audioWrap"></div> <div id="audioWrap"></div>
</div> </div>
<table id="playlist" lay-filter="music" ></table> <ul class="layui-nav layui-bg-cyan" lay-filter="menus" style="margin-right: 10px;">
<div style="margin-top: 10px;">
<span id="menu" class="layui-breadcrumb" lay-separator="/"
style="visibility: visible;">
<a href="javascript:onMenuClick('root');">主目录</a>
</span>
</div>
</ul>
<table id="playlist" lay-filter="music"></table>
</div> </div>
<div id="footer"></div> <div id="footer"></div>
</div> </div>
</div> </div>
<script src="/layui/layui.js"></script> <script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script> <script src="/js/jquery-3.2.1.js"></script>
@ -42,13 +52,14 @@
<script src="/js/AudioPlayer.js"></script> <script src="/js/AudioPlayer.js"></script>
<script> <script>
//let localhost="http://192.168.31.88:8000"; //let localhost="http://192.168.31.88:8000";
let localhost=""; let localhost = "";
layui.use('table', function () { layui.use(['table', 'element'], function () {
let table = layui.table; let table = layui.table;
let element = layui.element;
table.render({ table.render({
elem: '#playlist' elem: '#playlist'
, height: 312 , height: 312
, url: localhost+'/nas/music/all.do' //数据接口 , url: localhost + '/nas/music/all.do' //数据接口
, page: true //开启分页 , page: true //开启分页
, cols: [[ , cols: [[
{field: 'title', title: '标题', width: 400} {field: 'title', title: '标题', width: 400}
@ -58,12 +69,16 @@
, {field: 'track', title: '音轨号', width: 100} , {field: 'track', title: '音轨号', width: 100}
]] ]]
}) })
table.on('rowDouble(music)', function(obj){ table.on('rowDouble(music)', function (obj) {
//obj 同上 //obj 同上
console.log(obj.data.file) //得到当前行元素对象 console.log(obj.data.file) //得到当前行元素对象
play(obj.data.file) play(obj.data.file)
}); });
element.on('nav(menus)', function (elem) {
console.log(elem); //得到当前点击的DOM对象
}); });
});
$.get("/login/check.do", function (data) { $.get("/login/check.do", function (data) {
let json = JSON.parse(data); let json = JSON.parse(data);
if (json.code !== 0) { if (json.code !== 0) {
@ -80,7 +95,7 @@
, endedCallback: function () { , endedCallback: function () {
console.log('播放完成') console.log('播放完成')
random() random()
},canplayCallback:function () { }, canplayCallback: function () {
console.log("点击播放") console.log("点击播放")
} }
}); });
@ -92,12 +107,16 @@
random() random()
}); });
function onMenuClick(that) {
console.log(that)
}
function random() { function random() {
$.get(localhost+"/nas/music/random.do", function (data) { $.get(localhost + "/nas/music/random.do", function (data) {
let json = JSON.parse(data) let json = JSON.parse(data)
if (json.code === 1) { if (json.code === 1) {
player.updateSource({ player.updateSource({
source: localhost+"/nas/music/play.do?random=false&filePath=" + json.data source: localhost + "/nas/music/play.do?random=false&filePath=" + json.data
}); });
update(json.data) update(json.data)
player.play() player.play()
@ -107,16 +126,18 @@
} }
}) })
} }
function play(file) { function play(file) {
let filePath=escape(new Base64().encode(file)) let filePath = escape(new Base64().encode(file))
player.updateSource({ player.updateSource({
source:localhost+"/nas/music/play.do?random=false&filePath=" +filePath source: localhost + "/nas/music/play.do?random=false&filePath=" + filePath
}); });
update(filePath) update(filePath)
player.play() player.play()
} }
function update(fileName) { function update(fileName) {
$.get(localhost+'/nas/music/find/file.do?path=' + fileName, function (data) { $.get(localhost + '/nas/music/find/file.do?path=' + fileName, function (data) {
let json = JSON.parse(data); let json = JSON.parse(data);
if (json.code === 0) { if (json.code === 0) {
layer.msg(json.msg); layer.msg(json.msg);
@ -127,7 +148,7 @@
$('#album').html("专辑:" + json.data.album) $('#album').html("专辑:" + json.data.album)
$('#composer').html("作曲:" + json.data.composer) $('#composer').html("作曲:" + json.data.composer)
}) })
$.get(localhost+"/nas/music/image.do?fileName=" + fileName, function (data) { $.get(localhost + "/nas/music/image.do?fileName=" + fileName, function (data) {
let json = JSON.parse(data); let json = JSON.parse(data);
if (json.code === 0) { if (json.code === 0) {
layer.msg(json.msg); layer.msg(json.msg);