Commit 2adf245989f6939ff997885e9e1ee08eae4adde1

Authored by 648540858
1 parent a0aaf5ab

优化推流鉴权对后续录像查询的影响

src/main/java/top/panll/assist/config/StartConfig.java
... ... @@ -73,7 +73,7 @@ public class StartConfig implements CommandLineRunner {
73 73 File[] files = dateFile.listFiles();
74 74 if (files != null && files.length > 0) {
75 75 for (File file : files) {
76   - videoFileService.handFile(file);
  76 + videoFileService.handFile(file, appFile.getName(), streamFile.getName());
77 77 }
78 78 }
79 79 }
... ...
src/main/java/top/panll/assist/controller/RecordController.java
... ... @@ -18,6 +18,7 @@ import top.panll.assist.dto.SignInfo;
18 18 import top.panll.assist.dto.SpaceInfo;
19 19 import top.panll.assist.service.VideoFileService;
20 20 import top.panll.assist.utils.PageInfo;
  21 +import top.panll.assist.utils.RedisUtil;
21 22  
22 23 import javax.servlet.http.HttpServletRequest;
23 24 import java.io.File;
... ... @@ -38,6 +39,9 @@ public class RecordController {
38 39 @Autowired
39 40 private VideoFileService videoFileService;
40 41  
  42 + @Autowired
  43 + private RedisUtil redisUtil;
  44 +
41 45 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
42 46  
43 47  
... ... @@ -390,9 +394,11 @@ public class RecordController {
390 394 ret.put("code", 0);
391 395 ret.put("msg", "success");
392 396 String file_path = json.getString("file_path");
  397 + String app = json.getString("app");
  398 + String stream = json.getString("stream");
393 399 logger.debug("ZLM 录制完成,参数:" + file_path);
394 400 if (file_path == null) return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
395   - videoFileService.handFile(new File(file_path));
  401 + videoFileService.handFile(new File(file_path), app, stream);
396 402  
397 403 return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
398 404 }
... ... @@ -432,4 +438,22 @@ public class RecordController {
432 438 ret.put("data", duration);
433 439 return new ResponseEntity<>(ret.toString(), HttpStatus.OK);
434 440 }
  441 +
  442 + /**
  443 + * 增加推流的鉴权信息,用于录像存储使用
  444 + */
  445 + @ApiOperation("增加推流的鉴权信息")
  446 + @ApiImplicitParams({
  447 + @ApiImplicitParam(name="app", value = "应用名", required = true, dataTypeClass = String.class),
  448 + @ApiImplicitParam(name="stream", value = "流ID", required = true, dataTypeClass = String.class),
  449 + @ApiImplicitParam(name="callId", value = "录像自鉴权ID", required = true, dataTypeClass = String.class),
  450 + })
  451 + @ResponseBody
  452 + @GetMapping(value = "/addStreamCallInfo", produces = "application/json;charset=UTF-8")
  453 + @PostMapping(value = "/addStreamCallInfo", produces = "application/json;charset=UTF-8")
  454 + public ResponseEntity<String> addStreamCallInfo(String app, String stream, String callId) {
  455 + String key = "Stream_Call_Info" + app + "_" + stream;
  456 + redisUtil.set(key, callId, -1);
  457 + return new ResponseEntity<>(null, HttpStatus.OK);
  458 + }
435 459 }
... ...
src/main/java/top/panll/assist/service/VideoFileService.java
... ... @@ -131,10 +131,11 @@ public class VideoFileService {
131 131 * @param file
132 132 * @throws ParseException
133 133 */
134   - public void handFile(File file) {
  134 + public void handFile(File file,String app, String stream) {
135 135 FFprobe ffprobe = ffmpegExecUtils.getFfprobe();
136 136 if(file.exists() && file.isFile() && !file.getName().startsWith(".")&& file.getName().endsWith(".mp4") && file.getName().indexOf(":") < 0) {
137 137 try {
  138 +
138 139 FFmpegProbeResult in = ffprobe.probe(file.getAbsolutePath());
139 140 double duration = in.getFormat().duration * 1000;
140 141 String endTimeStr = file.getName().replace(".mp4", "");
... ... @@ -149,9 +150,23 @@ public class VideoFileService {
149 150 long endTime = startTime + durationLong;
150 151 endTime = endTime - endTime%1000;
151 152  
152   - String newName = file.getAbsolutePath().replace(file.getName(),
153   - simpleDateFormat.format(startTime) + "-" + simpleDateFormat.format(endTime) + "-" + durationLong + ".mp4");
154   - file.renameTo(new File(newName));
  153 + String key = "Stream_Call_Info" + app + "_" + stream;
  154 + String callId = (String) redisUtil.get(key);
  155 + if (callId != null) {
  156 +
  157 + File newPath = new File(file.getParentFile().getParent() + "_" + callId + File.separator + file.getParentFile().getName());
  158 + if (!newPath.exists()) {
  159 + newPath.mkdirs();
  160 + }
  161 + String newName = newPath.getAbsolutePath() + File.separator+ simpleDateFormat.format(startTime) + "-" + simpleDateFormat.format(endTime) + "-" + durationLong + ".mp4";
  162 + file.renameTo(new File(newName));
  163 + }else {
  164 + String newName = file.getAbsolutePath().replace(file.getName(),
  165 + simpleDateFormat.format(startTime) + "-" + simpleDateFormat.format(endTime) + "-" + durationLong + ".mp4");
  166 +
  167 + file.renameTo(new File(newName));
  168 + }
  169 +
155 170 logger.debug("[处理文件] {}", file.getName());
156 171 } catch (IOException e) {
157 172 logger.warn("文件可能以损坏[{}]", file.getAbsolutePath());
... ... @@ -340,7 +355,9 @@ public class VideoFileService {
340 355 logger.info("[录像合并] 开始合并,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId);
341 356 String destDir = "recordTemp" + File.separator + taskId + File.separator + app;
342 357 File recordFile = new File(new File(userSettings.getRecord()).getParentFile().getAbsolutePath() + File.separator + destDir );
343   - if (!recordFile.exists()) recordFile.mkdirs();
  358 + if (!recordFile.exists()) {
  359 + recordFile.mkdirs();
  360 + }
344 361 MergeOrCutTaskInfo mergeOrCutTaskInfo = new MergeOrCutTaskInfo();
345 362 mergeOrCutTaskInfo.setId(taskId);
346 363 mergeOrCutTaskInfo.setApp(app);
... ... @@ -474,9 +491,15 @@ public class VideoFileService {
474 491  
475 492 public List<MergeOrCutTaskInfo> getTaskListForDownload(Boolean idEnd, String app, String stream, String taskId) {
476 493 ArrayList<MergeOrCutTaskInfo> result = new ArrayList<>();
477   - if (app == null) app = "*";
478   - if (stream == null) stream = "*";
479   - if (taskId == null) taskId = "*";
  494 + if (app == null) {
  495 + app = "*";
  496 + }
  497 + if (stream == null) {
  498 + stream = "*";
  499 + }
  500 + if (taskId == null) {
  501 + taskId = "*";
  502 + }
480 503 List<Object> taskCatch = redisUtil.scan(String.format("%S_%S_%S_%S", keyStr, app, stream, taskId));
481 504 for (int i = 0; i < taskCatch.size(); i++) {
482 505 String keyItem = taskCatch.get(i).toString();
... ...