优化下载

This commit is contained in:
2024-11-26 17:47:27 +08:00
parent 44d23b6bac
commit d9919f4d26
10 changed files with 129 additions and 49 deletions

View File

@@ -36,6 +36,7 @@ public class LiveVideoController {
public JSONObject startDownload(String roomId) {
BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase();
List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig();
liveConfigDatabase.close();
for (LiveConfigDatabaseBean bean : list) {
if (bean.getRoomId().toString().equals(roomId)) {
videoService.start(bean, true);

View File

@@ -62,6 +62,9 @@ public class DateFormatUtils {
public Date parse(String date, String format) {
try {
if(date.startsWith("1")){
return new Date(Long.parseLong(date));
}
return getFormat(format).parse(date);
} catch (ParseException e) {
System.err.println("Error parsing date: " + e.getMessage());

View File

@@ -1,9 +1,15 @@
package com.yutou.bilibili.Tools;
import com.yutou.common.utils.AppTools;
import com.yutou.common.utils.Base64Tools;
import com.yutou.common.utils.Log;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
public class ProcessUtils {
@@ -31,35 +37,46 @@ public class ProcessUtils {
if (pid == null) {
return false;
}
String command;
String[] command;
if (isRuntimeSystemOfWindow()) {
// Windows 操作系统
command = "tasklist /FI \"PID eq " + pid + "\"";
command = new String[]{"cmd.exe", "/c", "tasklist /FI \"PID eq " + pid + "\""};
} else {
// Unix/Linux 操作系统
command = "ps -p " + pid;
command = new String[]{"sh", "-c", "ps -p " + pid};
}
Process process = exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(Long.toString(pid))) {
return true;
Process process = Runtime.getRuntime().exec(command);
boolean isRunning = false;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(String.valueOf(pid))) {
isRunning = true;
}
}
reader.close();
process.destroy();
} catch (Exception e) {
Log.e(e);
}
return false;
return isRunning;
}
public static Process exec(String exec) throws Exception {
if(isRuntimeSystemOfWindow()){
return exec("cmd.exe","/c",exec);
}else{
return exec("sh","-c",exec);
if (isRuntimeSystemOfWindow()) {
return exec("cmd.exe", "/c", exec);
} else {
return exec("sh", "-c", exec);
}
}
public static Process exec(String... command) throws Exception {
ProcessBuilder pb;
pb = new ProcessBuilder(command);
List<String> list = Arrays.stream(command).map(Base64Tools::decode).toList();
pb = new ProcessBuilder(list);
return pb.start();
}

View File

@@ -18,9 +18,11 @@ import com.yutou.biliapi.enums.LiveVideoCodec;
import com.yutou.biliapi.enums.LiveVideoDefinition;
import com.yutou.biliapi.enums.LiveVideoFormat;
import com.yutou.biliapi.net.BiliLiveNetApiManager;
import com.yutou.biliapi.net.WebSignManager;
import com.yutou.bilibili.Tools.DateFormatUtils;
import com.yutou.bilibili.Tools.FileServerUtils;
import com.yutou.bilibili.Tools.LiveInfoNfoTools;
import com.yutou.bilibili.Tools.ProcessUtils;
import com.yutou.bilibili.datas.VideoFilePath;
import com.yutou.bilibili.interfaces.DownloadInterface;
import com.yutou.common.okhttp.HttpCallback;
@@ -36,6 +38,7 @@ import org.springframework.util.StringUtils;
import java.io.File;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -78,13 +81,13 @@ public class LiveVideoDownloadService {
VideoTask task = new VideoTask(bean, response);
executor.execute(task);
} else {
Log.i("移除下载");
Log.i(bean.getRoomId(), "没有开播");
}
}
@Override
public void onFailure(Throwable throwable) {
Log.i("移除下载");
Log.e(throwable, "移除下载");
}
});
@@ -195,8 +198,29 @@ public class LiveVideoDownloadService {
LiveVideoDefinition.ORIGINAL.getValue()).enqueue(new HttpCallback<LiveRoomPlayInfo>() {
@Override
public void onResponse(Headers headers, int code, String status, LiveRoomPlayInfo response, String rawResponse) {
LiveRoomPlayInfo.Codec codec = response.getPlayurlInfo().getPlayurl().getStream().get(0).getFormat().get(0).getCodec().get(0);
String url = codec.getUrlInfo().get(0).getHost() + codec.getBaseUrl() + codec.getUrlInfo().get(0).getExtra();
Random random = new Random();
List<LiveRoomPlayInfo.Stream> streams = response.getPlayurlInfo().getPlayurl().getStream();
List<LiveRoomPlayInfo.Stream> streamList = streams.stream()
.filter(it -> "http_stream".equals(it.getProtocolName()))
.toList();
LiveRoomPlayInfo.Stream stream = streamList.get(random.nextInt(streamList.size()));
if (stream == null) {
return;
}
List<LiveRoomPlayInfo.Format> formats = stream.getFormat().stream().filter(it -> "flv".equals(it.getFormatName())).toList();
LiveRoomPlayInfo.Format format = formats.get(random.nextInt(formats.size()));
List<LiveRoomPlayInfo.Codec> codecs = format.getCodec().stream().filter(item -> "avc".equals(item.getCodecName())).toList();
LiveRoomPlayInfo.Codec codec = codecs.get(random.nextInt(codecs.size()));
int urlIndex = random.nextInt(codec.getUrlInfo().size());
LiveRoomPlayInfo.UrlInfo urlInfo = codec.getUrlInfo().get(urlIndex);
String url = urlInfo.getHost() + codec.getBaseUrl() + urlInfo.getExtra();
Log.i("下载直播",rawResponse,codec.toString(),urlInfo.toString(),"URL:"+url);
if (bean.getRecordLiveModel() == 1) {
javaRecord(url, response);
@@ -253,32 +277,46 @@ public class LiveVideoDownloadService {
FFmpegUtils.Builder builder = new FFmpegUtils.Builder()
.withParam("-user_agent", ConfigTools.getUserAgent())
.withParam("-headers", "Referer: https://live.bilibili.com")
.withParam("-headers", "Referer: https://live.bilibili.com/" + playInfo.getRoomId())
// .withNotSymbolParam("-reconnect", "1")
// .withNotSymbolParam("-reconnect_at_eof", "1")
// .withNotSymbolParam("-reconnect_streamed", "1")
// .withNotSymbolParam("-reconnect_delay_max", "2")
// .withNotSymbolParam("-loglevel", "error")
// .withNotSymbolParam("-progress", "-")
// .withNotSymbolParam("-fflags", "+genpts")
.withNotSymbolParam("-threads", "8")
.withNotSymbolParam("-c:v", "copy")
.withNotSymbolParam("-fflags", "+genpts")
.withNotSymbolParam("-bufsize", "10M")
.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("-reconnect", "1")
.withNotSymbolParam("-reconnect_at_eof", "1")
.withNotSymbolParam("-reconnect_streamed", "1")
.withNotSymbolParam("-reconnect_delay_max", "2")
.withNotSymbolParam("-loglevel", "error")
// .withNotSymbolParam("-progress",new File("cache",config.getRoomId()+".txt").getAbsolutePath()); //输出进度日志,暂时没啥用
;
if (ck != null) {
builder = builder.withParam("-cookies", cookie);
// builder = builder.withParam("-cookies", cookie);
}
FFmpegUtils command = builder.build(config.getRoomId(), ffmpegPath, url, savePath);
Log.i(command.getCommand());
Log.i(command.getCommandDecode());
try {
command.start(new DownloadInterface() {
TimerTask task = null;
@Override
public void onDownloadStart() {
super.onDownloadStart();
VideoTask.this.onStart();
Log.i("启动录制:" + playInfo.getRoomId());
task = new TimerTask() {
@Override
public void run() {
VideoTask.this.onStart();
Log.i("启动录制:" + playInfo.getRoomId());
task = null;
cancel();
}
};
new Timer().schedule(task, 5 * 1000);
}
@Override
@@ -289,6 +327,10 @@ public class LiveVideoDownloadService {
@Override
public void onDownload(File file) {
super.onDownload(file);
if (task != null) {
task.cancel();
task = null;
}
}
});
@@ -386,7 +428,7 @@ public class LiveVideoDownloadService {
videoFile = new File(videoInfo.getPath());
}
FFmpegUtils ffmpeg = FFmpegUtils.segment(videoId, ffmpegPath, videoFile, ConfigTools.load(ConfigTools.CONFIG, "outVideoPath", String.class));
System.out.println(ffmpeg.getCommand());
System.out.println(ffmpeg.getCommandDecode());
ffmpeg.start(new DownloadInterface() {
@Override
public void onDownload(File file) {