簡體   English   中英

從Apache exec Commons獲取並解析輸出流

[英]Get and parse output stream from Apache exec commons

我正在嘗試使用av​​conv捕獲IP Cam的流。 我設法使用Java的apache exec commons庫將其流保存到文件中,如下所示:

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);

這樣,avconv開始捕獲流並將其保存到文件中,並且在控制台上,我可以看到avconv的工作方式以及進程的輸出。 該輸出的每一行都顯示當前正在捕獲的視頻的持續時間,比特率等。我需要捕獲該輸出並在運行時對其進行處理。 有什么想法嗎?

我讀了很多帖子:

處理來自apache-commons exec的輸出

如何使用Commons Exec將命令的輸出捕獲為字符串?

但是他們都在進程完成后讀取輸出,因此我需要在運行時讀取輸出。

用以下方法解決:

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
}

因此,我將輸出更改為ByteArrayOutputStream,然后讀取該輸出。 此塊必須在循環內,以便字節數組可以更新從進程的輸出生成的內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM