question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stop an ffmpeg process

See original GitHub issue

Would be great to see an implementation of a class method to kill the process gracefully (with ‘q’).

I´m trying to stop the ffmpeg process by killing it with SIGINT. Here is a stripped down code of my threading class:

    def is_process_running(self, process_name):
        p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
        out, err = p.communicate()    

        for line in out.splitlines():
            if bytes(process_name, 'utf-8') in line:
                pid = line.split()[0]
                return pid
        return False

    def kill_process(self):
        logger.info('Stopping FFMPEG')
        proc = is_process_running('ffmpeg')
        if proc:
            os.kill(proc, signal.SIGINT)

Here is the traceback:

Exiting normally, received signal 2.
Exception in thread ffmpeg-thread:
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "observer.py", line 45, in run
    self.ff_stream.run()
  File "/Users/levy/Code/streampimp/venv/lib/python3.5/site-packages/ffmpy.py", line 104, in run
    raise FFRuntimeError(self.cmd, ff_command.returncode, out[0], out[1])
ffmpy.FFRuntimeError: `ffmpeg -re -i [the-input-output-parameters]` exited with status 255

I´m curious to know how you are dealing with stopping ffmpeg. Note that I´m running ffmpeg in a separate thread.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
Ch00kcommented, Oct 25, 2016

@busla I made this change in version 0.2.2. You can now call terminate() on FFmpeg.process from another thread to terminate the process forcefully.

1reaction
L05commented, Mar 1, 2022

Was able to use the above suggestions to make this work for me:

import ffmpy
import time
from threading import Thread
import subprocess

ff = ffmpy.FFmpeg(
    inputs={'in.mp4': None},
    outputs={'out.mp4' : '-t 10'}
)

def execute():
    try:
        ff.run(input_data=subprocess.PIPE)
    except ffmpy.FFRuntimeError as ex:
        if ex.exit_code and ex.exit_code != 255:
            raise
    return

if __name__ == '__main__':
    process = Thread(target=execute)
    process.start()
    time.sleep(7)
    ff.process.communicate(str.encode('q'))

You need to pass input_data=subprocess.PIPE into run() so that you can send the encoded ‘q’ keystroke later on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to stop ffmpeg remotely? - linux - Stack Overflow
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...
Read more >
How can I stop FFMPEG? - linux - Server Fault
On Linux, you can use sudo killall ffmpeg to kill every process called "ffmpeg" or ps -ef | grep ffmpeg to get the...
Read more >
how to cleanly stop ffmpeg? - LinuxQuestions.org
i'd like to know how to gracefully stop ffmpeg and have it finish as if the feed has ended. i want to play...
Read more >
No Way to Cleanly Terminate ffmpeg.exe while capturing screen
You can manually terminate the program with CTRL+BREAK which is better than nothing but if you use the command (in a batch/automated process):...
Read more >
How to stop the ffmpeg command · Issue #361 - GitHub
i run that ffmpeg command in separate thread. Now i am able ho cancel the ffmpeg onGoing process. Thanks for your response.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found