ffmpeg什么时候终止?

dev*_*ani 3 java video encoding ffmpeg xuggle

我正在运行ffmpeg.exeJava代码来编码视频文件.当ffmpeg终止时(即视频文件被编码),我的程序将如何知道?

这是代码:

Runtime.getRuntime().exec("ffmpeg -ac 2 -i audio.wav -i video.flv -sameq out.flv");
Run Code Online (Sandbox Code Playgroud)

jal*_*aba 5

您可以使用以下waitFor()方法java.lang.Process:

Process p = Runtime.getRuntime().exec("ffmpeg...");
int exitValue = p.waitFor()
Run Code Online (Sandbox Code Playgroud)

这样,当前线程等待直到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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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());
Run Code Online (Sandbox Code Playgroud)