Forráskód Böngészése

feat: java 工具解决windows命令执行问题

hexleo 5 éve
szülő
commit
74270b5f2c

+ 8 - 0
Android/PlayerProj/animtool/build.gradle

@@ -15,4 +15,12 @@ buildscript {
 }
 repositories {
     mavenCentral()
+}
+
+jar {
+    manifest {
+        attributes(
+            'Main-Class': 'com.tencent.qgame.playerproj.animtool.Main'
+        )
+    }
 }

+ 9 - 4
Android/PlayerProj/animtool/src/main/java/com/tencent/qgame/playerproj/animtool/AnimTool.java

@@ -17,10 +17,13 @@ package com.tencent.qgame.playerproj.animtool;
 
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.concurrent.TimeUnit;
 
 public class AnimTool {
 
@@ -50,6 +53,7 @@ public class AnimTool {
      * @param needVideo true 生成完整视频 false 只合成帧图片
      */
     public void create(final CommonArg commonArg, final boolean needVideo) throws Exception{
+        TLog.i(TAG, "start create");
         createAllFrameImage(commonArg, new Runnable() {
             @Override
             public void run() {
@@ -75,6 +79,7 @@ public class AnimTool {
             return;
         }
 
+        TLog.i(TAG, "createAllFrameImage");
         time = System.currentTimeMillis();
 
         // 检测output文件是否存在,不存在则生成
@@ -276,8 +281,8 @@ public class AnimTool {
                     "-y", videoPath + TEM_VIDEO_FILE};
         }
 
-        Process pro = Runtime.getRuntime().exec(cmd);
-        int result = pro.waitFor();
+        TLog.i(TAG, "run createMp4");
+        int result = ProcessUtil.run(cmd);
         TLog.i(TAG, "createMp4 result=" + (result == 0? "success" : "fail"));
         return result == 0;
     }
@@ -289,8 +294,8 @@ public class AnimTool {
      */
     private boolean mergeBin2Mp4(CommonArg commonArg, String inputFile, String videoPath) throws Exception{
         String[] cmd = new String[] {commonArg.mp4editCmd, "--insert", ":"+inputFile+":1", videoPath + TEM_VIDEO_FILE, videoPath + VIDEO_FILE};
-        Process pro = Runtime.getRuntime().exec(cmd);
-        int result = pro.waitFor();
+        TLog.i(TAG, "run mergeBin2Mp4");
+        int result = ProcessUtil.run(cmd);
         TLog.i(TAG, "mergeBin2Mp4 result=" + (result == 0? "success" : "fail"));
         return result == 0;
     }

+ 10 - 0
Android/PlayerProj/animtool/src/main/java/com/tencent/qgame/playerproj/animtool/CommonArg.java

@@ -55,4 +55,14 @@ public class CommonArg {
 
     public int totalFrame;
 
+    @Override
+    public String toString() {
+        return "CommonArg{" +
+                "ffmpegCmd='" + ffmpegCmd + '\'' +
+                ", mp4editCmd='" + mp4editCmd + '\'' +
+                ", enableH265=" + enableH265 +
+                ", fps=" + fps +
+                ", inputPath='" + inputPath + '\'' +
+                '}';
+    }
 }

+ 52 - 0
Android/PlayerProj/animtool/src/main/java/com/tencent/qgame/playerproj/animtool/ProcessUtil.java

@@ -0,0 +1,52 @@
+package com.tencent.qgame.playerproj.animtool;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+
+/**
+ * porcess.waitFor() 必须读取执行结果
+ * https://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
+ */
+class ProcessUtil {
+
+    public static int run(String[] cmd) throws Exception {
+        Process proc = Runtime.getRuntime().exec(cmd);
+
+        StreamReader errorReader = new StreamReader(proc.getErrorStream(), "ERROR");
+        StreamReader outputReader = new StreamReader(proc.getInputStream(), "OUTPUT");
+        errorReader.start();
+        outputReader.start();
+
+        int result = proc.waitFor();
+        if (result != 0) TLog.i(errorReader.type, errorReader.sb.toString());
+        return result;
+    }
+
+    private static class StreamReader extends Thread {
+        InputStream is;
+        String type;
+        StringBuilder sb = new StringBuilder();
+
+        StreamReader(InputStream is, String type) {
+            this.is = is;
+            this.type = type;
+        }
+
+        public void run() {
+            try {
+                InputStreamReader isr = new InputStreamReader(is);
+                BufferedReader br = new BufferedReader(isr);
+                String line = null;
+                while ((line = br.readLine()) != null) {
+                    sb.append(line);
+                    sb.append("\n");
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

+ 39 - 9
Android/PlayerProj/animtool/src/main/java/com/tencent/qgame/playerproj/animtool/ui/ToolUI.java

@@ -11,6 +11,8 @@ import java.awt.GridLayout;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -44,11 +46,11 @@ public class ToolUI {
     private final JRadioButton btnH264 = new JRadioButton("h264");
     private final SpinnerModel modelFps = new SpinnerNumberModel(24, 1, 60, 1);
     private final JTextField textInputPath = new JTextField();
+    private final JButton btnCreate = new JButton("create VAP");
     private final JTextArea txtAreaLog = new JTextArea();
     private final JLabel labelOutInfo = new JLabel();
     private final Dimension labelSize = new Dimension(100, 20);
     private final Properties props = new Properties();
-    private final JButton btnOpenSource = new JButton("open source software");
 
 
 
@@ -85,6 +87,7 @@ public class ToolUI {
                     runAnimTool();
                 } catch (Exception e) {
                     TLog.i(TAG, "ERROR -> " + e.getMessage());
+                    btnCreate.setEnabled(true);
                 }
             }
         }).start();
@@ -92,11 +95,24 @@ public class ToolUI {
 
     private void runAnimTool() throws Exception {
         final CommonArg commonArg = new CommonArg();
+        String os = System.getProperty("os.name").toLowerCase();
+
         commonArg.ffmpegCmd = "ffmpeg";
         commonArg.mp4editCmd = "mp4edit";
+
+        if (os != null && !"".equals(os)) {
+            if (os.contains("mac") && new File("mac").exists()) {
+                commonArg.ffmpegCmd = "mac/ffmpeg";
+                commonArg.mp4editCmd = "mac/mp4edit";
+            } else if (os.contains("windows") && new File("win").exists()) {
+                commonArg.ffmpegCmd = "win/ffmpeg";
+                commonArg.mp4editCmd = "win/mp4edit";
+            }
+        }
         commonArg.enableH265 = group.isSelected(btnH265.getModel());
         commonArg.fps = (Integer)modelFps.getValue();
         commonArg.inputPath = textInputPath.getText();
+        TLog.i(TAG, commonArg.toString());
 
         AnimTool animTool = new AnimTool();
         animTool.setToolListener(new AnimTool.IToolListener() {
@@ -108,7 +124,8 @@ public class ToolUI {
 
             @Override
             public void onComplete() {
-                labelOutInfo.setText("output: " + commonArg.outputPath);
+                btnCreate.setEnabled(true);
+                setOutput(commonArg.outputPath);
                 try {
                     setProperties(commonArg);
                     Desktop.getDesktop().open(new File(commonArg.outputPath));
@@ -117,8 +134,9 @@ public class ToolUI {
                 }
             }
         });
-
+        btnCreate.setEnabled(false);
         animTool.create(commonArg, true);
+
     }
 
     private void createUI() {
@@ -218,16 +236,27 @@ public class ToolUI {
             }
         });
 
-
-
         return panel;
     }
 
+    private void setOutput(final String path) {
+        labelOutInfo.setText("<html>output: <font color='blue'>" + path + "</font></html>");
+        labelOutInfo.addMouseListener(new MouseAdapter() {
+            @Override
+            public void mouseClicked(MouseEvent mouseEvent) {
+                try {
+                    Desktop.getDesktop().open(new File(path));
+                } catch (IOException e) {
+                    TLog.i(TAG, "ERROR -> " + e.getMessage());
+                }
+            }
+        });
+    }
+
 
     private JPanel getCreateLayout() {
         JPanel panel = new JPanel();
         panel.setLayout(new FlowLayout(FlowLayout.LEFT));
-        JButton btnCreate = new JButton("create VAP");
         panel.add(btnCreate);
         btnCreate.addActionListener(new ActionListener() {
             @Override
@@ -264,13 +293,14 @@ public class ToolUI {
         JPanel panel = new JPanel();
         panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
 
-        btnOpenSource.addActionListener(new ActionListener() {
+        JLabel label = new JLabel("open source software");
+        label.addMouseListener(new MouseAdapter() {
             @Override
-            public void actionPerformed(ActionEvent actionEvent) {
+            public void mouseClicked(MouseEvent mouseEvent) {
                 new OpenSourceUI().createUI();
             }
         });
-        panel.add(btnOpenSource);
+        panel.add(label);
         return panel;
     }