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

huangapple go评论56阅读模式
英文:

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

问题

我有一个Java程序,应该复制视频的片段然后使用ffmpeg将它们拼接在一起。我的“snip”方法,用于制作段文件,有一个问题,当我调用“process.waitfor()”时它会卡住。当我将它去掉时,视频部分加载,但直到我关闭程序之前都无法访问。当我尝试在程序运行时删除它们时,它说无法删除,因为它们正在使用中。有人能指导我正确的方向吗?这是该方法:

// 从主视频中剪切所有剪辑
public void snip() throws IOException, InterruptedException {
    
    for(int i = 0; i < snippets.size(); i++) {
        // 以后参考:https://stackoverflow.com/questions/9885643/ffmpeg-executed-from-javas-processbuilder-does-not-return-under-windows-7/9885717#9885717
        // 示例: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);
        
        // 我首先尝试这个,然后添加了下面的process/process.waitfor
        // processBuilder.start();
        
        Process process = processBuilder.start();
        process.waitFor();
        
        System.out.println("Snip " + i + "\n");
        
        // 添加到以后要连接的文件的格式化文件列表
        if(i == snippets.size() - 1) {
            stitchFiles += newFile + "\\";
        }
        
        else {
            stitchFiles += newFile + "|";
        }
    }
}

希望这能帮助你解决问题。

英文:

I have a Java program that is supposed to make copies of segments of a video and then stitch them back together, using ffmpeg. My "snip" method, the one that makes the segment files, has a problem, it gets stuck when I call "process.waitfor()". When I take it out, the videos load partly, but cannot be accessed until I close the program. When I try to delete them, while the program is running, it says that they cannot be deleted because they are in use. Could anyone lead me in the right direction? Here is the method:

//snips out all the clips from the main video
public void snip() throws IOException, InterruptedException {
	
	for(int i = 0; i &lt; 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 = &quot;foobar&quot; + String.valueOf(i) + &quot;.mp4&quot;;
		ProcessBuilder processBuilder = new ProcessBuilder(&quot;ffmpeg&quot;, &quot;-i&quot;, videoName, &quot;-ss&quot;,
				snippets.get(i).getStartTime(), &quot;-to&quot;, 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(&quot;Snip &quot; + i + &quot;\n&quot;);
		
		//add to the formatted list of files to be concat later
		if(i == snippets.size() - 1) {
			stitchFiles += newFile + &quot;\&quot;&quot;;
		}
		
		else {
			stitchFiles += newFile + &quot;|&quot;;
		}
	}
}

答案1

得分: 1

程序通常会产生日志或错误输出,这些输出需要被处理。默认情况下,Java设置了“管道”来处理这些输出,允许您从Java中读取生成的输出。不过,管道有限的容量,如果您不读取它们,外部程序在尝试写更多输出时将最终被阻塞。

如果您不关心捕获日志输出,您可以让ffmpeg 继承 Java 应用程序的输入/输出流:

Process process = processBuilder.inheritIO().start();
英文:

Programs often produce log or error output which has to go somewhere. By default Java sets up "pipes" for these, which allow you to read the produced output from Java. The downside is, pipes have a limited capacity, and if you don't read from them, the external program will eventually get blocked when it tries to write more output.

If you're not interested in capturing the log output, you can for example let ffmpeg inherit the Java application's I/O streams:

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

huangapple
  • 本文由 发表于 2020年7月31日 09:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63184484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定