How to stop ffmpeg remotely?

I run ffmpeg on another machine to capture the screen. I would like to be able to stop recording it remotely. FFMPEG requires q to be pressed to stop the encoding, as it needs to do some finalization to complete the file. I know that I can kill him with kill / killall, but this can damage the video.

Press [q] to stop encoding 

I cannot find anything on Google specifically for this, but there is an assumption that the echo in / proc // fd / 0 will work.

I tried this, but that does not stop ffmpeg. However, q is displayed in the terminal where ffmpeg is running.

 echo -nq > /proc/16837/fd/0 

So, how can I send a symbol to another existing process in such a way that it seems to be typed locally? Or is there another way to remotely stop ffmpeg.

+6
source share
3 answers

Newer versions of ffmpeg no longer use "q", at least on Ubuntu Oneiric, instead they say to press Ctrl + C to stop them. Thus, with the newer version, you can simply use "killall -INT" to send SIGINT instead of SIGTERM, and they should exit cleanly.

+6
source

Here I found that when I ran into this problem: make an empty file (it should not be a named pipe or something else), and then write "q" to it when it comes time to stop recording.

  • $ touch stop
  • $ <./ stop ffmpeg -i ... output.ext> / dev / null 2 ​​-> Capture.log &
  • $ Waiting for a stop
  • $ echo 'q'> stop

FFmpeg stops as if it received a "q" from the STDIN terminal.

+12
source

You can also try using β€œwait” to automate the execution and stop of a program. You will need to start it using some virtual shell, for example screen , tmux or byobu , and then run ffmpeg inside it. Thus, you can again get the virtual shell screen and provide the "q" parameter.

  • Run a virtual shell session locally or remotely, say, using a β€œscreen”. Name the session the -S option, for example screen -S recvideo Then you can run ffmpeg as you like. You can optionally disconnect from this session with Ctrl + a + d.

  • Connect to the machine where ffmpeg is running inside the screen (or tmux or something else) and connect to it: screen -d -RR recvideo , and then send "q"

To do this from within the script, you can use expect, for example:

 prompt="> " expect << EOF set timeout 20 spawn screen -S recvideo expect "$prompt" send -- "ffmpeg xxxxx\r" set timeout 1 expect eof EOF 

Then at another point, either at the script point or in another script, you will restore it:

 expect << EOF set timeout 30 spawn screen -d -RR recvideo expect "$prompt" send -- "q" expect "$prompt" send -- "exit\r" expect eof EOF 

You can also automate an entire waiting ssh session by passing a sequence of commands and β€œwaiting” to do what you want.

0
source

Source: https://habr.com/ru/post/910809/


All Articles