Commit e175ca1763b86cd26910b01ef3e5987dd47e902a
1 parent
d7d49dee
优化对旧录像文件的处理
Showing
4 changed files
with
60 additions
and
22 deletions
src/main/java/top/panll/assist/config/StartConfig.java
| ... | ... | @@ -46,7 +46,7 @@ public class StartConfig implements CommandLineRunner { |
| 46 | 46 | |
| 47 | 47 | |
| 48 | 48 | @Override |
| 49 | - public void run(String... args) { | |
| 49 | + public void run(String... args) throws IOException { | |
| 50 | 50 | if (!record.endsWith(File.separator)) { |
| 51 | 51 | record = record + File.separator; |
| 52 | 52 | } |
| ... | ... | @@ -68,6 +68,7 @@ public class StartConfig implements CommandLineRunner { |
| 68 | 68 | } |
| 69 | 69 | List<CloudRecordItem> cloudRecordItemList = new ArrayList<>(); |
| 70 | 70 | Map<String, String> renameMap = new HashMap<>(); |
| 71 | + Map<String, List<CloudRecordItem>> dateVideoFileIndexList = new HashMap<>(); | |
| 71 | 72 | List<String> streamFileList = new ArrayList<>(); |
| 72 | 73 | // 搜集数据 |
| 73 | 74 | for (File appFile : appFiles) { |
| ... | ... | @@ -92,24 +93,27 @@ public class StartConfig implements CommandLineRunner { |
| 92 | 93 | } |
| 93 | 94 | stream = streamInfoArray[0]; |
| 94 | 95 | String callId = streamInfoArray[1]; |
| 95 | - File[] dateFiles = streamFile.listFiles(); | |
| 96 | + boolean collect = false; | |
| 97 | + boolean reserve = false; | |
| 98 | + File[] signFiles = streamFile.listFiles(File::isFile); | |
| 99 | + if (signFiles.length > 0) { | |
| 100 | + for (File signFile : signFiles) { | |
| 101 | + if (signFile.getName().equals("a.sign")) { | |
| 102 | + reserve = true; // 关联 | |
| 103 | + }else if (signFile.getName().equals("b.sign")) { | |
| 104 | + collect = true; // 归档 | |
| 105 | + } | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + File[] dateFiles = streamFile.listFiles(File::isDirectory); | |
| 96 | 110 | if (dateFiles == null || dateFiles.length == 0) { |
| 97 | 111 | continue; |
| 98 | 112 | } |
| 99 | 113 | streamFileList.add(streamFile.getAbsolutePath()); |
| 100 | - // TODC 确定关联和归档分别使用了什么类型名称 | |
| 101 | - boolean collect = false; | |
| 102 | - boolean reserve = false; | |
| 114 | + | |
| 103 | 115 | for (File dateFile : dateFiles) { |
| 104 | - if (dateFile.isFile()) { | |
| 105 | - if (dateFile.getName().endsWith(".sign")) { | |
| 106 | - if (dateFile.getName().startsWith("a")) { | |
| 107 | - collect = true; | |
| 108 | - }else if (dateFile.getName().startsWith("b")) { | |
| 109 | - reserve = true; | |
| 110 | - } | |
| 111 | - } | |
| 112 | - }else { | |
| 116 | + if (dateFile.isDirectory()) { | |
| 113 | 117 | // 检验是否是日期格式 |
| 114 | 118 | if (!DateUtils.checkDateFormat(dateFile.getName())) { |
| 115 | 119 | continue; |
| ... | ... | @@ -119,6 +123,8 @@ public class StartConfig implements CommandLineRunner { |
| 119 | 123 | if (videoFiles == null || videoFiles.length == 0) { |
| 120 | 124 | continue; |
| 121 | 125 | } |
| 126 | + | |
| 127 | + | |
| 122 | 128 | for (int i = 0; i < videoFiles.length; i++) { |
| 123 | 129 | File videoFile = videoFiles[i]; |
| 124 | 130 | if (!videoFile.getName().endsWith(".mp4") && !videoFile.getName().contains("-")) { |
| ... | ... | @@ -136,10 +142,16 @@ public class StartConfig implements CommandLineRunner { |
| 136 | 142 | } |
| 137 | 143 | String startTime = date + " " + videoInfoArray[0]; |
| 138 | 144 | String endTime = date + " " + videoInfoArray[1]; |
| 139 | - Long startTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime); | |
| 140 | - Long endTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime); | |
| 145 | + Long startTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime) * 1000; | |
| 146 | + Long endTimeStamp = DateUtils.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) * 1000; | |
| 141 | 147 | |
| 142 | 148 | long timeLength = Long.parseLong(videoInfoArray[2].substring(0, videoInfoArray[2].length() - 4)); |
| 149 | + | |
| 150 | + String dataPath = appFile.getAbsolutePath() + File.separator + stream + File.separator + dateFile.getName(); | |
| 151 | + if (dateVideoFileIndexList.get(dataPath) == null) { | |
| 152 | + dateVideoFileIndexList.put(dataPath, new ArrayList<>()); | |
| 153 | + } | |
| 154 | + | |
| 143 | 155 | CloudRecordItem cloudRecordItem = new CloudRecordItem(); |
| 144 | 156 | cloudRecordItem.setApp(app); |
| 145 | 157 | cloudRecordItem.setStream(stream); |
| ... | ... | @@ -149,14 +161,14 @@ public class StartConfig implements CommandLineRunner { |
| 149 | 161 | cloudRecordItem.setCollect(collect); |
| 150 | 162 | cloudRecordItem.setReserve(reserve); |
| 151 | 163 | cloudRecordItem.setMediaServerId(mediaServerId); |
| 152 | - cloudRecordItem.setFileName(DateUtils.getTimeStr(startTimeStamp) + "-" + i + ".mp4"); | |
| 164 | + cloudRecordItem.setFileName(DateUtils.getTimeStr(startTimeStamp) + "-" + dateVideoFileIndexList.get(dataPath).size() + ".mp4"); | |
| 153 | 165 | cloudRecordItem.setFolder(streamFile.getAbsolutePath()); |
| 154 | 166 | cloudRecordItem.setFileSize(videoFile.length()); |
| 155 | 167 | cloudRecordItem.setTimeLen(timeLength); |
| 156 | 168 | cloudRecordItem.setFilePath(appFile.getAbsolutePath() + File.separator + stream + File.separator + dateFile.getName() + File.separator + cloudRecordItem.getFileName()); |
| 157 | 169 | cloudRecordItemList.add(cloudRecordItem); |
| 170 | + dateVideoFileIndexList.get(dataPath).add(cloudRecordItem); | |
| 158 | 171 | renameMap.put(videoFile.getAbsolutePath(), cloudRecordItem.getFilePath()); |
| 159 | - System.out.println(cloudRecordItem.getFilePath()); | |
| 160 | 172 | } |
| 161 | 173 | } |
| 162 | 174 | } |
| ... | ... | @@ -195,6 +207,10 @@ public class StartConfig implements CommandLineRunner { |
| 195 | 207 | logger.info("开始修改磁盘文件"); |
| 196 | 208 | for (String oldFileName : renameMap.keySet()) { |
| 197 | 209 | File oldFile = new File(oldFileName); |
| 210 | + File newFile = new File(renameMap.get(oldFileName)); | |
| 211 | + if (!newFile.getParentFile().exists()) { | |
| 212 | + newFile.getParentFile().mkdirs(); | |
| 213 | + } | |
| 198 | 214 | boolean result = oldFile.renameTo(new File(renameMap.get(oldFileName))); |
| 199 | 215 | if (result) { |
| 200 | 216 | logger.info("重命名成功: " + oldFileName + "===" + renameMap.get(oldFileName)); |
| ... | ... | @@ -203,7 +219,9 @@ public class StartConfig implements CommandLineRunner { |
| 203 | 219 | logger.info("修改磁盘文件完成"); |
| 204 | 220 | logger.info("清理失效文件夹"); |
| 205 | 221 | for (String streamFileStr : streamFileList) { |
| 206 | - new File(streamFileStr).delete(); | |
| 222 | + System.out.println(streamFileStr); | |
| 223 | + File deleteFile = new File(streamFileStr); | |
| 224 | + deleteFile(deleteFile); | |
| 207 | 225 | } |
| 208 | 226 | logger.info("清理失效文件夹结束"); |
| 209 | 227 | } |
| ... | ... | @@ -212,4 +230,22 @@ public class StartConfig implements CommandLineRunner { |
| 212 | 230 | } |
| 213 | 231 | |
| 214 | 232 | |
| 233 | + public void deleteFile(File file) { | |
| 234 | + if (!file.exists()) { | |
| 235 | + logger.warn("[删除文件] {} 不存在 ", file.getAbsolutePath()); | |
| 236 | + }else { | |
| 237 | + if (file.isFile()) { | |
| 238 | + file.delete(); | |
| 239 | + return; | |
| 240 | + } | |
| 241 | + File[] files = file.listFiles(); | |
| 242 | + if (files.length > 0) { | |
| 243 | + for (File childFile : files) { | |
| 244 | + deleteFile(childFile); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + file.delete(); | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 215 | 251 | } | ... | ... |
src/main/java/top/panll/assist/mapper/CloudRecordServiceMapper.java
| ... | ... | @@ -90,13 +90,15 @@ public interface CloudRecordServiceMapper { |
| 90 | 90 | " folder," + |
| 91 | 91 | " file_path," + |
| 92 | 92 | " file_size," + |
| 93 | + " collect," + | |
| 94 | + " reserve," + | |
| 93 | 95 | " time_len " + |
| 94 | 96 | ") " + |
| 95 | 97 | "values " + |
| 96 | 98 | "<foreach collection='cloudRecordItems' index='index' item='item' separator=','> " + |
| 97 | 99 | "(#{item.app}, #{item.stream}, #{item.callId}, #{item.startTime}, " + |
| 98 | 100 | "#{item.endTime}, #{item.mediaServerId}, #{item.fileName},#{item.folder}," + |
| 99 | - "#{item.filePath}, #{item.fileSize}, #{item.timeLen}) " + | |
| 101 | + "#{item.filePath}, #{item.fileSize}, #{item.collect}, #{item.reserve}, #{item.timeLen}) " + | |
| 100 | 102 | "</foreach> " + |
| 101 | 103 | "</script>") |
| 102 | 104 | int batchAdd(@Param("cloudRecordItems") List<CloudRecordItem> cloudRecordItems); | ... | ... |
src/main/java/top/panll/assist/utils/DateUtils.java
| ... | ... | @@ -49,7 +49,7 @@ public class DateUtils { |
| 49 | 49 | return formatter.format(date); |
| 50 | 50 | } |
| 51 | 51 | public static String getTimeStr(Long dateTime) { |
| 52 | - SimpleDateFormat formatter = new SimpleDateFormat(PATTERNForTime); | |
| 52 | + SimpleDateFormat formatter = new SimpleDateFormat(PATTERNForTime, Locale.CHINESE); | |
| 53 | 53 | return formatter.format(new Date(dateTime)); |
| 54 | 54 | } |
| 55 | 55 | ... | ... |
src/main/resources/application.yml
| ... | ... | @@ -17,7 +17,7 @@ user-settings: |
| 17 | 17 | id: 1111 |
| 18 | 18 | media-server-id: 11212 |
| 19 | 19 | # [可选 ] zlm配置的录像路径,不配置则使用当前目录下的record目录 即: ./record |
| 20 | - record: /home/lin/record1/ | |
| 20 | + record: /home/lin/record/ | |
| 21 | 21 | # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理, 不配置则不删除 |
| 22 | 22 | recordDay: 7 |
| 23 | 23 | # [可选 ] 录像下载合成临时文件保存时长, 不配置默认取值recordDay(单位: 天)每天晚12点自动对过期文件执行清理 | ... | ... |