From 4eb9b9cdd71cb75cf5631edb078917dfa946e0f4 Mon Sep 17 00:00:00 2001
From: shikong <919411476@qq.com>
Date: Wed, 6 Sep 2023 10:59:10 +0800
Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=B5=81=20=E5=BD=95?=
=?UTF-8?q?=E5=88=B6=E5=88=B0=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=20?=
=?UTF-8?q?=E5=90=8E=20=E5=86=8D=E5=86=99=E5=85=A5=E5=93=8D=E5=BA=94?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 +-
.../gb28181/wvp/api/VideoController.java | 69 +++++--------------
2 files changed, 19 insertions(+), 52 deletions(-)
diff --git a/README.md b/README.md
index e6acf5a..f764259 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ mirror.id maven-default-http-blocker 部分改为
Pseudo repository to mirror external repositories initially using HTTP.
http://0.0.0.0/
true
-
+
```
### 项目打包
#### 打包 为 jar
diff --git a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/VideoController.java b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/VideoController.java
index a75372b..39078b8 100644
--- a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/VideoController.java
+++ b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/VideoController.java
@@ -13,19 +13,19 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bytedeco.ffmpeg.global.avcodec;
-import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
-import java.awt.image.BufferedImage;
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.nio.file.Path;
import static org.bytedeco.ffmpeg.global.avutil.AVMEDIA_TYPE_VIDEO;
@@ -47,8 +47,9 @@ public class VideoController {
@Operation(summary = "获取视频")
@GetMapping(produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
- public ResponseEntity video(HttpServletResponse response) throws IOException {
+ public void video(HttpServletResponse response) throws IOException {
response.reset();
+// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setContentType("video/mp4");
// File f = new File("E:\\#Camera\\DCIM\\100MEDIA\\20230818\\NORM0003.MP4");
// try (
@@ -62,18 +63,18 @@ public class VideoController {
File file = new File(tmp +".mp4");
log.info("创建文件 {}, {}",file, file.createNewFile());
try (Response live = zlmProxyClient.live("live", "test.live.mp4")) {
- // FFmpegLogCallback.set();
+ FFmpegLogCallback.set();
// live.body()
// FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(live.body().asInputStream());
-
String url = StringUtils.joinWith("/",config.getUrl(), "live","test.live.mp4");
log.info("url {}",url);
- FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
- grabber.start();
-
- try(FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(file,1920,1080,0)){
+ try(
+ FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
+ FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(file,1920,1080,0)
+ ){
+ grabber.start();
recorder.start();
log.info("开始录像");
log.info("{}",file);
@@ -83,23 +84,11 @@ public class VideoController {
recorder.setFormat("mp4");
recorder.setVideoOption("threads", String.valueOf(Runtime.getRuntime().availableProcessors())); //解码线程数
try (Java2DFrameConverter converter = new Java2DFrameConverter()){
-
- // 建议在线程中使用该方法
Frame frame;
-
- // 此处仅为本地预览
- // CanvasFrame cframe = new CanvasFrame("欢迎来到直播间", CanvasFrame.getDefaultGamma() /
- // grabber.getGamma());
- // cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗口关闭,则程序关闭
- // cframe.setAlwaysOnTop(true);
-
while ((frame = grabber.grab()) != null) {
if(frame.streamIndex == AVMEDIA_TYPE_VIDEO){
- // BufferedImage bufferedImage = converter.getBufferedImage(frame);
- // frame.image = converter.getFrame(bufferedImage).image;
recorder.record(frame);
}
- // cframe.showImage(frame);
}
grabber.stop();
grabber.release();
@@ -109,37 +98,15 @@ public class VideoController {
}
}
log.info("录像结束");
- // response.getOutputStream().close();
- // } finally {
- // log.info("临时文件 {} 写入完成",file);
- // FileInputStream fileInputStream = new FileInputStream(file);
- // OutputStream outputStream = response.getOutputStream();
- // IoUtil.copy(fileInputStream, outputStream);
- // log.info("临时文件 {} 写入 响应 完成",file);
- // fileInputStream.close();
-
- return ResponseEntity.ok().body(file);
-
- // InputStream inputStream = live.body().asInputStream();
- // Path tmp = Path.of(System.getProperty("java.io.tmpdir"),IdUtil.getSnowflakeNextIdStr()).toAbsolutePath();
- // File file = File.createTempFile(tmp.toString(),".mp4");
- // log.info("临时文件 {}", file);
- // FileOutputStream fileOutputStream = new FileOutputStream(file);
- // BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
- // IoUtil.copy(inputStream, bufferedOutputStream);
- // log.info("临时文件 {} 写入完成",file);
- // FileInputStream fileInputStream = new FileInputStream(file);
- // BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
- // OutputStream outputStream = response.getOutputStream();
- // IoUtil.copy(bufferedInputStream, outputStream);
- // log.info("临时文件 {} 写入 响应 完成",file);
- // fileInputStream.close();
- // file.delete();
+ 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 {
- file.deleteOnExit();
+ log.info("删除临时文件 {} {}",file,file.delete());
}
- // return ResponseEntity.noContent().build();
}
}