From 5ae1b474f90edf0a50040b6ad71b864d4f0e1eb0 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Fri, 29 Nov 2024 10:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E8=B5=B0LiveDatabaseServer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/index.html | 4 +-- .../biliapi/bean/live/LiveRoomConfig.java | 12 +++++---- .../biliapi/databases/BiliLiveDatabase.java | 2 +- .../Controllers/LiveVideoController.java | 7 ++--- .../bilibili/Tools/ApplicationClose.java | 8 +++++- .../bilibili/Tools/LiveInfoNfoTools.java | 2 +- .../bilibili/services/LiveConfigService.java | 23 ++++++++-------- .../bilibili/services/LiveDanmuService.java | 10 +++---- .../services/LiveDatabasesService.java | 27 ++++++++++++++++++- .../yutou/bilibili/services/LiveService.java | 14 ++-------- .../services/LiveVideoDownloadService.java | 15 +++++------ .../bilibili/services/SystemService.java | 9 +++---- 12 files changed, 75 insertions(+), 58 deletions(-) diff --git a/Web/index.html b/Web/index.html index 28b9280..cc9ac69 100644 --- a/Web/index.html +++ b/Web/index.html @@ -10,7 +10,7 @@ -
+
@@ -141,7 +141,7 @@ }) } }, function () { - layer.msg('点击取消的回调'); + }); } function confirmFollow(userId) { diff --git a/src/main/java/com/yutou/biliapi/bean/live/LiveRoomConfig.java b/src/main/java/com/yutou/biliapi/bean/live/LiveRoomConfig.java index 65fb0d2..e7715f7 100644 --- a/src/main/java/com/yutou/biliapi/bean/live/LiveRoomConfig.java +++ b/src/main/java/com/yutou/biliapi/bean/live/LiveRoomConfig.java @@ -14,13 +14,13 @@ public class LiveRoomConfig { String roomId; String anchorName; boolean isLogin; - String rootPath="live"; + String rootPath = "live"; LiveDanmuInfo liveInfo; LiveRoomInfo roomInfo; public String getLoginUid() { - if("null".equals(loginUid)){ - loginUid=null; + if ("null".equals(loginUid)) { + loginUid = null; } return loginUid; } @@ -42,9 +42,11 @@ public class LiveRoomConfig { return Objects.hashCode(roomId); } - public static LiveRoomConfig buildConfig(String roomId){ + + @Deprecated + public static LiveRoomConfig buildConfigTmp(String roomId) { BiliLiveConfigDatabase database = new BiliLiveConfigDatabase(); - LiveConfigDatabaseBean bean = database.getConfig(new String(roomId)); + LiveConfigDatabaseBean bean = database.getConfig(roomId); database.close(); LiveRoomConfig config = new LiveRoomConfig(); config.setLoginUid(bean.getRecordUid()); diff --git a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java index d55fc56..fd3ce2f 100644 --- a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java +++ b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java @@ -271,7 +271,7 @@ public class BiliLiveDatabase extends SQLiteManager { } public static void main(String[] args) { - BiliLiveDatabase biliLiveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig("33989")); + BiliLiveDatabase biliLiveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfigTmp("33989")); // biliLiveDatabase.resetSQL(); biliLiveDatabase.resetData(); diff --git a/src/main/java/com/yutou/bilibili/Controllers/LiveVideoController.java b/src/main/java/com/yutou/bilibili/Controllers/LiveVideoController.java index f6f99a6..6398a66 100644 --- a/src/main/java/com/yutou/bilibili/Controllers/LiveVideoController.java +++ b/src/main/java/com/yutou/bilibili/Controllers/LiveVideoController.java @@ -5,6 +5,7 @@ import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.bilibili.datas.ResultData; import com.yutou.bilibili.datas.ReturnCode; +import com.yutou.bilibili.services.LiveDatabasesService; import com.yutou.bilibili.services.LiveVideoDownloadService; import jakarta.annotation.Resource; import org.springframework.stereotype.Controller; @@ -17,6 +18,8 @@ import java.util.List; public class LiveVideoController { @Resource LiveVideoDownloadService videoService; + @Resource + LiveDatabasesService databasesService; @RequestMapping("/live/video/list") @ResponseBody @@ -34,9 +37,7 @@ public class LiveVideoController { @RequestMapping("/live/video/start") @ResponseBody public JSONObject startDownload(String roomId) { - BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase(); - List list = liveConfigDatabase.getAllConfig(); - liveConfigDatabase.close(); + List list = databasesService.getConfigDatabase().getAllConfig(); for (LiveConfigDatabaseBean bean : list) { if (bean.getRoomId().toString().equals(roomId)) { videoService.start(bean, true); diff --git a/src/main/java/com/yutou/bilibili/Tools/ApplicationClose.java b/src/main/java/com/yutou/bilibili/Tools/ApplicationClose.java index 2874efb..18e3eaa 100644 --- a/src/main/java/com/yutou/bilibili/Tools/ApplicationClose.java +++ b/src/main/java/com/yutou/bilibili/Tools/ApplicationClose.java @@ -1,9 +1,11 @@ package com.yutou.bilibili.Tools; +import com.yutou.bilibili.services.LiveDatabasesService; import com.yutou.bilibili.services.LiveVideoDownloadService; import com.yutou.bilibili.services.SystemService; import com.yutou.common.utils.Log; import jakarta.annotation.Resource; +import org.jetbrains.annotations.NotNull; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.stereotype.Component; @@ -14,10 +16,14 @@ public class ApplicationClose implements ApplicationListener SystemService systemConfigService; @Resource LiveVideoDownloadService videoService; + @Resource + LiveDatabasesService databasesService; + @Override - public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { + public void onApplicationEvent(@NotNull ContextClosedEvent contextClosedEvent) { Log.i("服务结束"); systemConfigService.stop(); videoService.stopAll(); + databasesService.closeAll(); } } diff --git a/src/main/java/com/yutou/bilibili/Tools/LiveInfoNfoTools.java b/src/main/java/com/yutou/bilibili/Tools/LiveInfoNfoTools.java index 69382ac..ef25387 100644 --- a/src/main/java/com/yutou/bilibili/Tools/LiveInfoNfoTools.java +++ b/src/main/java/com/yutou/bilibili/Tools/LiveInfoNfoTools.java @@ -22,7 +22,7 @@ import java.io.File; public class LiveInfoNfoTools { public static void saveLiveInfoNfo(LiveRoomInfo info, String path,String xmlName) { try { - LiveRoomConfig config = LiveRoomConfig.buildConfig(info.getRoomId().toString()); + LiveRoomConfig config = LiveRoomConfig.buildConfigTmp(info.getRoomId()); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = null; dBuilder = dbFactory.newDocumentBuilder(); diff --git a/src/main/java/com/yutou/bilibili/services/LiveConfigService.java b/src/main/java/com/yutou/bilibili/services/LiveConfigService.java index 04df7bd..83f6743 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveConfigService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveConfigService.java @@ -17,7 +17,8 @@ import java.util.List; @Service public class LiveConfigService { - BiliLiveConfigDatabase database = new BiliLiveConfigDatabase(); + @Resource + LiveDatabasesService databasesService; public LiveConfigDatabaseBean addConfig(String url, LiveConfigDatabaseBean bean) { if (!StringUtils.hasText(url)) { @@ -35,7 +36,7 @@ public class LiveConfigService { bean.setRoomId(body.getRoomId()); bean.setAnchorFace(infoBean.getInfo().getFace()); bean.setAnchorName(infoBean.getInfo().getUname()); - database.setConfig(bean); + databasesService.getConfigDatabase().setConfig(bean); return bean; } catch (IOException e) { throw new RuntimeException(e); @@ -43,38 +44,38 @@ public class LiveConfigService { } public LiveConfigDatabaseBean updateConfig(String roomId, LiveConfigDatabaseBean bean) { - LiveConfigDatabaseBean config = database.getConfig(roomId); + LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(roomId); if (config == null) { return null; } bean.setRoomId(roomId); bean.setSql_time(config.getSql_time()); - database.setConfig(bean); + databasesService.getConfigDatabase().setConfig(bean); return bean; } public boolean deleteConfig(String roomId) { - LiveConfigDatabaseBean config = database.getConfig(roomId); + LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(roomId); if (config == null) { return false; } - return database.deleteConfig(roomId); + return databasesService.getConfigDatabase().deleteConfig(roomId); } public List getAllConfig() { - return database.getAllConfig(); + return databasesService.getConfigDatabase().getAllConfig(); } public List getConfigs(int page,int limit) { - return database.getConfigs(page, limit); + return databasesService.getConfigDatabase().getConfigs(page, limit); } public LiveConfigDatabaseBean getConfig(String roomId) { - return database.getConfig(roomId); + return databasesService.getConfigDatabase().getConfig(roomId); } public File getFace(String roomId) { - LiveConfigDatabaseBean config = database.getConfig(new String(roomId)); + LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(new String(roomId)); if (config == null) { return null; } @@ -102,6 +103,6 @@ public class LiveConfigService { } public int getConfigCount() { - return database.getConfigCount(); + return databasesService.getConfigDatabase().getConfigCount(); } } diff --git a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java index 1082ac0..6407c0c 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java @@ -30,11 +30,11 @@ public class LiveDanmuService { WebSocketServer webSocketServer; public void start(String roomId, boolean isUser) { - webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId), isUser); + webSocketServer.addRoom(liveDatabasesService.buildConfig(roomId), isUser); } public void start(LiveConfigDatabaseBean roomId, boolean isUser) { - webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId.getRoomId()), isUser); + webSocketServer.addRoom(liveDatabasesService.buildConfig(roomId.getRoomId()), isUser); } public boolean check(String roomId) { @@ -56,9 +56,7 @@ public class LiveDanmuService { } public List getDanmuFileList(String roomId) { - BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); - LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId); - configDatabase.close(); + LiveConfigDatabaseBean bean = liveDatabasesService.getConfigDatabase().getConfig(roomId); return Tools.scanFile(new File(bean.getRecordPath() + File.separator + bean.getAnchorName())); } @@ -150,7 +148,7 @@ public class LiveDanmuService { } public static void main(String[] args) { - BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfig("17961")); + BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfigTmp("17961")); for (LiveVideoDatabaseBean info : database.getLiveInfos()) { System.out.println(info); } diff --git a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java index e698215..8bb2ced 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java @@ -1,8 +1,12 @@ package com.yutou.bilibili.services; import com.yutou.biliapi.bean.live.LiveRoomConfig; +import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; +import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; +import lombok.Getter; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.util.HashMap; import java.util.Map; @@ -10,12 +14,33 @@ import java.util.Map; @Service public class LiveDatabasesService { private final Map liveDatabases=new HashMap<>(); + @Getter + private final BiliLiveConfigDatabase configDatabase; + + private LiveDatabasesService() { + configDatabase=new BiliLiveConfigDatabase(); + } public BiliLiveDatabase getLiveDatabase(String roomId) { if(liveDatabases.containsKey(roomId)) { return liveDatabases.get(roomId); } - BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); + BiliLiveDatabase liveDatabase = new BiliLiveDatabase(buildConfig(roomId)); liveDatabases.put(roomId, liveDatabase); return liveDatabase; } + public void closeAll(){ + liveDatabases.forEach((k,v)->v.close()); + configDatabase.close(); + } + + public LiveRoomConfig buildConfig(String roomId){ + LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId); + LiveRoomConfig config = new LiveRoomConfig(); + config.setLoginUid(bean.getRecordUid()); + config.setRoomId(bean.getRoomId()); + config.setAnchorName(bean.getAnchorName()); + config.setLogin(StringUtils.hasText(bean.getRecordUid())); + config.setRootPath(bean.getRecordPath()); + return config; + } } diff --git a/src/main/java/com/yutou/bilibili/services/LiveService.java b/src/main/java/com/yutou/bilibili/services/LiveService.java index f8a1cd4..a3b0c3a 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveService.java @@ -4,31 +4,22 @@ import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.yutou.biliapi.api.LiveApi; import com.yutou.biliapi.bean.live.LiveAnchorInfo; -import com.yutou.biliapi.bean.live.LiveRoomConfig; -import com.yutou.biliapi.bean.live.LiveRoomInfo; -import com.yutou.biliapi.bean.live.LiveRoomPlayInfo; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean; -import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.net.BiliLiveNetApiManager; import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.datas.web.LiveData; -import com.yutou.common.okhttp.BaseBean; import com.yutou.common.okhttp.HttpLoggingInterceptor; -import com.yutou.common.utils.FFmpegUtils; -import com.yutou.common.utils.Log; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import java.io.File; import java.io.IOException; import java.util.*; @Service public class LiveService { - BiliLiveConfigDatabase liveConfigDatabase; @Resource LiveVideoDownloadService videoDownloadService; @Resource @@ -39,16 +30,15 @@ public class LiveService { public LiveService() { - liveConfigDatabase = new BiliLiveConfigDatabase(); api = BiliLiveNetApiManager.getInstance().getApi(null); } public int getConfigCount() { - return liveConfigDatabase.getAllConfig().size(); + return databasesService.getConfigDatabase().getAllConfig().size(); } public List getLiveList(int page, int limit) { - List allConfig = liveConfigDatabase.getAllConfig(); + List allConfig = databasesService.getConfigDatabase().getAllConfig(); List liveDataList = new ArrayList<>(); if (allConfig.isEmpty()) { return liveDataList; diff --git a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java index ab7c1fd..f0d0f8c 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java @@ -281,6 +281,7 @@ public class LiveVideoDownloadService { // .withNotSymbolParam("-threads", "8")//看bili-go也没有加这个,改成设置好了 // .withNotSymbolParam("-bufsize", "10M") .withNotSymbolParam("-f", "segment") + .withNotSymbolParam("-rw_timeout","60000000") .withNotSymbolParam("-segment_time", "60") .withNotSymbolParam("-segment_format", "mpegts") .withNotSymbolParam("-map", "0") @@ -360,13 +361,13 @@ public class LiveVideoDownloadService { //录制弹幕 private void recordDanmu() { - if (bean.isSyncDanmuForLive() && !webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { - webSocketServer.addRoom(LiveRoomConfig.buildConfig(bean.getRoomId()), true); + if (bean.isSyncDanmuForLive() && !webSocketServer.checkRoom(liveDatabasesService.buildConfig(bean.getRoomId()))) { + webSocketServer.addRoom(liveDatabasesService.buildConfig(bean.getRoomId()), true); } } private void stopRecordDanmu() { - if (bean.isSyncDanmuForLive() && webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { + if (bean.isSyncDanmuForLive() && webSocketServer.checkRoom(liveDatabasesService.buildConfig(bean.getRoomId()))) { webSocketServer.stopRoom(bean.getRoomId(), false); } } @@ -377,9 +378,7 @@ public class LiveVideoDownloadService { } public VideoFilePath getVideoPath(String roomId) { - BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); - LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId); - configDatabase.close(); + LiveConfigDatabaseBean bean = liveDatabasesService.getConfigDatabase().getConfig(roomId); return getVideoFilePath(bean); } @@ -428,10 +427,8 @@ public class LiveVideoDownloadService { public String getVideoPlay(String roomId, String videoId) { String ffmpegPath = ConfigTools.load(ConfigTools.CONFIG, "ffmpeg", String.class); - BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); - LiveConfigDatabaseBean config = configDatabase.getConfig(roomId); + LiveConfigDatabaseBean config = liveDatabasesService.getConfigDatabase().getConfig(roomId); String recordPath = config.getRecordPath() + File.separator + config.getAnchorName(); - configDatabase.close(); LiveVideoDatabaseBean videoInfo = null; for (LiveVideoDatabaseBean info : liveDatabasesService.getLiveDatabase(roomId).getLiveInfos()) { if (videoId.trim().equals(String.valueOf(info.getSql_time().getTime()))) { diff --git a/src/main/java/com/yutou/bilibili/services/SystemService.java b/src/main/java/com/yutou/bilibili/services/SystemService.java index e935b6c..7429bd2 100644 --- a/src/main/java/com/yutou/bilibili/services/SystemService.java +++ b/src/main/java/com/yutou/bilibili/services/SystemService.java @@ -22,11 +22,12 @@ public class SystemService { LiveVideoDownloadService videoService; @Resource LiveDanmuService danmuService; + @Resource + LiveDatabasesService databasesService; SystemConfigDatabases databases = new SystemConfigDatabases(); private ScheduledExecutorService timer; private ScheduledFuture scheduled; - BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase(); public long getLoopTimer() { SystemConfigDatabaseBean config = databases.getConfig(); @@ -47,7 +48,7 @@ public class SystemService { scheduled.cancel(true); } scheduled = timer.scheduleAtFixedRate(() -> { - List list = liveConfigDatabase.getAllConfig(); + List list = databasesService.getConfigDatabase().getAllConfig(); Log.i("循环任务:" + list.size()); if (DateFormatUtils.getInstance().checkTime(null, resetTimer)) { videoService.clearUserStopList(); @@ -64,10 +65,6 @@ public class SystemService { } // 如果bean需要录制直播,并且检查录制直播时间 if (bean.isRecordLive() && bean.checkRecordLiveTime()) { - // 如果需要同步直播弹幕,则录制弹幕 - if (bean.isSyncDanmuForLive()) { - recordDanmu(bean); - } // 录制视频 recordVideo(bean); }