Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/date/list 获取zlm录像文件有问题 #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 59 additions & 54 deletions src/main/java/top/panll/assist/service/VideoFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -56,19 +55,19 @@ public List<File> 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<File> 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());
Expand Down Expand Up @@ -114,26 +113,27 @@ public List<File> 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<File> 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;
Expand All @@ -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");
Expand Down Expand Up @@ -191,7 +191,7 @@ public List<Map<String, String>> getList() {
}
}
}
result.sort((Map f1, Map f2)->{
result.sort((Map f1, Map f2) -> {
Date time1 = null;
Date time2 = null;
try {
Expand All @@ -207,13 +207,14 @@ public List<Map<String, String>> getList() {

/**
* 获取制定推流的指定时间段内的推流
*
* @param app
* @param stream
* @param startTime
* @param endTime
* @return
*/
public List<File> getFilesInTime(String app, String stream, Date startTime, Date endTime){
public List<File> getFilesInTime(String app, String stream, Date startTime, Date endTime) {

List<File> result = new ArrayList<>();
if (app == null || stream == null) {
Expand Down Expand Up @@ -264,40 +265,42 @@ public List<File> 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;
Expand All @@ -312,7 +315,7 @@ public List<File> 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];
Expand All @@ -332,32 +335,32 @@ public List<File> getFilesInTime(String app, String stream, Date startTime, Date

public String mergeOrCut(String app, String stream, Date startTime, Date endTime, String remoteHost) {
List<File> 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) {
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -412,6 +415,7 @@ public String mergeOrCut(String app, String stream, Date startTime, Date endTime

/**
* 获取指定时间的日期文件夹
*
* @param app
* @param stream
* @param year
Expand All @@ -423,14 +427,15 @@ public List<File> 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<File> 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;
Expand All @@ -446,18 +451,18 @@ public List<File> 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<File> dateFileList = Arrays.asList(dateFiles);
if (sort != null && sort) {
dateFileList.sort((File f1, File f2)->{
dateFileList.sort((File f1, File f2) -> {
int sortResult = 0;

try {
Expand All @@ -480,25 +485,25 @@ public List<MergeOrCutTaskInfo> getTaskListForDownload(Boolean idEnd, String app
List<Object> 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()));
Expand Down Expand Up @@ -583,10 +588,10 @@ public List<SignInfo> 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());
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spring:
# [可选] 数据库 DB
database: 8
# [可选] 访问密码,若你的redis服务器没有设置密码,就不需要用密码去连接
password:
password:
# [可选] 超时时间
timeout: 10000

Expand Down Expand Up @@ -54,4 +54,4 @@ logging:
root: WARN
top:
panll:
assist: info
assist: info