| | |
| | | import com.ard.common.core.exception.ServiceException; |
| | | import com.ard.common.core.exception.device.CameraSDKException; |
| | | import com.ard.common.core.utils.StringUtils; |
| | | import com.ard.common.core.utils.file.FileMultipartFile; |
| | | import com.ard.common.core.utils.file.FileUtils; |
| | | import com.ard.common.core.utils.file.MimeTypeUtils; |
| | | import com.ard.common.redis.service.RedisService; |
| | |
| | | import com.ard.work.sdk.model.PtzScope; |
| | | import com.ard.work.utils.ArdTool; |
| | | import com.ard.work.utils.FFmpegUtils; |
| | | import com.ard.work.utils.file.MultipartFileUtil; |
| | | import com.ard.work.utils.gis.GisUtil; |
| | | import com.sun.jna.Pointer; |
| | | import com.sun.jna.ptr.IntByReference; |
| | |
| | | import org.springframework.util.StopWatch; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.concurrent.CompletableFuture; |
| | |
| | | |
| | | private String hikRecordStop(CameraCmd cmd) { |
| | | String url = ""; |
| | | File videoFile = null; |
| | | File transcodeFile = null; |
| | | |
| | | try { |
| | | String cameraId = cmd.getCameraId(); |
| | | Integer chanNo = cmd.getChanNo(); |
| | | String name = cameraId + "_" + chanNo; |
| | | |
| | | // 本地临时录像地址 |
| | | String path = FileUtils.createFile(tempDir + "/record/" + name + ".mp4"); |
| | | |
| | | Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId); |
| | | if (userId == null) { |
| | | throw new ServiceException("设备离线"); |
| | | } |
| | | //region 强制I帧 |
| | | NET_DVR_I_FRAME netDvrIFrame = new NET_DVR_I_FRAME(); //新建结构体对象 |
| | | netDvrIFrame.read(); |
| | | netDvrIFrame.dwChannel = chanNo;//因为上文代码中设置了通道号,按照上文中的设置 |
| | | netDvrIFrame.byStreamType = 0; |
| | | netDvrIFrame.dwSize = netDvrIFrame.size(); |
| | | netDvrIFrame.write(); |
| | | if (!hCNetSDK.NET_DVR_RemoteControl(userId.intValue(), 3402, netDvrIFrame.getPointer(), |
| | | netDvrIFrame.dwSize)) { |
| | | log.error("强制I帧 错误码为:{}", hCNetSDK.NET_DVR_GetLastError()); |
| | | } |
| | | //endregion |
| | | //region 预览参数 |
| | | NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO(); |
| | | previewInfo.read(); |
| | | previewInfo.lChannel = chanNo; |
| | | previewInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推 |
| | | previewInfo.dwLinkMode = 0;//连接方式:0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RTP/HTTP,6-HRUDP(可靠传输),7 |
| | | // -RTSP/HTTPS,8-NPQ |
| | | previewInfo.hPlayWnd = null;//播放窗口的句柄,为NULL表示不解码显示。 |
| | | previewInfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流 |
| | | previewInfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式 |
| | | previewInfo.write(); |
| | | //endregion |
| | | |
| | | // 停止录像 |
| | | if (GlobalVariable.previewMap.containsKey(name)) { |
| | | Integer lRealHandle = (Integer) GlobalVariable.previewMap.get(name); |
| | | hCNetSDK.NET_DVR_StopRealPlay(lRealHandle); |
| | | GlobalVariable.previewMap.remove(name); |
| | | } |
| | | log.debug("录像停止"); |
| | | //ffmpeg转码 |
| | | |
| | | // 检查原录像文件是否存在 |
| | | videoFile = new File(path); |
| | | if (!videoFile.exists() || videoFile.length() == 0) { |
| | | log.error("录像文件不存在或为空: {}", path); |
| | | return ""; |
| | | } |
| | | |
| | | // ffmpeg转码 |
| | | StopWatch watchStop = new StopWatch(); |
| | | watchStop.start(); |
| | | String newpath = FileUtils.createFile(tempDir + "/record/" + name + "_c.mp4"); |
| | | transcodeFile = new File(newpath); |
| | | FFmpegUtils.transcodeToMP4(path, newpath); |
| | | watchStop.stop(); |
| | | log.warn("转码耗时:{}", watchStop.getTotalTimeMillis()); |
| | | //存入minio |
| | | |
| | | // 检查转码后的文件是否存在 |
| | | if (!transcodeFile.exists() || transcodeFile.length() == 0) { |
| | | log.error("转码文件不存在或为空: {}", newpath); |
| | | return ""; |
| | | } |
| | | |
| | | // 存入minio - 使用 FileMultipartFile |
| | | String bucketName = "record"; |
| | | String objectName = cameraId + "_" + chanNo; |
| | | String type = cmd.getType(); |
| | | MultipartFile multipartFile = MultipartFileUtil.filePathToMultipartFile(newpath, objectName, |
| | | MimeTypeUtils.VIDEO_MP4); |
| | | String objectName = cameraId + "_" + chanNo + ".mp4"; |
| | | String type = cmd.getType() == null ? "record" : cmd.getType(); |
| | | |
| | | MultipartFile multipartFile = new FileMultipartFile(transcodeFile, objectName, MimeTypeUtils.VIDEO_MP4); |
| | | R<SysFile> sysFileR = remoteFileService.upload(multipartFile, bucketName, type); |
| | | |
| | | if (sysFileR.getCode() == R.SUCCESS) { |
| | | url = sysFileR.getData().getUrl(); |
| | | log.info("上传文件成功:{}", url); |
| | | } |
| | | |
| | | return url; |
| | | |
| | | } catch (Exception ex) { |
| | | log.error("录像异常:{}", ex.getMessage()); |
| | | return ""; |
| | | } finally { |
| | | // 清理临时文件 |
| | | try { |
| | | if (videoFile != null && videoFile.exists()) { |
| | | videoFile.delete(); |
| | | log.debug("删除原录像文件: {}", videoFile.getPath()); |
| | | } |
| | | if (transcodeFile != null && transcodeFile.exists()) { |
| | | transcodeFile.delete(); |
| | | log.debug("删除转码文件: {}", transcodeFile.getPath()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("删除临时文件失败", e); |
| | | } |
| | | } |
| | | } |
| | | |