CameraUtil.java 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. package com.ozs.utils;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.hikvision.artemis.sdk.ArtemisHttpUtil;
  8. import com.hikvision.artemis.sdk.config.ArtemisConfig;
  9. import com.ozs.common.config.BaseConfig;
  10. import com.ozs.common.constant.Constants;
  11. import com.ozs.common.core.domain.AjaxResult;
  12. import com.ozs.common.core.domain.entity.SysDictData;
  13. import com.ozs.common.core.redis.RedisCache;
  14. import com.ozs.common.exception.base.BaseException;
  15. import com.ozs.common.utils.DateUtils;
  16. import com.ozs.common.utils.HttpClientUtil;
  17. import com.ozs.common.utils.http.HttpUtils;
  18. import com.ozs.entity.BaseCameraManagement;
  19. import com.ozs.entity.BaseTerminal;
  20. import com.ozs.entity.BaseTerminalLog;
  21. import com.ozs.entity.MsgHeartbeatAlarmMessage;
  22. import com.ozs.entity.response.BaseCameraManagementResponse;
  23. import com.ozs.entity.vo.CamerasVo;
  24. import com.ozs.entity.vo.PlaybackVo;
  25. import com.ozs.framework.config.ServerConfig;
  26. import com.ozs.service.BaseCameraManagementService;
  27. import com.ozs.service.BaseTerminalLogService;
  28. import com.ozs.service.BaseTerminalService;
  29. import com.ozs.service.MsgHeartbeatAlarmMessageService;
  30. import com.ozs.system.mapper.SysDictDataMapper;
  31. import com.ozs.system.service.ISysDictTypeService;
  32. import lombok.SneakyThrows;
  33. import lombok.extern.slf4j.Slf4j;
  34. import okhttp3.Response;
  35. import org.apache.commons.lang3.StringUtils;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.context.annotation.Configuration;
  38. import org.springframework.util.ObjectUtils;
  39. import javax.annotation.PostConstruct;
  40. import javax.annotation.Resource;
  41. import java.io.BufferedReader;
  42. import java.io.BufferedWriter;
  43. import java.io.File;
  44. import java.io.FileFilter;
  45. import java.io.IOException;
  46. import java.io.InputStreamReader;
  47. import java.io.OutputStreamWriter;
  48. import java.io.PrintWriter;
  49. import java.text.ParseException;
  50. import java.text.SimpleDateFormat;
  51. import java.time.Duration;
  52. import java.time.Instant;
  53. import java.time.LocalDateTime;
  54. import java.time.OffsetDateTime;
  55. import java.time.ZonedDateTime;
  56. import java.time.format.DateTimeFormatter;
  57. import java.util.ArrayList;
  58. import java.util.Calendar;
  59. import java.util.Date;
  60. import java.util.GregorianCalendar;
  61. import java.util.HashMap;
  62. import java.util.List;
  63. import java.util.Map;
  64. import java.util.Random;
  65. import java.util.Set;
  66. import java.util.UUID;
  67. import java.util.concurrent.Callable;
  68. import java.util.concurrent.CompletableFuture;
  69. import java.util.concurrent.ExecutionException;
  70. import java.util.concurrent.ExecutorService;
  71. import java.util.concurrent.Executors;
  72. import java.util.concurrent.TimeUnit;
  73. import java.util.concurrent.TimeoutException;
  74. import java.util.stream.Collectors;
  75. /**
  76. * 相机工具
  77. */
  78. @Configuration
  79. @Slf4j
  80. public class CameraUtil {
  81. private static final ExecutorService executor = Executors.newFixedThreadPool(20);
  82. private static String historyUrl;
  83. private static String ffmpegPath;
  84. private static String filePath;
  85. private static String transcribeFilePath;
  86. private static String webUrl;
  87. private static String bakUrl;
  88. private static String bakUrlRtsp;
  89. private static String wsUrl;
  90. private static CmdCameraUtil cUtil;
  91. private static RedisCache rc;
  92. private static ServerConfig sc;
  93. private static String recordUrl;
  94. private static String hkUrl;
  95. private static String host;
  96. private static String appKey;
  97. private static String appSecret;
  98. private static String rtmpUrl;
  99. @Autowired
  100. private CaneraConfig caneraConfig;
  101. @Autowired
  102. private CmdCameraUtil cmdCameraUtil;
  103. @Autowired
  104. private SysDictDataMapper dictDataMapper;
  105. @Autowired
  106. private RedisCache redisCache;
  107. @Autowired
  108. private ServerConfig serverConfig;
  109. @Autowired
  110. private BaseTerminalService baseTerminalService;
  111. @Autowired
  112. private MsgHeartbeatAlarmMessageService msgHeartbeatAlarmMessageService;
  113. @Autowired
  114. private ISysDictTypeService dictTypeService;
  115. @Autowired
  116. private RtspToMP4 rtspToMP4;
  117. @Resource
  118. BaseCameraManagementService baseCameraManagementService;
  119. @Resource
  120. private BaseTerminalLogService baseTerminalLogService;
  121. private Map<String, Process> map = new HashMap<>();
  122. public final static String tsFilekey = "mergeVideoTsFile";
  123. /**
  124. * 历史回放
  125. *
  126. * @param fromVideoFileList
  127. * @param ph
  128. * @return
  129. * @throws IOException
  130. */
  131. public static String historyPlay(List<String> fromVideoFileList, String ph, boolean flay) {
  132. // 视频服务映射路径
  133. String NewfilePath = BaseConfig.getProfile() + "/" + ph;
  134. log.info("NewfilePath:{}", NewfilePath);
  135. log.info("fromVideoFileList:{}", fromVideoFileList);
  136. if (ObjectUtils.isEmpty(fromVideoFileList) || fromVideoFileList.size() <= 0) {
  137. throw new BaseException("当前相机无视频录像");
  138. }
  139. executor.submit(new Runnable() {
  140. @Override
  141. public void run() {
  142. try {
  143. txConvetor(fromVideoFileList, NewfilePath, flay);
  144. } catch (IOException e) {
  145. log.error(e.getMessage());
  146. e.printStackTrace();
  147. }
  148. }
  149. });
  150. // executor.shutdown();
  151. return Constants.RESOURCE_PREFIX + "/" + ph;
  152. }
  153. @SneakyThrows
  154. public static void txConvetor(List<String> fromVideoFileList,
  155. String newfilePath,
  156. boolean fly) throws IOException {
  157. /*
  158. * ffmpeg -i 20230411_155847_155947-d4c2265d-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 1.ts
  159. ffmpeg -i 20230411_155948_160048-f91fea03-d83e-11ed-8e7f-fa163e4e1e9f.flv -c:v copy 2.ts
  160. ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
  161. * */
  162. File file = new File(newfilePath);
  163. boolean flay = false;
  164. if (!file.getParentFile().exists()) {
  165. boolean mkdirs = file.getParentFile().mkdirs();
  166. log.info("创建文件夹:{}", file.getParentFile().getPath());
  167. log.info("创建文件夹结果:{}", mkdirs);
  168. flay = true;
  169. }
  170. cUtil.cmd("chomd -R 777 " + file.getParentFile().getPath());
  171. log.info("newfilePath:{}", newfilePath);
  172. StringBuffer sm = new StringBuffer(ffmpegPath + " -i \"concat:");
  173. List<String> fileTs = new ArrayList<>();
  174. for (int t = 0; t < fromVideoFileList.size(); t++) {
  175. File ft = new File(fromVideoFileList.get(t));
  176. if (ft.exists()) {
  177. log.info("file:{}", fromVideoFileList.get(t));
  178. String substring = fromVideoFileList.get(t).substring(0, fromVideoFileList.get(t).lastIndexOf("."));
  179. int x;//定义两变量
  180. Random ne = new Random();//实例化一个random的对象ne
  181. x = ne.nextInt(9999 - 1000 + 1) + 1000;//为变量赋随机值1000-9999
  182. substring = substring + x;//定义两变量
  183. String cmdstr = ffmpegPath + " -i " + fromVideoFileList.get(t) + " -c:v copy " + substring + ".ts";
  184. log.info("转换命令:{}", cmdstr);
  185. cUtil.cmd(cmdstr);
  186. fileTs.add(substring + ".ts");
  187. if (t != fromVideoFileList.size() - 1) {
  188. sm.append(substring + ".ts|");
  189. } else {
  190. sm.append(substring + ".ts\" ");
  191. }
  192. }
  193. }
  194. if (fileTs.size() > 0) {
  195. Map<String, Object> mergeVideoTsFile = rc.getCacheMap(tsFilekey);
  196. if (ObjectUtils.isEmpty(mergeVideoTsFile)) {
  197. mergeVideoTsFile = new HashMap<>();
  198. }
  199. if (!flay) {
  200. // 如果没有解除,把生成的文件放入要删除的定时任务 redis key 中
  201. fileTs.add(newfilePath);
  202. }
  203. mergeVideoTsFile.put(System.currentTimeMillis() + "", fileTs);
  204. rc.deleteObject(tsFilekey);
  205. if (mergeVideoTsFile.size() > 0) {
  206. rc.setCacheMap(tsFilekey, mergeVideoTsFile);
  207. }
  208. sm.append("-c copy " + newfilePath);
  209. log.info("合并命令:{}", sm.toString());
  210. cUtil.cmd(sm.toString());
  211. }
  212. }
  213. /**
  214. * 实时播放的拼接流
  215. *
  216. * @param cameraCode 相机编码
  217. * @param channel 相机通道
  218. * @return
  219. */
  220. public static String getPlayFlv(String cameraCode, String channel, boolean flay) {
  221. if (!flay) {
  222. return wsUrl + "/ws/" + cameraCode + "/" + channel + ".flv";
  223. }
  224. return wsUrl + "/ws/" + cameraCode + "/" + channel + ".flv";
  225. // if (!flay) {
  226. // return bakUrl + "/ws/" + channel + "/" + cameraCode + ".flv";
  227. // }
  228. // return webUrl + "/ws/" + channel + "/" + cameraCode + ".flv";
  229. }
  230. /**
  231. * Rtsp实时播放的拼接流
  232. *
  233. * @param cameraCode 相机编码
  234. * @param channel 相机通道
  235. * @return
  236. */
  237. public static String getPlayFlvRtsp(String cameraCode, String channel, boolean flay) {
  238. if (!flay) {
  239. return webUrl + "/" + channel + "/" + cameraCode;
  240. }
  241. return webUrl + "/" + channel + "/" + cameraCode;
  242. // if (!flay) {
  243. // return bakUrl + "/ws/" + channel + "/" + cameraCode + ".flv";
  244. // }
  245. // return webUrl + "/ws/" + channel + "/" + cameraCode + ".flv";
  246. }
  247. /**
  248. * web页面实时流接口
  249. *
  250. * @param cameraCode
  251. * @param channel
  252. * @return
  253. */
  254. public static String getPlayFlv(String cameraCode, String channel) {
  255. // List<String> pathList = new ArrayList<>();
  256. // String data = HttpUtils.sendGet(webUrl + "/rtsp/api/list");
  257. // if (StringUtils.isNotEmpty(data)) {
  258. // JSONArray jsonArray = JSONArray.parseArray(data);
  259. // for (int i = 0; i < jsonArray.size(); i++) {
  260. // JSONObject jsonObject = jsonArray.getJSONObject(i);
  261. // String name = jsonObject.getString("Path");
  262. // pathList.add(name);
  263. // }
  264. // boolean contains = pathList.contains(cameraCode + "/" + channel);
  265. // if (contains) {
  266. // return getPlayFlv(cameraCode, channel, true);
  267. // } else {
  268. // /**
  269. // * jsonBody.put("cameraIndexCode", "01ea43e6676f4e47bd6c5cd9e02aa006");
  270. // * jsonBody.put("streamType", 0);
  271. // * jsonBody.put("protocol","rtsp");
  272. // * jsonBody.put("transmode", 1);
  273. // * jsonBody.put("expand","streamform=rtp");
  274. // */
  275. // CamerasVo camerasVo = new CamerasVo();
  276. // camerasVo.setCameraIndexCode(cameraCode);
  277. // camerasVo.setStreamType(0);
  278. // camerasVo.setTransmode(1);
  279. // camerasVo.setProtocol("rtsp");
  280. // camerasVo.setExpand("streamform=rtp");
  281. // previewURLs(camerasVo, channel);
  282. // return getPlayFlv(cameraCode, channel, true);
  283. // }
  284. // } else {
  285. // return "流媒体目前没有RTSP协议的流";
  286. // }
  287. return getPlayFlv(cameraCode, channel, true);
  288. }
  289. public static String invite(String cameraCode, String channel) {
  290. String result = null;
  291. String url = bakUrl + "/api/gb28181/invite?id=" + cameraCode + "&channel=" + channel;
  292. try {
  293. Response response = HttpClientUtil.get(url);
  294. log.info("result:{}", String.valueOf(response.code()));
  295. return String.valueOf(response.code());
  296. } catch (Exception e) {
  297. log.info(e.getMessage());
  298. e.printStackTrace();
  299. }
  300. return result;
  301. }
  302. /**
  303. * 开启录制功能
  304. *
  305. * @param cameraCode 相机编码
  306. * @param channel 相机通道
  307. * @return
  308. */
  309. public static String startRecording(String cameraCode, String channel) {
  310. return historyUrl + "/recordpro/api/start?streamPath=" + channel + "/" + cameraCode;
  311. }
  312. /**
  313. * 关闭录制功能
  314. *
  315. * @param taskId 录像接口返回的任务ID
  316. * @return
  317. */
  318. public static String endRecording(String taskId) {
  319. return historyUrl + "/recordpro/api/stop?id=" + taskId;
  320. }
  321. /**
  322. * 历史回放流(获取)
  323. *
  324. * @param channel 相机通道
  325. * @param startTm 开始时间
  326. * @param endTm 结束时间
  327. * @return
  328. */
  329. public static String historyPlayListStr(String channel, Date startTm, Date endTm, boolean flay) {
  330. List<String> list = filterPlayList(channel, startTm, endTm, filePath);
  331. String uuid = UUID.randomUUID().toString();
  332. String ph = "record/flv/" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date())
  333. + "/" + channel + "/"
  334. + uuid + ".mp4";
  335. if (!ObjectUtils.isEmpty(list)) {
  336. try {
  337. return historyPlay(list, ph, flay);
  338. } catch (Exception e) {
  339. log.error(e.getMessage());
  340. e.printStackTrace();
  341. }
  342. } else {
  343. throw new BaseException("当前相机无视频录像");
  344. }
  345. return null;
  346. }
  347. /**
  348. * 过滤符合条件的视频
  349. *
  350. * @param channel
  351. * @param startTm
  352. * @param endTm
  353. * @param mappingUrl
  354. * @return
  355. */
  356. public static List<String> filterPlayList(String channel, Date startTm, Date endTm, String mappingUrl) {
  357. if (StringUtils.isBlank(channel)
  358. || ObjectUtils.isEmpty(startTm)
  359. || ObjectUtils.isEmpty(endTm)) {
  360. return null;
  361. }
  362. List<String> ls = new ArrayList<>();
  363. Map<Date, String> m = new HashMap<>();
  364. // 调用视频服务返回参数
  365. String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startTm);
  366. String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endTm);
  367. String param = "channel=" + channel + "&startTime=" + startTime + "&endTime=" + endTime;
  368. String s = HttpUtils.sendGet(historyUrl + "/api/record/flv/list", param);
  369. // 视频拼接
  370. if (!StringUtils.isBlank(s) && !"null".equals(s) && !s.startsWith("<!DOCTYPE html>")) {
  371. List<Map<String, Object>> maps = JSON.parseArray(s, Map.class);
  372. if (ObjectUtils.isEmpty(maps)) {
  373. return null;
  374. }
  375. for (Map<String, Object> map : maps) {
  376. Object path = map.get("Path");
  377. if (!ObjectUtils.isEmpty(path)) {
  378. String s1 = path.toString();
  379. String substring = s1.substring(s1.lastIndexOf("/") + 1, s1.length());
  380. String substring1 = substring.substring(0, substring.indexOf("-"));
  381. String[] s2 = substring1.split("_");
  382. if (!ObjectUtils.isEmpty(s2)) {
  383. String s3 = s2[0] + s2[1];
  384. String s4 = s2[0] + s2[2];
  385. Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s3);
  386. if (s2[1].startsWith("23") && s2[2].startsWith("00")) {
  387. sdate = DateUtils.addDays(sdate, -1);
  388. }
  389. Date edate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s4);
  390. /* sdate |startTm| edate |endTm| */
  391. if (startTm.compareTo(sdate) >= 0
  392. && startTm.compareTo(edate) <= 0
  393. && endTm.compareTo(edate) >= 0) {
  394. m.put(sdate, mappingUrl + path.toString());
  395. /* |startTm| sdate edate |endTm| */
  396. } else if (startTm.compareTo(sdate) <= 0 && endTm.compareTo(edate) >= 0) {
  397. m.put(sdate, mappingUrl + path.toString());
  398. /* |startTm| sdate |endTm| edate */
  399. } else if (startTm.compareTo(sdate) <= 0
  400. && endTm.compareTo(sdate) >= 0
  401. && endTm.compareTo(edate) <= 0) {
  402. m.put(sdate, mappingUrl + path.toString());
  403. /* sdate |startTm| |endTm| edate */
  404. } else if (startTm.compareTo(sdate) >= 0 && endTm.compareTo(edate) <= 0) {
  405. m.put(sdate, mappingUrl + path.toString());
  406. }
  407. }
  408. }
  409. }
  410. }
  411. if (!ObjectUtils.isEmpty(m) && m.size() > 0) {
  412. Set<Date> dates = m.keySet();
  413. // 排序
  414. dates.stream().parallel().collect(Collectors.toList()).stream().sorted().forEach(d -> {
  415. ls.add(m.get(d));
  416. });
  417. return ls;
  418. }
  419. return null;
  420. }
  421. /**
  422. * 合并视频(转化文件)
  423. *
  424. * @param fromVideoFileList 视频路径
  425. * @param newfilePath 生产新的视频文件路径
  426. * @throws IOException
  427. */
  428. public static Map<String, String> myConvetor(List<String> fromVideoFileList,
  429. String newfilePath,
  430. String uuid) throws IOException {
  431. /*
  432. * for f in *.flv; do echo "file '$f'" >> mylist.txt; done
  433. ffmpeg -f concat -i mylist.txt -c copy output.flv
  434. * */
  435. File file = new File(newfilePath);
  436. boolean flay = false;
  437. if (!file.getParentFile().exists()) {
  438. file.getParentFile().mkdirs();
  439. log.info("创建文件夹:{}", file.getParentFile().getPath());
  440. flay = true;
  441. }
  442. log.info("newfilePath:{}", newfilePath);
  443. StringBuffer sm = new StringBuffer("for f in ");
  444. String pathStr = null;
  445. for (int t = 0; t < fromVideoFileList.size(); t++) {
  446. File ft = new File(fromVideoFileList.get(t));
  447. if (ft.exists()) {
  448. if (t != fromVideoFileList.size() - 1) {
  449. // sm.append(fromVideoFileList.get(t) + " ");
  450. sm.append(fromVideoFileList.get(t).substring(fromVideoFileList.get(t).lastIndexOf("/") + 1, fromVideoFileList.get(t).length()) + " ");
  451. } else {
  452. // sm.append(fromVideoFileList.get(t));
  453. sm.append(fromVideoFileList.get(t).substring(fromVideoFileList.get(t).lastIndexOf("/") + 1, fromVideoFileList.get(t).length()));
  454. pathStr = fromVideoFileList.get(t).substring(0, fromVideoFileList.get(t).lastIndexOf("/"));
  455. }
  456. }
  457. }
  458. // String substring = newfilePath.substring(0, newfilePath.lastIndexOf("."));
  459. // substring = substring + ".txt";
  460. String substring = uuid + ".txt";
  461. log.info("临时转化的文件:{}", substring);
  462. sm.append("; do echo \"file '$f'\" >> " + substring + "; done");
  463. String commit = sm.toString();
  464. log.info("合并转化文件的命令:{}", commit);
  465. Process proc = Runtime.getRuntime().exec("/bin/bash", null, null);
  466. BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  467. PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
  468. try {
  469. String commit1 = ffmpegPath + " -f concat -safe 0 -i " + pathStr + "/" + substring + " -c copy " + newfilePath;
  470. log.info("合并视频命令:{}", commit1);
  471. List<String> commands = new ArrayList<>();
  472. if (flay) {
  473. commands.add("chomd -R 777 " + file.getParentFile().getPath());
  474. }
  475. //
  476. commands.add("cd " + pathStr);
  477. // 合并成一个临时文件
  478. commands.add(commit);
  479. // 临时文件转化为 flv
  480. // commands.add(commit1);
  481. log.info("录像视频命令:{}", commands);
  482. for (String line : commands) {
  483. log.info("命令:{}", line);
  484. out.println(line);
  485. }
  486. out.println("exit");// 这个命令必须执行,否则in流不结束。
  487. log.info("命令:{}", "exit");
  488. String rspLine = "";
  489. while ((rspLine = in.readLine()) != null) {
  490. log.info("*****:{}", rspLine);
  491. }
  492. proc.waitFor();
  493. Map<String, String> map = new HashMap<>();
  494. map.put("cmd", commit1);
  495. map.put("path", pathStr + "/" + substring);
  496. return map;
  497. } catch (InterruptedException e) {
  498. log.error(e.getMessage());
  499. e.printStackTrace();
  500. } finally {
  501. in.close();
  502. out.close();
  503. proc.destroy();
  504. }
  505. return null;
  506. }
  507. /**
  508. * web页面视频回放接口
  509. *
  510. * @param code
  511. * @param startTm
  512. * @param endTm
  513. * @return
  514. */
  515. public static String getRecordList(String code, Date startTm, Date endTm) {
  516. PlaybackVo playbackVo = new PlaybackVo();
  517. playbackVo.setCameraIndexCode(code);
  518. // 格式化时间
  519. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  520. playbackVo.setBeginTime(dateFormat.format(startTm));
  521. playbackVo.setEndTime(dateFormat.format(endTm));
  522. playbackVo.setProtocol("ws");
  523. //对象转换json字符串
  524. String body = JSONObject.toJSONString(playbackVo);
  525. //调用天网接口
  526. String playbackURLs = getPreviewURLs("/api/video/v1/cameras/playbackURLs", body);
  527. JSONObject outJson = (JSONObject) JSONObject.parse(playbackURLs);
  528. log.info("outJson-------->" + outJson);
  529. if ("0".equals(outJson.getString("code"))) {
  530. JSONObject data = outJson.getJSONObject("data");
  531. String urls = data.getString("url");
  532. return urls;
  533. } else {
  534. return "调用天网接口失败";
  535. }
  536. // return filterRecordList(channel, startTm, endTm, filePath, recordUrl + "profile/");
  537. }
  538. /**
  539. * 定时任务参数flv文件
  540. *
  541. * @throws IOException
  542. * @throws InterruptedException
  543. */
  544. public void deleteFlv() throws IOException, InterruptedException {
  545. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
  546. Date date = new Date();
  547. Calendar calendar = new GregorianCalendar();
  548. calendar.setTime(date);
  549. calendar.add(Calendar.DATE, -1); //把日期往后增加一天,整数 往后推,负数往前移动
  550. date = calendar.getTime(); //这个时间就是日期往后推一天的结果
  551. String path = BaseConfig.getProfile() + "/flv/" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
  552. File file = new File(path);
  553. if (file.exists()) {
  554. List<String> rspList = new ArrayList<String>();
  555. Process proc = Runtime.getRuntime().exec("/bin/bash", null, null);
  556. BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  557. PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
  558. String commit = " rm -rf " + path;
  559. List<String> commands = new ArrayList<>();
  560. // 删除
  561. commands.add(commit);
  562. log.info("删除昨天的录像视频命令:{}", commands);
  563. for (String line : commands) {
  564. out.println(line);
  565. }
  566. out.println("exit");// 这个命令必须执行,否则in流不结束。
  567. String rspLine = "";
  568. while ((rspLine = in.readLine()) != null) {
  569. System.out.println(rspLine);
  570. rspList.add(rspLine);
  571. }
  572. int i = proc.waitFor();
  573. log.info("执行结果:{}", i);
  574. in.close();
  575. out.close();
  576. proc.destroy();
  577. }
  578. }
  579. /**
  580. * 定时任务:删除超过配置时长的录制视频
  581. *
  582. * @throws IOException
  583. * @throws InterruptedException
  584. */
  585. public void deleteFlvExceed() throws IOException, InterruptedException, ParseException {
  586. //字典中设置的值
  587. List<SysDictData> sysCameraRecordTime = dictDataMapper.selectDictDataByType("sys_camera_record_time");
  588. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  589. Date date = new Date();
  590. Calendar calendar = new GregorianCalendar();
  591. calendar.setTime(date);
  592. //正数,日期天数加
  593. //负数,日期天数减 提前45天
  594. if (!ObjectUtils.isEmpty(sysCameraRecordTime)) {
  595. String dictValue = sysCameraRecordTime.get(0).getDictValue();
  596. calendar.add(Calendar.DAY_OF_YEAR, Integer.parseInt("-" + dictValue));
  597. } else {
  598. calendar.add(Calendar.DAY_OF_YEAR, -45);
  599. }
  600. date = calendar.getTime();
  601. //遍历每个进行视频录制的摄像头
  602. File file = new File(transcribeFilePath);
  603. log.info("file.Name()======================================" + file.getName());
  604. log.info("file.exists()======================================" + file.exists());
  605. if (file.exists() && file.isDirectory()) {
  606. //获取文件夹中所有的子文件夹和文件
  607. File[] files = file.listFiles();
  608. if (!ObjectUtils.isEmpty(files) && files.length > 0) {
  609. for (File file1 : files) {
  610. // file1=/opt/streams/record/flv/34020000001320000167
  611. log.info("file1.Name()======================================" + file1.getName());
  612. if (file1.exists() && file1.isDirectory()) {
  613. //相机文件
  614. File[] fs = file1.listFiles();
  615. if (!ObjectUtils.isEmpty(fs) && fs.length > 0) {
  616. for (File f : fs) {
  617. // f=/opt/streams/record/flv/34020000001320000167/20230328_192033_192037-91509173-cd5a-11ed-8a42-fa163e4e1e9f.flv
  618. //fName=20230328_192033_192037-91509173-cd5a-11ed-8a42-fa163e4e1e9f.flv
  619. String fName = f.getName();
  620. log.info("fName======================================" + fName);
  621. String[] split = fName.split("_");
  622. if (split.length > 0) {
  623. //20230328
  624. String s = split[0];
  625. Date parse = sdf.parse(s);
  626. long fTime = parse.getTime();
  627. long dateTime = date.getTime();
  628. //删除过期文件
  629. if (fTime < dateTime) {
  630. f.delete();
  631. }
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. }
  640. public static File[] getCurFilesList(String filePath) {
  641. File path = new File(filePath);
  642. File[] listFiles = path.listFiles(new FileFilter() {
  643. @Override
  644. public boolean accept(File pathname) {
  645. if (pathname.isFile()) {
  646. return true;
  647. } else {
  648. return false;
  649. }
  650. }
  651. });
  652. return listFiles;
  653. }
  654. public static void execute(String command) {
  655. try {
  656. ProcessBuilder process = new ProcessBuilder(command);
  657. process.inheritIO().start().waitFor();
  658. } catch (Exception e) {
  659. e.printStackTrace();
  660. }
  661. }
  662. /**
  663. * web页面实时流调用的远程天网接口
  664. *
  665. * @param camerasVo
  666. * @param channel
  667. */
  668. public static void previewURLs(CamerasVo camerasVo, String channel) {
  669. /**
  670. * jsonBody.put("cameraIndexCode", "01ea43e6676f4e47bd6c5cd9e02aa006");
  671. * jsonBody.put("streamType", 0);
  672. * jsonBody.put("protocol","rtsp");
  673. * jsonBody.put("transmode", 1);
  674. * jsonBody.put("expand","streamform=rtp");
  675. */
  676. camerasVo.setStreamType(0);
  677. camerasVo.setProtocol("rtsp");
  678. camerasVo.setTransmode(1);
  679. camerasVo.setExpand("streamform=rtp");
  680. String body = JSONObject.toJSONString(camerasVo);
  681. String previewURLs = getPreviewURLs("/api/video/v1/cameras/previewURLs", body);
  682. log.info("-------------------------------->>>>>previewURLs" + previewURLs);
  683. JSONObject outJson = JSONObject.parse(previewURLs);
  684. if ("0".equals(outJson.getString("code"))) {
  685. log.info("outJson----->" + outJson);
  686. JSONObject data = outJson.getJSONObject("data");
  687. String urls = data.getString("url");
  688. HttpUtils.sendGet(historyUrl + "rtsp/api/pull?target=" + urls + "&streamPath=" + camerasVo.getCameraIndexCode() + "/" + channel + "&save=0");
  689. }
  690. }
  691. @PostConstruct
  692. public void init() {
  693. historyUrl = caneraConfig.getHistoryUrl();
  694. ffmpegPath = caneraConfig.getFfmpegPath();
  695. filePath = caneraConfig.getFilePath();
  696. transcribeFilePath = caneraConfig.getTranscribeFilePath();
  697. webUrl = caneraConfig.getWebUrl();
  698. bakUrl = caneraConfig.getBakUrl();
  699. bakUrlRtsp = caneraConfig.getBakUrlRtsp();
  700. rc = redisCache;
  701. cUtil = cmdCameraUtil;
  702. sc = serverConfig;
  703. wsUrl = caneraConfig.getWsUrl();
  704. recordUrl = caneraConfig.getRecordUrl();
  705. hkUrl = caneraConfig.getHkUrl();
  706. host = caneraConfig.getHost();
  707. appKey = caneraConfig.getAppKey();
  708. appSecret = caneraConfig.getAppSecret();
  709. rtmpUrl = caneraConfig.getRtmpUrl();
  710. }
  711. /**
  712. * HTTP流转RTSP流
  713. */
  714. public String streamConversion(String code) {
  715. ExecutorService executor = Executors.newSingleThreadExecutor();
  716. Callable<String> task = new Callable<String>() {
  717. @Override
  718. public String call() throws Exception {
  719. // 执行网络请求...
  720. log.info(ffmpegPath + " -re -i /opt/streams/compress/" + code + ".mp4 -c:v copy -c:a copy -f flv " + rtmpUrl + "/live/" + code);
  721. cmdCameraUtil.cmd(ffmpegPath + " -re -i /opt/streams/compress/" + code + ".mp4 -c:v copy -c:a copy -f flv " + rtmpUrl + "/live/" + code);
  722. return "Response";
  723. }
  724. };
  725. try {
  726. String result = executor.submit(task).get(1, TimeUnit.SECONDS); // 设置5秒超时时间
  727. System.out.println("Response: " + result);
  728. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  729. // 请求超时处理逻辑
  730. System.out.println("Request timeout");
  731. }
  732. executor.shutdown();
  733. return bakUrlRtsp + "/live/" + code;
  734. }
  735. public static List<Map<String, Object>> filterRecordList(String channel,
  736. Date startTm,
  737. Date endTm,
  738. String mappingUrl,
  739. String wUrl) {
  740. List<Map<String, Object>> rmaps = new ArrayList<>();
  741. if (StringUtils.isBlank(channel)
  742. || ObjectUtils.isEmpty(startTm)
  743. || ObjectUtils.isEmpty(endTm)) {
  744. return null;
  745. }
  746. Map<Date, Map<String, Object>> m = new HashMap<>();
  747. // 调用视频服务返回参数
  748. String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startTm);
  749. String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endTm);
  750. String param = "channel=" + channel + "&startTime=" + startTime + "&endTime=" + endTime;
  751. // /api/record/flv/list
  752. String s = HttpUtils.sendGet(webUrl + "/recordpro/api/list", param);
  753. // 视频拼接
  754. if (!StringUtils.isBlank(s) || "null".equals(s)) {
  755. List<Map<String, Object>> maps = JSON.parseArray(s, Map.class);
  756. if (ObjectUtils.isEmpty(maps)) {
  757. return null;
  758. }
  759. for (Map<String, Object> map : maps) {
  760. Object path = map.get("Path");
  761. Object size = map.get("Size");
  762. Object duration = map.get("Duration");
  763. if (!ObjectUtils.isEmpty(path)) {
  764. String s1 = path.toString();
  765. String substring = s1.substring(s1.lastIndexOf("/") + 1, s1.length());
  766. String substring1 = substring.substring(0, substring.indexOf("-"));
  767. String[] s2 = substring1.split("_");
  768. if (!ObjectUtils.isEmpty(s2)) {
  769. Map<String, Object> mo = new HashMap<>();
  770. String s3 = s2[0] + s2[1];
  771. String s4 = s2[0] + s2[2];
  772. Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s3);
  773. if (s2[1].startsWith("23") && s2[2].startsWith("00")) {
  774. sdate = DateUtils.addDays(sdate, -1);
  775. }
  776. Date edate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s4);
  777. mo.put("startTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, sdate));
  778. mo.put("entTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, edate));
  779. mo.put("url", wUrl + path.toString());
  780. mo.put("path", mappingUrl + path.toString());
  781. mo.put("fileName", substring);
  782. mo.put("size", size);
  783. mo.put("duration", duration);
  784. /* sdate |startTm| edate |endTm| */
  785. if (startTm.compareTo(sdate) >= 0
  786. && startTm.compareTo(edate) <= 0
  787. && endTm.compareTo(edate) >= 0) {
  788. m.put(sdate, mo);
  789. /* |startTm| sdate edate |endTm| */
  790. } else if (startTm.compareTo(sdate) <= 0 && endTm.compareTo(edate) >= 0) {
  791. m.put(sdate, mo);
  792. /* |startTm| sdate |endTm| edate */
  793. } else if (startTm.compareTo(sdate) <= 0
  794. && endTm.compareTo(sdate) >= 0
  795. && endTm.compareTo(edate) <= 0) {
  796. m.put(sdate, mo);
  797. /* sdate |startTm| |endTm| edate */
  798. } else if (startTm.compareTo(sdate) >= 0 && endTm.compareTo(edate) <= 0) {
  799. m.put(sdate, mo);
  800. }
  801. }
  802. }
  803. }
  804. }
  805. if (!ObjectUtils.isEmpty(m) && m.size() > 0) {
  806. Set<Date> dates = m.keySet();
  807. // 排序
  808. dates.stream().parallel().collect(Collectors.toList()).stream().sorted().forEach(d -> {
  809. rmaps.add(m.get(d));
  810. });
  811. log.info("rmaps:{}", rmaps);
  812. return rmaps;
  813. }
  814. return null;
  815. }
  816. /**
  817. * 机车状态修改
  818. */
  819. public void heartbeat() {
  820. List<BaseTerminal> list = baseTerminalService.list();
  821. for (BaseTerminal baseTerminal : list) {
  822. QueryWrapper<BaseTerminalLog> wrapper = new QueryWrapper<>();
  823. wrapper.eq("terminal_code", baseTerminal);
  824. wrapper.orderByDesc("create_time");
  825. wrapper.last("limit 1");
  826. BaseTerminalLog baseTerminalLog = baseTerminalLogService.getOne(wrapper);
  827. if (ObjectUtils.isEmpty(baseTerminalLog)) {
  828. baseTerminal.setStatus(2);
  829. baseTerminalService.updateById(baseTerminal);
  830. } else {
  831. List<SysDictData> heartbeat = dictTypeService.selectDictDataByType("heartbeat");
  832. long timeNow = System.currentTimeMillis();
  833. long createTime = baseTerminalLog.getCreateTime().getTime();
  834. int times = Integer.parseInt(heartbeat.get(0).getDictValue());
  835. long thereHourMillis = 60 * 1000 * 3 * times;
  836. long timeNew = timeNow - thereHourMillis;
  837. if (timeNew > createTime) {
  838. baseTerminal.setStatus(2);
  839. baseTerminalService.updateById(baseTerminal);
  840. } else {
  841. baseTerminal.setStatus(1);
  842. baseTerminalService.updateById(baseTerminal);
  843. }
  844. }
  845. }
  846. }
  847. /**
  848. * 天网接口
  849. *
  850. * @return
  851. */
  852. public static String getPreviewURLs(String url, String body) {
  853. /**
  854. * STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
  855. */
  856. ArtemisConfig.host = host; // 平台的ip端口
  857. ArtemisConfig.appKey = appKey; // 密钥appkey
  858. ArtemisConfig.appSecret = appSecret;// 密钥appSecret
  859. /**
  860. * STEP2:设置OpenAPI接口的上下文
  861. */
  862. final String ARTEMIS_PATH = "/artemis";
  863. /**
  864. * STEP3:设置接口的URI地址
  865. */
  866. final String previewURLsApi = ARTEMIS_PATH + url;
  867. Map<String, String> path = new HashMap<String, String>(2) {
  868. {
  869. put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
  870. }
  871. };
  872. /**
  873. * STEP4:设置参数提交方式
  874. */
  875. String contentType = "application/json";
  876. /**
  877. * STEP6:调用接口
  878. */
  879. String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
  880. log.info("GetCameraPreviewURL----->" + result);
  881. return result;
  882. }
  883. /**
  884. * RTSP流视频压缩
  885. */
  886. public void videoCompression(String code) {
  887. // 执行网络请求...
  888. log.info(ffmpegPath + " -i /opt/streams/map/" + code + ".mp4 -c:v libx264 -s 640x480 -c:a adpcm_swf -ar 44100 /opt/streams/compress/" + code + ".mp4");
  889. cmdCameraUtil.cmd(ffmpegPath + " -i /opt/streams/map/" + code + ".mp4 -c:v libx264 -s 640x480 -c:a adpcm_swf -ar 44100 /opt/streams/compress/" + code + ".mp4");
  890. }
  891. // public String stopStream(String cameraIndexCode, String channel) {
  892. // try {
  893. // log.info("code---------->" + httpUrl + "/api/gateway/stop?streamPath=" + cameraIndexCode + "/" + channel);
  894. // String code = HttpUtils.sendGet(httpUrl + "/api/gateway/stop?stream=" + cameraIndexCode + "/" + channel);
  895. // log.info("code---------->" + code);
  896. // return code;
  897. // } catch (Exception e) {
  898. // e.printStackTrace();
  899. // }
  900. // return null;
  901. // }
  902. /**
  903. * 车载终端实时流调用的远程天网接口
  904. *
  905. * @param camerasVo
  906. */
  907. public String apiPreviewURLs(CamerasVo camerasVo) {
  908. /**
  909. * jsonBody.put("cameraIndexCode", "01ea43e6676f4e47bd6c5cd9e02aa006");
  910. * jsonBody.put("streamType", 0);
  911. * jsonBody.put("protocol","rtsp");
  912. * jsonBody.put("transmode", 1);
  913. * jsonBody.put("expand","streamform=rtp");
  914. */
  915. camerasVo.setProtocol("rtsp");
  916. camerasVo.setTransmode(1);
  917. camerasVo.setExpand("streamform=rtp");
  918. String body = JSONObject.toJSONString(camerasVo);
  919. String previewURLs = getPreviewURLs("/api/video/v1/cameras/previewURLs", body);
  920. log.info("-------------------------------->>>>>previewURLs" + previewURLs);
  921. JSONObject outJson = JSONObject.parse(previewURLs);
  922. if ("0".equals(outJson.getString("code"))) {
  923. log.info("outJson----->" + outJson);
  924. JSONObject data = outJson.getJSONObject("data");
  925. String urls = data.getString("url");
  926. return urls;
  927. } else {
  928. return "1";
  929. }
  930. }
  931. /**
  932. * 报警回放本地测试
  933. * 从天网拉回放流,然后根据url把流下载下来变成文件,然后进行视频压缩
  934. *
  935. * @param list
  936. * @param alarmPlayTimeValue
  937. */
  938. public void playbackURLs(List<String> list, String alarmPlayTimeValue) {
  939. log.info("--------------->playbackURLs");
  940. CompletableFuture future = CompletableFuture.supplyAsync(() -> {
  941. log.info("异步任务开始-----》");
  942. // for (String code : list) {
  943. String code = "42010001541320000024";
  944. PlaybackVo playbackVo = new PlaybackVo();
  945. playbackVo.setCameraIndexCode(code);
  946. // 获取当前时间
  947. OffsetDateTime currentTime = OffsetDateTime.now();
  948. // 获取当前时间的前10分钟时间
  949. OffsetDateTime beforeTenMinutes = currentTime.minusMinutes(Long.parseLong(alarmPlayTimeValue));
  950. // 获取当前时间的后10分钟时间
  951. OffsetDateTime afterTenMinutes = currentTime.plusMinutes(Long.parseLong(alarmPlayTimeValue));
  952. // 格式化时间
  953. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
  954. playbackVo.setBeginTime(beforeTenMinutes.format(formatter));
  955. //现在当前时间:后续需要修改成报警时间的后几分钟
  956. playbackVo.setEndTime(afterTenMinutes.format(formatter));
  957. // jsonBody.put("protocol","rtsp");
  958. // jsonBody.put("expand","streamform=rtp");
  959. playbackVo.setProtocol("rtsp");
  960. playbackVo.setExpand("streamform=rtp");
  961. //对象转换json字符串
  962. String body = JSONObject.toJSONString(playbackVo);
  963. //调用天网接口
  964. log.info("body----->" + body);
  965. // String playbackURLs = getPreviewURLs("/api/video/v1/cameras/playbackURLs", body);
  966. // log.info("playbackURLs----->" + playbackURLs);
  967. // JSONObject outJson = (JSONObject) JSONObject.parse(playbackURLs);
  968. // if ("0".equals(outJson.getString("code"))) {
  969. // log.info("outJson----->" + outJson);
  970. // JSONObject data = outJson.getJSONObject("data");
  971. // String urls = data.getString("url");
  972. // log.info("--------------->urls::" + urls);
  973. String FilePath = "/opt/streams/map/" + code + ".mp4";
  974. // String FilePath = "/opt/streams/map/01ea43e6676f4e47bd6c5cd9e02aa006.mp4";
  975. // try {
  976. // TimeUnit.SECONDS.sleep(62*Integer.parseInt(alarmPlayTimeValue));
  977. // } catch (InterruptedException e) {
  978. // e.printStackTrace();
  979. // }
  980. // Process process = rtspToMP4.StartRecord(ffmpegPath, "rtsp://124.70.58.209:8554/" + code + "/" + code, FilePath);
  981. // log.info("------playbackURLs----->>>>:" + process);
  982. // if (null != process) {
  983. // map.put(code, process);
  984. // }
  985. // try {
  986. // TimeUnit.SECONDS.sleep(70 * Integer.parseInt(alarmPlayTimeValue));
  987. // } catch (InterruptedException e) {
  988. // e.printStackTrace();
  989. // }
  990. // log.info("------videoCompression----->>>>:");
  991. // videoCompression(code);
  992. return 1;
  993. });
  994. future.join();
  995. }
  996. public AjaxResult stop(String id) {
  997. if (map.containsKey(id)) {
  998. Process process = map.get(id);
  999. log.info("-----stop------>>>" + process);
  1000. if (null != process) {
  1001. rtspToMP4.stopRecord(process);
  1002. return AjaxResult.success();
  1003. }
  1004. }
  1005. return AjaxResult.error();
  1006. }
  1007. public static void main(String[] args) throws InterruptedException, ParseException, IOException {
  1008. // CameraUtil cameraUtil = new CameraUtil();
  1009. // cameraUtil.closeRecording();
  1010. // String s = "/opt/streams/record/flv/42010001541320001116/20230403_235506_000506-22e1523b-d170-11ed-8a42-fa163e4e1e9f.flv";
  1011. // String fileStr = s.substring(s.lastIndexOf("/") + 1, s.length());
  1012. // String pathStr = s.substring(0, s.lastIndexOf("/"));
  1013. // System.out.println(fileStr);
  1014. // System.out.println(pathStr);
  1015. String s = "20230403235512";
  1016. Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s);
  1017. sdate = DateUtils.addDays(sdate, -1);
  1018. System.out.println(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, sdate));
  1019. }
  1020. public List<BaseCameraManagementResponse> heartbeatGetPlayFlv() {
  1021. int maxRetries = 5;
  1022. int retryCount = 0;
  1023. boolean success = false;
  1024. JSONArray streamArray = null;
  1025. List<BaseCameraManagementResponse> baseCameraManagementResponseList = new ArrayList<>();
  1026. while (!success && retryCount < maxRetries) {
  1027. try {
  1028. String data = HttpClientUtil.gets(webUrl + "/api/summary");
  1029. log.info("data------------------>" + data);
  1030. if (StringUtils.isNotEmpty(data)) {
  1031. JSONObject jsonObject = JSONObject.parseObject(data);
  1032. streamArray = jsonObject.getJSONArray("Streams");
  1033. success = true;
  1034. }
  1035. } catch (Exception e) {
  1036. log.info("Exception caught: " + e.getMessage());
  1037. // 增加重试计数
  1038. retryCount++;
  1039. }
  1040. }
  1041. for (int i = 0; i < streamArray.size(); i++) {
  1042. BaseCameraManagementResponse baseCameraManagementResponse = new BaseCameraManagementResponse();
  1043. JSONObject subObj = streamArray.getJSONObject(i);
  1044. String subValue = subObj.getString("Subscribers");
  1045. String PathValue = subObj.getString("Path");
  1046. String type = subObj.getString("Type");
  1047. String timestampString = subObj.getString("StartTime");
  1048. ZonedDateTime givenTime = ZonedDateTime.parse(timestampString);
  1049. // 当前时间
  1050. Instant currentTime = Instant.now();
  1051. // 将给定时间转换为Instant对象
  1052. Instant givenInstant = givenTime.toInstant();
  1053. // 计算时间差
  1054. Duration duration = Duration.between(givenInstant, currentTime);
  1055. //封装
  1056. // 获取小时差
  1057. baseCameraManagementResponse.setStartTime(duration.toHours() + "小时前");
  1058. baseCameraManagementResponse.setStreamPath(PathValue);
  1059. baseCameraManagementResponse.setSubscribers(subValue);
  1060. baseCameraManagementResponse.setType(type);
  1061. // 寻找第一个斜杠的位置
  1062. int slashIndex = PathValue.indexOf("/");
  1063. if (slashIndex != -1) {
  1064. BaseCameraManagementResponse response = new BaseCameraManagementResponse();
  1065. String result = PathValue.substring(0, slashIndex);
  1066. BaseCameraManagement cameraManagement = baseCameraManagementService.getOne(new LambdaQueryWrapper<BaseCameraManagement>()
  1067. .eq(BaseCameraManagement::getCameraCode, result));
  1068. if (!ObjectUtils.isEmpty(cameraManagement)) {
  1069. baseCameraManagementResponse.setCameraCode(cameraManagement.getCameraCode());
  1070. baseCameraManagementResponse.setInstallMile(cameraManagement.getInstallMile());
  1071. }
  1072. }
  1073. //取出AudioTracks
  1074. JSONArray audioJson = subObj.getJSONArray("AudioTracks");
  1075. if (audioJson != null && audioJson.size() > 0) {
  1076. for (int v = 0; v < audioJson.size(); v++) {
  1077. BaseCameraManagementResponse.AudioTracks audioTracks = new BaseCameraManagementResponse.AudioTracks();
  1078. JSONObject arrayJSONObject = audioJson.getJSONObject(v);
  1079. String name = arrayJSONObject.getString("Name");
  1080. String sampleRate = arrayJSONObject.getString("SampleRate");
  1081. String channels = arrayJSONObject.getString("Channels");
  1082. String bps = arrayJSONObject.getString("BPS");
  1083. audioTracks.setBps(bps);
  1084. audioTracks.setChannels(channels);
  1085. audioTracks.setName(name);
  1086. audioTracks.setSampleRate(sampleRate);
  1087. baseCameraManagementResponse.setAudioTracks(audioTracks);
  1088. }
  1089. }
  1090. // //取出VideoTracks
  1091. // JSONArray videoJson = subObj.getJSONArray("VideoTracks");
  1092. // if (videoJson != null && videoJson.size() > 0) {
  1093. // for (int v = 0; v < videoJson.size(); v++) {
  1094. // BaseCameraManagementResponse.VideoTracks videoTracks = new BaseCameraManagementResponse.VideoTracks();
  1095. // JSONObject arrayJSONObject = videoJson.getJSONObject(v);
  1096. // String name = arrayJSONObject.getString("Name");
  1097. // String width = arrayJSONObject.getString("Width");
  1098. // String height = arrayJSONObject.getString("Height");
  1099. // String resolution = width + "*" + height;
  1100. // String gop = arrayJSONObject.getString("GOP");
  1101. // String bps = arrayJSONObject.getString("BPS");
  1102. // videoTracks.setBps(bps);
  1103. // videoTracks.setGop(gop);
  1104. // videoTracks.setName(name);
  1105. // videoTracks.setResolution(resolution);
  1106. // baseCameraManagementResponse.setVideoTracks(videoTracks);
  1107. // }
  1108. // }
  1109. baseCameraManagementResponseList.add(baseCameraManagementResponse);
  1110. }
  1111. return baseCameraManagementResponseList;
  1112. }
  1113. /**
  1114. * 根据相机ID找出分组并排序
  1115. *
  1116. * @param baseCameraManagement
  1117. */
  1118. public List<BaseCameraManagement> getBaseCameraManagementList(Long id, Integer line) {
  1119. //根据ID获取当前相机的信息
  1120. BaseCameraManagement baseCameraManagement = baseCameraManagementService.getById(id);
  1121. if (!ObjectUtils.isEmpty(baseCameraManagement)) {
  1122. LambdaQueryWrapper<BaseCameraManagement> wrapper = new LambdaQueryWrapper<>();
  1123. //找出同一分组的相机
  1124. wrapper.eq(BaseCameraManagement::getGrou, baseCameraManagement.getGrou());
  1125. if (line == 1) {
  1126. wrapper.orderByAsc(BaseCameraManagement::getSort);
  1127. } else if (line == 2) {
  1128. wrapper.orderByDesc(BaseCameraManagement::getSort);
  1129. }
  1130. List<BaseCameraManagement> list = baseCameraManagementService.list(wrapper);
  1131. if (!ObjectUtils.isEmpty(list)) {
  1132. return list;
  1133. } else {
  1134. return null;
  1135. }
  1136. } else {
  1137. return null;
  1138. }
  1139. }
  1140. /**
  1141. * 流媒体的条数
  1142. * @return
  1143. */
  1144. public int urlCount(){
  1145. int maxRetries = 5;
  1146. int retryCount = 0;
  1147. int count = 0;
  1148. boolean success = false;
  1149. JSONArray streamArray = null;
  1150. List<BaseCameraManagementResponse> baseCameraManagementResponseList = new ArrayList<>();
  1151. while (!success && retryCount < maxRetries) {
  1152. try {
  1153. String data = HttpClientUtil.gets(webUrl + "/api/summary");
  1154. log.info("data------------------>" + data);
  1155. if (StringUtils.isNotEmpty(data)) {
  1156. JSONObject jsonObject = JSONObject.parseObject(data);
  1157. streamArray = jsonObject.getJSONArray("Streams");
  1158. success = true;
  1159. }
  1160. } catch (Exception e) {
  1161. log.info("Exception caught: " + e.getMessage());
  1162. // 增加重试计数
  1163. retryCount++;
  1164. }
  1165. }
  1166. for (int i = 0; i < streamArray.size(); i++) {
  1167. JSONObject subObj = streamArray.getJSONObject(i);
  1168. String PathValue = subObj.getString("Path");
  1169. if (PathValue.contains("/h264_720")){
  1170. count++;
  1171. }
  1172. }
  1173. return count;
  1174. }
  1175. }