💡 如果想阅读最新的文章,或者有技术问题需要交流和沟通,可搜索并关注微信公众号“希望睿智”。
概述
图像编码,简单来说,就是把摄像头拍摄的画面转换成数字信号的过程。摄像头拍摄的原始画面非常大,但网络传输的带宽是有限的,磁盘存储的空间也是有限的,因此必须进行编码压缩。常见的视频编码格式有H264、H265、MJPEG等,它们就像压缩饼干,能在保证画面质量的同时,大幅度减少视频数据的大小,这对于节省存储空间和提高网络传输效率至关重要。
SOAP报文
在Onvif中,GetVideoEncoderConfiguration用于获取设备的图像编码配置,包括:编码格式、分辨率、帧率、码率等。GetVideoEncoderConfiguration命令的SOAP请求比较简单,可参考下面的示例报文。其中,<tds:ConfigurationToken>标签内的内容为配置令牌,通常需要从GetProfiles等方法中预先获取。
<soapenv:Envelope
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<tds:GetVideoEncoderConfiguration>
<tds:ConfigurationToken>[Configuration_Token]</tds:ConfigurationToken>
</tds:GetVideoEncoderConfiguration>
</soapenv:Body>
</soapenv:Envelope>
设备接收到请求命令后,会返回给客户端SOAP响应。SOAP响应中包含编码格式、分辨率、帧率、码率等信息,可参考下面的示例报文。
<soapenv:Envelope
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soapenv:Body>
<tds:GetVideoEncoderConfigurationResponse>
<tds:Configuration>
<tt:ProfileToken>Profile_1</tt:ProfileToken>
<tt:Name>MainStream</tt:Name>
<tt:UseCount>1</tt:UseCount>
<tt:Encoding>H264</tt:Encoding>
<tt:Resolution>
<tt:Width>1920</tt:Width>
<tt:Height>1080</tt:Height>
</tt:Resolution>
<tt:Quality>80</tt:Quality>
<tt:FrameRateLimit>
<tt:Min>1</tt:Min>
<tt:Max>30</tt:Max>
<tt:Interval>1</tt:Interval>
<tt:Mode>Variable</tt:Mode>
</tt:FrameRateLimit>
<tt:BitrateLimit>
<tt:Min>100000</tt:Min>
<tt:Max>4000000</tt:Max>
</tt:BitrateLimit>
<!-- 更多配置细节 -->
</tds:Configuration>
</tds:GetVideoEncoderConfigurationResponse>
</soapenv:Body>
</soapenv:Envelope>
代码实现
下面,我们使用Python来模拟获取图像编码配置的整个过程。
首先,导入必要的模块,然后建立到Onvif设备的服务端点的连接。下面示例代码中摄像机的IP地址、用户名和密码,需要替换成用户实际使用的信息。
from zeep import Client
from zeep.transports import Transport
from requests.auth import HTTPDigestAuth
camera_ip = '192.168.50.188'
username = 'admin'
password = 'password'
# 创建客户端连接
transport = Transport(timeout = 10, operation_timeout = 10)
client = Client(f'http://{camera_ip}/onvif/device_service?wsdl',
transport = transport,
wsse = HTTPDigestAuth(username, password))
其次,GetVideoEncoderConfiguration属于Media服务的一部分,故我们需要创建一个针对Media服务的客户端。
media_service =
client.create_service('{http://www.onvif.org/ver10/media/wsdl}MediaBinding',
f'http://{camera_ip}:80/onvif/Media')
最后,我们便可以调用GetVideoEncoderConfiguration方法来获取视频编码配置了。通常情况下,需要先通过GetProfiles获取配置的令牌。
# 替换为实际的配置令牌
config_token = 'real_configuration_token'
# 获取视频编码配置
response = media_service.GetVideoEncoderConfiguration(ConfigurationToken = config_token)
print(response)