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?