在一对一直播app源码开发中,
摄像头帧数25帧,
一张图片: 16+40ms 需要去掉一半帧数
1256+1216 一共860ms
25帧需要1s以内处理
可以隔帧丢帧,如果丢10帧,解10帧,画面会跳变,不连续
如果摄像头25帧,每张图片有40ms时间,算法时间超过这个, 就会产生很大延时。
如果摄像头15帧,每张图片有70ms时间,算法时间超过这个, 就会产生很大延时。
每隔一帧算法处理,也能减少延时。
可见,在一对一直播app源码开发中,摄像头设置帧率,是有效的。
一对一直播app源码开发,视频流延时解决方案的具体实现如下:
#!/usr/bin/env Python
# coding=utf-8
import os
import sys
import threading
import time
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
sys.path.append(current_dir+'/hik_dll/')
from ctypes import *
import time
import numpy as np
import cv2
import struct
import datetime
from numba import jit
cam_dict={}
index=0
def add_timestr(self, img):
time_str = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
color = (0, 0, 0)
if np.mean(img[700:780, 900:950]) > 128:
color = (0, 0, 0)
cv2.putText(img, time_str, (100, 70), cv2.FONT_HERSHEY_SIMPLEX, 1.4, color, 2)
return img
# @jit
def trans(data,size,height,width):
bbb = string_at(data,size)
nparr = np.fromstring(bbb, np.uint8)
r = nparr.reshape(height,width, 3)
return r
def str2char_p(str_v):
pStr = c_char_p( )
pStr.value = str_v
return pStr
def callb_stream(data,size,cam_no,height,width):
r = trans(data, size,height,width)
# print(1, counter)
global index
index+=1
# if index%2<1:
# return
# cv2.imwrite(filedir+str(index)+".jpg",r)
r=cv2.cvtColor(r, cv2.COLOR_RGB2BGR)
r=add_timestr(1,r)
counter = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
print(counter, index)
cv2.imshow(str(cam_no), r)
cv2.waitKeyEx(1)
# k = cv2.waitKey(400) & 0xFF
# if k == 13:
# cv2.waitKey()
# elif k == ord('m'):
# cv2.waitKey()
# elif k == 32: # 空格
# cv2.waitKey()
def play_video():
dll = CDLL(r"./hik_client.dll")
dll.testchar.restype = c_char_p
a = dll.testchar(11)
b = string_at(a, 20)
print(b)
CMPFUNC = CFUNCTYPE(c_void_p, c_void_p, c_int, c_int, c_int, c_int)
m_callback = CMPFUNC(callb_stream)
# filename=r"D:\project\CV_client\py_client\camera_pic\sh_wuding\video\201807111600.dat"
# filename=r"D:\project\CV_client\py_client\camera_pic\bj_ban01\video\201807111500.dat"
filename = r"D:\project\CV_client\py_client\camera_pic\bj_800\201807240000.dat"
# filename = r'rtsp://admin:sbdwl123@192.168.25.45:554/h264/ch1/main/av_stream'
filename = bytes(filename, encoding="utf8")
# filename = b"D:\\data\\test_video\\play_failed/201805032342.dat"
dll.play_url(str2char_p(filename), m_callback)
if __name__ == '__main__':
dll = CDLL(r"hik_client.dll")
dll.testchar.restype = c_char_p
a = dll.testchar(11)
b = string_at(a, 20)
print(b)
CMPFUNC = CFUNCTYPE(c_void_p, c_void_p, c_int, c_int, c_int, c_int)
m_callback = CMPFUNC(callb_stream)
# filename=r"D:\project\CV_client\py_client\camera_pic\sh_wuding\video\201807111600.dat"
# filename=r"D:\project\CV_client\py_client\camera_pic\bj_ban01\video\201807111500.dat"
filename=r"D:\project\CV_client\py_client\camera_pic\bj_800\201807240000.dat"
filename=r'rtsp://admin:sbdwl123@192.168.25.47:554/h264/ch1/sub/av_stream'
filename=bytes(filename, encoding="utf8")
# filename = b"D:\\data\\test_video\\play_failed/201805032342.dat"
dll.play_url(str2char_p(filename), m_callback)
以上就是一对一直播app源码开发,视频流延时解决方案的全部内容了。