PyAV播放rtsp代码

import av
import cv2

dicOption={'buffer_size':'1024000','rtsp_transport':'tcp','stimeout':'20000000','max_delay':'3000000'}
video = av.open('rtsp://admin:[email protected]:554/h264/ch0/main/av_stream', 'r',format=None,options=dicOption, metadata_errors='strict')
#video = av.open('rtsp://admin:[email protected]:554/h264/ch0/main/av_stream', 'r')
print("format:" + video.dumps_format())
print('after open')
index = 0
try:
    for frame in video.decode():
        # Do something with `frame`
        index += 1
        #print("frame:{}".format(index))

        img = frame.to_ndarray(format='bgr24')
        cv2.imshow("Play", img)
        #cv2.imwrite("Test{}.jpg".format(index), img)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
except Exception as e:
    print('fate erro:{}'.format(e))

cv2.destroyAllWindows()

另一种方式代码

import av
import cv2
import time

dicOption={'buffer_size':'1024000','rtsp_transport':'tcp','stimeout':'20000000','max_delay':'200000'}
video = av.open('rtsp://admin:[email protected]:554/h264/ch0/main/av_stream', 'r',format=None,options=dicOption, metadata_errors='nostrict')
#video = av.open('rtsp://admin:[email protected]:554/h264/ch0/main/av_stream', 'r')
try:
    for packet in video.demux():
        for frame in packet.decode():
            if packet.stream.type == 'video':
                img = frame.to_ndarray(format='bgr24')
                #time.sleep(1)
                cv2.imshow("Video", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
except KeyboardInterrupt:
    pass
cv2.destroyAllWindows()

 

你可能感兴趣的:(Python)