1

I have an issue

I want to create a new ffmpeg process using subprocess.popen find out his pid and in the body of a python program to see if the process is alive or not

args = shlex.split ('ffmpeg -i rtsp: //192.168.1.68: 8554 / mystream -f segment -strftime 1 -segment_time 5 13_REG1_CHANNEL3_% Y-% m-% d_% H-% M-% S-% s .mp3 ')
print (args)
proc = subprocess.Popen (args, stdout = subprocess.PIPE)
ch_pid = proc.pid
print (proc.pid)
proc.wait ()
print (proc.communicate ())
while (1):
 if (os.system (str ('kill -0 {PID}'). format (PID = ch_pid)) == 0):
   print ('proc is alive')
 else:
   break

at while loop i tried to check this process pid via kill -0 pid this command will return zero if everything is alright and process is running

BUT

IF ffmpeg will fall there will be no changes

kill -0 pid will continue to returning zero code that everything is good

What should i do?

TiHan
  • 35
  • 5

1 Answers1

0

You may solve it using proc.poll().
See: Is there a way to check if a subprocess is still running?.

Here is an example:

import shlex
import subprocess

#args = shlex.split ('ffmpeg -i rtsp: //192.168.1.68: 8554 / mystream -f segment -strftime 1 -segment_time 5 13_REG1_CHANNEL3_% Y-% m-% d_% H-% M-% S-% s .mp3 ')
args = shlex.split ('ffmpeg -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -vcodec copy -acodec copy BigBuckBunny_115k.mov')

print (args)
proc = subprocess.Popen (args, stdout = subprocess.PIPE)
ch_pid = proc.pid
print (proc.pid)
proc.wait ()
print (proc.communicate ())
while (1):
    poll = proc.poll()
    if poll is None:
        print ('proc is alive')
        # p.subprocess is alive
    else:
        print ('proc is dead')
        break

Here is a cleaner code sample:

import shlex
import subprocess
import time

args = shlex.split ('ffmpeg -y -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -vcodec copy -acodec copy BigBuckBunny_115k.mov')

proc = subprocess.Popen(args)

while True:
    poll = proc.poll()
    if poll is None:
        print ('proc is alive')
    else:
        print ('proc is dead')
        break
    time.sleep(1)

proc.wait()
Rotem
  • 30,366
  • 4
  • 32
  • 65