'Killing FFMPEG command via subprocess

I have the following command in linux pkill -f ffmpeg which works on my Raspberry Pi's terminal. However it doesn't work in my Python code.

This is what I've tried:

subprocess.run('pkill -f ffmpeg', shell=True)

and

subprocess.run('pkill -f ffmpeg', shell=True)

and

os.system('pkill -f ffmpeg')

However they don't seem t work.



Solution 1:[1]

You have different options to run external processes and interact with the operating system.

os.popen() opens a pipe from a command, this allows the command to send its output to another command.

import os

stream = os.popen('pkill -f ffmpeg')
output = stream.readlines()

The susbprocess.Popen method from subprocess module was created with the intention of replacing several methods available in the os module, which were not considered to be very efficient.

import subprocess

subprocess.Popen('pkill -f ffmpeg', shell=True) # or 'pkill -n ffmpeg'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1