基础版 优化

merge-requests/3/head
qiushui 4 years ago
parent 5369365c9a
commit 01bb3b48f0

5
.gitignore vendored

@ -27,3 +27,8 @@ hs_err_pid*
.log .log
target target
!resources/libs !resources/libs
/web/duoji.iml
/springbootstart.iml
/modules/modules.iml
/modules/filter/filter.iml
/modules/common/common.iml

@ -57,4 +57,15 @@ public class PathUtil {
return file; return file;
} }
public static void deleteFile(String fileName) {
File file = new File(fileName);
if (file.isFile() && file.exists()) {
file.delete();
}
}
public static void rename(){
}
} }

@ -301,7 +301,7 @@ public class CameraControlController {
@ApiOperation(value = "录像") @ApiOperation(value = "录像")
public Result MP4(@PathVariable Integer id) { public Result MP4(@PathVariable Integer id) {
checkLogin(id); checkLogin(id);
cameraControlModule.downloadMp4(id,"D:\\work\\qqq_1.mp4", LocalDateTime.now().minusMinutes(10),LocalDateTime.now().minusMinutes(5)); cameraControlModule.downloadMp4(id,"D:\\work\\qqq.mp4", LocalDateTime.now().minusMinutes(10),LocalDateTime.now().minusMinutes(8));
return Result.success(); return Result.success();
} }

@ -1,5 +1,6 @@
package com.zhehekeji.web.lib.hik; package com.zhehekeji.web.lib.hik;
import com.sun.jna.ptr.IntByReference;
import com.zhehekeji.common.util.PathUtil; import com.zhehekeji.common.util.PathUtil;
import com.zhehekeji.core.util.Assert; import com.zhehekeji.core.util.Assert;
import com.zhehekeji.web.lib.CameraConnMap; import com.zhehekeji.web.lib.CameraConnMap;
@ -7,8 +8,12 @@ import com.zhehekeji.web.lib.CameraControlModule;
import com.zhehekeji.web.lib.joyware.NetSDKLib; import com.zhehekeji.web.lib.joyware.NetSDKLib;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
/** /**
* *
@ -252,24 +257,109 @@ public class HikCameraControlModuleImpl implements CameraControlModule {
log.info("start download mp4 path:{} ",path); log.info("start download mp4 path:{} ",path);
int lUserID = CameraConnMap.getConnId(cameraId).intValue(); int lUserID = CameraConnMap.getConnId(cameraId).intValue();
// HCNetSDK.NET_DVR_PLAYCOND net_dvr_playcond = new HCNetSDK.NET_DVR_PLAYCOND();
// net_dvr_playcond.struStartTime = startTime;
// net_dvr_playcond.struStopTime = startTime;
int result = HikLoginModuleImpl.hcNetsdk.NET_DVR_GetFileByTime_V40(lUserID,path.getBytes(StandardCharsets.UTF_8),struDownloadCond); int result = HikLoginModuleImpl.hcNetsdk.NET_DVR_GetFileByTime_V40(lUserID,path.getBytes(StandardCharsets.UTF_8),struDownloadCond);
if (result < 0) { if (result < 0) {
log.error("downloadMp4 error:{},cameraId:{}", HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError(),cameraId); log.error("downloadMp4 error code:{},cameraId:{}", HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError(),cameraId);
} else { } else {
Integer process = 0;
//Boolean ok1 = HikLoginModuleImpl.hcNetsdk.NET_DVR_PlayBackControl(result, HikLoginModuleImpl.hcNetsdk.NET_DVR_SET_TRANS_TYPE,process,null); Boolean ok = HikLoginModuleImpl.hcNetsdk.NET_DVR_PlayBackControl(result, HikLoginModuleImpl.hcNetsdk.NET_DVR_PLAYSTART,0,null);
Boolean ok = HikLoginModuleImpl.hcNetsdk.NET_DVR_PlayBackControl(result, HikLoginModuleImpl.hcNetsdk.NET_DVR_PLAYSTART,process,null);
if(!ok){ if(!ok){
log.error("downloadMp4 error:{},cameraId:{}", HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError(),cameraId); log.error("downloadMp4 error code:{},cameraId:{}", HikLoginModuleImpl.hcNetsdk.NET_DVR_GetLastError(),cameraId);
}
Timer downloadtimer = new Timer();
downloadtimer.schedule(new DownloadTask(result,downloadtimer,path), 0, 500);
}
}
class DownloadTask extends java.util.TimerTask {
private Integer handler;
private Timer downloadtimer;
private String path;
public DownloadTask(Integer handler,Timer downloadtimer,String path){
this.handler = handler;
this.downloadtimer = downloadtimer;
this.path = path;
}
//定时器函数
@Override
public void run()
{
IntByReference nPos = new IntByReference(0);
HikLoginModuleImpl.hcNetsdk.NET_DVR_PlayBackControl(handler, HCNetSDK.NET_DVR_PLAYGETPOS, 0, nPos);
if (nPos.getValue() > 100)
{
HikLoginModuleImpl.hcNetsdk.NET_DVR_StopGetFile(handler);
downloadtimer.cancel();
log.error("download mp4 error");
}
if (nPos.getValue() == 100)
{
HikLoginModuleImpl.hcNetsdk.NET_DVR_StopGetFile(handler);
downloadtimer.cancel();
//ffmpeg转码
log.info("download over");
String ffmpegFile = path+"ffmpeg.mp4";
convetor(path,ffmpegFile);
PathUtil.deleteFile(path);
File file = new File(ffmpegFile);
file.renameTo(new File(path));
}
}
}
public static void convetor(String videoInputPath, String videoOutPath){
List<String> command = new ArrayList<String>();
command.add("E:\\ffmpeg-N-104583-ge5367b481b-win64-gpl\\bin\\ffmpeg.exe");
command.add("-i");
command.add(videoInputPath);
command.add("-c");
command.add("copy");
command.add("-an");
command.add(videoOutPath);
ProcessBuilder builder = new ProcessBuilder(command);
Process process = null;
try {
process = builder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源所以这里我们需要对流进行处理
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
try {
while ((line = br.readLine()) != null) {
}
}catch (Exception e){
}finally {
try {
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}catch (Exception e){
} }
} }
} }
/** /**
* *
* *

@ -113,66 +113,4 @@ public class HikLoginModuleImpl implements CameraControlLoginModule {
return bRet; return bRet;
} }
@Data
public static class CameraCaptureMap {
private static Map<Integer, OrderInfo> cameraMap = new HashMap<>();
public static void put(Integer cameraId,OrderInfo orderInfo){
if(cameraId != null && orderInfo != null){
cameraMap.put(cameraId,orderInfo);
}
}
public static void remove(Integer cameraId){
cameraMap.remove(cameraId);
}
public static OrderInfo getOrderInfoByStreetId(Integer cameraId){
return cameraMap.get(cameraId);
}
}
public static class CameraConnMap{
/**
* key :cameraId
* value: loginId
*/
public static Map<Integer, NetSDKLib.LLong> cameraMap = new HashMap<>();
/**
* key:loginId
* value: cameraId
*/
public static Map<NetSDKLib.LLong,Integer> loginMap = new HashMap<>();
public static void conn(Integer cameraId,NetSDKLib.LLong handlerId){
synchronized (cameraId.toString().intern()){
cameraMap.put(cameraId,handlerId);
loginMap.put(handlerId,cameraId);
}
}
public static void disConn(Integer cameraId){
synchronized (cameraId.toString().intern()){
NetSDKLib.LLong lLong = cameraMap.get(cameraId);
cameraMap.remove(cameraId);
loginMap.remove(lLong);
}
}
public static NetSDKLib.LLong getConnId(Integer cameraId){
return cameraMap.get(cameraId);
}
public static Integer getCameraIdByLoginId(NetSDKLib.LLong lLong){
return loginMap.get(lLong);
}
}
} }

Loading…
Cancel
Save