Python3 PyAV 解析Rtsp流显示和保存为mp4文件

您所在的位置:网站首页 python打开mp4文件 Python3 PyAV 解析Rtsp流显示和保存为mp4文件

Python3 PyAV 解析Rtsp流显示和保存为mp4文件

2023-08-11 05:25| 来源: 网络整理| 查看: 265

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

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

import av import cv2 # rtsp 是标准的海康威视3级子码流 video = av.open('rtsp://admin:[email protected]: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()

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3