Browse Source

相机回放视频过滤排序

hexiao 2 years ago
parent
commit
d6ad385797
1 changed files with 82 additions and 87 deletions
  1. 82 87
      hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

+ 82 - 87
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -29,7 +29,7 @@ public class CameraUtil {
 
     private static String url;
     private static String historyUrl;
-    private static String ffmpegPath = "C:\\Users\\Administrator.DESKTOP-0NUUTMM\\Downloads\\ffmpeg-5.1.2-essentials_build\\ffmpeg-5.1.2-essentials_build\\bin\\ffmpeg.exe";
+    private static String ffmpegPath;
     private static String mappingUrl;
     private static String flvPath;
     private static String filePath;
@@ -168,99 +168,94 @@ public class CameraUtil {
      */
     public static void convetor(List<String> fromVideoFileList,
                                 String NewfilePath) throws IOException {
+        List<String> voidTS = new ArrayList<>();
+        Process process = null;
+        ProcessBuilder builder = null;
+        List<String> command = null;
 
-
-        try {
-            List<String> voidTS = new ArrayList<>();
-            Process process = null;
-            ProcessBuilder builder = null;
-            List<String> command = null;
-            for (int i = 0; i < fromVideoFileList.size(); i++) {
-                String fromVideoFile = fromVideoFileList.get(i);
-                command = new ArrayList<String>();
-                command.add(ffmpegPath);
-                command.add("-y");
-                command.add("-i");
-                command.add(fromVideoFile);
-                command.add("-vcodec");
-                command.add("copy");
-                command.add("-bsf:v");
-                command.add("h264_mp4toannexb");
-                command.add("-f");
-                command.add("mpegts");
-                command.add(fromVideoFile.substring(0,
-                        fromVideoFile.lastIndexOf(".")) + ".ts");
-                builder = new ProcessBuilder(command);
-                voidTS.add(fromVideoFile.substring(0,
-                        fromVideoFile.lastIndexOf("."))
-                        + ".ts");
-                try {
-                    process = builder.start();
-                    InputStream errorStream = process
-                            .getErrorStream();
-                    InputStreamReader inputStreamReader = new InputStreamReader(
-                            errorStream);
-                    BufferedReader br = new BufferedReader(
-                            inputStreamReader);
-                    String line = "";
-                    StringBuffer sb = new StringBuffer();
-                    while ((line = br.readLine()) != null) {
-                        sb.append(line);
-                    }
-                    String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
-                    Pattern pattern = Pattern
-                            .compile(regexDuration);
-                    Matcher m = pattern.matcher(sb.toString());
-                    System.out.println(sb.toString());
-                    br.close();
-                    inputStreamReader.close();
-                    errorStream.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            List<String> dos = new ArrayList<>();
-            StringBuffer tsPath = new StringBuffer();
-            tsPath.append(ffmpegPath);
-            tsPath.append(" -i ");
-            tsPath.append("concat:");
-            for (int t = 0; t < voidTS.size(); t++) {
-                if (t != voidTS.size() - 1) {
-                    tsPath.append(voidTS.get(t) + "|");
-                } else {
-                    tsPath.append(voidTS.get(t));
-                }
-            }
-            tsPath.append(" -vcodec ");
-            tsPath.append(" copy ");
-            tsPath.append(" -bsf:a ");
-            tsPath.append(" aac_adtstoasc ");
-            tsPath.append(" -movflags ");
-            tsPath.append(" +faststart ");
-            tsPath.append(NewfilePath);
-            Process pr = Runtime.getRuntime().exec(
-                    tsPath.toString());
-            process.getInputStream();
-            pr.getOutputStream().close();
-            pr.getInputStream().close();
-            pr.getErrorStream().close();
+        for (int i = 0; i < fromVideoFileList.size(); i++) {
+            String fromVideoFile = fromVideoFileList.get(i);
+            command = new ArrayList<String>();
+            command.add(ffmpegPath);
+            command.add("-y");
+            command.add("-i");
+            command.add(fromVideoFile);
+            command.add("-vcodec");
+            command.add("copy");
+            command.add("-bsf:v");
+            command.add("h264_mp4toannexb");
+            command.add("-f");
+            command.add("mpegts");
+            command.add(fromVideoFile.substring(0,
+                    fromVideoFile.lastIndexOf(".")) + ".ts");
+            builder = new ProcessBuilder(command);
+            voidTS.add(fromVideoFile.substring(0,
+                    fromVideoFile.lastIndexOf("."))
+                    + ".ts");
             try {
-                pr.waitFor();
-                Thread.sleep(1000);
-                pr.destroy();
-            } catch (InterruptedException e) {
-                // TODO Auto-generated catch block
+                process = builder.start();
+                InputStream errorStream = process
+                        .getErrorStream();
+                InputStreamReader inputStreamReader = new InputStreamReader(
+                        errorStream);
+                BufferedReader br = new BufferedReader(
+                        inputStreamReader);
+                String line = "";
+                StringBuffer sb = new StringBuffer();
+                while ((line = br.readLine()) != null) {
+                    sb.append(line);
+                }
+                String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
+                Pattern pattern = Pattern
+                        .compile(regexDuration);
+                Matcher m = pattern.matcher(sb.toString());
+                System.out.println(sb.toString());
+                br.close();
+                inputStreamReader.close();
+                errorStream.close();
+            } catch (IOException e) {
                 e.printStackTrace();
             }
-            //删除生成的ts文件
-            for (String filePath : voidTS) {
-                File file = new File(filePath);
-                file.delete();
-                pr.destroy();
+        }
+        List<String> dos = new ArrayList<>();
+        StringBuffer tsPath = new StringBuffer();
+        tsPath.append(ffmpegPath);
+        tsPath.append(" -i ");
+        tsPath.append("concat:");
+        for (int t = 0; t < voidTS.size(); t++) {
+            if (t != voidTS.size() - 1) {
+                tsPath.append(voidTS.get(t) + "|");
+            } else {
+                tsPath.append(voidTS.get(t));
             }
-        } catch (Exception e) {
+        }
+        tsPath.append(" -vcodec ");
+        tsPath.append(" copy ");
+        tsPath.append(" -bsf:a ");
+        tsPath.append(" aac_adtstoasc ");
+        tsPath.append(" -movflags ");
+        tsPath.append(" +faststart ");
+        tsPath.append(NewfilePath);
+        Process pr = Runtime.getRuntime().exec(
+                tsPath.toString());
+        process.getInputStream();
+        pr.getOutputStream().close();
+        pr.getInputStream().close();
+        pr.getErrorStream().close();
+        try {
+            pr.waitFor();
+            Thread.sleep(1000);
+            pr.destroy();
+        } catch (InterruptedException e) {
+            // TODO Auto-generated catch block
             e.printStackTrace();
         }
+        //删除生成的ts文件
+        for (String filePath : voidTS) {
+            File file = new File(filePath);
+            file.delete();
+            pr.destroy();
+        }
     }
 
     public static void main(String[] args) throws IOException {