配置 ffmpegRecord 写入临时文件

This commit is contained in:
shikong 2024-03-13 16:53:35 +08:00
parent ef13720181
commit ccbb1ad186
2 changed files with 28 additions and 4 deletions

View File

@ -30,4 +30,6 @@ public class FfmpegConfig {
private Boolean input = false;
private Boolean output = false;
}
public Boolean useTmpFile = true;
}

View File

@ -3,7 +3,9 @@ package cn.skcks.docking.gb28181.wvp.service.video;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.skcks.docking.gb28181.core.sip.utils.SipUtil;
import cn.skcks.docking.gb28181.wvp.config.FfmpegConfig;
import cn.skcks.docking.gb28181.wvp.config.ProxySipConfig;
import cn.skcks.docking.gb28181.wvp.config.WvpProxyConfig;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice;
@ -31,9 +33,7 @@ import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameGrabber;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -50,6 +50,7 @@ public class VideoService {
private final ProxySipConfig proxySipConfig;
private final DockingService dockingService;
private final SipSender sender;
private final FfmpegConfig ffmpegConfig;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
/**
@ -231,7 +232,21 @@ public class VideoService {
*/
@SneakyThrows
public void ffmpegRecord(ServletResponse response, String url, long time, WvpProxyDevice device,String callId){
ServletOutputStream outputStream = response.getOutputStream();
OutputStream outputStream;
String tmpDir = System.getProperty("java.io.tmpdir");
String fileName = callId + ".mp4";
File file = new File(tmpDir, fileName);
if(ffmpegConfig.getUseTmpFile()) {
if(!file.exists()){
log.info("创建临时文件 {}", fileName);
file.createNewFile();
}
outputStream = new FileOutputStream(file);
} else {
outputStream = response.getOutputStream();
}
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
DefaultExecuteResultHandler executeResultHandler = mediaStatus(device,callId);
DateTime startTime = DateUtil.date();
@ -252,6 +267,13 @@ public class VideoService {
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);
boolean delete = file.delete();
log.info("删除临时文件 {} => {}", file, delete);
}
}
/**