1

What I am trying to do is capture the stream of an IP Cam by using avconv. I have managed to get that stream and save it to a file using the apache exec commons library for Java like this:

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
String str = avconv_command; 
CommandLine commandLine = CommandLine.parse(str);
ExecuteWatchdog watchdog = new ExecuteWatchdog(1000000);
Executor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
executor.execute(commandLine, resultHandler);

With that, the avconv starts to capture the streaming and saves it to a file, and on the console I can see how the avconv is working and the output of the process. Each line of that output shows the duration of the video currently being captured, the bitrate, etc. I need to capture that output and process it while it is running. Any thoughts?

I have read many posts:

Process output from apache-commons exec

How can I capture the output of a command as a String with Commons Exec?

But they all read the output when the process has finished, and I need to read it while it is running.

Community
  • 1
  • 1

1 Answers1

1

Figured it out with the following:

ByteArrayOutputStream os = new ByteArrayOutputStream();
PumpStreamHandler ps = new PumpStreamHandler(os);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
String str = avconv_command; 
CommandLine commandLine = CommandLine.parse(str);
ExecuteWatchdog watchdog = new ExecuteWatchdog(1000000);
Executor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
executor.setStreamHandler(ps);
executor.execute(commandLine, resultHandler);
Reader reader = new InputStreamReader(new ByteArrayInputStream(os.toByteArray()));
BufferedReader r = new BufferedReader(reader);
String tmp = null;
while ((tmp = r.readLine()) != null) 
{
     //Do something with tmp line
}

So, I changed the output to a ByteArrayOutputStream, and then read that output. This block must be inside a loop so the byte array can update the content thas was generated from the output of the process