使用OpenCV/FFmpeg访问DVR多通道RTSP流报401未授权错误
问题根因
你使用的2016年款HI3521A方案XVR的RTSP服务存在兼容BUG:
- 当RTSP URL携带查询参数(包含
=符号)时,设备端计算摘要认证的哈希值时,仅使用了?前的路径部分,未包含查询参数 - FFmpeg/OpenCV默认遵循RFC标准,计算哈希时会带入完整URL(含查询参数),导致两端哈希校验不通过,返回401未授权错误
- 无查询参数时,两端计算逻辑一致,所以通道1可以正常访问,甚至错误路径也会默认返回通道1的流
解决方案
方案1:单独传递RTSP认证参数(优先级最高,可优先测试)
不要把用户名密码写在RTSP URL中,通过FFmpeg独立参数传递认证信息:
FFmpeg命令示例
ffmpeg -loglevel debug -rtsp_user test -rtsp_pass test -i "rtsp://192.168.1.48/cam/realmonitor?channel=3&subtype=0" ./folder/output.m3u8
ffplay命令示例
ffplay -rtsp_user test -rtsp_pass test "rtsp://192.168.1.48/cam/realmonitor?channel=3&subtype=0"
OpenCV代码示例
public_link = 'rtsp://192.168.1.48/cam/realmonitor?channel=3&subtype=0' # 单独传入用户名密码参数 cap = cv2.VideoCapture(public_link, cv2.CAP_FFMPEG, [ cv2.CAP_PROP_RTSP_USER, 'test', cv2.CAP_PROP_RTSP_PASSWORD, 'test' ])
方案2:添加FFmpeg参数忽略查询参数的认证计算
如果方案1无效,可以添加FFmpeg参数,让认证计算时忽略URL的查询参数部分:
ffmpeg -loglevel debug -rtsp_flags ignore_digest_params -i "rtsp://test:test@192.168.1.48/cam/realmonitor?channel=3&subtype=0" ./folder/output.m3u8
对应OpenCV写法:
public_link = 'rtsp://test:test@192.168.1.48/cam/realmonitor?channel=3&subtype=0' cap = cv2.VideoCapture(public_link, cv2.CAP_FFMPEG, [ cv2.CAP_PROP_FFMPEG_OPTIONS, "rtsp_flags=ignore_digest_params" ])
方案3:更换非查询参数格式的RTSP路径
该款HI3521A方案的XVR通常兼容无参数的路径格式,可尝试以下路径格式(N替换为实际通道号):
- 主码流:
rtsp://test:test@192.168.1.48/cam/realmonitor/N/0 - 子码流:
rtsp://test:test@192.168.1.48/cam/realmonitor/N/1 - 雄迈固件兼容格式:
rtsp://test:test@192.168.1.48:554/stream=N
内容的提问来源于stack exchange,提问作者PxHoussem
相关产品推荐
相关产品推荐

