我正在使用python工作ffmpeg。这在远程服务器工作正常时有效,但是当远程服务器关闭时,我可以看到shell上的消息说 '连接到tcp:// xxxxxxx失败:连接被拒绝,blabla'
pro = sp.Popen(command, preexec_fn=os.setsid, shell=False, stderr=sp.PIPE, stdout=sp.PIPE)
捕捉异常方法1:
try:
out = self.pro.stderr.readline()
while out:
print '......'
except BrokenPipeError:
print 'err'
捕捉异常方法2:
for line in self.pro.stderr:
try:
print line
except BrokenPipeError:
print 'error'
然而,这些都不起作用。
答案 0 :(得分:0)
communicate()
返回元组(stdoutdata, stderrdata)
,因此您只需要打印第二个元素:
cmd = ('ffmpeg', '-hide_banner', '-i', 'tcp://127.0.0.1:10000', '-c', 'copy', '-f', 'null', '/dev/null');
s = subprocess.Popen(cmd, shell=False, preexec_fn=os.setsid, stderr=subprocess.PIPE)
# print stderr
print s.communicate()[1]
输出:
$ ./test.py
[tcp @ 0x55a01c945000] Connection to tcp://127.0.0.1:10000 failed: Connection refused
tcp://127.0.0.1:10000: Connection refused