Bladeren bron

新增获取回放流的接口

zhangqiankun 3 jaren geleden
bovenliggende
commit
19597175ee

+ 2 - 1
src/main/java/com/persagy/cameractl/controller/HelloController.java

@@ -157,7 +157,8 @@ public class HelloController {
 	@RequestMapping(value = "/vision/{opertype}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
 	public @ResponseBody String hikvisionOper(@PathVariable("opertype") String operType, @RequestBody Camera camera) {
 		ResultClass executeResult = operType.equals("preview") ? hkCameraExecuteApi.GetCameraPreviewURL(camera)
-				: operType.equals("playback") ? hkCameraExecuteApi.GetCameraPlayBackURL(camera)
+				: operType.equals("playback") ? hkCameraExecuteApi.GetCameraPlayBackURL(camera, false)
+						: operType.equals("playbackUrl") ? hkCameraExecuteApi.GetCameraPlayBackURL(camera, true)
 						: operType.equals("control") ? hkCameraExecuteApi.PtzControllingMain(camera)
 								: operType.equals("points") ? hkCameraExecuteApi.GetCameraPointsByPage(camera)
 										: operType.equals("token") ? hkCameraExecuteApi.GetAccessToken(camera)

+ 22 - 7
src/main/java/com/persagy/cameractl/service/HkCameraExecuteApi.java

@@ -7,6 +7,7 @@ import com.persagy.cameractl.utils.Camera;
 import com.persagy.cameractl.utils.CmdStreamThread;
 import com.persagy.cameractl.utils.EnumTools;
 import com.persagy.cameractl.utils.ResultClass;
+import com.persagy.cameractl.utils.EnumTools.OperatingSystem;
 import com.persagy.cameractl.utils.JsonTools;
 import com.persagy.cameractl.utils.OtherTools;
 import cn.hutool.core.date.DateUtil;
@@ -132,7 +133,7 @@ public class HkCameraExecuteApi {
 	/*
 	 * 根据监控点获取回放流
 	 */
-	public ResultClass GetCameraPlayBackURL(Camera hkCamera) {
+	public ResultClass GetCameraPlayBackURL(Camera hkCamera, boolean returnUrl) {
 		try {
 			String reqUrl = "video/v2/cameras/playbackURLs";
 			/**
@@ -196,13 +197,18 @@ public class HkCameraExecuteApi {
 			String videoUrl = String.valueOf(resultDataMap.get("url"));
 			if (videoUrl == null || videoUrl.equals(""))
 				return new ResultClass(false, "未获取到视频源地址");
-
+			// 是否需要直接返回URL
+			if (returnUrl) {
+				Map<String, String> dataMap = new HashMap<String, String>();
+				dataMap.put("videoUrl", videoUrl);
+				return new ResultClass(true, dataMap);
+			}
+			
 			String playFilePath = OtherTools.getVideoFilePath();
 			if (playFilePath.equals("")) {
 				return new ResultClass(false, "回放文件名称生成失败");
 			}
 			// 抓取视频流转为页面上可播放的MP4
-			Runtime run = Runtime.getRuntime();
 			/*
 			 * stimeout、rw_timeout的单位为微秒  1秒=1000000微秒
 			 * 因为香港置地返回的回放rtsp流总是在剩余大概15秒的时候卡顿,所以传过去的结束时间要加30秒,所以ffmpeg命令里要用-t控制实际读取时长
@@ -211,9 +217,20 @@ public class HkCameraExecuteApi {
 					+ (millSecondCount > 0 ? "." + millSecondCount : "");
 			String cmdStr = hkCamera.protocol.equals("hls")
 					? ("ffmpeg -i \"" + videoUrl + "\" -c copy -y \"" + playFilePath + "\"")
-					: ("ffmpeg -t " + timeDuration + " -rtsp_transport tcp -i \"" + videoUrl + "\" -y \"" + playFilePath
+					: ("ffmpeg -t " + timeDuration + " -rtsp_transport tcp -i \"" + videoUrl + "\" -vcodec copy -y \"" + playFilePath
 							+ "\"");
-			Process p = run.exec(cmdStr);
+			logger.info(cmdStr);
+			
+			OperatingSystem systemName = OtherTools.getSystemName();
+			Process p = null;
+			switch (systemName) {
+				case windows:
+					p = Runtime.getRuntime().exec(cmdStr);
+					break;
+				default:
+					p = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmdStr});
+			}
+			
 
 			// 读取标准输入流、输出流,防止进程阻塞
 			CmdStreamThread cmdStreamThread = new CmdStreamThread(p.getInputStream(), p.getErrorStream());
@@ -221,7 +238,6 @@ public class HkCameraExecuteApi {
 			int retCode = p.waitFor();
 			p.destroy();
 			if (retCode == 0) {
-//		    	正常转换结束
 				File mp4File = new File(playFilePath);
 				String playFileName = mp4File.getName();
 				String token = OtherTools.getMp4NamePrefix(playFileName);
@@ -231,7 +247,6 @@ public class HkCameraExecuteApi {
 				dataMap.put("videoUrl", videoUrl);
 				return new ResultClass(true, dataMap);
 			} else {
-//		    	转换出错
 				String errStr = cmdStreamThread.errString != null ? cmdStreamThread.errString
 						: cmdStreamThread.outString;
 				logger.error("ffmpeg抓取rtsp转为mp4时失败,错误信息:" + errStr);

+ 2 - 0
src/main/java/com/persagy/cameractl/utils/CmdStreamThread.java

@@ -35,6 +35,7 @@ public class CmdStreamThread {
 				inputBfr.close();
 				outString = sb.toString();
 			} catch (Exception e) {
+				e.printStackTrace();
 				outString = e.getMessage();
 			}
 		}
@@ -55,6 +56,7 @@ public class CmdStreamThread {
 				errBfr.close();
 				errString = sb.toString();
 			} catch (Exception e) {
+				e.printStackTrace();
 				errString = e.getMessage();
 			}
 		}