ffmpegRecord调整

This commit is contained in:
shikong 2024-03-14 15:26:57 +08:00
parent 0b3a80bce1
commit 87423c734f
2 changed files with 5 additions and 8 deletions

View File

@ -95,7 +95,8 @@ public class Gb28181DownloadService {
@Qualifier(DefaultVideoExecutor.EXECUTOR_BEAN_NAME)
private final Executor executor;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private final ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(64);
private final ConcurrentMap<String, DeferredResult<JsonResponse<String>>> requestMap = new ConcurrentHashMap<>();
private final RealtimeManager realtimeManager;
@ -368,13 +369,12 @@ public class Gb28181DownloadService {
} else if(videoInfo == null){
writeErrorToResponse(asyncResponse, JsonResponse.error("下载失败"));
} else if(wvpProxyConfig.getUseFfmpeg()){
scheduledExecutorService.submit(()->{
executor.execute(()->{
videoService.ffmpegRecord(asyncResponse, videoInfo.getUrl(), DateUtil.between(startTime,endTime,DateUnit.SECOND) + 15,videoInfo.getDevice(),videoInfo.getCallId());
});
} else {
videoService.javaCVrecord(asyncResponse, videoInfo.getUrl(), DateUtil.between(startTime,endTime,DateUnit.SECOND) + 15);
}
});
} catch(Exception e) {
writeErrorToResponse(asyncResponse, JsonResponse.error(e.getMessage()));

View File

@ -232,14 +232,13 @@ public class VideoService {
*/
@SneakyThrows
public void ffmpegRecord(ServletResponse response, String url, long time, WvpProxyDevice device,String callId){
OutputStream outputStream;
String tmpDir = ffmpegConfig.getTmpDir();
String fileName = callId + ".mp4";
File file = new File(tmpDir, fileName);
Executor executor;
DefaultExecuteResultHandler executeResultHandler = mediaStatus(device,callId);
if(ffmpegConfig.getUseTmpFile()) {
outputStream = new PipedOutputStream();
OutputStream outputStream = new PipedOutputStream();
String filePath = file.getAbsolutePath();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
if(proxySipConfig.isUsePlaybackToDownload()){
@ -248,7 +247,7 @@ public class VideoService {
executor = ffmpegSupportService.downloadToStream(url, filePath, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
}
} else {
outputStream = response.getOutputStream();
OutputStream outputStream = response.getOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
if(proxySipConfig.isUsePlaybackToDownload()){
executor = ffmpegSupportService.playbackToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
@ -268,13 +267,11 @@ public class VideoService {
schedule.cancel(true);
DateTime endTime = DateUtil.date();
log.info("录制进程结束 {}, 录制耗时: {}", url, DateUtil.between(startTime,endTime, DateUnit.SECOND));
outputStream.close();
if(ffmpegConfig.getUseTmpFile()) {
ServletOutputStream servletOutputStream = response.getOutputStream();
IoUtil.copy(new FileInputStream(file), servletOutputStream);
response.flushBuffer();
servletOutputStream.close();
System.gc();
boolean delete = file.delete();
log.info("删除临时文件 {} => {}", file, delete);