我是 运行 Java 中的一个进程,在等待它完成时卡住了

I'm running a process in Java and am getting stuck when I wait for it to finish

我有一个 Java 程序,它应该使用 ffmpeg 制作视频片段的副本,然后将它们拼接在一起。我的“snip”方法,即制作段文件的方法,有问题,当我调用“process.waitfor()”时它卡住了。当我取出它时,视频加载了一部分,但在我关闭程序之前无法访问。当我尝试删除它们时,程序显示 运行,它说无法删除它们,因为它们正在使用中。谁能引导我朝着正确的方向前进?方法如下:

//snips out all the clips from the main video
public void snip() throws IOException, InterruptedException {
    
    for(int i = 0; i < snippets.size(); i++) {
        //Future reference: 
        //Example: ffmpeg -i 20sec.mp4 -ss 0:0:1 -to 0:0:5 -c copy foobar.mp4
        String newFile = "foobar" + String.valueOf(i) + ".mp4";
        ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoName, "-ss",
                snippets.get(i).getStartTime(), "-to", snippets.get(i).getEndTime(), newFile);
        
        //I tried this first and then added in the process/process.waitfor below
        //processBuilder.start();
        
        Process process = processBuilder.start();
        process.waitFor();
        
        System.out.println("Snip " + i + "\n");
        
        //add to the formatted list of files to be concat later
        if(i == snippets.size() - 1) {
            stitchFiles += newFile + "\"";
        }
        
        else {
            stitchFiles += newFile + "|";
        }
    }
}

程序经常会产生日志或错误输出,这些输出必须送到某个地方。默认情况下 Java 为这些设置“管道”,允许您从 Java 读取生成的输出。缺点是管道的容量有限,如果你不从管道读取,外部程序在尝试写入更多输出时最终会被阻塞。

如果您对捕获日志输出不感兴趣,您可以让 ffmpeg 继承 Java 应用程序的 I/O流:

Process process = processBuilder.inheritIO().start();