配置 ffmpegRecord 写入临时文件

This commit is contained in:
shikong 2024-03-13 17:14:27 +08:00
parent ccbb1ad186
commit 5611bbbe38
2 changed files with 37 additions and 21 deletions

View File

@ -18,13 +18,31 @@ public class FfmpegSupportService {
private final FfmpegConfig ffmpegConfig;
@SneakyThrows
public Executor downloadToStream(String input, long time, TimeUnit unit, ExecuteStreamHandler streamHandler, ExecuteResultHandler executeResultHandler) {
public Executor downloadToStream(String input, String out, long time, TimeUnit unit, ExecuteStreamHandler streamHandler, ExecuteResultHandler executeResultHandler) {
FfmpegConfig.Rtp rtp = ffmpegConfig.getRtp();
FfmpegConfig.Debug debug = ffmpegConfig.getDebug();
String inputParam = debug.getDownload() ? rtp.getDownload() : StringUtils.joinWith(" ", rtp.getDownload(), input);
log.info("视频输入参数 {}", inputParam);
String outputParam = debug.getOutput() ? rtp.getOutput() : StringUtils.joinWith(" ", rtp.getOutput(), "-");
String outputParam = debug.getOutput() ? rtp.getOutput() : StringUtils.joinWith(" ", rtp.getOutput(), out);
log.info("视频输出参数 {}", outputParam);
return ffmpegExecutor(inputParam, outputParam, time, unit, streamHandler, executeResultHandler);
}
@SneakyThrows
public Executor downloadToStream(String input, long time, TimeUnit unit, ExecuteStreamHandler streamHandler, ExecuteResultHandler executeResultHandler) {
return downloadToStream(input, "-", time, unit, streamHandler, executeResultHandler);
}
@SneakyThrows
public Executor playbackToStream(String input, String out, long time, TimeUnit unit, ExecuteStreamHandler streamHandler, ExecuteResultHandler executeResultHandler){
FfmpegConfig.Rtp rtp = ffmpegConfig.getRtp();
FfmpegConfig.Debug debug = ffmpegConfig.getDebug();
String inputParam = debug.getInput() ? rtp.getInput() : StringUtils.joinWith(" ", rtp.getInput(), input);
log.info("视频输入参数 {}", inputParam);
String outputParam = debug.getOutput() ? rtp.getOutput() : StringUtils.joinWith(" ", rtp.getOutput(), out);
log.info("视频输出参数 {}", outputParam);
return ffmpegExecutor(inputParam, outputParam, time, unit, streamHandler, executeResultHandler);
@ -32,15 +50,7 @@ public class FfmpegSupportService {
@SneakyThrows
public Executor playbackToStream(String input, long time, TimeUnit unit, ExecuteStreamHandler streamHandler, ExecuteResultHandler executeResultHandler) {
FfmpegConfig.Rtp rtp = ffmpegConfig.getRtp();
FfmpegConfig.Debug debug = ffmpegConfig.getDebug();
String inputParam = debug.getInput() ? rtp.getInput() : StringUtils.joinWith(" ", rtp.getInput(), input);
log.info("视频输入参数 {}", inputParam);
String outputParam = debug.getOutput() ? rtp.getOutput() : StringUtils.joinWith(" ", rtp.getOutput(), "-");
log.info("视频输出参数 {}", outputParam);
return ffmpegExecutor(inputParam, outputParam, time, unit, streamHandler, executeResultHandler);
return playbackToStream(input, "-", time, unit, streamHandler, executeResultHandler);
}
@SneakyThrows

View File

@ -236,26 +236,32 @@ public class VideoService {
String tmpDir = System.getProperty("java.io.tmpdir");
String fileName = callId + ".mp4";
File file = new File(tmpDir, fileName);
Executor executor;
DefaultExecuteResultHandler executeResultHandler = mediaStatus(device,callId);
if(ffmpegConfig.getUseTmpFile()) {
if(!file.exists()){
log.info("创建临时文件 {}", fileName);
file.createNewFile();
}
outputStream = new FileOutputStream(file);
outputStream = new PipedOutputStream();
String filePath = file.getAbsolutePath();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
if(proxySipConfig.isUsePlaybackToDownload()){
executor = ffmpegSupportService.playbackToStream(url, filePath, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
} else {
executor = ffmpegSupportService.downloadToStream(url, filePath, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
}
} else {
outputStream = response.getOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
if(proxySipConfig.isUsePlaybackToDownload()){
executor = ffmpegSupportService.playbackToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
} else {
executor = ffmpegSupportService.downloadToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
}
}
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
DefaultExecuteResultHandler executeResultHandler = mediaStatus(device,callId);
DateTime startTime = DateUtil.date();
Executor executor;
if(proxySipConfig.isUsePlaybackToDownload()){
executor = ffmpegSupportService.playbackToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
} else {
executor = ffmpegSupportService.downloadToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler);
}
log.info("开始录制 {}", url);
ScheduledFuture<?> schedule = scheduledExecutorService.schedule(() -> {
log.info("到达结束时间, 结束录制 {}", url);