Commit 3191d0c6a4f0877c217123baf53798e404321167
1 parent
e8ac6bf8
优化获取视频时长接口
Showing
3 changed files
with
11 additions
and
58 deletions
src/main/java/top/panll/assist/controller/RecordController.java
| @@ -423,12 +423,12 @@ public class RecordController { | @@ -423,12 +423,12 @@ public class RecordController { | ||
| 423 | }) | 423 | }) |
| 424 | @ResponseBody | 424 | @ResponseBody |
| 425 | @GetMapping(value = "/file/duration", produces = "application/json;charset=UTF-8") | 425 | @GetMapping(value = "/file/duration", produces = "application/json;charset=UTF-8") |
| 426 | - public ResponseEntity<String> fileDuration( @RequestParam String app, @RequestParam String stream, Boolean recordIng) { | ||
| 427 | - if (recordIng == null) recordIng = true; | 426 | + @PostMapping(value = "/file/duration", produces = "application/json;charset=UTF-8") |
| 427 | + public ResponseEntity<String> fileDuration( @RequestParam String app, @RequestParam String stream) { | ||
| 428 | JSONObject ret = new JSONObject(); | 428 | JSONObject ret = new JSONObject(); |
| 429 | ret.put("code", 0); | 429 | ret.put("code", 0); |
| 430 | ret.put("msg", "success"); | 430 | ret.put("msg", "success"); |
| 431 | - Double duration = videoFileService.fileDuration(app, stream, recordIng); | 431 | + long duration = videoFileService.fileDuration(app, stream); |
| 432 | ret.put("data", duration); | 432 | ret.put("data", duration); |
| 433 | return new ResponseEntity<>(ret.toString(), HttpStatus.OK); | 433 | return new ResponseEntity<>(ret.toString(), HttpStatus.OK); |
| 434 | } | 434 | } |
src/main/java/top/panll/assist/service/FFmpegExecUtils.java
| @@ -151,9 +151,11 @@ public class FFmpegExecUtils implements InitializingBean{ | @@ -151,9 +151,11 @@ public class FFmpegExecUtils implements InitializingBean{ | ||
| 151 | job.run(); | 151 | job.run(); |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | - public double duration(File file) throws IOException { | 154 | + public long duration(File file) throws IOException { |
| 155 | FFmpegProbeResult in = ffprobe.probe(file.getAbsolutePath()); | 155 | FFmpegProbeResult in = ffprobe.probe(file.getAbsolutePath()); |
| 156 | - return in.getFormat().duration * 1000; | 156 | + double duration = in.getFormat().duration * 1000; |
| 157 | + long durationLong = new Double(duration).longValue(); | ||
| 158 | + return durationLong; | ||
| 157 | } | 159 | } |
| 158 | 160 | ||
| 159 | } | 161 | } |
src/main/java/top/panll/assist/service/VideoFileService.java
| @@ -205,55 +205,6 @@ public class VideoFileService { | @@ -205,55 +205,6 @@ public class VideoFileService { | ||
| 205 | return result; | 205 | return result; |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | - public List<File> getAllFiles(String app, String stream, Boolean recording){ | ||
| 209 | - | ||
| 210 | - List<File> result = new ArrayList<>(); | ||
| 211 | - if (app == null || stream == null) { | ||
| 212 | - return result; | ||
| 213 | - } | ||
| 214 | - | ||
| 215 | - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
| 216 | - SimpleDateFormat formatterForDate = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 217 | - | ||
| 218 | - File recordFile = new File(userSettings.getRecord()); | ||
| 219 | - File streamFile = new File(recordFile.getAbsolutePath() + File.separator + app + File.separator + stream + File.separator); | ||
| 220 | - if (!streamFile.exists()) { | ||
| 221 | - logger.warn("获取[app: {}, stream: {}]的视频时未找到目录: {}", app, stream, stream); | ||
| 222 | - return null; | ||
| 223 | - } | ||
| 224 | - | ||
| 225 | - File[] dateFiles = streamFile.listFiles((File dir, String name) -> { | ||
| 226 | - if (new File(dir + File.separator + name).isFile()) { | ||
| 227 | - return false; | ||
| 228 | - } | ||
| 229 | - try { | ||
| 230 | - formatterForDate.parse(name); | ||
| 231 | - } catch (ParseException e) { | ||
| 232 | - logger.error("过滤日期文件时异常: {}-{}", name, e.getMessage()); | ||
| 233 | - return false; | ||
| 234 | - } | ||
| 235 | - | ||
| 236 | - return true ; | ||
| 237 | - }); | ||
| 238 | - | ||
| 239 | - if (dateFiles != null && dateFiles.length > 0) { | ||
| 240 | - for (File dateFile : dateFiles) { | ||
| 241 | - File[] files = dateFile.listFiles((File dir, String name) ->{ | ||
| 242 | - File currentFile = new File(dir + File.separator + name); | ||
| 243 | - if (recording) { | ||
| 244 | - return currentFile.isFile() && name.contains(":") && name.endsWith(".mp4") && currentFile.length() > 0; | ||
| 245 | - }else { | ||
| 246 | - return currentFile.isFile() && name.contains(":") && name.endsWith(".mp4") && currentFile.length() > 0 && !name.startsWith("."); | ||
| 247 | - } | ||
| 248 | - }); | ||
| 249 | - List<File> fileList = Arrays.asList(files); | ||
| 250 | - result.addAll(fileList); | ||
| 251 | - } | ||
| 252 | - } | ||
| 253 | - return result; | ||
| 254 | - } | ||
| 255 | - | ||
| 256 | - | ||
| 257 | /** | 208 | /** |
| 258 | * 获取制定推流的指定时间段内的推流 | 209 | * 获取制定推流的指定时间段内的推流 |
| 259 | * @param app | 210 | * @param app |
| @@ -653,10 +604,10 @@ public class VideoFileService { | @@ -653,10 +604,10 @@ public class VideoFileService { | ||
| 653 | return result; | 604 | return result; |
| 654 | } | 605 | } |
| 655 | 606 | ||
| 656 | - public Double fileDuration(String app, String stream, Boolean recordIng) { | ||
| 657 | - List<File> allFiles = getAllFiles(app, stream, recordIng); | ||
| 658 | - Double durationResult = 0.0; | ||
| 659 | - if (allFiles.size() > 0) { | 607 | + public long fileDuration(String app, String stream) { |
| 608 | + List<File> allFiles = getFilesInTime(app, stream, null, null); | ||
| 609 | + long durationResult = 0; | ||
| 610 | + if (allFiles != null && allFiles.size() > 0) { | ||
| 660 | for (File file : allFiles) { | 611 | for (File file : allFiles) { |
| 661 | try { | 612 | try { |
| 662 | durationResult += ffmpegExecUtils.duration(file); | 613 | durationResult += ffmpegExecUtils.duration(file); |