从Apache Commons-Exec捕获大量输出
-
12-09-2019 - |
题
我正在通过执行来编写Java的视频应用程序 ffmpeg
并捕获其输出到标准输出。我决定使用Apache Commons-Exec而不是Java的 Runtime
, ,因为看起来更好。但是,我很难捕获所有输出。
我认为使用管道是必经之路,因为这是过程间通信的标准方式。但是,我使用的设置 PipedInputStream
和 PipedOutputStream
是错的。它似乎有效,但仅适用于流的前1042个字节,这是奇怪的是 PipedInputStream.PIPE_SIZE
.
我对使用管道没有爱的恋情,但是由于数据的速度和数量,我想避免使用磁盘I/O(如果可能的话)(512x384分辨率的1M 20S视频产生690M
管道数据)。
关于处理来自管道的大量数据的最佳解决方案的想法?我的两个课程的代码如下。 (是的, sleep
不好。对此有何想法? wait()
和 notifyAll()
?)
WriteFrames.java
public class WriteFrames {
public static void main(String[] args) {
String commandName = "ffmpeg";
CommandLine commandLine = new CommandLine(commandName);
File filename = new File(args[0]);
String[] options = new String[] {
"-i",
filename.getAbsolutePath(),
"-an",
"-f",
"yuv4mpegpipe",
"-"};
for (String s : options) {
commandLine.addArgument(s);
}
PipedOutputStream output = new PipedOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
DefaultExecutor executor = new DefaultExecutor();
try {
DataInputStream is = new DataInputStream(new PipedInputStream(output));
YUV4MPEGPipeParser p = new YUV4MPEGPipeParser(is);
p.start();
executor.setStreamHandler(streamHandler);
executor.execute(commandLine);
} catch (IOException e) {
e.printStackTrace();
}
}
}
YUV4MPEGPipeParser.java
public class YUV4MPEGPipeParser extends Thread {
private InputStream is;
int width, height;
public YUV4MPEGPipeParser(InputStream is) {
this.is = is;
}
public void run() {
try {
while (is.available() == 0) {
Thread.sleep(100);
}
while (is.available() != 0) {
// do stuff.... like write out YUV frames
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
解决方案
问题在于YUV4MPEGPIPEPARSER类的运行方法。有两个连续的循环。如果流在流上当前没有数据,则第二个循环将立即终止(例如,到目前为止,所有输入均由Parser处理,FFMPEG或Stream Pump不够快,无法为其提供一些新数据 - >可用()== 0 - >循环被终止 - >泵线饰面)。
只需摆脱这两个循环并睡觉,然后执行简单的阻止读(),而不是检查是否可以处理任何数据。由于解析器代码是在单独的线程上启动的,因此也可能不需要wait()/notify()或什至sleep()。
您可以重写这样的run()方法的代码:
public class YUV4MPEGPipeParser extends Thread {
...
// optimal size of buffer for reading from pipe stream :-)
private static final int BUFSIZE = PipedInputStream.PIPE_SIZE;
public void run() {
try {
byte buffer[] = new byte[BUFSIZE];
int len = 0;
while ((len = is.read(buffer, 0, BUFSIZE) != -1) {
// we have valid data available
// in first 'len' bytes of 'buffer' array.
// do stuff.... like write out YUV frames
}
} catch ...
}
}
不隶属于 StackOverflow