视频流 录制到临时文件 后 再写入响应

This commit is contained in:
shikong 2023-09-06 10:59:10 +08:00
parent 6266f0c7d1
commit 4eb9b9cdd7
2 changed files with 19 additions and 52 deletions

View File

@ -15,7 +15,7 @@ mirror.id maven-default-http-blocker 部分改为
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
</mirror>
```
### 项目打包
#### 打包 为 jar

View File

@ -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<File> 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();
}
}