新增切换流逻辑
This commit is contained in:
@@ -20,6 +20,12 @@ ffmpeg_process: Optional[subprocess.Popen] = None
|
||||
running = True
|
||||
should_restart = False # 标记是否需要重新启动ffmpeg(凌晨3点触发)
|
||||
|
||||
# ffmpeg 退出追踪状态(用于判断是否切换 stream_type)
|
||||
state = {
|
||||
'exit_times': [], # 记录 ffmpeg 退出时间戳
|
||||
'use_fallback_stream': False # 标记下次是否使用 stream_type=1
|
||||
}
|
||||
|
||||
|
||||
def parse_args():
|
||||
"""解析命令行参数"""
|
||||
@@ -55,9 +61,20 @@ def parse_args():
|
||||
|
||||
def get_hls_url(index_code: str) -> Optional[str]:
|
||||
"""获取HLS播放地址"""
|
||||
# 确定使用哪个 stream_type
|
||||
stream_type = 0
|
||||
try:
|
||||
stream_type = 1 if state['use_fallback_stream'] else 0
|
||||
if state['use_fallback_stream']:
|
||||
state['use_fallback_stream'] = False
|
||||
state['exit_times'].clear() # 使用 fallback 时清空退出记录
|
||||
logger.info("Using fallback stream_type=1")
|
||||
except Exception as e:
|
||||
logger.error(f"Error determining stream_type: {e}")
|
||||
|
||||
try:
|
||||
import test_cam
|
||||
result = test_cam.get_camera_hls_url(index_code, 0)
|
||||
result = test_cam.get_camera_hls_url(index_code, stream_type)
|
||||
if result.get("code") == "0" and result.get("data", {}).get("url"):
|
||||
return result["data"]["url"]
|
||||
else:
|
||||
@@ -239,6 +256,18 @@ def main():
|
||||
if ffmpeg_process is not None and ffmpeg_process.poll() is not None:
|
||||
logger.warning(f"FFmpeg exited unexpectedly with code: {ffmpeg_process.returncode}")
|
||||
|
||||
# 记录退出时间,检查是否需要切换 stream_type
|
||||
try:
|
||||
state['exit_times'].append(datetime.now())
|
||||
# 清理超过10分钟的旧记录
|
||||
state['exit_times'][:] = [t for t in state['exit_times'] if datetime.now() - t < timedelta(minutes=10)]
|
||||
|
||||
if len(state['exit_times']) > 3: # 10分钟内退出超过3次(即第4次)
|
||||
state['use_fallback_stream'] = True
|
||||
logger.warning("FFmpeg exited 4+ times in 10 minutes, will use stream_type=1 next call")
|
||||
except Exception as e:
|
||||
logger.error(f"Error tracking exit times: {e}")
|
||||
|
||||
# 尝试启动下载
|
||||
success = download_cycle(
|
||||
args.hls_root_path,
|
||||
|
||||
Reference in New Issue
Block a user