From adc6e10df16892d00f04e2ae58c5eb473f24b227 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 22 Oct 2019 10:09:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=94=E9=99=A4=E4=B8=80=E4=BA=9B=E6=97=A0?= =?UTF-8?q?=E6=95=88=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BC=98=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common.py | 43 ++++++++++++++++++++++++++++++++++++++++--- liveDownloader.py | 36 +++++------------------------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/Common.py b/Common.py index 1b2f53d..eff9abe 100644 --- a/Common.py +++ b/Common.py @@ -34,9 +34,11 @@ config = { # 仅下载 "dlO": True, # 下播延迟投稿 - "dly": 30 + "dly": 30, + "enc": "ffmpeg -i {f} -c:v copy -c:a copy -f mp4 {t} -y" } doCleanTime = datetime.now() +loginTime = datetime.now() _clean_flag = None delay = 30 b = Bilibili() @@ -75,10 +77,12 @@ def resetDelay(): def doDelay(): - global delay + global delay, isBroadcasting, isEncode, isUpload + if isBroadcasting or isEncode or isUpload: + resetDelay() + return False if delay < 0: resetDelay() - sleep(60) delay -= 1 return delay < 0 @@ -167,6 +171,8 @@ if config["dlO"] is True: forceNotEncode = True forceStartEncodeThread = False forceStartUploadThread = False +isEncode = False +isUpload = False uploadQueue = queue.Queue() encodeQueue = queue.Queue() @@ -295,7 +301,11 @@ def appendError(obj): def loginBilibili(): if "dlO" not in config or config["dlO"] is False or forceNotUpload is False: + global loginTime + if getTimeDelta(datetime.now(), loginTime) < 86400 * 3: + return True res = b.login(config["b_u"], config["b_p"]) + loginTime = datetime.now() appendOperation("登陆账号,结果为:[{}]".format(res)) @@ -368,15 +378,19 @@ def refreshDownloader(): def uploadVideo(name): + global isUpload if not os.path.exists(name): Common.appendError("Upload File Not Exist {}".format(name)) if forceNotUpload is False: + isUpload = True b.preUpload(VideoPart(name, os.path.basename(name))) + isUpload = False else: appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(name)) if not Common.forceNotEncode: os.remove(name) + def publishVideo(date): if forceNotUpload is False: b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"], @@ -384,3 +398,26 @@ def publishVideo(date): b.clear() else: appendUploadStatus("设置了不上传,所以[{}]的录播不会上传了".format(date)) + + +def encodeVideo(name): + if forceNotEncode: + appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(name)) + return False + if not os.path.exists(name): + appendEncodeStatus("文件[{}]不存在".format(name)) + return False + if os.path.getsize(name) < 8 * 1024 * 1024: + appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(name)) + return False + appendEncodeStatus("Encoding >{}< Start".format(name)) + global isEncode + isEncode=True + _new_name = os.path.splitext(name)[0]+".mp4" + _code = os.system(config["enc"].format(f=name, t=_new_name)) + isEncode=False + if _code != 0: + Common.appendError("Encode {} with Non-Zero Return.".format(name)) + return False + Common.modifyLastEncodeStatus("Encode >{}< Finished".format(name)) + uploadQueue.put(_new_name) diff --git a/liveDownloader.py b/liveDownloader.py index 0195213..a22d427 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -7,12 +7,9 @@ import Common import os import requests -isEncode = False -isDownload = False def download(): - global isDownload session = requests.session() while Common.api.isLive and not Common.forceNotDownload: if not Common.streamUrl: @@ -23,7 +20,6 @@ def download(): if p.status_code != 200: Common.appendDownloadStatus("Download with Response {}".format(p.status_code)) break - isDownload = True Common.appendDownloadStatus("Download >{}< Start".format(path)) f = open(path, "wb") _size = 0 @@ -43,7 +39,6 @@ def download(): except Exception as e: Common.appendError("Download >{}< With Exception {}".format(path, e.__str__())) f.close() - isDownload = False if os.path.getsize(path) < 1024 * 1024: Common.modifyLastDownloadStatus("Downloaded File >{}< is too small, will ignore it".format(path)) os.remove(path) @@ -53,28 +48,10 @@ def download(): def encode(): - global isEncode Common.appendEncodeStatus("Encode Daemon Starting") while True: - isEncode = False i = Common.encodeQueue.get() - if Common.forceNotEncode: - Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i)) - elif os.path.exists(i): - 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)) - continue - else: - Common.modifyLastEncodeStatus("Encode >{}< Finished".format(i)) - i = i[:13] + ".mp4" - Common.uploadQueue.put(i) + Common.encodeVideo(i) def upload(): @@ -84,14 +61,14 @@ def upload(): i = Common.uploadQueue.get() while True: Common.doClean() - if isinstance(i, bool): - if i is True: - Common.publishVideo(date) + if i is True: + Common.publishVideo(date) break try: Common.uploadVideo(i) except Exception as e: Common.appendError(e.__str__()) + time.sleep(120) continue i = Common.uploadQueue.get() Common.appendUploadStatus("Upload Daemon Quiting") @@ -134,7 +111,6 @@ def awakeUpload(): def run(): - global isEncode, isDownload Common.refreshDownloader() if not Common.api.isValidRoom: Common.appendError("[{}]房间未找到".format(Common.config["l_u"])) @@ -172,8 +148,6 @@ def run(): if Common.forceStartUploadThread: awakeUpload() Common.forceStartUploadThread = False - if not isEncode and not isDownload and Common.doDelay(): + if Common.doDelay(): Common.uploadQueue.put(True) - isEncode = True - isDownload = True time.sleep(60)