Browse Source

优化实时拉取摄像头视频流接口,实现界面停止调用后ffmpeg拉流停止的逻辑1

james 1 month ago
parent
commit
90f47412f6

+ 11 - 6
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/VcDeviceServiceImpl.java

@@ -75,19 +75,20 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
         String videoPort = one.getVideoPort().toString();
         String command = "ffmpeg -i rtsp://"+accountNumber+":"+videoPassword+"@"+videoIp+":"+videoPort+" -vcodec copy -acodec aac -ar 44100 -c:v libx264 -strict -2 -ac 1 -f flv -r 30 -s 1920x1080 -q 10 -f flv rtmp://"+rtmpIp+":1935/hls/"+deviceUuid;
         System.out.println(command);
+        Process process = null;
         try {
             log.info("【begin】实时拉取摄像头数据流 "+command);
             // 创建 ProcessBuilder 实例
             ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
             // 启动进程
-            Process process = processBuilder.start();
+            process = processBuilder.start();
 
             // 获取进程的输入流
-            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-            String line;
-            while ((line = reader.readLine()) != null) {
-                System.out.println(line);
-            }
+//            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+//            String line;
+//            while ((line = reader.readLine()) != null) {
+//                System.out.println(line);
+//            }
 
             // 等待进程执行完成并获取退出码
             int exitCode = process.waitFor();
@@ -104,6 +105,10 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
         } catch (IOException | InterruptedException e) {
             e.printStackTrace();
             return null;
+        } finally {
+            if (process != null) {
+                process.destroy();
+            }
         }