Best way to call terminal command repeatedly
Asked Answered
H

1

2

I'm using mencoder to split files and I'd like to turn this into an Object Oriented approach, if possible, using Java or similar, for example. But I'm not sure the best way, so I leave it in the open. Here is what I need:

I have an excel file with start times and end times, and I need to extract out the appropriate clips from a video file. In the terminal (I'm on Mac OS X) I've had success using, for example:

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

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

But, like I said, I want to make it so that a program reads in from an excel file, and calls this mencoder command multiple times (over 100) for each of the start times and end times.

I know how to read in the excel file in Java, but I'm not sure it is best to call this command from Java. Plus, I'd like to be able to see the output of mencoder (because it prints out a nice percentage so you know about how much longer a single command will take). Is this type of thing feasible to do in a shell script? I would really like to use Java if possible, since I have many years of experience in Java and no experience in shell scripting.


UPDATE

Here is what I've tried in Java, but it freezes at 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();
           }
        }
Hedgehog answered 16/2, 2013 at 21:26 Comment(4)
oh, but is Process not enough for this task ?Twinscrew
I haven't had success in Java doing this.Hedgehog
Hey @Hedgehog why are you using input stream and not output stream ? I mean you want to see the output and the input stream will wait for input so I think thats why your loop never endsTwinscrew
Then how do you suppose I do it?Hedgehog
I
0

I'm not quite sure about mencoders multicore-capabilities, but I think with Java you can use Multiple Threads to get the maximal power of all cpu-cores.

You shouldn't use Runtime like your using it.

When using Runtime, you should not run bash and send commands via inputstream like when you are typing commands on a terminal.

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

To get the Output, you can use the 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 Workingdirectory where your command is executed.

I also prefer the version with the String[] as parameters. It's much more readable, than the a concatenated String.

Inflate answered 16/2, 2013 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.