我正在通过Java代码运行ffmpeg.exe
来编码视频文件。当ffmpeg终止时(即视频文件是否被编码),我的程序将如何知道?
以下是代码:
Runtime.getRuntime().exec("ffmpeg -ac 2 -i audio.wav -i video.flv -sameq out.flv");
答案 0 :(得分:5)
您可以使用java.lang.Process
的waitFor()
方法:
Process p = Runtime.getRuntime().exec("ffmpeg...");
int exitValue = p.waitFor()
这样,当前线程一直等到Process p
终止。
编辑:
您可以尝试查看ffmpeg的输出:
class StreamDump implements Runnable {
private InputStream stream;
StreamDump(InputStream input) {
this.stream = input;
}
public void run() {
try {
int c;
while ((c = stream.read()) != -1) {
System.out.write(c);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
和
Process p = Runtime.getRuntime().exec("ffmpeg.exe...");
new Thread(new StreamDump(p.getErrorStream()), "error stream").start();
new Thread(new StreamDump(p.getInputStream()), "output stream").start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Exit value: " + p.exitValue());
答案 1 :(得分:0)
我找到了一个解决方法,如果文件out.flv在编码时尝试重命名它不允许,它只能在编码结束后重命名:
Runtime.getRuntime().exec("ffmpeg -ac 2 -i audio.wav -i video.flv -sameq out.flv");
File tempFile=new File("out.flv");
File renamedFile=new File("renamed.flv");
while(!tempFile.renameTo(renamedFile)); //try re-naming the file which is being encoded by ffmpeg
System.out.println("Encoding done");