diff --git a/src/main/java/com/tuoheng/steam/service/TaskService.java b/src/main/java/com/tuoheng/steam/service/TaskService.java index 0fb5a64..119d6dc 100644 --- a/src/main/java/com/tuoheng/steam/service/TaskService.java +++ b/src/main/java/com/tuoheng/steam/service/TaskService.java @@ -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(); } diff --git a/src/main/java/com/tuoheng/steam/service/innerService/ProcessService.java b/src/main/java/com/tuoheng/steam/service/innerService/ProcessService.java index 6057b92..c5d9acb 100644 --- a/src/main/java/com/tuoheng/steam/service/innerService/ProcessService.java +++ b/src/main/java/com/tuoheng/steam/service/innerService/ProcessService.java @@ -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); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index df25245..7a1575a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file +#srs.targetPath=/Users/sunpeng/workspace/stream/html +#ffmpeg=ffmpeg \ No newline at end of file