Commit 783a9a57 authored by liyuanhong's avatar liyuanhong

第一次提交

parents
Pipeline #342 failed with stages
File added
File added
#coding:utf-8
import binascii
import socket
from ctypes import string_at
import time
import cv2
class Protocal_1078:
def __init__(self):
self.frameHeader1078 = "30316364" # JT/T 1078 头 固定为0x30 0x31 0x63 0x64,ASCII码为01cd
self.V = 2 << 6 # V 固定为2
self.P = 0 << 5 # P 固定为0
self.X = 0 << 4 # X RTP头是否需要扩展位,固定为0
self.CC = 1 # CC 固定为1
self.M = 1 << 7 # M 标志位,确定是否完整数据帧的边界,因为数据体的最大长度是950字节,而一个视频I帧通道要远远超过950字节,所以视频的一个帧通常会分包
self.PT = 98 # PT 98:表示H264 99:表示H265
self.sn = 0 # 包序号 初始为0,每发送一个RTP数据包,序列号加1
self.sim = "013146201117" # 终端设备SIM卡号
self.logicC = 1 # 逻辑通道
self.dataType = 0 << 4 # 数据类型 , 0000:数据I祯,0001:视频P帧,0010:视频B帧,0011:音频帧,0100:透传数据
self.pkgTag = 1 # 分包处理 ,0000:原子包,不可拆分等,0001:分包处理时的第一个包,0010:分包处理时的最后一个包,0011:分包处理时的中间包
self.time = 33 # 时间戳
self.lastKeyTime = 33 # Last I Frame Interval 该祯与上一个关键祯之间的时间间隔,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lastTime = 33 # Last Frame Interval 该祯与上一个祯之间的时间时间,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lenS = 950 # 数据长度
self.dataBody = "" # 数据体,要发送的帧数据
# self.host = "10.100.11.125"
self.host = "10.16.15.85"
self.port = 1078
self.BUF_SIZE = 2048
def setDataBody(self,data):
self.dataBody = data
def setM(self,data):
self.M = data << 7
def setSim(self,data):
while len(data) < 12:
data = "0" + data
self.sim = data
def setSn(self,data):
self.sn = data
def setTime(self,data):
self.time = data
def setLastKeyTime(self,data):
self.lastKeyTime = data
def setLastTime(self,data):
self.lastTime = data
#####################################################
# 从帧数据中获取帧类型
#####################################################
def getFrameType(self):
# 0x67 (0 11 00111) SPS 非常重要 type = 7
# 0x68 (0 11 01000) PPS 非常重要 type = 8
# 0x65 (0 11 00101) IDR帧 关键帧 非常重要 type = 5
# 0x61 (0 11 00001) I帧 重要 type=1 非IDR的I帧 不大常见
# 0x41 (0 10 00001) P帧 重要 type = 1
# 0x01 (0 00 00001) B帧 不重要 type = 1
# 0x06 (0 00 00110) SEI 不重要 type = 6
frameTypeHex = self.dataBody[8:10]
return frameTypeHex
#####################################################
# 数字转换为16进制字符串,通过传入字节数可自动补0
# 传入数据格式所占字节数
#####################################################
def int2hexStringByBytes(self, num,bytescount=1):
hexStr = hex(num)[2:]
while len(hexStr) < (bytescount * 2):
hexStr = "0" + hexStr
return hexStr
#####################################################
# 根据长度分割字符串
#####################################################
def splitStrByLen(self,strs,num):
result = []
if num > len(strs):
result.append(strs)
else:
result.append(strs[:num])
strs = strs[num:]
while len(strs) > num:
result.append(strs[:num])
strs = strs[num:]
if len(strs) > 0:
result.append(strs)
return result
#####################################################
# 生成单个数据包
#####################################################
def genPkg(self,data):
msg = ""
msg = self.frameHeader1078
msg = msg + self.int2hexStringByBytes(self.V + self.P + self.X + self.CC)
msg = msg + self.int2hexStringByBytes(self.M + self.PT)
msg = msg + self.int2hexStringByBytes(self.sn,2)
msg = msg + self.sim
msg = msg + self.int2hexStringByBytes(self.logicC)
msg = msg + self.int2hexStringByBytes(self.dataType + self.pkgTag)
msg = msg + self.int2hexStringByBytes(self.time,8)
msg = msg + self.int2hexStringByBytes(self.lastKeyTime,2)
msg = msg + self.int2hexStringByBytes(self.lastTime,2)
msg = msg + self.int2hexStringByBytes(int(len(data) / 2),2)
msg = msg + data
return msg
#####################################################
# 生成单个数据包
#####################################################
def genAudioPkg(self,data):
msg = ""
msg = self.frameHeader1078
msg = msg + self.int2hexStringByBytes(self.V + self.P + self.X + self.CC)
self.PT = 19
msg = msg + self.int2hexStringByBytes(self.M + self.PT)
msg = msg + self.int2hexStringByBytes(self.sn,2)
msg = msg + self.sim
msg = msg + self.int2hexStringByBytes(self.logicC)
msg = msg + self.int2hexStringByBytes(self.dataType + self.pkgTag)
msg = msg + self.int2hexStringByBytes(self.time,8)
msg = msg + self.int2hexStringByBytes(int(len(data) / 2),2)
msg = msg + data
return msg
#####################################################
# 根据帧数据,生成多个数据包(视频)
#####################################################
def genPkgsByFrame(self):
f_type = self.getFrameType()
if f_type == "65" or f_type == "61":
self.dataType = 0 << 4 # 0000
elif f_type == "41":
self.dataType = 1 << 4 # 0001
elif f_type == "01":
self.dataType = 2 << 4 # 0010
else:
self.dataType = 2 << 4 # 0010
datas = self.splitStrByLen(self.dataBody,1900)
pkgs = []
for i in range(0,len(datas)):
if len(datas) == 1:
self.pkgTag = 0 # 原子包 0000
else:
if i == 0:
self.pkgTag = 1 # 第一个包 0001
elif i < (len(datas) - 1):
self.pkgTag = 3 # 中间包 0011
else:
self.pkgTag = 2 # 最后一个包 0010
pkg = self.genPkg(datas[i])
pkgs.append(pkg)
if self.sn >= 65535:
self.sn = 0
self.sn = self.sn + 1
return pkgs
#####################################################
# 根据帧数据,生成多个数据包(音频频)
#####################################################
def genAudioPkgsByFrame(self):
self.dataType = 3 << 4
datas = self.splitStrByLen(self.dataBody,1900)
pkgs = []
for i in range(0,len(datas)):
if len(datas) == 1:
self.pkgTag = 0 # 原子包 0000
else:
if i == 0:
self.pkgTag = 1 # 第一个包 0001
elif i < (len(datas) - 1):
self.pkgTag = 3 # 中间包 0011
else:
self.pkgTag = 2 # 最后一个包 0010
pkg = self.genAudioPkg(datas[i])
pkgs.append(pkg)
if self.sn == 65535:
self.sn = 0
self.sn = self.sn + 1
return pkgs
if __name__ == "__main__":
pro = Protocal_1078()
# print(Protocal_1078().genPkg())
# Protocal_1078().readvideoFile()
# pro.getFrameType()
#coding:utf-8
import binascii
import socket
from ctypes import string_at
import time
import cv2
class Protocal_1078:
def __init__(self):
self.frameHeader1078 = "30316364" # JT/T 1078 头 固定为0x30 0x31 0x63 0x64,ASCII码为01cd
self.V = 2 << 6 # V 固定为2
self.P = 0 << 5 # P 固定为0
self.X = 0 << 4 # X RTP头是否需要扩展位,固定为0
self.CC = 1 # CC 固定为1
self.M = 1 << 7 # M 标志位,确定是否完整数据帧的边界,因为数据体的最大长度是950字节,而一个视频I帧通道要远远超过950字节,所以视频的一个帧通常会分包
self.PT = 98 # PT 98:表示H264 99:表示H265
self.sn = 0 # 包序号 初始为0,每发送一个RTP数据包,序列号加1
self.sim = "013146201117" # 终端设备SIM卡号
self.logicC = 1 # 逻辑通道
self.dataType = 0 << 4 # 数据类型 , 0000:数据I祯,0001:视频P帧,0010:视频B帧,0011:音频帧,0100:透传数据
self.pkgTag = 1 # 分包处理 ,0000:原子包,不可拆分等,0001:分包处理时的第一个包,0010:分包处理时的最后一个包,0011:分包处理时的中间包
self.time = 33 # 时间戳
self.lastKeyTime = 33 # Last I Frame Interval 该祯与上一个关键祯之间的时间间隔,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lastTime = 33 # Last Frame Interval 该祯与上一个祯之间的时间时间,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lenS = 950 # 数据长度
self.dataBody = "" # 数据体,要发送的帧数据
# self.host = "10.100.11.125"
self.host = "10.16.15.85"
self.port = 1078
self.BUF_SIZE = 2048
def setDataBody(self,data):
self.dataBody = data
def setM(self,data):
self.M = data << 7
def setSim(self,data):
while len(data) < 12:
data = "0" + data
self.sim = data
def setLogcC(self,data):
self.logicC = data
def setSn(self,data):
self.sn = data
def setTime(self,data):
self.time = data
def setLastKeyTime(self,data):
self.lastKeyTime = data
def setLastTime(self,data):
self.lastTime = data
#####################################################
# 从帧数据中获取帧类型
#####################################################
def getFrameType(self):
# 0x67 (0 11 00111) SPS 非常重要 type = 7
# 0x68 (0 11 01000) PPS 非常重要 type = 8
# 0x65 (0 11 00101) IDR帧 关键帧 非常重要 type = 5
# 0x61 (0 11 00001) I帧 重要 type=1 非IDR的I帧 不大常见
# 0x41 (0 10 00001) P帧 重要 type = 1
# 0x01 (0 00 00001) B帧 不重要 type = 1
# 0x06 (0 00 00110) SEI 不重要 type = 6
frameTypeHex = self.dataBody[8:10]
return frameTypeHex
#####################################################
# 数字转换为16进制字符串,通过传入字节数可自动补0
# 传入数据格式所占字节数
#####################################################
def int2hexStringByBytes(self, num,bytescount=1):
hexStr = hex(num)[2:]
while len(hexStr) < (bytescount * 2):
hexStr = "0" + hexStr
return hexStr
#####################################################
# 根据长度分割字符串
#####################################################
def splitStrByLen(self,strs,num):
result = []
if num > len(strs):
result.append(strs)
else:
result.append(strs[:num])
strs = strs[num:]
while len(strs) > num:
result.append(strs[:num])
strs = strs[num:]
if len(strs) > 0:
result.append(strs)
return result
#####################################################
# 生成单个数据包
#####################################################
def genPkg(self,data):
msg = ""
msg = self.frameHeader1078
msg = msg + self.int2hexStringByBytes(self.V + self.P + self.X + self.CC)
msg = msg + self.int2hexStringByBytes(self.M + self.PT)
msg = msg + self.int2hexStringByBytes(self.sn,2)
msg = msg + self.sim
msg = msg + self.int2hexStringByBytes(self.logicC)
msg = msg + self.int2hexStringByBytes(self.dataType + self.pkgTag)
msg = msg + self.int2hexStringByBytes(self.time,8)
msg = msg + self.int2hexStringByBytes(self.lastKeyTime,2)
msg = msg + self.int2hexStringByBytes(self.lastTime,2)
msg = msg + self.int2hexStringByBytes(int(len(data) / 2),2)
msg = msg + data
return msg
#####################################################
# 生成单个数据包
#####################################################
def genAudioPkg(self,data):
msg = ""
msg = self.frameHeader1078
msg = msg + self.int2hexStringByBytes(self.V + self.P + self.X + self.CC)
self.PT = 19
msg = msg + self.int2hexStringByBytes(self.M + self.PT)
msg = msg + self.int2hexStringByBytes(self.sn,2)
msg = msg + self.sim
msg = msg + self.int2hexStringByBytes(self.logicC)
msg = msg + self.int2hexStringByBytes(self.dataType + self.pkgTag)
msg = msg + self.int2hexStringByBytes(self.time,8)
msg = msg + self.int2hexStringByBytes(int(len(data) / 2),2)
msg = msg + data
return msg
#####################################################
# 根据帧数据,生成多个数据包(视频)
#####################################################
def genPkgsByFrame(self):
f_type = self.getFrameType()
if f_type == "65" or f_type == "61":
self.dataType = 0 << 4 # 0000
elif f_type == "41":
self.dataType = 1 << 4 # 0001
elif f_type == "01":
self.dataType = 2 << 4 # 0010
else:
self.dataType = 2 << 4 # 0010
datas = self.splitStrByLen(self.dataBody,1900)
pkgs = []
for i in range(0,len(datas)):
if len(datas) == 1:
self.pkgTag = 0 # 原子包 0000
else:
if i == 0:
self.pkgTag = 1 # 第一个包 0001
elif i < (len(datas) - 1):
self.pkgTag = 3 # 中间包 0011
else:
self.pkgTag = 2 # 最后一个包 0010
pkg = self.genPkg(datas[i])
pkgs.append(pkg)
if self.sn >= 65535:
self.sn = 0
self.sn = self.sn + 1
return pkgs
#####################################################
# 根据帧数据,生成多个数据包(音频频)
#####################################################
def genAudioPkgsByFrame(self):
self.dataType = 3 << 4
datas = self.splitStrByLen(self.dataBody,1900)
pkgs = []
for i in range(0,len(datas)):
if len(datas) == 1:
self.pkgTag = 0 # 原子包 0000
else:
if i == 0:
self.pkgTag = 1 # 第一个包 0001
elif i < (len(datas) - 1):
self.pkgTag = 3 # 中间包 0011
else:
self.pkgTag = 2 # 最后一个包 0010
pkg = self.genAudioPkg(datas[i])
pkgs.append(pkg)
if self.sn == 65535:
self.sn = 0
self.sn = self.sn + 1
return pkgs
if __name__ == "__main__":
pro = Protocal_1078()
# print(Protocal_1078().genPkg())
# Protocal_1078().readvideoFile()
# pro.getFrameType()
#coding: utf-8
import binascii
import socket
import time
# http://10.100.11.125:8085/live?port=1985&app=vandyo&stream=013146201117-1
# http://10.16.15.85:8085/live?port=1985&app=vandyo&stream=013146201117-3
# http://localhost:3333/video/013146201117-3
from lib.protocal.Protocal_1078 import Protocal_1078
class StreamH264():
def __init__(self):
self.readSize = 10240 # 每次读取文件的字节大小 (视频)
self.readSizeAudio = 1024 # 每次读取文件的字节大小 (音频频)
self.filePos = 0.001 # 文件的读取位置
self.mobile = "013146201117" # 手机号
self.channel = 1 # 频道号
# self.host = "10.100.11.125"
self.host = "10.16.12.249"
# self.host = "10.100.12.3"
# self.host = "localhost"
self.port = 1078
self.client = None
self.BUF_SIZE = 2048
self.sendDur = 0 # 消息发送间隔
self.AVChangeBySecond = 2 # 每秒音视频切换数
self.pos = 1 # 当前的帧数(视频)
self.posAudio = 1 # 当前的帧数(音频)
self.frameCount = 1 # 对发送的帧计数(视频),如果发送的帧刚搞到达帧率发送的帧,则需要加上补齐1秒的值 (例如1秒发送30帧,则发送了30帧后需要补齐 1毫秒,使时间刚好到1秒),对应repairTime
self.frameCountAudio = 1 # 对发送的帧计数(视频),用来补齐1秒的时间(同上)
self.videoCount = 0 # 用来使音视频同步的变量
self.audioCount = 0 # 用来使音视频同步的变量
self.repairTime = 1 # 需要补齐到1秒的时间,对应frameCount
self.time = 33 # 时间戳,视频 (帧的时间,每发送一帧自动增加)
self.timeAudio = 21 # 时间戳,音频 (帧的时间,每发送一帧自动增加)
self.singleFrameTime = 33 # 视频单帧的时间(帧率为30 fps,单帧的时间为33 毫秒)
self.singleFrameTimeAudio = 21 # 音频的单帧时间 (毫秒)
self.lastKeyTime = 33 # Last I Frame Interval 该祯与上一个关键祯之间的时间间隔,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lastTime = 33 # Last Frame Interval 该祯与上一个祯之间的时间时间,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.videoPath = "../h264/bbb3.h264" # 要发送的视频地址
self.audioPath = "../aac/bbb3.aac" # 要发送的音频地址
self.sendFrameType = 0 # 要发送的帧类型, 0:视频帧 1:音频帧 (针对视频和音频他同时发送的方法)
self.aacFront_28 = "" # aac 音频前28个固定bit 的值
self.aacIsFront_28 = 0 # aac 是否已经取出了前28个字节
def setSendDur(self,data):
self.sendDur = data
def setAVChangeBySecond(self,data):
self.AVChangeBySecond = data
def setVideoPath(self,data):
self.videoPath = data
def setAudioPath(self,data):
self.audioPath = data
def setMobile(self,data):
self.mobile = data
def setChannel(self,data):
self.channel = data
def setHost(self,data):
self.host = data
def setPort(self,data):
self.port = data
####################################################
# 设置视频帧率
####################################################
def setFPSVideo(self,data = 0):
if(data == 0):
pass
else:
self.time = int(1000 / data)
self.singleFrameTime = int(1000 / data)
####################################################
# 设置音频帧率
####################################################
def setFPSAudio(self,data = 0):
if (data == 0):
pass
else:
self.timeAudio = int(1000 / data)
self.singleFrameTimeAudio = int(1000 / data)
####################################################
# socket 连接
####################################################
def connectServer(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳
self.client.connect((self.host, self.port))
####################################################
# 同时发送视频和音频
####################################################
def sendAVStream(self):
protocal_1078 = Protocal_1078()
protocal_1078.setSim(self.mobile)
protocal_1078.setLogcC(self.channel)
# 打开视频文件
fv2 = open(self.videoPath, 'rb')
con = fv2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr_2(data)
for fra in frames:
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
# print("发送视频消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
frames = []
# 打开音频文件
fa2 = open(self.audioPath, 'rb')
conAudio = fa2.read(self.readSizeAudio)
dataAudio = conAudio.hex() # 16进制字节码转字符串
framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio)
for fraAudio in framesAudio:
protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
self.doAudioFrameCount(protocal_1078)
pkgsAudio = protocal_1078.genAudioPkgsByFrame()
for msgAudio in pkgsAudio:
# print("发送音频消息:" + msgAudio)
self.client.send(binascii.a2b_hex(msgAudio))
time.sleep(self.sendDur)
framesAudio = []
while con or conAudio:
if self.sendFrameType == 0:
videoTempFrame = []
isVloop = 0
for fra in frames:
if self.videoCount >= int(self.singleFrameTime / self.AVChangeBySecond):
self.sendFrameType = 1
self.videoCount = 0
if self.videoCount == 0:
videoTempFrame.append(fra)
isVloop = 1
continue
else:
if isVloop == 0:
if len(fra) % 2 != 0: # 处理有问题的帧数据
print("出现了视频问题帧,并自动修复...")
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
# print("发送视频消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
else:
videoTempFrame.append(fra)
frames = videoTempFrame
if len(frames) != 0:
continue
con = fv2.read(self.readSize)
data = con.hex()
if con:
frames, lastData = self.getFramesFromStr_2(data, lastData)
for fra in frames:
if self.videoCount >= int(self.singleFrameTime / self.AVChangeBySecond):
self.sendFrameType = 1
self.videoCount = 0
if self.videoCount == 0:
videoTempFrame.append(fra)
else:
if len(fra) % 2 != 0: # 处理有问题的帧数据
print("出现了视频问题帧,并自动修复...")
print(fra)
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
# print("发送视频消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
frames = videoTempFrame
else:
self.sendFrameType = 1
elif self.sendFrameType == 1:
audioTempFrame = []
isALoop = 0
for fraAudio in framesAudio:
if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
self.sendFrameType = 0
self.audioCount = 0
if self.audioCount == 0:
audioTempFrame.append(fraAudio)
isALoop = 1
continue
else:
if isALoop == 0:
if len(self.aacFront_28 + fraAudio) % 2 != 0:
print("出现了问题帧,并自动修复...")
print(fraAudio)
fraAudio = fraAudio[:len(fraAudio) - 1]
protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
self.doAudioFrameCount(protocal_1078)
pkgsAudio = protocal_1078.genAudioPkgsByFrame()
for msgAudio in pkgsAudio:
# print("发送音频消息:" + msgAudio)
self.client.send(binascii.a2b_hex(msgAudio))
time.sleep(self.sendDur)
else:
audioTempFrame.append(fraAudio)
framesAudio = audioTempFrame
if len(framesAudio) != 0:
continue
conAudio = fa2.read(self.readSizeAudio)
dataAudio = conAudio.hex()
if conAudio:
framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio, lastDataAudio)
for fraAudio in framesAudio:
if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
self.sendFrameType = 0
self.audioCount = 0
if self.audioCount == 0:
audioTempFrame.append(fraAudio)
else:
if len(self.aacFront_28 + fraAudio) % 2 != 0:
print("出现了问题帧,并自动修复...")
print(fraAudio)
fraAudio = fraAudio[:len(fraAudio) - 1]
protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
self.doAudioFrameCount(protocal_1078)
pkgsAudio = protocal_1078.genAudioPkgsByFrame()
for msgAudio in pkgsAudio:
# print("发送音频消息:" + msgAudio)
self.client.send(binascii.a2b_hex(msgAudio))
time.sleep(self.sendDur)
framesAudio = audioTempFrame
else:
self.sendFrameType = 0
fv2.close()
fa2.close()
self.client.close()
# print("----------视频" + str(self.pos))
# print("----------音频" + str(self.posAudio))
# print("---------------------------------")
####################################################
# 读取h264文件并发送
####################################################
def readStreamFromFileAndSend(self,fileName):
protocal_1078 = Protocal_1078()
protocal_1078.setSim(self.mobile)
protocal_1078.setLogcC(self.channel)
with open(fileName, 'rb') as f2:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr_2(data)
for fra in frames:
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr_2(data, lastData)
for fra in frames:
if len(fra) % 2 != 0: # 处理有问题的帧数据
print("出现了问题帧,并自动修复...")
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
print(self.pos)
####################################################
# 读取aac文件并发送
####################################################
def readAacStreamFromFileAndSend(self,fileName):
protocal_1078 = Protocal_1078()
protocal_1078.setSim(self.mobile)
protocal_1078.setLogcC(self.channel)
with open(fileName, 'rb') as f2:
con = f2.read(self.readSizeAudio)
data = con.hex() # 16进制字节码转字符串
frames,lastData = self.getAudioFramesFromStr_2(data)
for fra in frames:
protocal_1078.setDataBody(self.aacFront_28 + fra)
self.doAudioFrameCount(protocal_1078)
pkgs = protocal_1078.genAudioPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getAudioFramesFromStr_2(data, lastData)
for fra in frames:
if len(self.aacFront_28 + fra) % 2 != 0:
print("出现了问题帧,并自动修复...")
print(fra)
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody(self.aacFront_28 + fra)
self.doAudioFrameCount(protocal_1078)
pkgs = protocal_1078.genAudioPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
print(self.posAudio)
####################################################
# 读取h264文件(用来测试)
####################################################
def readStreamFromFile(self,fileName):
protocal_1078 = Protocal_1078()
with open(fileName, 'rb') as f2:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames,lastData = self.getFramesFromStr(data)
for fra in frames:
protocal_1078.setDataBody(fra)
frameType = protocal_1078.getFrameType()
if frameType == "67":
print(frameType)
self.pos = self.pos + 1
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr(data, lastData)
for fra in frames:
protocal_1078.setDataBody(fra)
frameType = protocal_1078.getFrameType()
if frameType == "67":
print(frameType)
self.pos = self.pos + 1
# print(self.pos)
print(self.pos)
####################################################
# 对帧时间进行计数(视频帧)
###################################################
def doFrameCount(self,frameType,obj_1078):
if frameType == "65" or frameType == "61": # 68 之后接00000001 便是关键帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = 0
self.frameCount = self.frameCount + 1
self.pos = self.pos + 1
# timeStamp = time.time()
# print(timeStamp)
# timeArray = time.localtime(int(timeStamp - 8 * 3600))
# dateTimeM = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
# print(dateTimeM)
elif frameType == "41": # P帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = self.lastKeyTime + self.singleFrameTime
self.frameCount = self.frameCount + 1
self.pos = self.pos + 1
elif frameType == "01": # B帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = self.lastKeyTime + self.singleFrameTime
self.frameCount = self.frameCount + 1
self.pos = self.pos + 1
if self.frameCount >= 30:
self.time = self.time + 1
self.frameCount = 1
self.videoCount = self.videoCount + 1
obj_1078.setTime(self.time)
obj_1078.setLastKeyTime(self.lastKeyTime)
obj_1078.setLastTime(self.lastTime)
####################################################
# 对帧时间进行计数(音频帧)
###################################################
def doAudioFrameCount(self,obj_1078):
self.timeAudio = self.timeAudio + self.singleFrameTimeAudio
if self.frameCountAudio >= int(1000 / self.singleFrameTimeAudio):
self.frameCountAudio = 0
self.timeAudio = self.timeAudio + 1000 % self.singleFrameTimeAudio
self.frameCountAudio = self.frameCountAudio + 1
self.audioCount = self.audioCount + 1
self.posAudio = self.posAudio + 1
obj_1078.setTime(self.timeAudio)
####################################################
# 读取h264文件(测试程序)
####################################################
def readTest(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(204800)
data = con.hex() # 16进制字节码转字符串
data_H = data.split("00000001")
for i in range(1,len(data_H)):
print("00000001" + data_H[i])
####################################################
# 读取h264文件(测试程序)
####################################################
def readTest2(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(200)
data = con.hex()
print(data)
con = f2.read(207)
data = con.hex()
print(data)
con = f2.read(212)
data = con.hex()
print(data)
####################################################
# 读取h264文件(测试程序),从指定位置开始读取
####################################################
def readTest3(self,fileName):
with open(fileName, 'rb') as f2:
f2.seek(4,0)
con = f2.read(1)
data = con.hex()
print(data)
#####################################################
# 从帧数据中获取帧类型
#####################################################
def getFrameType(self,frame):
# 0x67 (0 11 00111) SPS 非常重要 type = 7
# 0x68 (0 11 01000) PPS 非常重要 type = 8
# 0x65 (0 11 00101) IDR帧 关键帧 非常重要 type = 5
# 0x61 (0 11 00001) I帧 重要 type=1 非IDR的I帧 不大常见
# 0x41 (0 10 00001) P帧 重要 type = 1
# 0x01 (0 00 00001) B帧 不重要 type = 1
# 0x06 (0 00 00110) SEI 不重要 type = 6
frameTypeHex = frame[8:10]
return frameTypeHex
####################################################
# 16进制字符串分割为个多个帧(通过split方式分割)
####################################################
def getFramesFromStr(self,data,lastDataParam = ""):
data_H = data.split("00000001")
frames = []
if(len(data_H) == 1): #如果字符串中不包含 000001(说明内容都属于一帧的中间位置)
lastData = lastDataParam + data
return frames,lastData
else:
for i in range(0, len(data_H)):
if i != (len(data_H) - 1):
if data_H[i] != "":
if lastDataParam == "": #文件的开始
frame = "" + data_H[i]
frames.append(frame)
else:
if i == 0:
frame = "" + lastDataParam + data_H[i]
frames.append(frame)
else:
frame = "" + data_H[i]
frames.append(frame)
else:
if lastDataParam == "": #文件的开始
pass
else:
frame = "" + data_H[i] + lastDataParam
frames.append(frame)
else:
lastData = data_H[i]
tempFrames = []
for tmp in frames:
if self.getFrameType("00000001" + tmp) == "68" or self.getFrameType("00000001" + tmp) == "09":
fra = tmp.split("000001")
for fra2 in fra:
tempFrames.append(fra2)
else:
tempFrames.append(tmp)
frames = tempFrames
return frames,lastData
####################################################
# 16进制字符串分割为个多个帧(一个字节一个字节的读取)
####################################################
def getFramesFromStr_2(self,data,lastDataParam = ""):
dataStr = data
frame = ""
frames = []
frameSlice = dataStr[:2]
frame = lastDataParam + frame + frameSlice
dataStr = dataStr[2:]
while len(dataStr) > 0:
frameSlice = dataStr[:2]
dataStr = dataStr[2:]
if "00" == frameSlice:
frameTmp = frameSlice
frameTmp2 = dataStr[:2]
dataStr = dataStr[2:]
while frameTmp2 == "00":
frameTmp = frameTmp + frameTmp2
frameTmp2 = dataStr[:2]
dataStr = dataStr[2:]
frameTmp = frameTmp + frameTmp2
if frameTmp == "00000001":
frames.append(frame)
frame = ""
else:
frame = frame + frameTmp
else:
frame = frame + frameSlice
lastData = frame
tempFrames = []
for tmp in frames:
if self.getFrameType("00000001" + tmp) == "68" or self.getFrameType("00000001" + tmp) == "09":
fra = tmp.split("000001")
for fra2 in fra:
tempFrames.append(fra2)
else:
tempFrames.append(tmp)
frames = tempFrames
return frames,lastData
####################################################
# 获取音频帧(通过前面28个字节来截取)
####################################################
def getAudioFramesFromStr_2(self,data,lastDataParam = ""):
if self.aacIsFront_28 == 0:
self. aacFront_28 = data[:7]
self.aacIsFront_28 = 1
data_H = data.split(self.aacFront_28)
frames = []
if(len(data_H) == 1): #如果字符串中不包含 000001(说明内容都属于一帧的中间位置)
lastData = lastDataParam + data
return frames,lastData
else:
for i in range(0, len(data_H)):
if i != (len(data_H) - 1):
if data_H[i] != "":
if lastDataParam == "": #文件的开始
frame = "" + data_H[i]
frames.append(frame)
else:
if i == 0:
frame = "" + lastDataParam + data_H[i]
frames.append(frame)
else:
frame = "" + data_H[i]
frames.append(frame)
else:
if lastDataParam == "": #文件的开始
pass
else:
frame = "" + data_H[i] + lastDataParam
frames.append(frame)
else:
lastData = data_H[i]
return frames,lastData
if __name__ == "__main__":
obj = StreamH264()
obj.connectServer()
obj.setFPSVideo(30)
obj.setFPSAudio(47)
obj.setSendDur(0.001)
obj.setAVChangeBySecond(2)
obj.setVideoPath("../../h264/bbb3.h264")
obj.setAudioPath("../../aac/bbb3.aac")
# obj.readStreamFromFileAndSend("../../h264/bbb.h264") # 发送视频
# obj.readAacStreamFromFileAndSend("../../aac/bbb3.aac") # 发送音频
obj.sendAVStream()
# StreamH264().readTest("../../h264/aaa.h264")
# StreamH264().readTest2("../../h264/bbb3.h264")
# StreamH264().readTest2("../aac/aaa.aac")
# StreamH264().readStreamFromFile("../h264/aaa2.264")
#coding: utf-8
class StreamH264Flv():
def __init__(self):
self.isFileHead = 1 # 读取文件的时候,是否是文件的开头 0:不是 1:是
self.readSize = 10240 # 读取文件的大小
def readFlvAndSend(self,fileName):
with open(fileName, 'rb') as fi:
con = fi.read(self.readSize)
self.isFileHead = 0
data = con.hex()
if self.isFileHead == 0:
pass
else:
data = data[18:]
while con:
con = fi.read(self.readSize)
data = con.hex()
tagSize = self.getAVTagSize(data)
####################################################
# 获取TagType
# 0x08 音频
# 0x09 视频
# 0x12 脚本
####################################################
def getTagType(self,tag):
tagType = tag[8:10]
return tagType
####################################################
# 获取 flv tag 的数据长度,其实如图里 audio tag 头及其数据长度
####################################################
def getAVTagSize(self,tag):
tagSize = tag[10:16]
tagSize = int(tagSize,16)
return tagSize
####################################################
# 获取相对时间戳
####################################################
def getAVTimeStamp(self,tag):
timeStamp = tag[16:22]
timeStamp = int(timeStamp,16)
return timeStamp
####################################################
# 获取音视频数据
####################################################
def getAVData(self,tag,tagSize):
flvData = tag[34:(tagSize * 2)]
lastData = tag[(tagSize * 2):]
return flvData,lastData
####################################################
# 读取文件(测试程序)
####################################################
def readTest(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(204800)
data = con.hex() # 16进制字节码转字符串
data_H = data.split("00000001")
for i in range(1,len(data_H)):
print("00000001" + data_H[i])
####################################################
# 读取文件(测试程序)
####################################################
def readTest2(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(512)
data = con.hex()
print(data)
####################################################
# 读取文件(测试程序),从指定位置开始读取
####################################################
def readTest3(self,fileName):
with open(fileName, 'rb') as f2:
f2.seek(4,0)
con = f2.read(1)
data = con.hex()
print(data)
if __name__ == "__main__":
StreamH264Flv().readTest2("../../flv/aaa.flv")
# StreamH264Flv().readTest2("../flv/aaah.h264")
# aaa = "02000a6f6e4d65746144617461080000001000086475726174696f6e0040695da1cac083120005776964746800407e0000000000000006686569676874004070e00000000000000d766964656f646174617261746500000000000000000000096672616d657261746500403e000000000000000c766964656f636f646563696400401c000000000000000d617564696f6461746172617465000000000000000000000f617564696f73616d706c65726174650040e7700000000000000f617564696f73616d706c6573697a65004030000000000000000673746572656f0101000c617564696f636f6465636964004000000000000000000b6d616a6f725f6272616e6402000469736f6d000d6d696e6f725f76657273696f6e0200033531320011636f6d70617469626c655f6272616e647302001069736f6d69736f32617663316d7034310007656e636f64657202000d4c61766635382e36352e313030000866696c6573697a650041621da1a00000000000090000017f0900003400000000000000170000000001640015ffe1001a67640015acd941e08feb0110000003001000000303c0f162d96001000668ebe3cb22c0fdf8f8000000003f09001f69000000000000001701000043000002ae0605ffffaadc45e9bde6d948b7962cd820d923eeef78323634"
# print(len(aaa))
# print(aaa[:744])
\ No newline at end of file
#coding: utf-8
import binascii
import socket
import time
# http://10.100.11.125:8085/live?port=1985&app=vandyo&stream=013146201117-1
# http://10.16.15.85:8085/live?port=1985&app=vandyo&stream=013146201117-3
# http://localhost:3333/video/013146201117-3
from lib.protocal.Protocal_1078 import Protocal_1078
class StreamH264_bat():
def __init__(self):
self.readSize = 10240 # 每次读取文件的字节大小 (视频)
self.readSizeAudio = 1024 # 每次读取文件的字节大小 (音频频)
self.filePos = 0 # 文件的读取位置
# self.host = "10.100.11.125"
# self.host = "10.16.12.249"
self.host = "10.100.12.3"
# self.host = "localhost"
self.port = 1078
self.client = None
self.BUF_SIZE = 2048
self.sendDur = 0.001 # 消息发送间隔
self.pos = 1 # 当前的帧数(视频)
self.posAudio = 1 # 当前的帧数(音频)
self.frameCount = 1 # 对发送的帧计数(视频),如果发送的帧刚搞到达帧率发送的帧,则需要加上补齐1秒的值 (例如1秒发送30帧,则发送了30帧后需要补齐 1毫秒,使时间刚好到1秒),对应repairTime
self.frameCountAudio = 1 # 对发送的帧计数(视频),用来补齐1秒的时间(同上)
self.videoCount = 0 # 用来使音视频同步的变量
self.audioCount = 0 # 用来使音视频同步的变量
self.repairTime = 1 # 需要补齐到1秒的时间,对应frameCount
self.time = 33 # 时间戳,视频 (帧的时间,每发送一帧自动增加)
self.timeAudio = 21 # 时间戳,音频 (帧的时间,每发送一帧自动增加)
self.singleFrameTime = 33 # 视频单帧的时间(帧率为30 fps,单帧的时间为33 毫秒)
self.singleFrameTimeAudio = 21 # 音频的单帧时间 (毫秒)
self.lastKeyTime = 33 # Last I Frame Interval 该祯与上一个关键祯之间的时间间隔,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.lastTime = 33 # Last Frame Interval 该祯与上一个祯之间的时间时间,单位毫秒(ms),当数据类型为非视频祯时,则没有该字段
self.videoPath = "../h264/bbb3.h264" # 要发送的视频地址
self.audioPath = "../aac/bbb3.aac" # 要发送的音频地址
# self.videoPath = "../h264/aaa3.h264" # 要发送的视频地址
# self.audioPath = "../aac/aaa3.aac" # 要发送的音频地址
self.sendFrameType = 0 # 要发送的帧类型, 0:视频帧 1:音频帧 (针对视频和音频他同时发送的方法)
self.aacFront_28 = "" # aac 音频前28个固定bit 的值
self.aacIsFront_28 = 0 # aac 是否已经取出了前28个字节
####################################################
# 设置视频帧率
####################################################
def setFPSVideo(self,data = 0):
if(data == 0):
pass
else:
self.time = int(1000 / data)
self.singleFrameTime = int(1000 / data)
####################################################
# 设置音频帧率
####################################################
def setFPSAudio(self,data = 0):
if (data == 0):
pass
else:
self.timeAudio = int(1000 / data)
self.singleFrameTimeAudio = int(1000 / data)
####################################################
# socket 连接
####################################################
def connectServer(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳
self.client.connect((self.host, self.port))
####################################################
# 同时发送视频和音频
####################################################
def sendAVStream(self):
protocal_1078 = Protocal_1078()
# 打开视频文件
fv2 = open(self.videoPath, 'rb')
con = fv2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr_2(data)
for fra in frames:
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
print("发送视频消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
frames = []
# 打开音频文件
fa2 = open(self.audioPath, 'rb')
conAudio = fa2.read(self.readSizeAudio)
dataAudio = conAudio.hex() # 16进制字节码转字符串
framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio)
for fraAudio in framesAudio:
protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
self.doAudioFrameCount(protocal_1078)
pkgsAudio = protocal_1078.genAudioPkgsByFrame()
for msgAudio in pkgsAudio:
print("发送音频消息:" + msgAudio)
self.client.send(binascii.a2b_hex(msgAudio))
time.sleep(self.sendDur)
framesAudio = []
#####################################################################
# --------------------------------------------------------
while con or conAudio:
# print("----------视频" + str(self.pos))
# print("----------音频" + str(self.posAudio))
# print("---------------------------------")
if self.sendFrameType == 0:
con = fv2.read(self.readSize)
data = con.hex()
# print("视频" + str(self.videoCount))
if con:
if self.videoCount >= int(self.singleFrameTime / 2):
self.sendFrameType = 1
self.videoCount = 0
frames, lastData = self.getFramesFromStr_2(data, lastData)
for fra in frames:
print("视频" + str(self.videoCount))
if len(fra) % 2 != 0: # 处理有问题的帧数据
print("出现了视频问题帧,并自动修复...")
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
# print("发送视频消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
else:
self.sendFrameType = 1
elif self.sendFrameType == 1:
# print("音频" + str(self.audioCount))
conAudio = fa2.read(self.readSizeAudio)
dataAudio = conAudio.hex()
if conAudio:
if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
self.sendFrameType = 0
self.audioCount = 0
framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio, lastDataAudio)
for fraAudio in framesAudio:
print("音频" + str(self.audioCount))
if len(self.aacFront_28 + fraAudio) % 2 != 0:
print("出现了问题帧,并自动修复...")
print(fraAudio)
fraAudio = fraAudio[:len(fraAudio) - 1]
protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
self.doAudioFrameCount(protocal_1078)
pkgsAudio = protocal_1078.genAudioPkgsByFrame()
for msgAudio in pkgsAudio:
# print("发送音频消息:" + msgAudio)
self.client.send(binascii.a2b_hex(msgAudio))
time.sleep(self.sendDur)
else:
self.sendFrameType = 0
self.client.close()
# print("视频" + str(self.pos))
# print("音频" + str(self.posAudio))
#######################################################################################
# # --------------------------------------------------------
# while con or conAudio:
# # print("----------视频" + str(self.pos))
# # print("----------音频" + str(self.posAudio))
# # print("---------------------------------")
#
# if self.sendFrameType == 0:
# videoTempFrame = []
# for fra in frames:
# print("视频--" + str(self.videoCount))
# if len(fra) % 2 != 0: # 处理有问题的帧数据
# print("出现了视频问题帧,并自动修复...")
# fra = fra[:len(fra) - 1]
# protocal_1078.setDataBody("00000001" + fra)
# frameType = protocal_1078.getFrameType()
# self.doFrameCount(frameType, protocal_1078)
# pkgs = protocal_1078.genPkgsByFrame()
# for msg in pkgs:
# # print("发送视频消息:" + msg)
# self.client.send(binascii.a2b_hex(msg))
# time.sleep(self.sendDur)
# frames = []
#
# con = fv2.read(self.readSize)
# data = con.hex()
#
# if con:
# frames, lastData = self.getFramesFromStr_2(data, lastData)
# for fra in frames:
# if self.videoCount >= int(self.singleFrameTime / 2):
# self.sendFrameType = 1
# self.videoCount = 0
# if self.videoCount == 0:
# videoTempFrame.append(fra)
# else:
# print("视频" + str(self.videoCount))
# if len(fra) % 2 != 0: # 处理有问题的帧数据
# print("出现了视频问题帧,并自动修复...")
# fra = fra[:len(fra) - 1]
# protocal_1078.setDataBody("00000001" + fra)
# frameType = protocal_1078.getFrameType()
# self.doFrameCount(frameType, protocal_1078)
# pkgs = protocal_1078.genPkgsByFrame()
# for msg in pkgs:
# # print("发送视频消息:" + msg)
# self.client.send(binascii.a2b_hex(msg))
# time.sleep(self.sendDur)
# frames = videoTempFrame
# else:
# self.sendFrameType = 1
# elif self.sendFrameType == 1:
# audioTempFrame = []
# for fraAudio in framesAudio:
# print("音频--" + str(self.audioCount))
# if len(self.aacFront_28 + fraAudio) % 2 != 0:
# print("出现了问题帧,并自动修复...")
# print(fraAudio)
# fraAudio = fraAudio[:len(fraAudio) - 1]
# protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
# self.doAudioFrameCount(protocal_1078)
# pkgsAudio = protocal_1078.genAudioPkgsByFrame()
# for msgAudio in pkgsAudio:
# # print("发送音频消息:" + msgAudio)
# self.client.send(binascii.a2b_hex(msgAudio))
# time.sleep(self.sendDur)
# framesAudio = []
#
# conAudio = fa2.read(self.readSizeAudio)
# dataAudio = conAudio.hex()
# if conAudio:
# framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio, lastDataAudio)
# for fraAudio in framesAudio:
# if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
# self.sendFrameType = 0
# self.audioCount = 0
# if self.audioCount == 0:
# audioTempFrame.append(fraAudio)
# else:
# print("音频" + str(self.audioCount))
# if len(self.aacFront_28 + fraAudio) % 2 != 0:
# print("出现了问题帧,并自动修复...")
# print(fraAudio)
# fraAudio = fraAudio[:len(fraAudio) - 1]
# protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
# self.doAudioFrameCount(protocal_1078)
# pkgsAudio = protocal_1078.genAudioPkgsByFrame()
# for msgAudio in pkgsAudio:
# # print("发送音频消息:" + msgAudio)
# self.client.send(binascii.a2b_hex(msgAudio))
# time.sleep(self.sendDur)
# framesAudio = audioTempFrame
# else:
# self.sendFrameType = 0
# self.client.close()
#
# # print("视频" + str(self.pos))
# # print("音频" + str(self.posAudio))
############################################################################################
# # --------------------------------------------------------
# while con or conAudio:
# # print("----------视频" + str(self.pos))
# # print("----------音频" + str(self.posAudio))
# # print("---------------------------------")
#
# if self.sendFrameType == 0:
# videoTempFrame = []
# for fra in frames:
# if self.videoCount >= int(self.singleFrameTime / 2):
# self.sendFrameType = 1
# self.videoCount = 0
# if self.videoCount == 0:
# videoTempFrame.append(fra)
# continue
# else:
# # print("视频--" + str(self.videoCount))
# if len(fra) % 2 != 0: # 处理有问题的帧数据
# print("出现了视频问题帧,并自动修复...")
# fra = fra[:len(fra) - 1]
# protocal_1078.setDataBody("00000001" + fra)
# frameType = protocal_1078.getFrameType()
# self.doFrameCount(frameType, protocal_1078)
# pkgs = protocal_1078.genPkgsByFrame()
# for msg in pkgs:
# print("发送视频消息:" + msg)
# self.client.send(binascii.a2b_hex(msg))
# time.sleep(self.sendDur)
# frames = videoTempFrame
# if len(frames) != 0:
# continue
#
# con = fv2.read(self.readSize)
# data = con.hex()
#
# if con:
# frames, lastData = self.getFramesFromStr_2(data, lastData)
# for fra in frames:
# if self.videoCount >= int(self.singleFrameTime / 2):
# self.sendFrameType = 1
# self.videoCount = 0
# if self.videoCount == 0:
# videoTempFrame.append(fra)
# else:
# # print("视频" + str(self.videoCount))
# if len(fra) % 2 != 0: # 处理有问题的帧数据
# print("出现了视频问题帧,并自动修复...")
# fra = fra[:len(fra) - 1]
# protocal_1078.setDataBody("00000001" + fra)
# frameType = protocal_1078.getFrameType()
# self.doFrameCount(frameType, protocal_1078)
# pkgs = protocal_1078.genPkgsByFrame()
# for msg in pkgs:
# print("发送视频消息:" + msg)
# self.client.send(binascii.a2b_hex(msg))
# time.sleep(self.sendDur)
# frames = videoTempFrame
# else:
# self.sendFrameType = 1
# elif self.sendFrameType == 1:
# audioTempFrame = []
# for fraAudio in framesAudio:
# if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
# self.sendFrameType = 0
# self.audioCount = 0
# if self.audioCount == 0:
# audioTempFrame.append(fraAudio)
# continue
# else:
# # print("音频--" + str(self.audioCount))
# if len(self.aacFront_28 + fraAudio) % 2 != 0:
# print("出现了问题帧,并自动修复...")
# print(fraAudio)
# fraAudio = fraAudio[:len(fraAudio) - 1]
# protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
# self.doAudioFrameCount(protocal_1078)
# pkgsAudio = protocal_1078.genAudioPkgsByFrame()
# for msgAudio in pkgsAudio:
# # print("发送音频消息:" + msgAudio)
# self.client.send(binascii.a2b_hex(msgAudio))
# time.sleep(self.sendDur)
# framesAudio = audioTempFrame
# if len(framesAudio) != 0:
# continue
#
# conAudio = fa2.read(self.readSizeAudio)
# dataAudio = conAudio.hex()
# if conAudio:
# framesAudio, lastDataAudio = self.getAudioFramesFromStr_2(dataAudio, lastDataAudio)
# for fraAudio in framesAudio:
# if self.audioCount >= int(1000 / (self.singleFrameTimeAudio * 2)):
# self.sendFrameType = 0
# self.audioCount = 0
# if self.audioCount == 0:
# audioTempFrame.append(fraAudio)
# else:
# # print("音频" + str(self.audioCount))
# if len(self.aacFront_28 + fraAudio) % 2 != 0:
# print("出现了问题帧,并自动修复...")
# print(fraAudio)
# fraAudio = fraAudio[:len(fraAudio) - 1]
# protocal_1078.setDataBody(self.aacFront_28 + fraAudio)
# self.doAudioFrameCount(protocal_1078)
# pkgsAudio = protocal_1078.genAudioPkgsByFrame()
# for msgAudio in pkgsAudio:
# # print("发送音频消息:" + msgAudio)
# self.client.send(binascii.a2b_hex(msgAudio))
# time.sleep(self.sendDur)
# framesAudio = audioTempFrame
# else:
# self.sendFrameType = 0
# fv2.close()
# fa2.close()
# self.client.close()
#
# # print("视频" + str(self.pos))
# # print("音频" + str(self.posAudio))
####################################################
# 读取h264文件并发送
####################################################
def readStreamFromFileAndSend(self,fileName):
protocal_1078 = Protocal_1078()
with open(fileName, 'rb') as f2:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
# frames,lastData = self.getFramesFromStr(data)
frames, lastData = self.getFramesFromStr_2(data)
for fra in frames:
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
# self.pos = self.pos + 1
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
# frames, lastData = self.getFramesFromStr(data, lastData)
frames, lastData = self.getFramesFromStr_2(data, lastData)
for fra in frames:
if len(fra) % 2 != 0: # 处理有问题的帧数据
print("出现了问题帧,并自动修复...")
fra = fra[:len(fra) - 1]
protocal_1078.setDataBody("00000001" + fra)
frameType = protocal_1078.getFrameType()
self.doFrameCount(frameType, protocal_1078)
pkgs = protocal_1078.genPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
# self.pos = self.pos + 1
# print(self.pos)
####################################################
# 读取aac文件并发送
####################################################
def readAacStreamFromFileAndSend(self,fileName):
protocal_1078 = Protocal_1078()
with open(fileName, 'rb') as f2:
con = f2.read(self.readSizeAudio)
data = con.hex() # 16进制字节码转字符串
# frames, lastData = self.getAudioFramesFromStr(data)
frames,lastData = self.getAudioFramesFromStr_2(data)
for fra in frames:
# protocal_1078.setDataBody("fff" + fra)
protocal_1078.setDataBody(self.aacFront_28 + fra)
self.doAudioFrameCount(protocal_1078)
pkgs = protocal_1078.genAudioPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
self.posAudio = self.posAudio + 1
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
# frames, lastData = self.getAudioFramesFromStr(data, lastData)
frames, lastData = self.getAudioFramesFromStr_2(data, lastData)
for fra in frames:
if len(self.aacFront_28 + fra) % 2 != 0:
print("出现了问题帧,并自动修复...")
print(fra)
fra = fra[:len(fra) - 1]
# protocal_1078.setDataBody("fff" + fra)
protocal_1078.setDataBody(self.aacFront_28 + fra)
self.doAudioFrameCount(protocal_1078)
pkgs = protocal_1078.genAudioPkgsByFrame()
for msg in pkgs:
print("发送消息:" + msg)
self.client.send(binascii.a2b_hex(msg))
time.sleep(self.sendDur)
# self.posAudio = self.posAudio + 1
print(self.posAudio)
####################################################
# 读取h264文件(用来测试)
####################################################
def readStreamFromFile(self,fileName):
protocal_1078 = Protocal_1078()
with open(fileName, 'rb') as f2:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames,lastData = self.getFramesFromStr(data)
for fra in frames:
protocal_1078.setDataBody(fra)
frameType = protocal_1078.getFrameType()
if frameType == "67":
print(frameType)
self.pos = self.pos + 1
while con:
con = f2.read(self.readSize)
data = con.hex() # 16进制字节码转字符串
frames, lastData = self.getFramesFromStr(data, lastData)
for fra in frames:
protocal_1078.setDataBody(fra)
frameType = protocal_1078.getFrameType()
if frameType == "67":
print(frameType)
self.pos = self.pos + 1
# print(self.pos)
print(self.pos)
####################################################
# 对帧时间进行计数(视频帧)
###################################################
def doFrameCount(self,frameType,obj_1078):
if frameType == "65" or frameType == "61": # 68 之后接00000001 便是关键帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = 0
self.frameCount = self.frameCount + 1
self.videoCount = self.videoCount + 1
self.pos = self.pos + 1
elif frameType == "41": # P帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = self.lastKeyTime + self.singleFrameTime
self.frameCount = self.frameCount + 1
self.videoCount = self.videoCount + 1
self.pos = self.pos + 1
elif frameType == "01": # B帧
self.time = self.time + self.singleFrameTime
self.lastKeyTime = self.lastKeyTime + self.singleFrameTime
self.frameCount = self.frameCount + 1
self.videoCount = self.videoCount + 1
self.pos = self.pos + 1
else:
self.videoCount = self.videoCount + 1
if self.frameCount >= 30:
self.time = self.time + 1
self.frameCount = 1
obj_1078.setTime(self.time)
obj_1078.setLastKeyTime(self.lastKeyTime)
obj_1078.setLastTime(self.lastTime)
####################################################
# 对帧时间进行计数(音频帧)
###################################################
def doAudioFrameCount(self,obj_1078):
self.timeAudio = self.timeAudio + self.singleFrameTimeAudio
if self.frameCountAudio >= int(1000 / self.singleFrameTimeAudio):
self.frameCountAudio = 0
self.timeAudio = self.timeAudio + 1000 % self.singleFrameTimeAudio
self.frameCountAudio = self.frameCountAudio + 1
self.audioCount = self.audioCount + 1
self.posAudio = self.posAudio + 1
obj_1078.setTime(self.timeAudio)
####################################################
# 读取h264文件(测试程序)
####################################################
def readTest(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(204800)
data = con.hex() # 16进制字节码转字符串
data_H = data.split("00000001")
for i in range(1,len(data_H)):
print("00000001" + data_H[i])
####################################################
# 读取h264文件(测试程序)
####################################################
def readTest2(self,fileName):
with open(fileName, 'rb') as f2:
con = f2.read(200)
data = con.hex()
print(data)
con = f2.read(207)
data = con.hex()
print(data)
con = f2.read(212)
data = con.hex()
print(data)
####################################################
# 读取h264文件(测试程序),从指定位置开始读取
####################################################
def readTest3(self,fileName):
with open(fileName, 'rb') as f2:
f2.seek(4,0)
con = f2.read(1)
data = con.hex()
print(data)
#####################################################
# 从帧数据中获取帧类型
#####################################################
def getFrameType(self,frame):
# 0x67 (0 11 00111) SPS 非常重要 type = 7
# 0x68 (0 11 01000) PPS 非常重要 type = 8
# 0x65 (0 11 00101) IDR帧 关键帧 非常重要 type = 5
# 0x61 (0 11 00001) I帧 重要 type=1 非IDR的I帧 不大常见
# 0x41 (0 10 00001) P帧 重要 type = 1
# 0x01 (0 00 00001) B帧 不重要 type = 1
# 0x06 (0 00 00110) SEI 不重要 type = 6
frameTypeHex = frame[8:10]
return frameTypeHex
####################################################
# 16进制字符串分割为个多个帧(通过split方式分割)
####################################################
def getFramesFromStr(self,data,lastDataParam = ""):
data_H = data.split("00000001")
frames = []
if(len(data_H) == 1): #如果字符串中不包含 000001(说明内容都属于一帧的中间位置)
lastData = lastDataParam + data
return frames,lastData
else:
for i in range(0, len(data_H)):
if i != (len(data_H) - 1):
if data_H[i] != "":
if lastDataParam == "": #文件的开始
frame = "" + data_H[i]
frames.append(frame)
else:
if i == 0:
frame = "" + lastDataParam + data_H[i]
frames.append(frame)
else:
frame = "" + data_H[i]
frames.append(frame)
else:
if lastDataParam == "": #文件的开始
pass
else:
frame = "" + data_H[i] + lastDataParam
frames.append(frame)
else:
lastData = data_H[i]
tempFrames = []
for tmp in frames:
if self.getFrameType("00000001" + tmp) == "68" or self.getFrameType("00000001" + tmp) == "09":
fra = tmp.split("000001")
for fra2 in fra:
tempFrames.append(fra2)
else:
tempFrames.append(tmp)
frames = tempFrames
return frames,lastData
####################################################
# 16进制字符串分割为个多个帧(一个字节一个字节的读取)
####################################################
def getFramesFromStr_2(self,data,lastDataParam = ""):
dataStr = data
frame = ""
frames = []
frameSlice = dataStr[:2]
frame = lastDataParam + frame + frameSlice
dataStr = dataStr[2:]
while len(dataStr) > 0:
frameSlice = dataStr[:2]
dataStr = dataStr[2:]
if "00" == frameSlice:
frameTmp = frameSlice
frameTmp2 = dataStr[:2]
dataStr = dataStr[2:]
while frameTmp2 == "00":
frameTmp = frameTmp + frameTmp2
frameTmp2 = dataStr[:2]
dataStr = dataStr[2:]
frameTmp = frameTmp + frameTmp2
if frameTmp == "00000001":
frames.append(frame)
frame = ""
else:
frame = frame + frameTmp
else:
frame = frame + frameSlice
lastData = frame
tempFrames = []
for tmp in frames:
if self.getFrameType("00000001" + tmp) == "68" or self.getFrameType("00000001" + tmp) == "09":
fra = tmp.split("000001")
for fra2 in fra:
tempFrames.append(fra2)
else:
tempFrames.append(tmp)
frames = tempFrames
return frames,lastData
####################################################
# 获取音频帧
####################################################
# def getAudioFramesFromStr_2(self,data,lastDataParam = ""):
# dataStr = data
# frames = []
# frameSlice = dataStr[:2]
# frame = lastDataParam + frameSlice
# dataStr = dataStr[2:]
# while len(dataStr) > 0:
# frameSlice = dataStr[:2]
# dataStr = dataStr[2:]
# if "ff" == frameSlice:
# frameTmp = frameSlice
# frameTmp2 = dataStr[:2]
# dataStr = dataStr[2:]
# if frameTmp2[:1] == "f":
# if len(frame) > 14:
# frames.append(frame[3:])
# frame = ""
# if len(dataStr) < 10:
# frame = frame + frameTmp + frameTmp2 + dataStr
# dataStr = ""
# else:
# frameSlice = dataStr[:10]
# dataStr = dataStr[10:]
# frame = frame + frameTmp + frameTmp2 + frameSlice
# else:
# frame = frame + frameTmp + frameTmp2
# else:
# frame = frame + frameTmp + frameTmp2
# else:
# frame = frame + frameSlice
# lastData = frame
# return frames, lastData
####################################################
# 获取音频帧(通过前面28个字节来截取)
####################################################
def getAudioFramesFromStr_2(self,data,lastDataParam = ""):
if self.aacIsFront_28 == 0:
self. aacFront_28 = data[:7]
self.aacIsFront_28 = 1
data_H = data.split(self.aacFront_28)
frames = []
if(len(data_H) == 1): #如果字符串中不包含 000001(说明内容都属于一帧的中间位置)
lastData = lastDataParam + data
return frames,lastData
else:
for i in range(0, len(data_H)):
if i != (len(data_H) - 1):
if data_H[i] != "":
if lastDataParam == "": #文件的开始
frame = "" + data_H[i]
frames.append(frame)
else:
if i == 0:
frame = "" + lastDataParam + data_H[i]
frames.append(frame)
else:
frame = "" + data_H[i]
frames.append(frame)
else:
if lastDataParam == "": #文件的开始
pass
else:
frame = "" + data_H[i] + lastDataParam
frames.append(frame)
else:
lastData = data_H[i]
return frames,lastData
if __name__ == "__main__":
obj = StreamH264()
obj.connectServer()
obj.setFPSVideo(30)
obj.setFPSAudio(47)
# obj.readStreamFromFileAndSend("../h264/bbb3.h264") # 发送视频
# obj.readAacStreamFromFileAndSend("../aac/bbb3.aac") # 发送音频
obj.sendAVStream()
# StreamH264().readTest("../h264/aaa.h264")
# StreamH264().readTest2("../h264/aaa2.264")
# StreamH264().readTest2("../aac/aaa.aac")
# StreamH264().readStreamFromFile("../h264/aaa2.264")
pass
# coding:utf-8
import threading
import time
from lib.service.StreamH264 import StreamH264
class H264PresureTest():
def __init__(self):
self.mobileStart = 100000000000 # 开始手机号
self.channel = 1 # 频道号
self.terNum = 2 # 要压力测试的线程数
self.videoPath = "../../h264/bbb3.h264" # 要推流的视频路劲
self.audioPath = "../../h264/bbb3.aac" # 要推流的音频路劲
self.host = "10.100.12.3"
self.port = 1078
self.threadInfo = {} # 存放线程集
self.threadInfo["threadObj"] = {}
self.threadInfo["sucessT"] = {}
self.threadInfo["failT"] = {}
def setTerNum(self,data):
self.terNum = data
def setMobileStart(self,data="10000000000"):
self.mobileStart = data
def setChannel(self,data):
self.channel = data
def setVideoPath(self,data):
self.videoPath = data
def setAudioPath(self,data):
self.audioPath = data
def setHost(self,data):
self.host = data
def setPort(self,data):
self.port = data
def testService(self,cloudMirror):
cloudMirror.sendAVStream()
def run(self,mobile,threadId):
cloudMirror = StreamH264()
cloudMirror.setHost(self.host)
cloudMirror.setPort(self.port)
cloudMirror.connectServer()
cloudMirror.setFPSVideo(30)
cloudMirror.setFPSAudio(47)
cloudMirror.setSendDur(0)
cloudMirror.setVideoPath(self.videoPath)
cloudMirror.setAudioPath(self.audioPath)
cloudMirror.setMobile(mobile)
cloudMirror.setChannel(self.channel)
conThread = threading.Thread(target=self.testService,args=(cloudMirror,))
self.threadInfo["threadObj"][threadId] = conThread
conThread.start()
print(threadId + "启动了:" + mobile + "-" + str(self.channel))
def start(self):
for i in range(0,self.terNum):
mobile = self.mobileStart + i
mobile = "0" + str(mobile)
threadId = "threadId__" + str(i)
self.run(mobile,threadId)
time.sleep(0.1)
if __name__ == "__main__":
test = H264PresureTest()
test.setHost("10.100.12.3")
test.setPort(1078)
test.setMobileStart(10000000000)
test.setChannel(1)
test.setTerNum(20)
test.setVideoPath("../../h264/bbb3.h264")
test.setAudioPath("../../aac/bbb3.aac")
test.start()
\ No newline at end of file
#coding: utf-8
import binascii
def readH():
with open("h264/aaa2.264", 'rb') as f2:
con = f2.read(2048)
data = con.hex()
data2 = data.split("00000001")
for i in data2:
print("00000001" + i)
print(data2)
with open("h264/tmp.bin", 'wb') as f3:
f3.write(con)
if __name__ == "__main__":
readH()
\ No newline at end of file
#coding: utf-8
from lib.service.StreamH264 import StreamH264
if __name__ == "__main__":
StreamH264().readStreamFromFileAndSend("h264/aaa2.264")
# StreamH264().readTest("h264/aaa2.264")
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
# coding: utf-8
import subprocess
import threading
import time
import os
VIDEO_PATH = "video" #自动推流视频目录
# PUSH_URL = "rtmp://192.168.0.108:1935/live/room"
PUSH_URL = "rtmp://10.100.12.50:1935/live/room" #推流服务地址
pushThread = {}
isLoop = 1 #是否循环推流:0:不循环 1:循环
#############################################
# 读取当前目录文件
#############################################
def readVideoFile(path=VIDEO_PATH):
videoFiles = os.listdir(VIDEO_PATH)
return videoFiles
#############################################
# 推流方法,被线程调用
#############################################
def pushStream(fi,url):
print(fi + "----推流开始 ")
m_command = ["ffmpeg.exe",
"-re",
"-i", fi,
"-vcodec", "copy",
"-f",
"flv", url]
print(m_command)
subprocess.run(m_command)
print(fi + "----推流结束 ")
if isLoop == 1:
time.sleep(0.5)
pushStream(fi,url)
else:
pushThread.pop(fi)
#############################################
# 开始推流启动多个线程去推流(使用VIDEO_PATH目录下的有所文件)
#############################################
def startPush():
v_files = readVideoFile()
i = 1
for fi in v_files:
pushFile = VIDEO_PATH + "/" + fi
pushUrl = PUSH_URL + str(i)
td = threading.Thread(target=pushStream,args=[pushFile,pushUrl])
td.start()
i = i + 1
pushThread[pushFile] = pushUrl
time.sleep(1)
while len(pushThread) > 0:
for key in pushThread:
print(key + " 的播放地址为:" + pushThread[key])
time.sleep(5)
print("\n")
print("所有推流全部结束")
#############################################
# 开始推流(使用写死的文件地址)
#############################################
def pushStreamFixedFile():
m_command = ["ffmpeg.exe",
"-re",
"-i","E:\视频\舞蹈\【かや】Doja Cat - Say So _ KAYA Ver.【踊ってみた】.mp4",
# "-i", "【かや】thank u, next _ Ariana Grande【踊ってみた _ KAYA Ver.】.mp4",
"-vcodec", "copy",
"-f",
"flv", "rtmp://10.100.12.50:1935/live/room1"]
subprocess.run(m_command)
if __name__ == "__main__":
# startPush()
pushStreamFixedFile()
#coding:utf-8
import binascii
import time
import socket
from xml.dom.minidom import parse
#http://10.100.11.125:8085/live?port=1985&app=vandyo&stream=013146201116-4
# host = "10.100.11.125"
# host = "10.16.15.85"
host = "localhost"
port = 1078
BUF_SIZE = 2048
isLoop = 0 #是否循环发送视频流)0:不 1:是
SIM = "013146201117"
channel = "03"
def readFile_M(fileName):
with open(fileName, 'r') as f1:
f_content = f1.readlines()
for con in f_content:
# print(con)
with open("temp/res.txt", 'a') as f2:
f2.write(con.replace("\n",""))
#####################################################
# 从帧数据中获取帧类型
#####################################################
def getFrameType(data):
# 0x67 (0 11 00111) SPS 非常重要 type = 7
# 0x68 (0 11 01000) PPS 非常重要 type = 8
# 0x65 (0 11 00101) IDR帧 关键帧 非常重要 type = 5
# 0x61 (0 11 00001) I帧 重要 type=1 非IDR的I帧 不大常见
# 0x41 (0 10 00001) P帧 重要 type = 1
# 0x01 (0 00 00001) B帧 不重要 type = 1
# 0x06 (0 00 00110) SEI 不重要 type = 6
frameTypeHex = data[68:70]
return frameTypeHex
def readFile_M2(fileName):
with open(fileName, 'r') as f1:
f_content = f1.read()
f_content = f_content.split("30316364")
# print(len(f_content))
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳
client.connect((host, port))
for i in range(2,len(f_content)):
msg = "30316364" + f_content[i]
msg = msg[:16] + SIM + channel + msg[30:]
if getFrameType(msg) == "61":
print("关键帧")
client.send(binascii.a2b_hex(msg))
time.sleep(0.01)
print(msg)
print("\n\n\n\n 第一波推流完成 --------------------------------")
client.close()
time.sleep(5)
if isLoop == 1:
readFile_M2("temp/res.txt")
def paseXml():
domTree = parse("xml/frame.xml")
rootNode = domTree.documentElement
frameSizes = []
# 所有顾客
customers = rootNode.getElementsByTagName("frame")
for customer in customers:
frameSizes.append(int(customer.getAttribute("pkt_size")))
with open("aac/aaa.aac", 'rb') as f1:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳
client.connect((host, port))
for i in frameSizes:
msg = f1.read(i)
msg = msg.hex()
print(msg)
client.send(binascii.a2b_hex(msg))
time.sleep(0.001)
client.close()
if __name__ == "__main__":
# readFile_M("temp/tmp1.txt")
# readFile_M2("temp/res.txt")
# readFile_M("temp/video.txt")
# readFile_M2("temp/res.txt")
paseXml()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment