优化逻辑

This commit is contained in:
Jerry Yan 2019-10-19 08:23:41 +08:00
parent cf169021c6
commit 0fc33fccff
5 changed files with 48 additions and 27 deletions

View File

@ -32,7 +32,9 @@ config = {
"exp": 1, "exp": 1,
"dow": "echo 'clean'", "dow": "echo 'clean'",
# 仅下载 # 仅下载
"dlO": True "dlO": True,
# 下播延迟投稿
"dly": 30
} }
_config_fp = open("config.json", "r", encoding="utf8") _config_fp = open("config.json", "r", encoding="utf8")
@ -41,7 +43,7 @@ _config_fp.close()
del _config_fp del _config_fp
doCleanTime = datetime.now() doCleanTime = datetime.now()
_clean_flag = None _clean_flag = None
delay = 30
b = Bilibili() b = Bilibili()
network = [{ network = [{
@ -63,6 +65,20 @@ network = [{
}] }]
def resetDelay():
global delay
delay = config['dly']
def doDelay():
global delay
if delay < 0:
resetDelay()
sleep(60)
delay -= 1
return delay < 0
def updateNetwork(): def updateNetwork():
global network global network
network.append({ network.append({
@ -356,6 +372,8 @@ def refreshDownloader():
def uploadVideo(name): def uploadVideo(name):
if not os.path.exists(name):
Common.appendError("Upload File Not Exist {}".format(name))
if forceNotUpload is False: if forceNotUpload is False:
b.preUpload(VideoPart(name, os.path.basename(name))) b.preUpload(VideoPart(name, os.path.basename(name)))
else: else:

View File

@ -133,13 +133,15 @@ def getAllStats():
"broadcaster": Common.broadcaster.__str__(), "broadcaster": Common.broadcaster.__str__(),
"isBroadcasting": Common.isBroadcasting, "isBroadcasting": Common.isBroadcasting,
"streamUrl": Common.streamUrl, "streamUrl": Common.streamUrl,
"updateTime": Common.updateTime "updateTime": Common.updateTime,
"delayTime": Common.delay
}, },
"config": { "config": {
"forceNotBroadcasting": Common.forceNotBroadcasting, "forceNotBroadcasting": Common.forceNotBroadcasting,
"forceNotDownload": Common.forceNotDownload, "forceNotDownload": Common.forceNotDownload,
"forceNotUpload": Common.forceNotUpload, "forceNotUpload": Common.forceNotUpload,
"forceNotEncode": Common.forceNotEncode, "forceNotEncode": Common.forceNotEncode,
"downloadOnly": Common.config['dlO'],
}, },
}}) }})
@ -158,7 +160,8 @@ def getBroadcastStats():
"broadcaster": Common.broadcaster.__str__(), "broadcaster": Common.broadcaster.__str__(),
"isBroadcasting": Common.isBroadcasting, "isBroadcasting": Common.isBroadcasting,
"streamUrl": Common.streamUrl, "streamUrl": Common.streamUrl,
"updateTime": Common.updateTime "updateTime": Common.updateTime,
"delayTime": Common.delay
} }
}}) }})
@ -171,6 +174,7 @@ def getConfigStats():
"forceNotDownload": Common.forceNotDownload, "forceNotDownload": Common.forceNotDownload,
"forceNotUpload": Common.forceNotUpload, "forceNotUpload": Common.forceNotUpload,
"forceNotEncode": Common.forceNotEncode, "forceNotEncode": Common.forceNotEncode,
"downloadOnly": Common.config['dlO'],
} }
}}) }})

View File

@ -61,12 +61,12 @@ def encode():
if Common.forceNotEncode: if Common.forceNotEncode:
Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i)) Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
elif os.path.exists(i): elif os.path.exists(i):
isEncode = True
if os.path.getsize(i) < 8 * 1024 * 1024: if os.path.getsize(i) < 8 * 1024 * 1024:
Common.appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(i)) Common.appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(i))
continue continue
else: else:
Common.appendEncodeStatus("Encoding >{}< Start".format(i)) Common.appendEncodeStatus("Encoding >{}< Start".format(i))
isEncode = True
_code = os.system("ffmpeg -i {} -c:v copy -c:a copy -f mp4 {} -y".format(i, i[:13] + ".mp4")) _code = os.system("ffmpeg -i {} -c:v copy -c:a copy -f mp4 {} -y".format(i, i[:13] + ".mp4"))
if _code != 0: if _code != 0:
Common.appendError("Encode {} with Non-Zero Return.".format(i)) Common.appendError("Encode {} with Non-Zero Return.".format(i))
@ -88,15 +88,11 @@ def upload():
if i is True: if i is True:
Common.publishVideo(date) Common.publishVideo(date)
break break
if not os.path.exists(i):
Common.appendError("Upload File Not Exist {}".format(i))
else:
try: try:
Common.uploadVideo(i) Common.uploadVideo(i)
except Exception as e: except Exception as e:
Common.appendError(e.__str__()) Common.appendError(e.__str__())
continue continue
i = Common.uploadQueue.get() i = Common.uploadQueue.get()
Common.appendUploadStatus("Upload Daemon Quiting") Common.appendUploadStatus("Upload Daemon Quiting")
@ -143,15 +139,14 @@ def run():
if not Common.api.isValidRoom: if not Common.api.isValidRoom:
Common.appendError("[{}]房间未找到".format(Common.config["l_u"])) Common.appendError("[{}]房间未找到".format(Common.config["l_u"]))
return return
awakeEncode()
_count = 0 _count = 0
_firstDown = 0
while True: while True:
if Common.api.isLive and not Common.forceNotBroadcasting: if Common.api.isLive and not Common.forceNotBroadcasting:
_firstDown = 0
if not Common.forceNotDownload: if not Common.forceNotDownload:
awakeDownload() awakeDownload()
if not Common.forceNotUpload:
awakeUpload() awakeUpload()
if not Common.forceNotEncode:
awakeEncode() awakeEncode()
if _count % 15 == 14: if _count % 15 == 14:
try: try:
@ -177,14 +172,8 @@ def run():
if Common.forceStartUploadThread: if Common.forceStartUploadThread:
awakeUpload() awakeUpload()
Common.forceStartUploadThread = False Common.forceStartUploadThread = False
if not isEncode and not isDownload: if not isEncode and not isDownload and Common.doDelay():
# 防抖,避免主播因特殊情况下播导致直接投递了
if _firstDown < 30:
_firstDown += 1
sleep(60)
continue
Common.uploadQueue.put(True) Common.uploadQueue.put(True)
_firstDown = 0
isEncode = True isEncode = True
isDownload = True isDownload = True
time.sleep(60) time.sleep(60)

View File

@ -6,10 +6,12 @@ function taskUpdate(){
$("#broadcaster").text(res.data.broadcast.broadcaster) $("#broadcaster").text(res.data.broadcast.broadcaster)
$("#isBroadcasting").text(res.data.broadcast.isBroadcasting) $("#isBroadcasting").text(res.data.broadcast.isBroadcasting)
$("#streamUrl").text(res.data.broadcast.streamUrl) $("#streamUrl").text(res.data.broadcast.streamUrl)
$("#delayTime").text(res.data.broadcast.delayTime)
$("#forceNotBroadcasting").text(res.data.config.forceNotBroadcasting) $("#forceNotBroadcasting").text(res.data.config.forceNotBroadcasting)
$("#forceNotDownload").text(res.data.config.forceNotDownload) $("#forceNotDownload").text(res.data.config.forceNotDownload)
$("#forceNotUpload").text(res.data.config.forceNotUpload) $("#forceNotUpload").text(res.data.config.forceNotUpload)
$("#forceNotEncode").text(res.data.config.forceNotEncode) $("#forceNotEncode").text(res.data.config.forceNotEncode)
$("#downloadOnly").text(res.data.config.downloadOnly)
$("#updateTime").text(res.data.broadcast.updateTime) $("#updateTime").text(res.data.broadcast.updateTime)
$("#encodeQueueSize").text(res.data.encodeQueueSize) $("#encodeQueueSize").text(res.data.encodeQueueSize)
$("#uploadQueueSize").text(res.data.uploadQueueSize) $("#uploadQueueSize").text(res.data.uploadQueueSize)
@ -54,4 +56,4 @@ function taskUpdate(){
} }
taskUpdate() taskUpdate()
setInterval(taskUpdate,10000) setInterval(taskUpdate,8000)

View File

@ -24,6 +24,10 @@
<td>信息更新时间</td> <td>信息更新时间</td>
<td><span id="updateTime"></span></td> <td><span id="updateTime"></span></td>
</tr> </tr>
<tr>
<td>延迟投稿时间</td>
<td><span id="delayTime"></span></td>
</tr>
</table> </table>
<hr/> <hr/>
<h1>特殊设置</h1> <h1>特殊设置</h1>
@ -44,6 +48,10 @@
<td>是否设置强制不转码</td> <td>是否设置强制不转码</td>
<td><span id="forceNotEncode"></span></td> <td><span id="forceNotEncode"></span></td>
</tr> </tr>
<tr>
<td>是否设置为仅下载(不上传不转码)</td>
<td><span id="downloadOnly"></span></td>
</tr>
</table> </table>
<hr/> <hr/>
<h1>当前状态</h1> <h1>当前状态</h1>