稳定版本

This commit is contained in:
孙小云 2025-02-12 16:20:45 +08:00
parent a5fd6d8138
commit 35edf66b6d
3 changed files with 57 additions and 24 deletions

View File

@ -30,6 +30,10 @@ public class TaskService implements ITaskService{
@Value("${srs.targetPath}")
private String targetPath;
@Value("${ffmpeg}")
private String ffmpeg;
ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
/**
* 清除没有被关闭的进程
@ -193,31 +197,54 @@ public class TaskService implements ITaskService{
}
if( historyTasks.containsKey(streamUrl)){
historyTasks.get(streamUrl).add(currentStreamTask);
}else {
historyTasks.put(streamUrl, new LinkedList<>());
historyTasks.get(streamUrl).offerLast(currentStreamTask);
}
// if( historyTasks.containsKey(streamUrl)){
// historyTasks.get(streamUrl).add(currentStreamTask);
// }else {
// historyTasks.put(streamUrl, new LinkedList<>());
// historyTasks.get(streamUrl).offerLast(currentStreamTask);
// }
if(Objects.nonNull(currentStreamTask.getMergeProcess())){
if(Objects.nonNull(currentStreamTask.getMergeProcess().getFileName()) &&
!currentStreamTask.getMergeProcess().getFileName().isEmpty()){
try {
Process process = Runtime.getRuntime().exec("/data/ffmpeg/bin/ffmpeg -i " +
currentStreamTask.getMergeProcess().getFileName());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("Duration")) {
System.out.println("line: " + line);
String duration = line.split("Duration: ")[1].split(",")[0];
System.out.println("Duration: " + duration.substring(0, 8));
currentStreamTask.setDuration(duration.substring(0, 8));
break;
//
// scheduler.schedule(task, 3, TimeUnit.SECONDS);
executor.schedule(new Runnable() {
@Override
public void run() {
try {
System.out.println("FileName :"+currentStreamTask.getMergeProcess().getFileName());
Process process = Runtime.getRuntime().exec(ffmpeg + " -i " +
currentStreamTask.getMergeProcess().getFileName());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Duration line: " + line);
if (line.contains("Duration")) {
String duration = line.split("Duration: ")[1].split(",")[0];
System.out.println("Duration: " + duration.substring(0, 8));
currentStreamTask.setDuration(duration.substring(0, 8));
System.out.println("-----------------放入缓存-----------------");
/**
* 放入缓存
*/
if( historyTasks.containsKey(streamUrl)){
historyTasks.get(streamUrl).add(currentStreamTask);
}else {
historyTasks.put(streamUrl, new LinkedList<>());
historyTasks.get(streamUrl).offerLast(currentStreamTask);
}
break;
}
}
reader.close();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
reader.close();
},40, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -23,6 +23,9 @@ public class ProcessService {
@Value("${srs.splitPath}")
private String splitPath;
@Value("${ffmpeg}")
private String ffmpeg;
@Value("${srs.targetPath}")
private String targetPath;
@ -33,12 +36,11 @@ public class ProcessService {
*/
ExecutorService loggingService = Executors.newCachedThreadPool();
public StreamProcess recordStream(String streamUrl) throws IOException {
String recordFileName = splitPath+'/'+UUID.randomUUID().toString() +".ts";
String command = String.format(
"/data/ffmpeg/bin/ffmpeg -i %s -c copy -f mpegts -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 2 %s",
ffmpeg+ " -i %s -c copy -f mpegts -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 2 %s",
streamUrl, recordFileName);
logger.info("recordStream {}", command);
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
@ -82,12 +84,12 @@ public class ProcessService {
outFileName = targetPath + "/" +outFileName;
if(fileList.size() == 1){
command = String.format(
"/data/ffmpeg/bin/ffmpeg -i %s -c copy %s",
ffmpeg+ " -i %s -c copy %s",
fileList.get(0), outFileName);
}else {
String filePaths = String.join("|", fileList);
command = String.format(
"/data/ffmpeg/bin/ffmpeg -i \"concat:%s\" -c copy %s",
ffmpeg+ " -i \"concat:%s\" -c copy %s",
filePaths, outFileName);
}
logger.info("mergeStream {}", command);

View File

@ -2,5 +2,9 @@ spring.application.name=demo
server.port = 8989
srs.splitPath=/data/java/srs/stream_server/temp
srs.targetPath=/data/java/srs/srs/trunk/objs/nginx/html
ffmpeg=/data/ffmpeg/bin/ffmpeg
#server.port = 8080
#srs.splitPath=/Users/sunpeng/workspace/stream/temp
#srs.targetPath=/Users/sunpeng/workspace/stream/html
#srs.targetPath=/Users/sunpeng/workspace/stream/html
#ffmpeg=ffmpeg