剔除一些无效代码,优化部分逻辑

This commit is contained in:
Jerry Yan 2019-10-22 10:09:50 +08:00
parent 64fbe010ef
commit adc6e10df1
2 changed files with 45 additions and 34 deletions

View File

@ -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)

View File

@ -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)