我正在使用FFMPEG转换音频文件。我调用命令行,然后从 Java调用FFMPEG 。我使用 Runtime 和 Process Runtime.exec()来做到这一点。对于这个过程,我有一个InputStream 和一个 ErrorStream。出于某种原因,即使 FFMPEG 命令运行良好,它也会从错误流中打印出来,给人以失败的印象。有谁知道为什么会发生这种情况?显然这不是一个主要问题,因为它确实可以正常工作,但如果由于某种原因它确实出错,或者该项目的新手没有意识到它是如何工作的,它可能会令人困惑。有任何想法吗?以下是相关代码:
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd);
Process proc = rt.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
还有 StreamGobbler 类:
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}