我有这个代码:
public InputStream getInputStream() throws Exception {
try {
process = Runtime.getRuntime().exec("ffmpeg -f dshow -i video=\"" + query + "\":audio=\"" + microPhoneName + "\" -r 25 -vcodec mpeg4 -acodec mp3 -f avi -");
}
catch (Exception e) {
}
return process.getInputStream();
}
当我使用 inputStream.read(b)
命令时,它只能工作一小会儿(180 到 400 次,具体取决于我使用的格式和编解码器),然后 inputStream
锁定 read
并且应用程序不再运行。
有什么问题吗?内存饱和(ffmpeg进程内存至少14mb)? 有没有办法解锁这种情况(清理内存,使用文件作为桥梁来防止锁)?
当然,我需要一点“实时”,而不是“后处理”。 我不限于使用 ffmpeg,如果需要我可以更改它。
最佳答案
阅读this article后,我自己找到了解决方案:问题是 errorStream
已满,必须读取它才能让 process
继续其工作,所以我插入了一个 Thread
消耗errorStream
:
public InputStream getInputStream() throws Exception {
try {
process = Runtime.getRuntime().exec("ffmpeg -f dshow -i video=\"" + query + "\":audio=\"" + microPhoneName + "\" -r 25 -vcodec mjpeg -acodec mp3 -f " + getContentExtension() + " -");
new Thread("Webcam Process ErrorStream Consumer") {
public void run() {
InputStream i = process.getErrorStream();
try {
while (!isInterrupted()) {
i.read(new byte[bufferLength]);
}
} catch (IOException e) {
}
}
}.start();
} catch (Exception e) {
}
return process.getInputStream();
}
关于java - 捕获时 ffmpeg 管道阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271044/