gpt4 book ai didi

python - 如何判断一个ffmpeg子进程是否掉线

转载 作者:行者123 更新时间:2023-12-04 23:02:12 25 4
gpt4 key购买 nike

我有一个问题

我想使用 subprocess.popen 创建一个新的 ffmpeg 进程
找出他的pid
并在python程序的主体中查看进程是否处于事件状态

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

在 while 循环中,我尝试通过 kill -0 pid 检查此进程 pid
如果一切正常且进程正在运行,此命令将返回零



如果 ffmpeg 会下降
不会有任何变化

kill -0 pid 将继续返回零代码,一切都很好

我应该怎么办?

最佳答案

您可以使用 proc.poll() 解决它.
见:Is there a way to check if a subprocess is still running? .

这是一个例子:

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

这是一个更简洁的代码示例:
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()

关于python - 如何判断一个ffmpeg子进程是否掉线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61119432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com