This commit is contained in:
shikong 2023-09-06 17:15:43 +08:00
parent 0c62fa0584
commit 111d44b187
2 changed files with 15 additions and 7 deletions

View File

@ -4,10 +4,7 @@ import cn.skcks.docking.gb28181.wvp.service.video.RecordService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RequiredArgsConstructor
@ -16,6 +13,11 @@ import org.springframework.web.bind.annotation.RestController;
public class RecordController {
private final RecordService recordService;
@RequestMapping(method = {RequestMethod.HEAD,RequestMethod.OPTIONS})
public void record(HttpServletResponse response){
recordService.header(response);
}
@GetMapping
public void record(HttpServletResponse response, @RequestParam String url,@RequestParam long time){
recordService.record(response,url,time);

View File

@ -20,12 +20,17 @@ import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
@Service
public class RecordService {
public void header(HttpServletResponse response){
response.setContentType("video/mp4");
response.setHeader("Accept-Ranges","none");
response.setHeader("Connection","close");
}
@SneakyThrows
public void record(HttpServletResponse response, String url, long timeout){
response.reset();
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setContentType("video/mp4");
header(response);
Path tmp = Path.of(System.getProperty("java.io.tmpdir"), IdUtil.getSnowflakeNextIdStr()).toAbsolutePath();
File file = new File(tmp + ".mp4");
@ -57,7 +62,6 @@ public class RecordService {
recorder.record(frame);
}
grabber.stop();
grabber.release();
recorder.stop();
} catch (FFmpegFrameRecorder.Exception | FrameGrabber.Exception e) {
throw new RuntimeException(e);
@ -67,7 +71,9 @@ public class RecordService {
log.info("结束录制");
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
IoUtil.copy(inputStream, outputStream);
try{
IoUtil.copy(inputStream, outputStream);
} catch (Exception ignore){}
log.info("临时文件 {} 写入 响应 完成", file);
log.info("删除临时文件 {} {}", file, file.delete());
}