This commit is contained in:
shikong 2023-09-06 13:13:20 +08:00
parent 4eb9b9cdd7
commit 16d5bb3ba7

View File

@ -5,7 +5,6 @@ import cn.hutool.core.util.IdUtil;
import cn.skcks.docking.gb28181.media.config.ZlmMediaConfig;
import cn.skcks.docking.gb28181.wvp.config.SwaggerConfig;
import cn.skcks.docking.gb28181.wvp.proxy.ZlmProxyClient;
import feign.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
@ -22,10 +21,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.nio.file.Path;
import static org.bytedeco.ffmpeg.global.avutil.AVMEDIA_TYPE_VIDEO;
@ -49,64 +45,47 @@ public class VideoController {
@ResponseBody
public void video(HttpServletResponse response) throws IOException {
response.reset();
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setContentType("video/mp4");
// File f = new File("E:\\#Camera\\DCIM\\100MEDIA\\20230818\\NORM0003.MP4");
// try (
// BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(f));
// BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream())) {
// IoUtil.copy(inputStream, outputStream);
// }
// try{
Path tmp = Path.of(System.getProperty("java.io.tmpdir"),IdUtil.getSnowflakeNextIdStr()).toAbsolutePath();
File file = new File(tmp +".mp4");
log.info("创建文件 {}, {}",file, file.createNewFile());
try (Response live = zlmProxyClient.live("live", "test.live.mp4")) {
FFmpegLogCallback.set();
// live.body()
// FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(live.body().asInputStream());
Path tmp = Path.of(System.getProperty("java.io.tmpdir"), IdUtil.getSnowflakeNextIdStr()).toAbsolutePath();
File file = new File(tmp + ".mp4");
log.info("创建文件 {}, {}", file, file.createNewFile());
String url = StringUtils.joinWith("/",config.getUrl(), "live","test.live.mp4");
log.info("url {}",url);
try(
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(file,1920,1080,0)
){
grabber.start();
recorder.start();
log.info("开始录像");
log.info("{}",file);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
// recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); //视频源数据yuv
recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); //设置音频压缩方式
recorder.setFormat("mp4");
recorder.setVideoOption("threads", String.valueOf(Runtime.getRuntime().availableProcessors())); //解码线程数
try (Java2DFrameConverter converter = new Java2DFrameConverter()){
Frame frame;
while ((frame = grabber.grab()) != null) {
if(frame.streamIndex == AVMEDIA_TYPE_VIDEO){
recorder.record(frame);
}
String url = StringUtils.joinWith("/", config.getUrl(), "live", "test.live.mp4");
log.info("url {}", url);
try (
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(file, 1920, 1080, 0)
) {
grabber.start();
recorder.start();
log.info("开始录像");
log.info("{}", file);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
// recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); //视频源数据yuv
recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); //设置音频压缩方式
recorder.setFormat("mp4");
recorder.setVideoOption("threads", String.valueOf(Runtime.getRuntime().availableProcessors())); //解码线程数
try {
Frame frame;
while ((frame = grabber.grab()) != null) {
if (frame.streamIndex == AVMEDIA_TYPE_VIDEO) {
recorder.record(frame);
}
grabber.stop();
grabber.release();
recorder.stop();
} catch (FFmpegFrameRecorder.Exception | FrameGrabber.Exception e) {
throw new RuntimeException(e);
}
grabber.stop();
grabber.release();
recorder.stop();
} catch (FFmpegFrameRecorder.Exception | FrameGrabber.Exception e) {
throw new RuntimeException(e);
}
log.info("录像结束");
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
IoUtil.copy(fileInputStream, outputStream);
log.info("临时文件 {} 写入 响应 完成",file);
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
log.info("删除临时文件 {} {}",file,file.delete());
}
log.info("录像结束");
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
IoUtil.copy(inputStream, outputStream);
log.info("临时文件 {} 写入 响应 完成", file);
log.info("删除临时文件 {} {}", file, file.delete());
}
}