Skip to content

Commit

Permalink
1.支持强制全局hls 切片时间设置\n2.优化系统响应的JSON 字段内容\n3.修复偶发的无法找到任务导致视频重新拉流的BUG\n4.…
Browse files Browse the repository at this point in the history
…优化开启推流时的响应逻辑
  • Loading branch information
szh8052 committed Jun 9, 2021
1 parent 621965d commit ed07d7c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
33 changes: 23 additions & 10 deletions HN_Rtsp2Hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
_task_time = 0
_log_dir = ""
_hls_dir = ""
_ver = "1.3.1.beta"
_ver = "1.4.2"

api = Flask(__name__)

Expand Down Expand Up @@ -42,20 +42,25 @@ def task_PlayVideo():
ffmpeg = args.ffmpeg
else:
ffmpeg = None
taskAll = htask.getTaskLine()["all"]
# 检查是否有存活的进程
for task in taskAll:
if task["videoId"] == videoId:
task["wait_secord"] = 0
task["play_status"] = "alive"
return fre.success_response(task)
stask = public.GetSystemTask(videoId)
if len(stask) == 3:
while True:
taskAll = htask.getTaskLine()["all"]
# 检查是否有存活的进程
for task in taskAll:
if task["videoId"] == videoId:
task["wait_secord"] = 0
task["play_status"] = "alive"
return fre.success_response(task)


# 如果没有存在的进程 那么开始启动一个新的转流进程
thread, task = htask.addTaskLine(videoId, rtspUrl,hlsTime,hlsSize, ffmpeg)
# 等待m3u8 切片文件存在
wait_count = 0
play_status = "success"
while not os.path.exists(task["videoDir"] + "video1.ts"):
if wait_count > int(hlsTime) + 5:
while not os.path.exists(task["videoDir"] + "video"+task["hls_size"]+".ts"):
if wait_count > int(hlsTime) * int(task["hls_size"]) + 5:
play_status = "timeout"
break
public.Print_Log("等待切片文件生成中...")
Expand Down Expand Up @@ -125,6 +130,9 @@ def task_AliveVideo():

if __name__ == '__main__':

public.Print_Log("RTSP 转 HLS直播流控制工具,Version:" + _ver)
public.Print_Log("copyright © jshainei.com 2019-2021 ")

# 加载配置文件
try:
config = json.loads(public.ReadFile("config.json"))
Expand All @@ -137,19 +145,23 @@ def task_AliveVideo():
public.Print_Log("[致命错误]加载核心配置文件出错,程序终止!")
sys.exit(0)


# 检查是否存在 日志文件夹 和 hls 输出文件夹
if not os.path.exists(_log_dir):
os.mkdir(_log_dir)
if not os.path.exists(_hls_dir):
os.mkdir(_hls_dir)


if os.path.exists(_log_dir + "taskLine.json"):
os.remove(_log_dir + "taskLine.json")

if not os.path.exists("kill-super.sh"):
shell = requests.get("https://mirrors.jshainei.com/smb/codesrc/shell/kill-super.sh").text
public.WriteFile("kill-super.sh", shell)

public.cache_set("sysVer",_ver)

time.sleep(1)
public.Print_Log("转流任务队列初始化中...", _log_dir + "run.log")
htask = HN_Task.HN_Task(_log_dir,_hls_dir,_task_time)
Expand All @@ -169,3 +181,4 @@ def task_AliveVideo():




13 changes: 8 additions & 5 deletions HN_Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ def resetTaskLine(self,task):
taskLine = self.getTaskLine()
taskLine["all"].append(task)
public.WriteFile(self.log_taskLine, json.dumps(taskLine["all"]))

return thread,task




def addTaskLine(self,videoId,rtspUrl,hls_time,hls_size,ffmpeg=False):
if not os.path.exists(self.log_taskdir):
os.mkdir(self.log_taskdir)
Expand All @@ -74,15 +70,22 @@ def addTaskLine(self,videoId,rtspUrl,hls_time,hls_size,ffmpeg=False):
if not os.path.exists(self.hls_dir + videoId):
os.mkdir(self.hls_dir + videoId)
else:
os.system("rm -rf "+ self.hls_dir + videoId + "/*")
os.system("rm -rf "+ self.hls_dir + videoId )
os.mkdir(self.hls_dir + videoId)
shell = shell.replace("ffmpeg", "/usr/local/ffmpeg/bin/ffmpeg")
# 检查是否存在全局强制 hls 配置文件
if os.path.exists("hls.json"):
hls = json.loads(public.ReadFile("hls.json"))
hls_time = hls["time"]
hls_size = hls["size"]
shell = shell.replace("__HLSTIME__", hls_time)
shell = shell.replace("__HLSSIZE__", hls_size)
shell = shell.replace("__RTSPURL__", rtspUrl)
shell = shell.replace("__VIDEOOUTDIR__",self.hls_dir + videoId)
task = {
"taskId":public.GetStrUuid(),"task_Init":int(time.time()),"task_run":0,"task_runAlive":0,
"videoId": videoId, "rtspUrl": rtspUrl,"ffmpeg_shell":shell,"videoUrl":videoId + "/video.m3u8","videoDir":self.hls_dir + videoId +"/",
'hls_time':hls_time,"hls_size":hls_size,
"threadHeart":int(time.time()),"waitRun":True,
}
public.Print_Log("线程参数构造完成,准备启动线程...")
Expand Down
2 changes: 2 additions & 0 deletions build_dist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 删除编译缓存文件
rm -rf build && rm -rf dist && rm -rf HN_Rtsp2Hls.spec && pyinstaller -F HN_Rtsp2Hls.py
7 changes: 5 additions & 2 deletions frequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import public,HN_Task





def success_response(data):
return {"status": "success",
"request_id": public.GetStrUuid(), "request_time": int(time.time()), "request_ip": request.remote_addr, "request_ua": request.remote_user,
"response": data}
"response": data,"ver":public.cache_get("sysVer")}

def error_response(code,msg,e):
return {"status": "error",
"request_id": public.GetStrUuid(), "request_time": int(time.time()), "request_ip": request.remote_addr, "request_ua": request.remote_user,
"response": {"code": code, "msg": msg, "error": e}
"response": {"code": code, "msg": msg, "error": e},"ver":public.cache_get("sysVer")
},code

def check_args(args,input):
Expand Down

0 comments on commit ed07d7c

Please sign in to comment.