我正在尝试使用ffmpeg将一个字节数组导出到视频文件中,但我工作的人坚持认为我在参数中使用了-vcodec copy
。但是,这会导致代码挂起,而如果我不使用-vcodec copy,代码将不会挂起。我不知道问题是什么,过去两个小时我一直在尝试调试这段代码。
以下是相关的代码部分。我在代码挂起的行上方和下方添加了注释。有人能帮助我吗?
// This is the tricky part. We need to build an ffmpeg process that
// takes input from stdin, and then plug Java into that.
ProcessBuilder ffmpegBuilder = new ProcessBuilder();
String[] cmd = {"ffmpeg", "-i", "-","-vcodec", "copy", directory
+ "/" + fileName};
StringBuilder combinedCmd = new StringBuilder();
for (String s : cmd) {
combinedCmd.append(s);
combinedCmd.append(" ");
}
mLogger.log(Level.INFO,"Final command is " + combinedCmd.toString());
ffmpegBuilder.command(cmd);
ffmpegBuilder.redirectErrorStream(true); // So that stdout and stderr go
// to the same stream.
byte[] dataToWrite = new byte[data.size()];
for (int i = 0; i < dataToWrite.length; i++) {
dataToWrite[i] = data.get(i); // Is there really STILL no better way
// to convert an ArrayList to an
// array?!
}
try {
Process ffmpeg = ffmpegBuilder.start();
OutputStream stdin = ffmpeg.getOutputStream();
BufferedReader stdout = new BufferedReader(new InputStreamReader(
ffmpeg.getInputStream()));
//HANGS AT THIS LINE vvvvvvvvvvvvvvvv
stdin.write(dataToWrite);
//HANGS AT THIS LINE ^^^^^^^^^^^^^^^^
String line = "I know a song that gets on everybody's nerves...";
while ((line != null) && stdout.ready()) {
line = stdout.readLine();
mLogger.log(Level.INFO, line);
}
try {
ffmpeg.waitFor(2, TimeUnit.SECONDS);
ffmpeg.destroyForcibly();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:2)
事实证明,该程序在写入数据的过程中已经过了一半。那里有大量的数据,我知道此时出现了问题,所以我试着告诉java进程从我所做的BufferedReader开始读取stdout。固定的东西。事实证明,BufferedReader正在填满,而且由于stdout无处可去,整个过程都悬而未决。我在这里改变了挂在这段代码上的那一行:
for (int i = 0;i < dataToWrite.length;i++) {
stdin.write(dataToWrite[i]);
if (stdout.ready()) {
String line = stdout.readLine();
mLogger.log(Level.INFO, line);
}
}
修复了一切。