java - 我正在用 Java 运行一个进程,当我等待它完成时卡住了
问题描述
我有一个 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 为这些设置“管道”,允许您从 Java 读取生成的输出。缺点是管道的容量有限,如果你不从中读取,外部程序在尝试写入更多输出时最终会被阻塞。
如果您对捕获日志输出不感兴趣,例如可以让ffmpeg
继承Java 应用程序的 I/O 流:
Process process = processBuilder.inheritIO().start();
推荐阅读
- pandas - 重复的行但 1 列具有不同的值。仅保留重复且具有特定列值的行
- php - PHP数组数组:在内部数组上推送项目的问题
- python - 如何在这个作业问题中获得正确的输出?
- php - Laravel:未定义属性:Illuminate\Database\Eloquent\Relations\HasMany::$game
- php - 如何为 WordPress 编写一个 webhook,允许我使用一个优惠券代码获得两个单独的折扣?
- javascript - 当 GPU 不可用时从 Aframe 重定向
- go - 如何在 https 中以生产模式运行 Beego 二进制文件?
- r - 如何绘制按月分组的每小时平均时间序列?
- robotframework - “应该是真的 ${list} == ['a', 'b', 'c']” 和 “应该是真的 $list == ['a', 'b', 'c' 有什么区别? ]”?
- javascript - 在提交时保持价值