【问题标题】:I'm running a process in Java and am getting stuck when I wait for it to finish我正在用 Java 运行一个进程,当我等待它完成时卡住了
【发布时间】:2020-11-20 20:47:56
【问题描述】:

我有一个 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: https://stackoverflow.com/questions/9885643/ffmpeg-executed-from-javas-processbuilder-does-not-return-under-windows-7/9885717#9885717
        //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 ffmpeg processbuilder


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      相关资源
      最近更新 更多