Commit 507d95de authored by liyuanhong's avatar liyuanhong

兼容了mac部分崩溃问题

parent f73d499d
...@@ -124,72 +124,79 @@ class StreamH264Flv(): ...@@ -124,72 +124,79 @@ class StreamH264Flv():
self.isFileHead = 0 self.isFileHead = 0
timeStamp = 0 timeStamp = 0
self.timesStamp = timeStamp self.timesStamp = timeStamp
with open(self.videoPath, 'rb') as fi: try:
con = fi.read(self.readSize) with open(self.videoPath, 'rb') as fi:
data = con.hex() con = fi.read(self.readSize)
if self.isFileHead == 0: data = con.hex()
data = data[18:] if self.isFileHead == 0:
self.isFileHead = 1 data = data[18:]
else: self.isFileHead = 1
data = data
while con:
if self.isEndPush != 0:
break
else: else:
if self.isSend == 1: data = data
if len(data) < 30: while con:
con = fi.read(self.readSize) if self.isEndPush != 0:
data = data + con.hex() break
else: else:
tagSize = self.getAVTagSize(data) if self.isSend == 1:
if len(data) < (tagSize * 2 + 30): if len(data) < 30:
con = fi.read(self.readSize) con = fi.read(self.readSize)
data = data + con.hex() data = data + con.hex()
else: else:
tag = data[:tagSize * 2 + 30] # flv tag tagSize = self.getAVTagSize(data)
tagType = self.getTagType(tag) # flv tag 类型,是音频 还是视频 if len(data) < (tagSize * 2 + 30):
AVtag = tag[30:] # 音视频 tag con = fi.read(self.readSize)
if self.multiPushStatus < 1: data = data + con.hex()
avTimeStamp = self.getAVTimeStamp(tag) #获取时间戳
timeStamp = avTimeStamp
self.timesStamp = timeStamp
else: else:
avTimeStamp = self.getAVTimeStamp(tag) + self.timeStampSeek # 获取时间戳 tag = data[:tagSize * 2 + 30] # flv tag
timeStamp = avTimeStamp tagType = self.getTagType(tag) # flv tag 类型,是音频 还是视频
self.timesStamp = timeStamp AVtag = tag[30:] # 音视频 tag
if avTimeStamp > 100: if self.multiPushStatus < 1:
if self.isGetPlayUrl == 0: avTimeStamp = self.getAVTimeStamp(tag) #获取时间戳
self.getPlayUrl(self.mobile,self.channel) timeStamp = avTimeStamp
self.isGetPlayUrl = 1 self.timesStamp = timeStamp
if tagType == "08": # 音频 else:
AVdata = AVtag[4:] # 音频数据 avTimeStamp = self.getAVTimeStamp(tag) + self.timeStampSeek # 获取时间戳
if self.isSendAudio == 1: timeStamp = avTimeStamp
frameInfo = self.getAudioFrame(AVtag) self.timesStamp = timeStamp
if avTimeStamp > 100:
if self.isGetPlayUrl == 0:
self.getPlayUrl(self.mobile,self.channel)
self.isGetPlayUrl = 1
if tagType == "08": # 音频
AVdata = AVtag[4:] # 音频数据
if self.isSendAudio == 1:
frameInfo = self.getAudioFrame(AVtag)
for frame in frameInfo["frames"]:
self.sendAudioFrame(frame,avTimeStamp)
elif tagType == "09": # 视频
AVdata = AVtag[10:] # 视频数据
frameInfo = self.getVideoFrame(AVtag)
for frame in frameInfo["frames"]: for frame in frameInfo["frames"]:
self.sendAudioFrame(frame,avTimeStamp) self.sendVideoFrame(frame,avTimeStamp,AVtag)
elif tagType == "09": # 视频 elif tagType == "12": # 脚本
AVdata = AVtag[10:] # 视频数据 AVdata = AVtag # 脚本数据
frameInfo = self.getVideoFrame(AVtag) else:
for frame in frameInfo["frames"]: AVdata = AVtag
self.sendVideoFrame(frame,avTimeStamp,AVtag) data = data[(tagSize * 2 + 30):]
elif tagType == "12": # 脚本 else:
AVdata = AVtag # 脚本数据 time.sleep(1)
else: self.pushStatusText.SetValue("已连网")
AVdata = AVtag timeCur = self.getCurTime()
data = data[(tagSize * 2 + 30):] self.multiPushStatus = self.multiPushStatus + 1
else: self.timeStampSeek = timeStamp + 1000
time.sleep(1) self.logTextCtr.WriteText(timeCur + "视频推流结束,自动结束推流!\n")
self.pushStatusText.SetValue("已连网") if self.autoClose == 1:
timeCur = self.getCurTime() self.client.close()
self.multiPushStatus = self.multiPushStatus + 1 self.client = None
self.timeStampSeek = timeStamp + 1000 curTime = self.getCurTime()
self.logTextCtr.WriteText(timeCur + "视频推流结束,自动结束推流!\n") msgTxt = curTime + "视频推流结束,自动结束推流!(同时断开了连接)"
if self.autoClose == 1: wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt)
self.client.close() except Exception as e:
self.client = None if self.getOsName() == "Windows":
curTime = self.getCurTime() traceback.print_exc()
msgTxt = curTime + "视频推流结束,自动结束推流!(同时断开了连接)" else:
wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt) msgTxt = traceback.format_exc()
wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt)
#################################################### ####################################################
# 停止发送消息 # 停止发送消息
...@@ -248,14 +255,17 @@ class StreamH264Flv(): ...@@ -248,14 +255,17 @@ class StreamH264Flv():
pkgs = self.protocal_1078.genPkgsByFrame() pkgs = self.protocal_1078.genPkgsByFrame()
for msg in pkgs: for msg in pkgs:
if self.isOpenVideoLog != 0: if self.isOpenVideoLog != 0:
# print("发送视频消息:" + msg)
msgTxt = "发送视频消息:" + msg msgTxt = "发送视频消息:" + msg
wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=msgTxt) wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=msgTxt)
try: try:
self.client.send(binascii.a2b_hex(msg)) self.client.send(binascii.a2b_hex(msg))
except: except:
traceback.print_exc() if self.getOsName() == "Windows":
print(msg) traceback.print_exc()
print(msg)
else:
msgTxt = traceback.format_exc()
wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt)
time.sleep(self.sendDur) time.sleep(self.sendDur)
#################################################### ####################################################
...@@ -400,8 +410,8 @@ if __name__ == "__main__": ...@@ -400,8 +410,8 @@ if __name__ == "__main__":
# obj.setMobile("142043390091") # obj.setMobile("142043390091")
obj.setMobile("013146200000") obj.setMobile("013146200000")
obj.setChannel(1) obj.setChannel(1)
obj.setHost("10.100.11.125") # obj.setHost("10.100.11.125")
# obj.setHost("video-test.vandyo.com") obj.setHost("video-test.vandyo.com")
obj.setPort(1078) obj.setPort(1078)
obj.setVideoPath("../../flv/aaa3.flv") obj.setVideoPath("../../flv/aaa3.flv")
obj.connectServer() obj.connectServer()
......
...@@ -311,7 +311,6 @@ class CameraArea(): ...@@ -311,7 +311,6 @@ class CameraArea():
timeCur = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) timeCur = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
timeCur = "[" + timeCur + "] " timeCur = "[" + timeCur + "] "
return timeCur return timeCur
#################################################### ####################################################
# 获取当前时间(不加括号) # 获取当前时间(不加括号)
#################################################### ####################################################
...@@ -321,6 +320,24 @@ class CameraArea(): ...@@ -321,6 +320,24 @@ class CameraArea():
timeCur = time.strftime("%Y年%m月%d日--%H时%M分%S秒", timeArray) timeCur = time.strftime("%Y年%m月%d日--%H时%M分%S秒", timeArray)
timeCur = timeCur timeCur = timeCur
return timeCur return timeCur
####################################################
# 获取当前日期
####################################################
def getCurDate(self):
timeCur = int(time.time() * 1000)
timeArray = time.localtime(timeCur / 1000)
timeCur = time.strftime("%Y%m%d", timeArray)
timeCur = timeCur
return timeCur
####################################################
# 获取当前时间
####################################################
def getCurTheTume(self):
timeCur = int(time.time() * 1000)
timeArray = time.localtime(timeCur / 1000)
timeCur = time.strftime("%H%M%S", timeArray)
timeCur = timeCur
return timeCur
#################################################### ####################################################
# 断网 # 断网
...@@ -353,7 +370,11 @@ class CameraArea(): ...@@ -353,7 +370,11 @@ class CameraArea():
try: try:
self.pushObj.readFlvAndSend() self.pushObj.readFlvAndSend()
except: except:
traceback.print_exc() if self.getOsName() == "Windows":
traceback.print_exc()
else:
msgTxt = traceback.format_exc()
wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt)
#################################################### ####################################################
# 暂停推流 # 暂停推流
...@@ -398,11 +419,19 @@ class CameraArea(): ...@@ -398,11 +419,19 @@ class CameraArea():
threadObj = threading.Thread(target=self.doStopPush()) threadObj = threading.Thread(target=self.doStopPush())
threadObj.start() threadObj.start()
def doStopPush(self): def doStopPush(self):
timeCur = self.getCurTime() try:
self.logTextCtr.WriteText(timeCur + "推流停止!\n") timeCur = self.getCurTime()
self.pushStatusText.SetValue("连网") self.logTextCtr.WriteText(timeCur + "推流停止!\n")
self.pushObj.setIsEndPush(1) self.pushStatusText.SetValue("连网")
# self.pushObj = None self.pushObj.setIsEndPush(1)
# self.pushObj = None
except:
if self.getOsName() == "Windows":
traceback.print_exc()
else:
msgTxt = traceback.format_exc()
wx.CallAfter(pub.sendMessage, "closeMsg" + str(self.channel), msg=msgTxt)
traceback.print_exc()
#################################################### ####################################################
...@@ -614,23 +643,21 @@ class CameraArea(): ...@@ -614,23 +643,21 @@ class CameraArea():
if self.pushObj == None: if self.pushObj == None:
wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=cutTime + "未推流,不可进行视频抓拍!") wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=cutTime + "未推流,不可进行视频抓拍!")
else: else:
capTime = self.pushObj.getTimeStamp()
capTime = int(capTime / 1000)
capdur = 12 # 要抓拍的视频时长 capdur = 12 # 要抓拍的视频时长
inFi = self.videoPathText.GetValue() inFi = self.videoPathText.GetValue()
videoLen = self.getVideoDur(inFi)
curtimeO = self.getCurTimeO()
curPath = globalParams.scriptPath curPath = globalParams.scriptPath
outFi = 1 outFi = ""
theDate = self.getCurDate() # 视频抓拍日期
theTime = self.getCurTheTume() # 视频抓拍时间
videoLen = self.getVideoDur(inFi) # 视频总时长
capTime = str(int(self.pushObj.getTimeStamp() / 1000)) # 从第几秒开始截取的
if cam == 1: if cam == 1:
outFi = curPath + "/SDCard/video/CAM_1/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".mp4" outFi = curPath + "/SDCard/picture/CAM_1/" + "VID" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".mp4"
elif cam == 2: elif cam == 2:
outFi = curPath + "/SDCard/video/CAM_2/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".mp4" outFi = curPath + "/SDCard/picture/CAM_2/" + "VID" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".mp4"
elif cam == 3: elif cam == 3:
outFi = curPath + "/SDCard/video/CAM_3/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".mp4" outFi = curPath + "/SDCard/picture/CAM_3/" + "VID" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".mp4"
# outFi = curPath + "/SDCard/video/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".mp4"
self.splitVideo(capTime, capdur,inFi, outFi) self.splitVideo(capTime, capdur,inFi, outFi)
timeCur = int(time.time()) timeCur = int(time.time())
self.uploadFile(cam, outFi, 2, timeCur) self.uploadFile(cam, outFi, 2, timeCur)
ws.sendMsg(replyMsg) ws.sendMsg(replyMsg)
...@@ -649,19 +676,19 @@ class CameraArea(): ...@@ -649,19 +676,19 @@ class CameraArea():
if self.pushObj == None: if self.pushObj == None:
wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=cutTime + "未推流,不可进行图片抓拍!") wx.CallAfter(pub.sendMessage, "showLog" + str(self.channel), msg=cutTime + "未推流,不可进行图片抓拍!")
else: else:
capTime = str(int(self.pushObj.getTimeStamp() / 1000))
inFi = self.videoPathText.GetValue() inFi = self.videoPathText.GetValue()
curPath = globalParams.scriptPath curPath = globalParams.scriptPath
videoLen = self.getVideoDur(inFi) outFi = ""
curtimeO = self.getCurTimeO() theDate = self.getCurDate() # 视频抓拍日期
outFi = 1 theTime = self.getCurTheTume() # 视频抓拍时间
videoLen = self.getVideoDur(inFi) # 视频总时长
capTime = str(int(self.pushObj.getTimeStamp() / 1000)) # 从第几秒开始截取的
if cam == 1: if cam == 1:
outFi = curPath + "/SDCard/picture/CAM_1/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".jpg" outFi = curPath + "/SDCard/picture/CAM_1/" + "PIC" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".jpg"
elif cam == 2: elif cam == 2:
outFi = curPath + "/SDCard/picture/CAM_2/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".jpg" outFi = curPath + "/SDCard/picture/CAM_2/" + "PIC" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".jpg"
elif cam == 3: elif cam == 3:
outFi = curPath + "/SDCard/picture/CAM_3/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".jpg" outFi = curPath + "/SDCard/picture/CAM_3/" + "PIC" + str(videoLen) + "_" + str(capTime) + "_" + theDate + "_" + theTime + ".jpg"
# outFi = curPath + "/SDCard/picture/" + curtimeO + "_" + str(videoLen) + "__" + str(capTime) + ".jpg"
m_command = ["ffmpeg.exe", "-i", inFi, "-frames:v", "1", "-ss", capTime, "-f", "image2", outFi] m_command = ["ffmpeg.exe", "-i", inFi, "-frames:v", "1", "-ss", capTime, "-f", "image2", outFi]
subprocess.run(m_command) subprocess.run(m_command)
......
...@@ -6,5 +6,5 @@ camera_2 = None # 相机2 ...@@ -6,5 +6,5 @@ camera_2 = None # 相机2
camera_3 = None # 相机3 camera_3 = None # 相机3
eventArea = None # 事件发送对象 eventArea = None # 事件发送对象
isLogRedirect = 1 # 日志是否重定向到日志文本框 0: 不重定向 (用于调试) 1:重定向 (用于给别人用) isLogRedirect = 0 # 日志是否重定向到日志文本框 0: 不重定向 (用于调试) 1:重定向 (用于给别人用)
scriptPath = "" # 脚本执行的目录 scriptPath = "" # 脚本执行的目录
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