@ -19,7 +19,10 @@ import java.io.IOException;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.time.Instant ;
import java.time.LocalDate ;
import java.time.LocalDateTime ;
import java.time.ZoneId ;
import java.time.format.DateTimeFormatter ;
import java.util.ArrayList ;
import java.util.HashMap ;
@ -43,15 +46,17 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
@Override
public String
startRecord ( String app , String cameraId ) {
public String startRecord ( String app , String cameraId ) {
String zlmApiSecret = dictDataService . parseDictData ( "ZLMediaKit_conf" , "secret" ) . getValue ( ) ;
String mp4SavePath = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SavePath" ) . getValue ( ) ;
Map < String , Object > addParams = new HashMap < > ( ) ;
addParams . put ( "secret" , zlmApiSecret ) ;
addParams . put ( "type" , "1" ) ; //0为hls, 1为mp4
addParams . put ( "vhost" , "__defaultVhost__" ) ;
addParams . put ( "app" , "live" ) ;
addParams . put ( "customized_path" , mp4SavePath ) ;
addParams . put ( "stream" , "camera" + cameraId ) ;
// addParams.put("url", CameraChannel.getRtspUrl(camera.getIp(), camera.getRtspPort(), camera.getChannel(), camera.getUser(), camera.getPassword(),camera.getType()));
// sendHttp(addParams, "startRecord");
@ -67,7 +72,7 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
ScheduledExecutorService scheduler = Executors . newScheduledThreadPool ( 1 ) ;
Runnable task = ( ) - > {
stopRecord ( app , cameraId );
stopRecord ( app , cameraId ,LocalDateTime . now ( ) );
} ;
@ -77,6 +82,62 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
return path ;
}
// 检查指定文件夹中最新的文件名,且文件修改时间在指定时间之后
public String getLatestFileNameAfterTime ( String directoryPath , LocalDateTime afterTime ) {
try {
Path directory = Paths . get ( directoryPath ) ;
// 将LocalDateTime转换为Instant用于比较
Instant afterInstant = afterTime . atZone ( ZoneId . systemDefault ( ) ) . toInstant ( ) ;
// 获取目录下所有文件
try ( Stream < Path > paths = Files . list ( directory ) ) {
return paths
. filter ( Files : : isRegularFile ) // 只考虑文件
. filter ( path - > {
try {
// 获取文件最后修改时间
Instant lastModified = Files . getLastModifiedTime ( path ) . toInstant ( ) ;
// 筛选修改时间在指定时间之后的文件
return lastModified . isAfter ( afterInstant ) ;
} catch ( IOException e ) {
log . error ( "获取文件修改时间失败: {}" , e . getMessage ( ) ) ;
return false ;
}
} )
. max ( ( path1 , path2 ) - > {
try {
// 按最后修改时间比较
Instant time1 = Files . getLastModifiedTime ( path1 ) . toInstant ( ) ;
Instant time2 = Files . getLastModifiedTime ( path2 ) . toInstant ( ) ;
return time1 . compareTo ( time2 ) ;
} catch ( IOException e ) {
log . error ( "比较文件修改时间失败: {}" , e . getMessage ( ) ) ;
return 0 ;
}
} )
. map ( path - > path . getFileName ( ) . toString ( ) ) // 提取文件名
. orElse ( "" ) ; // 如果没有符合条件的文件,返回空字符串
}
} catch ( IOException e ) {
log . error ( "读取目录失败: {}" , e . getMessage ( ) ) ;
return "" ;
}
}
// 检查摄像头目录中最新的文件名,且文件修改时间在指定时间之后
public String getLatestCameraFileNameAfterTime ( String app , String cameraId , LocalDateTime afterTime ) {
String mp4SavePath = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SavePath" ) . getValue ( ) ;
String mp4SaveApi = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SaveApi" ) . getValue ( ) ;
// 获取当前日期
LocalDate currentDate = LocalDate . now ( ) ;
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ;
String formattedDate = currentDate . format ( formatter ) ;
String directoryPath = mp4SavePath + "record/" + app + "/" + cameraId + "/" + formattedDate ;
String mp4SaveApiPath = mp4SaveApi + "record/" + app + "/" + cameraId + "/" + formattedDate + "/" ;
return mp4SaveApiPath + getLatestFileNameAfterTime ( directoryPath , afterTime ) ;
}
// 新增方法:检查指定文件夹下是否有以.为开头的文件
public String checkHiddenFilesInDirectory ( String app , String cameraId ) {
@ -113,6 +174,43 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
return "" ;
}
// 新增方法:检查指定文件夹下是否有以.为开头的文件
public String checkAndRenameFile ( String app , String cameraId , String filePath ) {
String mp4SavePath = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SavePath" ) . getValue ( ) ;
// mp4SaveApi
String mp4SaveApi = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SaveApi" ) . getValue ( ) ;
// 获取当前日期
LocalDate currentDate = LocalDate . now ( ) ;
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ;
// 格式化日期
String formattedDate = currentDate . format ( formatter ) ;
// 构建完整路径
String fullPath = mp4SavePath + "record/" + app + "/camera" + cameraId + "/" + formattedDate + "/." + filePath ;
String renamedPath = mp4SavePath + "record/" + app + "/camera" + cameraId + "/" + formattedDate + "/" + filePath ;
// 构建访问路径
String accessPath = mp4SaveApi + "record/" + app + "/camera" + cameraId + "/" + formattedDate + "/" + filePath ;
try {
Path originalFilePath = Paths . get ( fullPath ) ;
// 检查带点的文件是否存在
if ( Files . exists ( originalFilePath ) ) {
// 重命名为去掉点的文件名
Path newFilePath = Paths . get ( renamedPath ) ;
Files . move ( originalFilePath , newFilePath ) ;
return accessPath ;
}
} catch ( IOException e ) {
log . error ( "文件重命名失败: {}" , e . getMessage ( ) ) ;
}
return "" ;
}
/ * *
* 调 用 ZLMediaKit 的 getSnap 接 口 获 取 截 图 并 保 存
*
@ -155,9 +253,6 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
log . info ( "截图保存成功: {}" , savePath ) ;
// 返回访问路径(根据你的需求调整)
String mp4SaveApi = dictDataService . parseDictData ( "ZLMediaKit_conf" , "mp4SaveApi" ) . getValue ( ) ;
} else {
log . error ( "获取截图失败, HTTP状态码: {}" , response . code ( ) ) ;
@ -179,7 +274,7 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
return input ;
}
@Override
public RtspSessionResponse stopRecord ( String app , String stream ) {
public String stopRecord ( String app , String stream , LocalDateTime time ) {
String zlmApiSecret = dictDataService . parseDictData ( "ZLMediaKit_conf" , "secret" ) . getValue ( ) ;
Map < String , Object > addParams = new HashMap < > ( ) ;
@ -190,9 +285,15 @@ public class ZLMediaKitServiceImpl implements ZLMediaKitService{
addParams . put ( "stream" , "camera" + stream ) ;
// addParams.put("url", CameraChannel.getRtspUrl(camera.getIp(), camera.getRtspPort(), camera.getChannel(), camera.getUser(), camera.getPassword(),camera.getType()));
RtspSessionResponse rtspSessionResponse = sendHttp ( addParams , "stopRecord" ) ;
return rtspSessionResponse ;
if ( rtspSessionResponse ! = null & & rtspSessionResponse . isResult ( ) ) {
String fileName = getLatestCameraFileNameAfterTime ( app , "camera" + stream , time . minusSeconds ( 10 ) ) ;
return fileName ;
} else {
return "" ;
}
}
@Override
public void zlmConf ( List < CameraDO > list ) {