How to stop ffmpeg that runs through java process

2019-08-08 18:32发布

I am running ffmpeg in Java. Using p = Runtime.getRuntime().exec(command); It is used to stream video through a red5 server.

My problem is that ffmpeg requires "q" to be pressed in order to stop. How can I do that? How can I send the q character to the running process so it will execute p.destroy(); or something similar? At the moment it runs forever until I kill the process in the task manager. I am using Windows7.

1条回答
时光不老,我们不散
2楼-- · 2019-08-08 19:13

To inject the 'q' key into the running process, you can do something like this:

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.

This should successfully 'quit' the running ffmpeg process.

查看更多
登录 后发表回答