Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop ffmpeg remotely?

Tags:

linux

ffmpeg

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.

like image 273
Adam Avatar asked Mar 15 '12 15:03

Adam


People also ask

How do I stop ffmpeg process?

On Linux, you can use sudo killall ffmpeg to kill every process called "ffmpeg" or ps -ef | grep ffmpeg to get the process ids and then sudo kill <PID> to kill each one individually. If they don't die, sudo kill -9 <PID> will terminate them.

How do I stop ffmpeg encoding?

Usual way to abort a ffmpeg process is to press q or Ctrl-C.


2 Answers

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.

like image 195
TheHelper Avatar answered Sep 19 '22 06:09

TheHelper


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.

like image 41
sashoalm Avatar answered Sep 20 '22 06:09

sashoalm