Best way to call terminal command again

I am using mencoder

to split files and I would like to turn this into an object oriented approach if possible using Java or similar for example. But I'm not sure if this is the best way, so I leave it open. This is what I need:

I have an excel file with a start time and end time and I need to extract the corresponding clips from a video file. In terminal (I am on Mac OS X) I had success, for example:

mencoder -ss 0 -endpos 10 MyVideo.avi -oac copy -ovc copy -o Output.avi

      

Which creates the Output.avi video by clipping the first 10 seconds of the MyVideo.avi video.

But as I said, I want to make the program read the excel file and call this mencoder command multiple times (over 100) for each start and end time.

I know how to read in an excel file in Java, but I'm not sure what is the best way to call this command from Java. Also, I would like to see the output mencoder

(because it prints a good percentage, so you know how long one command will take). Can this be done in a shell script? I would like to really

use Java if possible as I have years of experience in Java and no experience in shell scripting.


UPDATE

Here is what I tried in Java but it freezes when in.readLine()

        File wd = new File("/bin");
        System.out.println(wd);
        Process proc = null;
        try {
           proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
        }
        catch (IOException e) {
           e.printStackTrace();
        }
        if (proc != null) {
           BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
           out.println("cd ..");
           out.println("pwd");
           String video = "/Users/MyFolder/MyFile.avi";
           String output = "/Users/MyFolder/output.avi";
           int start = 0;
           int end = 6;
           String cmd = "mencoder -ss " + start + 
                          " -endpos " + end + 
                          " " + video + " -oac copy -ovc copy -o " + output;

           out.println(cmd);
           try {
              String line;
              System.out.println("top");
              while ((line = in.readLine()) != null) {

                 System.out.println(line);
              }
              System.out.println("end");
              proc.waitFor();
              in.close();
              out.close();
              proc.destroy();
           }
           catch (Exception e) {
              e.printStackTrace();
           }
        }

      

+2


source to share


1 answer


I'm not entirely sure about the multichannel capabilities of mencoders, but I think with Java you can use Multiple Threads to get the most power from all the cpu's.

You shouldn't be using Runtime for example using it.

When using Runtime, you shouldn't start bash and send commands through the input stream, like when you type commands in the terminal.

Runtime.getRuntime (). exec ("mencoder -ss" + start + "-endpos" + end + "+ video +" -ovc copy -o "+ output);



To get the Output you can use inputStream

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java. lang.String,% 20java.lang.String [],% 20java.io.File% 29

With this command, you can also set the Working Directory where your command runs.

I also prefer the version with String [] parameters. This is much more readable than the concatenated string.

0


source







All Articles