安装ffmpeg: windows电脑FFmpeg安装教程手把手详解
一 cv2.VideoCapture打开视频流
rtsp_url = 'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517%2418&substream=1'
cap = cv2.VideoCapture(rtsp_url)
while True:
ret, frame = cap.read()
if ret:
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == ord('s'):
print('img_shape:', frame.shape)
elif key == ord('q'):
break
else:
print('Failed to read frame')
break
cap.release()
cv2.destroyAllWindows()
二 ffmpeg打开视频流
安装ffmpeg文章来源:https://www.toymoban.com/news/detail-522207.html
pip install ffmpeg-python
运行ffmpeg:文章来源地址https://www.toymoban.com/news/detail-522207.html
import ffmpeg
import numpy as np
import cv2
camera = 'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003411%2417&substream=1'
probe = ffmpeg.probe(camera)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
width = int(video_stream['width'])
height = int(video_stream['height'])
out = (
ffmpeg
.input(camera, rtsp_transport='tcp')
.output('pipe:', format='rawvideo', pix_fmt='bgr24', loglevel="quiet", r=25)
.run_async(pipe_stdout=True)
)
cnt_empty = 0
while True:
in_bytes = out.stdout.read(height * width * 3)
if not in_bytes:
cnt_empty += 1
if cnt_empty > 10:
break
cnt_empty = 0
frame = np.frombuffer(in_bytes, dtype=np.uint8).reshape(height, width, 3)
# to process frame
cv2.imshow('test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
到了这里,关于cv2.VideoCapture 及 ffmpeg 打开视频流的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!