改为 flv 格式 实时流下载 不再需要临时文件转储

This commit is contained in:
shikong 2023-09-07 11:37:15 +08:00
parent 951bffea4e
commit af40be890e

View File

@ -11,11 +11,11 @@ import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FFmpegLogCallback;
import org.bytedeco.javacv.FrameGrabber;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -48,19 +48,15 @@ public class RecordService {
@SneakyThrows
public void record(ServletResponse response, String url, long time) {
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// Path tmp = Path.of(System.getProperty("java.io.tmpdir"), IdUtil.getSnowflakeNextIdStr()).toAbsolutePath();
// File file = new File(tmp + ".mp4");
// log.info("创建文件 {}, {}", file, file.createNewFile());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStream outputStream = response.getOutputStream();
log.info("url {}", url);
FFmpegLogCallback.set();
// FFmpeg 调试日志
// FFmpegLogCallback.set();
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(stream, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
// log.info("{}", file);
recorder.setInterleaved(true);
recorder.setVideoOption("preset", "ultrafast");
recorder.setVideoOption("tune", "zerolatency");
@ -104,7 +100,7 @@ public class RecordService {
recorder.close();
} catch (FFmpegFrameRecorder.Exception | FrameGrabber.Exception e) {
throw new RuntimeException(e);
}
} catch (IOException ignore){}
log.info("结束录制");
outputStream.close();