0

I'm trying to get the duration of a video file with Java, using the bash command

avconv -i test.avi 2>&1 | grep 'Duration' | awk '{print $2}' | sed s/,//

When I just enter the above command in terminal, there is exactly one output line displayed in terminal that shows the duration like this:

00:09:56.45

Now I would like to run this command from Java and receive the outputted duration as a String. My code is the following:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args){
        String command = "avconv -i test.avi 2>&1 | grep 'Duration' | awk '{print $2}' | sed s/,//";
        StringBuffer output = new StringBuffer();
        Process p;
        String result = "init";
        try {
            p = Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            result = in.readLine();
            in.close();
        } catch (IOException e) { e.printStackTrace(); }             

        System.out.println(result);
    }
}

However, this returns null. So the value of result gets changed by result = in.readLine()) but, instead of the actual terminal output, result = null gets set.

So the in.readLine()) seems not to be receiving the output of the command, even though this is perfectly outputted in terminal when I enter the command there.

Why can't Java read this terminal output?

Oliver
  • 21
  • 2
  • On another note, Don't use `avconv` Use `ffmpeg` from [ffmpeg.org](https://ffmpeg.org). avconv is a fork of ffmpeg, which shows some issues... Besides you need ffprobe command from ffmpeg package. – anishsane Aug 12 '14 at 09:35
  • 1
    You initialized `result` to null and then used it with `+=`. Try `String result = "";`. – Djon Aug 12 '14 at 09:35
  • @Djon thanks for pointing out the errors in the code I posted. I have corrected it in my question. Unfortunately, my problem still remains. PS: I'm using avconv instead of ffmpeg because avconv seems to be the replacement of ffmpeg in the latest Ubuntu (?) – Oliver Aug 12 '14 at 10:09
  • So you problem is that `in.readLine()` returns `null`. Have you tried with a simple command to make sure the process works? – Djon Aug 12 '14 at 10:13
  • I think you might have redirected the output to stderr, try `p.getErrorStream()`. – Djon Aug 12 '14 at 10:16
  • The command is not being run correctly, because `Runtime.exec()` runs *a single command with arguments*. It doesn't pass a line of text to your shell program. This makes this essentially a duplicate of http://stackoverflow.com/questions/5928225/ — see that question for some solutions. – Dan Getz Aug 12 '14 at 19:43

1 Answers1

0

Use this ->

try{
            Process duration = Runtime.getRuntime().exec(new String[] { "sh", "-c", "avconv -i test.avi 2>&1 | grep 'Duration' | awk '{print $2}' | sed s/,//"});   
String line
BufferedReader in = new BufferedReader(new InputStreamReader(duration.getInputStream()) );
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
               }
}catch(IOException e){
  e.printStackTrace();
}
abhishek
  • 133
  • 1
  • 1
  • 6