完善切片存储

This commit is contained in:
2024-11-27 17:38:45 +08:00
parent b5c2153ad0
commit 48a15c8769
8 changed files with 49 additions and 55 deletions

View File

@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
@@ -109,6 +110,13 @@ public class VideoFileController {
@RequestMapping("/video/play")
@ResponseBody
public JSONObject getVideoUrl(String roomId, String videoId) {
return ResultData.success("/live/"+videoService.getVideoPlay(roomId, videoId));
String url = videoService.getVideoPlay(roomId, videoId);
/* String[] split = url.split("/");
StringBuilder sb=new StringBuilder();
for (String s : split) {
sb.append(URLEncoder.encode(s,StandardCharsets.UTF_8)).append("/");
}
sb.setLength(sb.length()-1);*/
return ResultData.success("/live"+url);
}
}

View File

@@ -59,18 +59,21 @@ public class LiveDanmuService {
public void saveDanmuXML(LiveVideoDatabaseBean videoDatabaseBean, BiliLiveDatabase database) {
File videoFile = new File(videoDatabaseBean.getPath());
long videoTime = FFmpegUtils.getVideoTime(videoFile) + videoDatabaseBean.getStartTime().getTime();
long videoTime;
if (videoDatabaseBean.getStopTime() == null) {
videoTime = System.currentTimeMillis();
} else {
videoTime = videoDatabaseBean.getStopTime().getTime();
}
Log.i("开始时间:" + videoDatabaseBean.getStartTime().getTime());
Log.i("结束时间:" + videoTime);
String startTime = DateFormatUtils.getInstance().format(videoDatabaseBean.getStartTime());
String endTime = DateFormatUtils.getInstance().format(videoTime);
List<LiveDanmuDatabaseBean> danmus = database.getOfTime(startTime, endTime, LiveDanmuDatabaseBean.class);
List<LiveDanmuDatabaseBean> danmus = database.getOfTime(videoDatabaseBean.getStartTime().getTime(), videoTime, LiveDanmuDatabaseBean.class);
Log.i("弹幕数量:" + danmus.size());
AssTools assTools = new AssTools(videoFile.getName().replace(".flv", ""), videoDatabaseBean.getStartTime());
AssTools assTools = new AssTools(videoFile.getName().replace(".m3u8", ""), videoDatabaseBean.getStartTime());
for (LiveDanmuDatabaseBean dm : danmus) {
assTools.addDanmu(dm.createDanmuData());
}
assTools.saveDanmu(videoFile.getAbsolutePath().replace(".flv", ".ass"));
assTools.saveDanmu(videoFile.getAbsolutePath().replace(".m3u8", ".ass"));
}
public String toTimeString(long videoTime) {
@@ -90,19 +93,10 @@ public class LiveDanmuService {
if (videoBean == null) {
return new LiveVideoDanmu();
}
File videoFile = new File(videoBean.getPath().replace(".flv", ".mp4"));
if (!videoFile.exists()) {
videoFile = new File(videoBean.getPath());
}
long videoTime = FFmpegUtils.getVideoTime(videoFile);
long startTime = Long.parseLong(videoId);
long endTime = Long.parseLong(videoId) + videoTime;
// videoTime = 0;
if (videoTime == 0) {
endTime = System.currentTimeMillis();
}
List<LiveDanmuDatabaseBean> danmuList = liveDatabase.getOfTime(DateFormatUtils.getInstance().format(startTime), DateFormatUtils.getInstance().format(endTime), LiveDanmuDatabaseBean.class);
List<LiveSuperChatDatabaseBean> superChatList = liveDatabase.getOfTime(DateFormatUtils.getInstance().format(startTime), DateFormatUtils.getInstance().format(endTime), LiveSuperChatDatabaseBean.class);
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
List<LiveDanmuDatabaseBean> danmuList = liveDatabase.getOfTime(startTime, endTime, LiveDanmuDatabaseBean.class);
List<LiveSuperChatDatabaseBean> superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class);
for (LiveDanmuDatabaseBean bean : danmuList) {
LiveVideoDanmu.Danmu danmu = createDanmu(bean, startTime);
danmus.getDanmu().add(danmu);

View File

@@ -87,7 +87,7 @@ public class LiveService {
if (info.getLiveTime() == 0) {
liveData.setLiveTime("未开播");
} else {
liveData.setLiveTime(DateFormatUtils.getInstance().formatMillis(System.currentTimeMillis() - info.getLiveTime()*1000));
liveData.setLiveTime(DateFormatUtils.getInstance().formatMillis(System.currentTimeMillis() - info.getLiveTime() * 1000));
}
liveData.setDownloadVideo(videoDownloadService.checkDownload(info.getRoomId()));
liveData.setDanmu(danmuService.check(info.getRoomId()));
@@ -113,16 +113,8 @@ public class LiveService {
if (videoBean == null) {
return null;
}
File videoFile = new File(videoBean.getPath().replace(".flv", ".mp4"));
if (!videoFile.exists()) {
videoFile = new File(videoBean.getPath());
}
long videoTime = FFmpegUtils.getVideoTime(videoFile);
long startTime = Long.parseLong(videoId);
long endTime = Long.parseLong(videoId) + videoTime;
if(videoTime==0){
endTime=System.currentTimeMillis();
}
long startTime = videoBean.getStartTime().getTime();
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
return database.getGiftInfo(startTime, endTime);
}

View File

@@ -120,7 +120,7 @@ public class LiveVideoDownloadService {
File rootPath;
LiveConfigDatabaseBean config;
BiliLiveDatabase database;
LiveVideoDatabaseBean videoDatabaseBean;
LiveVideoDatabaseBean videoDatabaseBean = null;
LiveRoomInfo roomInfo;
public VideoTask(LiveConfigDatabaseBean bean, LiveRoomInfo roomInfo) {
@@ -136,8 +136,8 @@ public class LiveVideoDownloadService {
String time = DateUtils.format(new Date().getTime(), DATE_FORMAT_10_DASH);
rootPath = new File(bean.getRecordPath() + File.separator + bean.getAnchorName() + File.separator + time + File.separator + "[" +
DateUtils.format(new Date(),
"yyyy-MM-dd HH-mm-ss") + "]" + roomInfo.getTitle());
savePath = rootPath.getAbsolutePath() + File.separator + roomInfo.getTitle() + "-%04d.ts";
"HH-mm-ss") + "]" + roomInfo.getTitle());
savePath = rootPath.getAbsolutePath() + File.separator + roomInfo.getTitle() + ".m3u8";
if (!rootPath.exists()) {
rootPath.mkdirs();
}
@@ -148,14 +148,14 @@ public class LiveVideoDownloadService {
}
private void stop() {
videoRecord.kill(bean.getRoomId().toString());
api.getRoomInfo(config.getRoomId().toString()).enqueue(new HttpCallback<LiveRoomInfo>() {
videoRecord.kill(bean.getRoomId());
api.getRoomInfo(config.getRoomId()).enqueue(new HttpCallback<LiveRoomInfo>() {
@Override
public void onResponse(Headers headers, int code, String status, LiveRoomInfo response, String rawResponse) {
if (response.getLiveStatus() == 1) {
LiveVideoDownloadService.this.start(bean, false);
} else {
LiveVideoDownloadService.this.stop(bean.getRoomId().toString(), false);
LiveVideoDownloadService.this.stop(bean.getRoomId(), false);
}
}
@@ -289,20 +289,19 @@ public class LiveVideoDownloadService {
// .withNotSymbolParam("-bufsize", "10M")
.withNotSymbolParam("-f", "segment")
.withNotSymbolParam("-segment_time", "60")
.withNotSymbolParam("-segment_format", "flv")
.withParam("-segment_list", rootPath + File.separator + roomInfo.getTitle() + ".m3u8")
.withNotSymbolParam("-segment_format", "mpegts")
.withNotSymbolParam("-map", "0")
.withParam("-segment_list", savePath)
.withNotSymbolParam("-c", "copy")
.withNotSymbolParam("-bsf:a", "aac_adtstoasc")
// .withNotSymbolParam("-loglevel", "debug")
.withNotSymbolParam("-y", "")
//-reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2
// .withNotSymbolParam("-progress",new File("cache",config.getRoomId()+".txt").getAbsolutePath()); //输出进度日志,暂时没啥用
;
if (ck != null) {
// builder = builder.withParam("-cookies", cookie);
}
FFmpegUtils command = builder.build(config.getRoomId(), ffmpegPath, url, savePath);
FFmpegUtils command = builder.build(config.getRoomId(), ffmpegPath, url, savePath.replace(".m3u8","-%04d.ts"));
Log.i(command.getCommandDecode());
try {
command.start(new DownloadInterface() {
@@ -335,6 +334,10 @@ public class LiveVideoDownloadService {
task.cancel();
task = null;
}
if (videoDatabaseBean != null) {
videoDatabaseBean.setStopTime(new Date());
database.addLiveInfo(videoDatabaseBean);
}
}
});
@@ -430,8 +433,8 @@ public class LiveVideoDownloadService {
File videoFile = new File(videoInfo.getPath().replace("-%04d.ts", ".m3u8"));
if (!videoFile.exists()) {
videoFile = new File(videoInfo.getPath());
}else{
return videoInfo.getPath().replace(new File("live").getAbsolutePath(),"").replace(File.separator,"/").replace("-%04d.ts", ".m3u8");
} else {
return videoInfo.getPath().replace(new File("live").getAbsolutePath(), "").replace(File.separator, "/").replace("-%04d.ts", ".m3u8");
}
FFmpegUtils ffmpeg = FFmpegUtils.segment(videoId, ffmpegPath, videoFile, ConfigTools.load(ConfigTools.CONFIG, "outVideoPath", String.class));
System.out.println(ffmpeg.getCommandDecode());