From 1f3aaeb4336c5eea2cdeb53556468c40b1d51e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=98=B2?= Date: Wed, 25 May 2022 15:49:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=E6=96=B0=E7=89=88=E6=9C=ACzlm=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E5=BD=95=E5=83=8F=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=B8=8D=E5=90=AB=E6=9C=89=20:=20=E6=89=80=E4=BB=A5if=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=B2=A1=E5=BF=85=E8=A6=81=E5=8A=A0=E8=BF=99=E4=B8=AA?= =?UTF-8?q?=E4=BA=86=202.=E5=BD=95=E5=83=8F=E6=96=87=E4=BB=B6=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E3=80=82format=E4=B8=BA=20yyyy-MM-dd=20HH:mm:ss?= =?UTF-8?q?=EF=BC=8C=20=E4=BB=85=E6=8B=BC=E6=8E=A5=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=B8=BA=202022-5-25=2013=E6=98=AF=E4=B8=8D=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E3=80=82=E9=9C=80=E8=A6=81=E6=8B=BC=E6=8E=A5=E4=B8=BA2022-5-25?= =?UTF-8?q?=2013:00:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assist/service/VideoFileService.java | 113 +++++++++--------- src/main/resources/application-dev.yml | 2 +- 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/src/main/java/top/panll/assist/service/VideoFileService.java b/src/main/java/top/panll/assist/service/VideoFileService.java index 45351ab..910f802 100644 --- a/src/main/java/top/panll/assist/service/VideoFileService.java +++ b/src/main/java/top/panll/assist/service/VideoFileService.java @@ -45,7 +45,6 @@ public class VideoFileService { private FFmpegExecUtils ffmpegExecUtils; - private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); private final SimpleDateFormat simpleDateFormatForTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -56,19 +55,19 @@ public List getAppList(Boolean sort) { if (recordFile.isDirectory()) { File[] files = recordFile.listFiles((File dir, String name) -> { File currentFile = new File(dir.getAbsolutePath() + File.separator + name); - return currentFile.isDirectory(); + return currentFile.isDirectory(); }); List result = Arrays.asList(files); if (sort != null && sort) { Collections.sort(result); } return result; - }else { + } else { return null; } } - public SpaceInfo getSpaceInfo(){ + public SpaceInfo getSpaceInfo() { File recordFile = new File(userSettings.getRecord()); SpaceInfo spaceInfo = new SpaceInfo(); spaceInfo.setFree(recordFile.getFreeSpace()); @@ -114,26 +113,27 @@ public List getStreamList(File appFile, Boolean sort) { if (appFile != null && appFile.isDirectory()) { File[] files = appFile.listFiles((File dir, String name) -> { File currentFile = new File(dir.getAbsolutePath() + File.separator + name); - return currentFile.isDirectory(); + return currentFile.isDirectory(); }); List result = Arrays.asList(files); if (sort != null && sort) { Collections.sort(result); } return result; - }else { + } else { return null; } } /** * 对视频文件重命名, 00:00:00-00:00:00 + * * @param file * @throws ParseException */ public void handFile(File file) { FFprobe ffprobe = ffmpegExecUtils.getFfprobe(); - if(file.exists() && file.isFile() && !file.getName().startsWith(".")&& file.getName().endsWith(".mp4") && file.getName().indexOf(":") < 0) { + if (file.exists() && file.isFile() && !file.getName().startsWith(".") && file.getName().endsWith(".mp4") && file.getName().indexOf(":") < 0) { try { FFmpegProbeResult in = ffprobe.probe(file.getAbsolutePath()); double duration = in.getFormat().duration * 1000; @@ -147,7 +147,7 @@ public void handFile(File file) { long startTime = formatter.parse(dateFile.getName() + " " + endTimeStr).getTime(); long durationLong = new Double(duration).longValue(); long endTime = startTime + durationLong; - endTime = endTime - endTime%1000; + endTime = endTime - endTime % 1000; String newName = file.getAbsolutePath().replace(file.getName(), simpleDateFormat.format(startTime) + "-" + simpleDateFormat.format(endTime) + "-" + durationLong + ".mp4"); @@ -191,7 +191,7 @@ public List> getList() { } } } - result.sort((Map f1, Map f2)->{ + result.sort((Map f1, Map f2) -> { Date time1 = null; Date time2 = null; try { @@ -207,13 +207,14 @@ public List> getList() { /** * 获取制定推流的指定时间段内的推流 + * * @param app * @param stream * @param startTime * @param endTime * @return */ - public List getFilesInTime(String app, String stream, Date startTime, Date endTime){ + public List getFilesInTime(String app, String stream, Date startTime, Date endTime) { List result = new ArrayList<>(); if (app == null || stream == null) { @@ -264,40 +265,42 @@ public List getFilesInTime(String app, String stream, Date startTime, Date boolean filterResult = true; if (startDate != null) { - filterResult = filterResult && DateUtils.getStartOfDay(startDate).compareTo(fileDate) <= 0; + filterResult = filterResult && DateUtils.getStartOfDay(startDate).compareTo(fileDate) <= 0; } if (endDate != null) { - filterResult = filterResult && DateUtils.getEndOfDay(endDate).compareTo(fileDate) >= 0; + filterResult = filterResult && DateUtils.getEndOfDay(endDate).compareTo(fileDate) >= 0; } - return filterResult ; + return filterResult; }); if (dateFiles != null && dateFiles.length > 0) { for (File dateFile : dateFiles) { // TODO 按时间获取文件 - File[] files = dateFile.listFiles((File dir, String name) ->{ + File[] files = dateFile.listFiles((File dir, String name) -> { boolean filterResult = true; File currentFile = new File(dir + File.separator + name); - if (currentFile.isFile() && name.contains(":") && name.endsWith(".mp4") && !name.startsWith(".") && currentFile.length() > 0){ + //&& name.contains(":") 最新版本的zlm生成文件没有 : + if (currentFile.isFile() && name.endsWith(".mp4") && !name.startsWith(".") && currentFile.length() > 0) { String[] timeArray = name.split("-"); - if (timeArray.length == 3){ - String fileStartTimeStr = dateFile.getName() + " " + timeArray[0]; - String fileEndTimeStr = dateFile.getName() + " " + timeArray[1]; + if (timeArray.length == 3) { + //时间有问题 yyyy-MM-dd HH:mm:ss 2022-5-25 13 + String fileStartTimeStr = dateFile.getName() + " " + timeArray[0] + ":00:00"; + String fileEndTimeStr = dateFile.getName() + " " + timeArray[1] + ":00:00"; try { if (startTime != null) { - filterResult = filterResult && (formatter.parse(fileStartTimeStr).after(startTime) || (formatter.parse(fileStartTimeStr).before(startTime) && formatter.parse(fileEndTimeStr).after(startTime))); + filterResult = filterResult && (formatter.parse(fileStartTimeStr).after(startTime) || (formatter.parse(fileStartTimeStr).before(startTime) && formatter.parse(fileEndTimeStr).after(startTime))); } if (endTime != null) { - filterResult = filterResult && (formatter.parse(fileEndTimeStr).before(endTime) || (formatter.parse(fileEndTimeStr).after(endTime) && formatter.parse(fileStartTimeStr).before(endTime))); + filterResult = filterResult && (formatter.parse(fileEndTimeStr).before(endTime) || (formatter.parse(fileEndTimeStr).after(endTime) && formatter.parse(fileStartTimeStr).before(endTime))); } } catch (ParseException e) { logger.error("过滤视频文件时异常: {}-{}", name, e.getMessage()); return false; } } - }else { + } else { filterResult = false; } return filterResult; @@ -312,7 +315,7 @@ public List getFilesInTime(String app, String stream, Date startTime, Date int sortResult = 0; String[] timeArray1 = f1.getName().split("-"); String[] timeArray2 = f2.getName().split("-"); - if (timeArray1.length == 3 && timeArray2.length == 3){ + if (timeArray1.length == 3 && timeArray2.length == 3) { File dateFile1 = f1.getParentFile(); File dateFile2 = f2.getParentFile(); String fileStartTimeStr1 = dateFile1.getName() + " " + timeArray1[0]; @@ -332,32 +335,32 @@ public List getFilesInTime(String app, String stream, Date startTime, Date public String mergeOrCut(String app, String stream, Date startTime, Date endTime, String remoteHost) { List filesInTime = this.getFilesInTime(app, stream, startTime, endTime); - if (filesInTime== null || filesInTime.size() == 0){ + if (filesInTime == null || filesInTime.size() == 0) { logger.info("此时间段未未找到视频文件"); return null; } String taskId = DigestUtils.md5DigestAsHex(String.valueOf(System.currentTimeMillis()).getBytes()); logger.info("[录像合并] 开始合并,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId); String destDir = "recordTemp" + File.separator + taskId + File.separator + app; - File recordFile = new File(new File(userSettings.getRecord()).getParentFile().getAbsolutePath() + File.separator + destDir ); + File recordFile = new File(new File(userSettings.getRecord()).getParentFile().getAbsolutePath() + File.separator + destDir); if (!recordFile.exists()) recordFile.mkdirs(); MergeOrCutTaskInfo mergeOrCutTaskInfo = new MergeOrCutTaskInfo(); mergeOrCutTaskInfo.setId(taskId); mergeOrCutTaskInfo.setApp(app); mergeOrCutTaskInfo.setStream(stream); mergeOrCutTaskInfo.setCreateTime(simpleDateFormatForTime.format(System.currentTimeMillis())); - if(startTime != null) { + if (startTime != null) { mergeOrCutTaskInfo.setStartTime(simpleDateFormatForTime.format(startTime)); - }else { + } else { String startTimeInFile = filesInTime.get(0).getParentFile().getName() + " " + filesInTime.get(0).getName().split("-")[0]; mergeOrCutTaskInfo.setStartTime(startTimeInFile); } - if(endTime != null) { + if (endTime != null) { mergeOrCutTaskInfo.setEndTime(simpleDateFormatForTime.format(endTime)); - }else { - String endTimeInFile = filesInTime.get(filesInTime.size()- 1).getParentFile().getName() + " " - + filesInTime.get(filesInTime.size()- 1).getName().split("-")[1]; + } else { + String endTimeInFile = filesInTime.get(filesInTime.size() - 1).getParentFile().getName() + " " + + filesInTime.get(filesInTime.size() - 1).getName().split("-")[1]; mergeOrCutTaskInfo.setEndTime(endTimeInFile); } if (filesInTime.size() == 1) { @@ -383,8 +386,8 @@ public String mergeOrCut(String app, String stream, Date startTime, Date endTime redisUtil.set(key, mergeOrCutTaskInfo); redisUtil.convertAndSend("topic_mergeorcut_end", mergeOrCutTaskInfo); logger.info("[录像合并] 合并完成,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId); - }else { - ffmpegExecUtils.mergeOrCutFile(filesInTime, recordFile, stream, (status, percentage, result)->{ + } else { + ffmpegExecUtils.mergeOrCutFile(filesInTime, recordFile, stream, (status, percentage, result) -> { // 发出redis通知 if (status.equals(Progress.Status.END.name())) { mergeOrCutTaskInfo.setPercentage("1"); @@ -398,9 +401,9 @@ public String mergeOrCut(String app, String stream, Date startTime, Date endTime } redisUtil.convertAndSend("topic_mergeorcut_end", mergeOrCutTaskInfo); logger.info("[录像合并] 合并完成,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId); - }else { + } else { mergeOrCutTaskInfo.setPercentage(percentage + ""); - redisUtil.convertAndSend("topic_mergeorcut_continue", mergeOrCutTaskInfo); + redisUtil.convertAndSend("topic_mergeorcut_continue", mergeOrCutTaskInfo); } String key = String.format("%S_%S_%S_%S", keyStr, mergeOrCutTaskInfo.getApp(), mergeOrCutTaskInfo.getStream(), mergeOrCutTaskInfo.getId()); redisUtil.set(key, mergeOrCutTaskInfo); @@ -412,6 +415,7 @@ public String mergeOrCut(String app, String stream, Date startTime, Date endTime /** * 获取指定时间的日期文件夹 + * * @param app * @param stream * @param year @@ -423,14 +427,15 @@ public List getDateList(String app, String stream, Integer year, Integer m File streamFile = new File(recordFile.getAbsolutePath() + File.separator + app + File.separator + stream); return getDateList(streamFile, year, month, sort); } + public List getDateList(File streamFile, Integer year, Integer month, Boolean sort) { if (!streamFile.exists() && streamFile.isDirectory()) { - logger.warn("获取[]的视频时未找到目录: {}",streamFile.getName()); + logger.warn("获取[]的视频时未找到目录: {}", streamFile.getName()); return null; } - File[] dateFiles = streamFile.listFiles((File dir, String name)->{ + File[] dateFiles = streamFile.listFiles((File dir, String name) -> { File currentFile = new File(dir.getAbsolutePath() + File.separator + name); - if (!currentFile.isDirectory()){ + if (!currentFile.isDirectory()) { return false; } Date date = null; @@ -446,18 +451,18 @@ public List getDateList(File streamFile, Integer year, Integer month, Bool int m = c.get(Calendar.MONTH) + 1; if (year != null) { if (month != null) { - return y == year && m == month; - }else { - return y == year; + return y == year && m == month; + } else { + return y == year; } - }else { + } else { return true; } }); List dateFileList = Arrays.asList(dateFiles); if (sort != null && sort) { - dateFileList.sort((File f1, File f2)->{ + dateFileList.sort((File f1, File f2) -> { int sortResult = 0; try { @@ -480,25 +485,25 @@ public List getTaskListForDownload(Boolean idEnd, String app List taskCatch = redisUtil.scan(String.format("%S_%S_%S_%S", keyStr, app, stream, taskId)); for (int i = 0; i < taskCatch.size(); i++) { String keyItem = taskCatch.get(i).toString(); - MergeOrCutTaskInfo mergeOrCutTaskInfo = (MergeOrCutTaskInfo)redisUtil.get(keyItem); - if (mergeOrCutTaskInfo != null && mergeOrCutTaskInfo.getPercentage() != null){ - if (idEnd != null ) { + MergeOrCutTaskInfo mergeOrCutTaskInfo = (MergeOrCutTaskInfo) redisUtil.get(keyItem); + if (mergeOrCutTaskInfo != null && mergeOrCutTaskInfo.getPercentage() != null) { + if (idEnd != null) { if (idEnd) { - if (Double.parseDouble(mergeOrCutTaskInfo.getPercentage()) == 1){ + if (Double.parseDouble(mergeOrCutTaskInfo.getPercentage()) == 1) { result.add(mergeOrCutTaskInfo); } - }else { - if (Double.parseDouble(mergeOrCutTaskInfo.getPercentage()) < 1){ - result.add((MergeOrCutTaskInfo)redisUtil.get(keyItem)); + } else { + if (Double.parseDouble(mergeOrCutTaskInfo.getPercentage()) < 1) { + result.add((MergeOrCutTaskInfo) redisUtil.get(keyItem)); } } - }else { - result.add((MergeOrCutTaskInfo)redisUtil.get(keyItem)); + } else { + result.add((MergeOrCutTaskInfo) redisUtil.get(keyItem)); } } } - result.sort((MergeOrCutTaskInfo m1, MergeOrCutTaskInfo m2)->{ + result.sort((MergeOrCutTaskInfo m1, MergeOrCutTaskInfo m2) -> { int sortResult = 0; try { sortResult = simpleDateFormatForTime.parse(m1.getCreateTime()).compareTo(simpleDateFormatForTime.parse(m2.getCreateTime())); @@ -583,10 +588,10 @@ public List getCollectionList(String app, String stream, String type) signInfo.setType(type); result.add(signInfo); } - }else { + } else { streamFile.listFiles((File dir, String name) -> { File currentFile = new File(dir.getAbsolutePath() + File.separator + name); - if (currentFile.isFile() && name.endsWith(".sign")){ + if (currentFile.isFile() && name.endsWith(".sign")) { String currentType = name.substring(0, name.length() - ".sign".length()); SignInfo signInfo = new SignInfo(); signInfo.setApp(appFile.getName()); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index ff200bc..9e89f5a 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -8,7 +8,7 @@ spring: # [可选] 数据库 DB database: 8 # [可选] 访问密码,若你的redis服务器没有设置密码,就不需要用密码去连接 - password: + password: face2020 # [可选] 超时时间 timeout: 10000 From 67358263ccd078ce7c3c45f1d4844962bdab5dc6 Mon Sep 17 00:00:00 2001 From: Fang Date: Wed, 25 May 2022 15:50:33 +0800 Subject: [PATCH 2/2] Update application-dev.yml --- src/main/resources/application-dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 9e89f5a..22d7abd 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -8,7 +8,7 @@ spring: # [可选] 数据库 DB database: 8 # [可选] 访问密码,若你的redis服务器没有设置密码,就不需要用密码去连接 - password: face2020 + password: # [可选] 超时时间 timeout: 10000 @@ -54,4 +54,4 @@ logging: root: WARN top: panll: - assist: info \ No newline at end of file + assist: info