CameraUtil.java 49 KB

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