How to stop ffmpeg remotely?

2019-02-06 19:35发布

I'm running ffmpeg on another machine for screen capture. I'd like to be able to stop it recording remotely. FFMPEG requires that q is pressed to stop encoding as it has to do some finalization to finish the file cleanly. I know I could kill it with kill/killall however this can lead to corrupt videos.

Press [q] to stop encoding

I can't find anything on google specifically for this, but some there is suggestion that echoing into /proc//fd/0 will work.

I've tried this but it does not stop ffmpeg. The q is however shown in the terminal in which ffmpeg is running.

echo -n q > /proc/16837/fd/0

So how can I send a character to another existing process in such a way it is as if it were typed locally? Or is there another way of remotely stopping ffmpeg cleanly.

标签: linux ffmpeg
3条回答
Juvenile、少年°
2楼-- · 2019-02-06 20:20

You can also try to use "expect" to automate the execution and stop of the program. You would have to start it using some virtual shell like screen, tmux or byobu and then start the ffmpeg inside of it. This way you would be able to get again the virtual shell screen and give the "q" option.

  1. Locally or remotely start a virtual shell session, lets say with "screen". Name the session with -S option, like screen -S recvideo Then you can start the ffmpeg as you like. You can, optionally, detach from this session with a Ctrl+a + d.

  2. Connect to the machine where the ffmpeg is running inside the screen (or tmux or whatever) and reconnect to it: screen -d -RR recvideo and then send the "q"

To do that from inside a script you can then use expect, like:

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

Then, in another moment or script point or in another script you recover 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 the whole ssh session with expect, passing a sequence of commands and "expects" to do what you want.

查看更多
在下西门庆
3楼-- · 2019-02-06 20:24

Here's a neat trick I discovered when I was faced with this problem: Make an empty file (it doesn't have to be a named pipe or anything), then write 'q' to it when it's time to stop recording.

  1. $ touch stop
  2. $ <./stop ffmpeg -i ... output.ext >/dev/null 2>>Capture.log &
  3. $ wait for stopping time
  4. $ echo 'q' > stop

FFmpeg stops as though it got 'q' from the terminal STDIN.

查看更多
手持菜刀,她持情操
4楼-- · 2019-02-06 20:27

Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu Oneiric, instead they say to press Ctrl+C to stop them. So with a newer version you can simply use 'killall -INT' to send them SIGINT instead of SIGTERM, and they should exit cleanly.

查看更多
登录 后发表回答