赞
踩
在向管道中写入数据的时候,稳定输入一段时间后,若长时间不再输入,则会报错:BrokenPipeError: [Errno 32] Broken pipe。且该子进程会进入sleep状态(即pipe.poll() == 1
)
每次向管道中输入数据的时候,判断子进程的状态,非正常状态的话,重启子进程。
# 定义管道及ffmpeg命令,输出rtmp流的时候使用 command = ['ffmpeg', '-y', '-v', '24', # 日志显示等级 '-f', 'rawvideo', '-vcodec', 'rawvideo', '-pix_fmt', 'bgr24', '-s', str(self.width) + 'x' + str(self.height), '-i', '-', '-c:v', 'h264_nvenc', '-pix_fmt', 'yuv420p', '-f', 'rtsp', '-rtsp_transport', 'tcp', rtsp_url] # 使用指定的GPU索引 my_env = os.environ.copy() my_env["CUDA_VISIBLE_DEVICES"] = self.gpu_index # 定义ffmpeg的子进程并启动 pipe = sp.Popen(command, stdin=sp.PIPE, env=my_env) num = 0 while self._isLive: num += 1 frame = self.realtime_queue.get() if num == 100: print("start sleep 50") time.sleep(50) print("end sleep 50") if num >= 100: print("超时后的,第%d次写入" % (num-99)) time.sleep(5) pipe.stdin.write(frame.tostring()) # 存入管道用于直播 pipe.stdin.close() # 关闭输入管道 pipe.communicate() # 等待子进程关闭
# 定义管道及ffmpeg命令,输出rtmp流的时候使用 command = ['ffmpeg', '-y', '-v', '24', # 日志显示等级 '-f', 'rawvideo', '-vcodec', 'rawvideo', '-pix_fmt', 'bgr24', '-s', str(self.width) + 'x' + str(self.height), '-i', '-', '-c:v', 'h264_nvenc', '-pix_fmt', 'yuv420p', '-f', 'rtsp', '-rtsp_transport', 'tcp', rtsp_url] # 使用指定的GPU索引 my_env = os.environ.copy() my_env["CUDA_VISIBLE_DEVICES"] = self.gpu_index # 定义ffmpeg的子进程并启动 pipe = sp.Popen(command, stdin=sp.PIPE, env=my_env) num = 0 while self._isLive: num += 1 # poll()返回该子进程的状态,0正常结束,1sleep,2子进程不存在,-15 kill,None正在运行 if pipe.poll() is not None: time.sleep(3) print(pipe.poll()) print("the popen of ffmpeg not run, restart this:%s" % self.name) pipe = sp.Popen(command, stdin=sp.PIPE, env=my_env) frame = self.realtime_queue.get() if num == 100: print("start sleep 50") time.sleep(50) print("end sleep 50") if num >= 100: print("超时后的,第%d次写入" % (num-99)) time.sleep(5) try: pipe.stdin.write(frame.tostring()) # 存入管道用于直播 except BrokenPipeError: print("Pushing the camera of %s appear ERROR:%s" % (self.name, rtsp_url)) print(traceback.format_exc()) pipe.stdin.close() # 关闭输入管道 pipe.communicate() # 等待子进程关闭
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。