我正在尝试在桌面上流式传输 SONY FDR-X1000V 摄像机的实时视图。我使用python调用API并下载包并使用opencv解码jpeg。当我运行它时,它几乎无法每秒捕捉一帧。后来我发现一个 jpeg 的有效载荷大小可以是 8MB。但是,FDR-X1000V 不支持更改实时取景大小。但是当我在 iPhone 上使用该应用程序进行实时取景时,它可以流畅地播放。所以这是我的问题: 1. 一个 jpeg 有效载荷可以大到 8MB 是否正常?2. 如果是这样,我怎样才能流畅地直播实时取景?
这是我的代码:
try:
result = api.do('startLiveview')
url = result['result'][0]
except KeyError:
print result
f = urllib2.urlopen(url)
buff = ''
chunk_size = 32768
for i in xrange(3000):
if len(buff) < chunk_size:
time_s = time.time()
buff = buff + f.read(chunk_size)
print "Download Speed %f KB/s"%(chunk_size/1000/(time.time() - time_s))
time_s = time.time()
start_code = ''.join(buff).find('$5hy')
# print "LCS time cost", time.time() - time_s
if start_code < 0:
buff = buff[-12:]
print "skip", len(buff)-12
elif start_code < 8:
buff = buff[8:]
else:
if start_code > len(buff) - 129:
buff = buff + f.read(chunk_size)
payload_type = ord(buff[start_code-7])
payload_size, = struct.unpack('<I', buff[start_code+4:start_code+8].ljust(4,'\0'))
padding_size = ord(buff[start_code+8])
print "Type:%d\tPayload:%d\tPadding:%d\t"%(payload_type,payload_size,padding_size)
buff = buff[start_code+128:]
if payload_type == 1:
if payload_size + padding_size > len(buff):
time_s = time.time()
download_size = payload_size+padding_size-len(buff)
buff = buff + f.read(download_size)
print "Download Speed %f KB/s"%(download_size/1000/(time.time() - time_s))
img_data = buff[:payload_size]
buff = buff[payload_size:]
time_s = time.time()
d = np.asarray(bytearray(img_data), dtype='uint8')
img = cv2.imdecode(d,cv2.IMREAD_COLOR)
cv2.imshow('postview',img)
cv2.waitKey(30)
# print "Decode time cost", time.time() - time_s
一些输出:
Type:1 Payload:8410624 Padding:0
Download Speed 679.626326 KB/s