gpt4 book ai didi

java - 在 java 进程中运行 ffmpeg 命令在 waitFor() 中挂起

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:15 24 4
gpt4 key购买 nike

我正在运行 ffmpeg 命令为给定图像(img001.jpg、img002.jpg ...)生成视频,它正在创建 slide.mp4,但它会无限等待:

public class Ffmpeg {

public static void main(String[] args) throws IOException, InterruptedException {
String path = "E:\\pics\\Santhosh\\FadeOut\\testing";
String cmd = "ffmpeg -r 1/5 -i img%03d.jpg -c:v libx264 -r 30 -y -pix_fmt yuv420p slide.mp4";
runScript (path, cmd);
}

private static boolean runScript(String path, String cmd) throws IOException, InterruptedException {
List<String> commands = new ArrayList<String>();
commands.add("cmd");
commands.add("/c");
commands.add(cmd);
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File(path));
pb.redirectErrorStream(true);
Process process = pb.start();
flushInputStreamReader(process);
int exitCode = process.waitFor();
return exitCode == 0;
}
}

private static void flushInputStreamReader (Process process) throws IOException, InterruptedException {
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line=null;
StringBuilder s = new StringBuilder();
while((line=input.readLine()) != null) {
s.append(line);
}
}

有什么建议吗?

写完函数flushInputStreamReader后,它的工作

最佳答案

除了读取 ErrorStream 之外,还有更好的方法来处理这个问题。

在命令中加入-loglevel quiet,这样ErrorStream就不会在第一时间溢出而阻塞进程。

关于java - 在 java 进程中运行 ffmpeg 命令在 waitFor() 中挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com