直播暂停并保存

This commit is contained in:
Klein
2023-08-05 23:34:38 +08:00
parent 9bd5db9486
commit 6cefaa1e4d
5 changed files with 44 additions and 14 deletions

View File

@@ -9,7 +9,7 @@ public class DouyuLiveTest {
public static void main(String[] args) {
TaskManager taskManager = new TaskManager(5);
DouyuLiveConfig douyuLiveConfig = new DouyuLiveConfig("3637778", "C:\\Users\\admin\\Desktop\\douyu\\", "DouyuLiveOnline");
DouyuLiveConfig douyuLiveConfig = new DouyuLiveConfig("3637778", 4000,"C:\\Users\\admin\\Desktop\\douyu\\", "DouyuLiveOnline", true);
try {
// 向任务管理器中添加任务
String taskId = taskManager.addTask(douyuLiveConfig); // 获取任务的标识符
@@ -26,6 +26,7 @@ public class DouyuLiveTest {
System.out.println("已写入数据量:" + taskManager.getDownloadedBytes(taskId) + " bytes");
Thread.sleep(1000);
}
taskManager.terminateThenSave(douyuLiveConfig,taskId);
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -11,24 +11,27 @@ public class FlvHandle {
private static final int RETRY_TIMES = 3;
private static final int BUFFER_SIZE = 64 * 1024; // 64 KB buffer
private volatile boolean shouldTerminate = false;
public void parseStream(InputStream in, StatusMonitor statusMonitor, OutputStream out) {
int retryCount = 0;
while (retryCount < RETRY_TIMES) {
while (retryCount < RETRY_TIMES && !shouldTerminate) {
try (BufferedInputStream input = new BufferedInputStream(in);
BufferedOutputStream output = new BufferedOutputStream(out)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
if (shouldTerminate) {
break;
}
output.write(buffer, 0, bytesRead);
statusMonitor.addDownloadedBytes(bytesRead);
}
break;
} catch (IOException e) {
retryCount++;
if (retryCount < RETRY_TIMES) {
try {
Thread.sleep(1000L * retryCount); // Exponential backoff
Thread.sleep(1000L * retryCount);
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
}
@@ -36,9 +39,20 @@ public class FlvHandle {
statusMonitor.setConnectionClosed(true);
e.printStackTrace();
}
}finally {
try {
in.close();
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
public void terminateDownload() {
shouldTerminate = true;
}
}

View File

@@ -20,10 +20,10 @@ public class LiveStreamTask {
private String url;
private Map<String, String> headers;
private FlvHandle f = new FlvHandle();
public void start(ExecutorService executor, StatusMonitor statusMonitor, OutputStream fileIO) {
executor.execute(() -> {
FlvHandle f = new FlvHandle();
try {
URLConnection conn = new URL(this.url).openConnection();
if (this.headers != null) {
@@ -31,12 +31,17 @@ public class LiveStreamTask {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
InputStream in = conn.getInputStream();
f.parseStream(in, statusMonitor, fileIO);
try (InputStream in = conn.getInputStream()) {
f.parseStream(in, statusMonitor, fileIO);
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
public void terminate() {
f.terminateDownload();
}
}

View File

@@ -53,12 +53,6 @@ public class TaskManager {
Future<?> future = executor.submit(() -> {
task.start(executor, statusMonitor, fileIO);
// 将.flv文件转换为.mp4
if (liveConfig.isConvertToMp4()) {
String flvFilePath = liveConfig.getVideoPath() + liveConfig.getRoomId() + ".flv";
String mp4FilePath = liveConfig.getVideoPath() + liveConfig.getRoomId() + ".mp4";
VideoConverter.convertFlvToMp4(flvFilePath, mp4FilePath);
}
});
futures.put(taskId, future);
@@ -87,6 +81,19 @@ public class TaskManager {
}
}
public void terminateThenSave(LiveConfig liveConfig,String taskId){
LiveStreamTask task = tasks.get(taskId);
task.terminate();
removeTask(taskId);
if (liveConfig.isConvertToMp4()) {
String flvFilePath = liveConfig.getVideoPath() + liveConfig.getRoomId() + ".flv";
String mp4FilePath = liveConfig.getVideoPath() + liveConfig.getRoomId() + ".mp4";
VideoConverter.convertFlvToMp4(flvFilePath, mp4FilePath);
System.out.println("start: flv-->mp4");
}
}
private StatusMonitor getStatusMonitor(String taskId) {
return statusMonitors.get(taskId);
}

View File

@@ -65,7 +65,10 @@ public class DouyuFlvUrlParser implements PlatformVideoUrlParser {
String fileUrl = dataObj.getString("rtmp_live");
if(fileUrl!=null){
String name = fileUrl.substring(0,fileUrl.indexOf("."));
return String.format(flvBaseUrl+"%s.flv",name);
if(name.contains("_")){
return String.format(flvBaseUrl+"%s_%s.xs",name.substring(0,name.indexOf("_")),clarity);
}
return String.format(flvBaseUrl+"%s_%s.xs",name,clarity);
}
}
}