安装ffmpeg: windows电脑FFmpeg安装教程手把手详解
一 cv2.VideoCapture打开视频流
rtsp_url = 'rtsp://10.0.58.253:9090/dss/monitor/param?camera &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
pip install ffmpeg-python
运行ffmpeg:
import ffmpeg
import numpy as np
import cv2
camera = 'rtsp://10.0.58.253:9090/dss/monitor/param?cameraid &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
三 实时放视屏流
import ffmpeg
import numpy as np
import cv2
import datetime
def main(source):
args = {
"rtsp_transport": "tcp",
"fflags": "nobuffer",
"flags": "low_delay"
}
# 获取视频流信息
probe = ffmpeg.probe(source)
cap_info = next(x for x in probe['streams'] if x['codec_type'] == 'video')
print("fps: {}".format(cap_info['r_frame_rate']))
width = cap_info['width'] # 获取视频流的宽度
height = cap_info['height'] # 获取视频流的高度
up, down = str(cap_info['r_frame_rate']).split('/')
fps = eval(up) / eval(down)
print("fps: {}".format(fps))
# 设置FFmpeg进程
process1 = (
ffmpeg
.input(source, **args)
.output('pipe:', format='rawvideo', pix_fmt='rgb24')
.overwrite_output()
.run_async(pipe_stdout=True)
)
while True:
in_bytes = process1.stdout.read(width * height * 3) # 读取图片
if not in_bytes:
break
# 转成ndarray
in_frame = (
np
.frombuffer(in_bytes, np.uint8)
.reshape([height, width, 3])
)
frame = cv2.cvtColor(in_frame, cv2.COLOR_RGB2BGR) # 转成BGR
# 实时显示视频
cv2.imshow('Video', frame)
if cv2.waitKey(1) == ord('q'):
break
# 关闭进程和窗口
process1.kill()
cv2.destroyAllWindows()
def time_str(fmt=None):
if fmt is None:
fmt = '%Y_%m_%d_%H_%M_%S'
return datetime.datetime.today().strftime(fmt)
if __name__ == "__main__":
# rtsp流需要换成自己的
user_name, user_pwd = "admin", "134"
ca_ip = "192.168.1.168"
channel = 2
alhua_rtsp = "rtsp://%s:%s@%s//Streaming/Channels/%d" % (user_name, user_pwd, ca_ip, channel)
main(alhua_rtsp)
四 测试视频流
4.1 直接拉流
本地安装 ffmpeg 后直接拉流保存图片,需要切换 实时的url:
ffmpeg -i "rtsp://10.0.58.253:9090/dss/monitor/param?cameraid=1003517&substream=1" -y -f image2 -r 1/1 img%03d.jpg
4.2 实时播放器 vlc
4.3 ffplay
使用 ffplay拉流