我是靠谱客的博主 大胆戒指,这篇文章主要介绍Python3 PyAV 解析Rtsp流显示和保存为mp4文件,现在分享给大家,希望可以做个参考。

 本文使用pyav 解析视频流并且保存为mp4,详见代码。使用opencv 的VideoWriter 保存的mp4 编码有问题不能在web 端使用H5

播放器进行播放。所以采用pyav 来实现录像功能。

import av
import cv2


# rtsp 是标准的海康威视3级子码流
video = av.open('rtsp://admin:haifan123@10.0.0.69:554/Streaming/Channels/103')
print("format:" , video.dumps_format())
video_context = video.streams.video[0].codec_context

container = av.open('test.mp4', mode='w')
stream = container.add_stream('h264', rate= video_context.framerate)
stream.width = video_context.width
stream.height = video_context.height
stream.pix_fmt = 'yuv420p'

try:
    for packet in video.demux():
        for frame in packet.decode():
            if packet.stream.type == 'video':
                print("frame = ", frame)
                
                img = frame.to_ndarray(format='bgr24')
                frame2 = av.VideoFrame.from_ndarray(img, format='bgr24')
                for packet2 in stream.encode(frame2):
                    container.mux(packet2)
                cv2.imshow("Video", img)

            
        if cv2.waitKey(1) & 0xFF == ord('q'):
            # Flush stream
            for packet2 in stream.encode():
                container.mux(packet2)
            # Close the file
            container.close()
            break
except KeyboardInterrupt:
    pass
 
cv2.destroyAllWindows()

 

最后

以上就是大胆戒指最近收集整理的关于Python3 PyAV 解析Rtsp流显示和保存为mp4文件的全部内容,更多相关Python3内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部