0

I'm using Python 3.5. I'm launching ffmpeg to record a file but I can't seem to kill the process. I've tried this:

import os
import subprocess

pro = subprocess.Popen(recordcommand, shell=True)
...
do some things; wait for a stop command to come in 
...
if msg =='STOP':
     pro.kill()

My process starts fine but it never stops. I do not get any errors. I've checked the API and thought I was doing things right. Can anyone tell me where I'm going wrong? Thanks!

Edit: Fixed pro.kill to pro.kill() but still not working. Here's the command I'm sending:

 C:/Users/User/Desktop/ffmpeg-20150928-git-235381e-win64-static/bin/ffmpeg.exe -f dshow  -crossbar_video_input_pin_number 2 -i video="ATI AVStream Analog Capture" -f mpegts -fs 16777216 -y D:\Media\tvrecordings\SageSlingBox1onlocalhost4510TVTuner-0.mpgbuf 2>NUL

Perhaps something is keeping it from being killed? I forgot to mention that my OS is Win 7 64 bit.

dinki
  • 103
  • 7
  • You are killing the parent process not the child i.e the shell. Not overly familiar with windows but passing a list of args with shell=False may work, if not you could taskill or win32process – Padraic Cunningham Sep 30 '15 at 19:48
  • Thanks for this. Unfortunately I'm a very novice 'programmer' . Can you tell me how I kill the shell? – dinki Sep 30 '15 at 19:52
  • You are killing the shell, you need to kill the process. Try passing the args as individual args in a list and remove shell=True, if not just use check_call to kill ffmpeg with taskill if you want a simple solution – Padraic Cunningham Sep 30 '15 at 19:53
  • On Windows, you could [use `taskkill`](http://stackoverflow.com/a/17614872/4279) or [`psutil`](http://stackoverflow.com/a/25134985/4279). – jfs Oct 02 '15 at 08:09

2 Answers2

1

kill is a function, and as such has to be called to do anything.

Try changing it to:

if msg =='STOP':
     pro.kill()
Andreas
  • 622
  • 4
  • 11
  • afaik , iirc that will only work in *nix .. but yes that is certainly his issue (but it still might not kill the process ... , but that will be a different question +1 for this answer) – Joran Beasley Sep 30 '15 at 17:46
  • I made that correction but it still did not work. I updated my question with additional information. Thanks for the answer, but still not working. – dinki Sep 30 '15 at 17:58
0

if you try to kill some process try this:

import subprocess
import sys
proc = subprocess.Popen(
['sudo', 'kill', args], stdin=subprocess.PIPE)

proc.communicate(sys.argv[1])
Najeeb Choudhary
  • 396
  • 1
  • 3
  • 21