如何停止通过java进程运行的ffmpeg

如何停止通过java进程运行的ffmpeg,java,process,ffmpeg,runtime,Java,Process,Ffmpeg,Runtime,我正在用Java运行ffmpeg。使用p=Runtime.getRuntime().exec(命令)用于通过red5服务器传输视频 我的问题是ffmpeg需要按下“q”键才能停止。我该怎么做?如何将q字符发送到正在运行的进程,使其执行p.destroy()或类似的东西?目前,它将永远运行,直到我在任务管理器中终止进程。我正在使用Windows7 要将“q”键注入正在运行的进程,可以执行以下操作: OutputStream ostream = p.getOutputStream(); //Get

我正在用Java运行ffmpeg。使用
p=Runtime.getRuntime().exec(命令)用于通过red5服务器传输视频


我的问题是ffmpeg需要按下“q”键才能停止。我该怎么做?如何将q字符发送到正在运行的进程,使其执行
p.destroy()或类似的东西?目前,它将永远运行,直到我在任务管理器中终止进程。我正在使用Windows7

要将“q”键注入正在运行的进程,可以执行以下操作:

OutputStream ostream = p.getOutputStream(); //Get the output stream of the process, which translates to what would be user input for the commandline
ostream.write("q\n".getBytes());       //write out the character Q, followed by a newline or carriage return so it registers that Q has been 'typed' and 'entered'.
ostream.flush();                          //Write out the buffer.

这将成功地“退出”正在运行的ffmpeg进程。

您可以通过将
-nostdin
作为ffmpeg的参数之一来避免这种情况。这将禁用用户输入。

这是否有效?我在通过java停止录制时遇到了同样的问题。在实现时它对我起了作用。。。如果在您自己的实现过程中发现它不起作用,我很乐意在可能的情况下协助调试它。:)这在没有“\n”和“q”的情况下对我有效。。你需要一个输出流,不知道为什么,但你知道。BufferedReader is=新的BufferedReader(新的InputStreamReader(recordProcess.getErrorStream());弦线;而((line=is.readLine())!=null){}