diff --git a/Common.py b/Common.py index df9f657..baee9fc 100644 --- a/Common.py +++ b/Common.py @@ -32,7 +32,9 @@ config = { "exp": 1, "dow": "echo 'clean'", # 仅下载 - "dlO": True + "dlO": True, + # 下播延迟投稿 + "dly": 30 } _config_fp = open("config.json", "r", encoding="utf8") @@ -41,7 +43,7 @@ _config_fp.close() del _config_fp doCleanTime = datetime.now() _clean_flag = None - +delay = 30 b = Bilibili() 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(): global network network.append({ @@ -356,6 +372,8 @@ def refreshDownloader(): def uploadVideo(name): + if not os.path.exists(name): + Common.appendError("Upload File Not Exist {}".format(name)) if forceNotUpload is False: b.preUpload(VideoPart(name, os.path.basename(name))) else: diff --git a/WebMain.py b/WebMain.py index d496b3e..c6f0727 100644 --- a/WebMain.py +++ b/WebMain.py @@ -133,13 +133,15 @@ def getAllStats(): "broadcaster": Common.broadcaster.__str__(), "isBroadcasting": Common.isBroadcasting, "streamUrl": Common.streamUrl, - "updateTime": Common.updateTime + "updateTime": Common.updateTime, + "delayTime": Common.delay }, "config": { "forceNotBroadcasting": Common.forceNotBroadcasting, "forceNotDownload": Common.forceNotDownload, "forceNotUpload": Common.forceNotUpload, "forceNotEncode": Common.forceNotEncode, + "downloadOnly": Common.config['dlO'], }, }}) @@ -158,7 +160,8 @@ def getBroadcastStats(): "broadcaster": Common.broadcaster.__str__(), "isBroadcasting": Common.isBroadcasting, "streamUrl": Common.streamUrl, - "updateTime": Common.updateTime + "updateTime": Common.updateTime, + "delayTime": Common.delay } }}) @@ -171,6 +174,7 @@ def getConfigStats(): "forceNotDownload": Common.forceNotDownload, "forceNotUpload": Common.forceNotUpload, "forceNotEncode": Common.forceNotEncode, + "downloadOnly": Common.config['dlO'], } }}) diff --git a/liveDownloader.py b/liveDownloader.py index 661b3b9..0195213 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -61,12 +61,12 @@ def encode(): if Common.forceNotEncode: Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i)) elif os.path.exists(i): - isEncode = True if os.path.getsize(i) < 8 * 1024 * 1024: Common.appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(i)) continue else: 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")) if _code != 0: Common.appendError("Encode {} with Non-Zero Return.".format(i)) @@ -88,15 +88,11 @@ def upload(): if i is True: Common.publishVideo(date) break - if not os.path.exists(i): - Common.appendError("Upload File Not Exist {}".format(i)) - else: - try: - Common.uploadVideo(i) - except Exception as e: - Common.appendError(e.__str__()) - continue - + try: + Common.uploadVideo(i) + except Exception as e: + Common.appendError(e.__str__()) + continue i = Common.uploadQueue.get() Common.appendUploadStatus("Upload Daemon Quiting") @@ -143,16 +139,15 @@ def run(): if not Common.api.isValidRoom: Common.appendError("[{}]房间未找到".format(Common.config["l_u"])) return - awakeEncode() _count = 0 - _firstDown = 0 while True: if Common.api.isLive and not Common.forceNotBroadcasting: - _firstDown = 0 if not Common.forceNotDownload: awakeDownload() - awakeUpload() - awakeEncode() + if not Common.forceNotUpload: + awakeUpload() + if not Common.forceNotEncode: + awakeEncode() if _count % 15 == 14: try: Common.api.updRoomInfo() @@ -177,14 +172,8 @@ def run(): if Common.forceStartUploadThread: awakeUpload() Common.forceStartUploadThread = False - if not isEncode and not isDownload: - # 防抖,避免主播因特殊情况下播导致直接投递了 - if _firstDown < 30: - _firstDown += 1 - sleep(60) - continue + if not isEncode and not isDownload and Common.doDelay(): Common.uploadQueue.put(True) - _firstDown = 0 isEncode = True isDownload = True time.sleep(60) diff --git a/static/index.js b/static/index.js index 2d54541..4ad9983 100644 --- a/static/index.js +++ b/static/index.js @@ -6,10 +6,12 @@ function taskUpdate(){ $("#broadcaster").text(res.data.broadcast.broadcaster) $("#isBroadcasting").text(res.data.broadcast.isBroadcasting) $("#streamUrl").text(res.data.broadcast.streamUrl) + $("#delayTime").text(res.data.broadcast.delayTime) $("#forceNotBroadcasting").text(res.data.config.forceNotBroadcasting) $("#forceNotDownload").text(res.data.config.forceNotDownload) $("#forceNotUpload").text(res.data.config.forceNotUpload) $("#forceNotEncode").text(res.data.config.forceNotEncode) + $("#downloadOnly").text(res.data.config.downloadOnly) $("#updateTime").text(res.data.broadcast.updateTime) $("#encodeQueueSize").text(res.data.encodeQueueSize) $("#uploadQueueSize").text(res.data.uploadQueueSize) @@ -54,4 +56,4 @@ function taskUpdate(){ } taskUpdate() -setInterval(taskUpdate,10000) +setInterval(taskUpdate,8000) diff --git a/templates/index.html b/templates/index.html index 0c350ad..351910a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -24,6 +24,10 @@