python 解码 视频解码
目录
av解码rtsp rtmp多路视频。
av解码rtsp rtmp多路视频。
import av
import cv2
import threadingdef play_stream(rtmp_url, window_name):try:# 打开RTMP流container = av.open(rtmp_url, timeout=10)video_stream = next(s for s in container.streams if s.type == 'video')print(f"[{window_name}] 已连接: {rtmp_url}")print(f"[{window_name}] 编码: {video_stream.codec_context.name}, 分辨率: {video_stream.width}x{video_stream.height}, 帧率: {video_stream.average_rate}")for frame in container.decode(video_stream):img = frame.to_ndarray(format="bgr24") # 转成OpenCV格式cv2.imshow(window_name, img)if cv2.waitKey(1) & 0xFF == ord('q'): # 按 q 退出breakexcept Exception as e:print(f"[{window_name}] 出错: {e}")finally:cv2.destroyWindow(window_name)if __name__ == "__main__":rtmp_url1 = "rtmp://xxx/live/stream1"rtmp_url2 = "rtmp://xxx/live/stream2"# 创建两个线程t1 = threading.Thread(target=play_stream, args=(rtmp_url1, "Stream 1"))t2 = threading.Thread(target=play_stream, args=(rtmp_url2, "Stream 2"))t1.start()t2.start()t1.join()t2.join()cv2.destroyAllWindows()