video 测试改为 拉flv 不支持 mp4
This commit is contained in:
parent
dc56cbd65e
commit
bd9e15f232
@ -1,18 +1,15 @@
|
||||
package cn.skcks.docking.gb28181.wvp.api;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
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 cn.skcks.docking.gb28181.wvp.service.video.RecordService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bytedeco.ffmpeg.global.avcodec;
|
||||
import org.bytedeco.javacv.*;
|
||||
import org.springdoc.core.models.GroupedOpenApi;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -21,19 +18,14 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.bytedeco.ffmpeg.global.avutil.AVMEDIA_TYPE_VIDEO;
|
||||
|
||||
@Slf4j
|
||||
@Tag(name = "视频api")
|
||||
@Controller
|
||||
@RequestMapping("/video")
|
||||
@RequiredArgsConstructor
|
||||
public class VideoController {
|
||||
private final ZlmProxyClient zlmProxyClient;
|
||||
private final ZlmMediaConfig config;
|
||||
private final RecordService recordService;
|
||||
|
||||
@Bean
|
||||
public GroupedOpenApi videoApi() {
|
||||
@ -43,49 +35,9 @@ public class VideoController {
|
||||
@Operation(summary = "获取视频")
|
||||
@GetMapping(produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
||||
@ResponseBody
|
||||
public void video(HttpServletResponse response) throws IOException {
|
||||
response.reset();
|
||||
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||
response.setContentType("video/mp4");
|
||||
|
||||
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");
|
||||
public void video(HttpServletRequest request, HttpServletResponse response) {
|
||||
String url = StringUtils.joinWith("/", config.getUrl(), "live", "test.live.flv");
|
||||
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);
|
||||
}
|
||||
}
|
||||
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());
|
||||
recordService.record(request,response,url,15);
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class RecordService {
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void record(ServletResponse response, String url, long time) {
|
||||
private void record(ServletResponse response, String url, long time) {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
OutputStream outputStream = response.getOutputStream();
|
||||
log.info("url {}", url);
|
||||
@ -64,7 +64,7 @@ public class RecordService {
|
||||
recorder.setVideoOption("crf", "25");
|
||||
recorder.setFrameRate(grabber.getFrameRate());
|
||||
recorder.setSampleRate(grabber.getSampleRate());
|
||||
recorder.setOption("flvflags","no_duration_filesize");
|
||||
recorder.setOption("flvflags", "no_duration_filesize");
|
||||
if (grabber.getAudioChannels() > 0) {
|
||||
recorder.setAudioChannels(grabber.getAudioChannels());
|
||||
recorder.setAudioBitrate(grabber.getAudioBitrate());
|
||||
|
Loading…
Reference in New Issue
Block a user