package com.yutou.bilibili.services; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.databases.SystemConfigDatabases; import com.yutou.bilibili.datas.SystemConfigDatabaseBean; import com.yutou.common.utils.Log; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @Service public class SystemService { @Resource LiveVideoDownloadService videoService; @Resource LiveDanmuService danmuService; @Resource LiveDatabasesService databasesService; SystemConfigDatabases databases = new SystemConfigDatabases(); private ScheduledExecutorService timer; private ScheduledFuture scheduled; public long getLoopTimer() { SystemConfigDatabaseBean config = databases.getConfig(); if (config == null) { return databases.getDefaultConfig().getTimerLoop(); } return config.getTimerLoop(); } public void start() { if (timer == null) { timer = Executors.newScheduledThreadPool(1); } Log.i("执行任务"); if (scheduled != null) { scheduled.cancel(true); } scheduled = timer.scheduleAtFixedRate(() -> { List list = databasesService.getConfigDatabase().getAllConfig(); Log.i("循环任务:" + list.size()); for (LiveConfigDatabaseBean bean : list) { try { // 如果bean需要录制弹幕,并且检查录制弹幕时间,并且不需要同步直播弹幕,则录制弹幕 if (bean.isRecordDanmu() && bean.checkRecordDanmuTime() && !bean.isSyncDanmuForLive()) { recordDanmu(bean); } else if (!bean.checkRecordDanmuTime() && !bean.isSyncDanmuForLive()) { // 如果不在录制弹幕时间,并且不需要同步直播弹幕,则停止录制弹幕 stopRecordDanmu(bean); } // 如果bean需要录制直播,并且检查录制直播时间 if (bean.isRecordLive() && bean.checkRecordLiveTime()) { // 录制视频 recordVideo(bean); } } catch (Exception e) { Log.e(e); } } }, 0, getLoopTimer(), TimeUnit.MILLISECONDS); } private void stopRecordDanmu(LiveConfigDatabaseBean bean) { if (!videoService.checkDownload(bean.getRoomId())) { danmuService.stop(bean.getRoomId(), false); } } public void stop() { scheduled.cancel(true); videoService.stopAll(); } // 录制弹幕 private void recordDanmu(LiveConfigDatabaseBean bean) { danmuService.start(bean, false); } // 录制视频 private void recordVideo(LiveConfigDatabaseBean bean) { videoService.start(bean, false); } public static void main(String[] args) throws InterruptedException { SystemService service = new SystemService(); Log.i(new Date()); service.start(); Thread.sleep(15000); Log.i(new Date()); service.start(); } }