lajilaji
This commit is contained in:
@@ -1 +1,3 @@
|
|||||||
https://live.douyin.com/9638535297,主播: 异度世界
|
https://live.douyin.com/9638535297,主播: 异度世界
|
||||||
|
https://live.douyin.com/481942173701,主播: 王火火拌辣串
|
||||||
|
https://live.douyin.com/342979433932,主播: 七七古早芋泥饼
|
||||||
|
|||||||
98
main.py
98
main.py
@@ -1622,18 +1622,15 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
error_window.append(1)
|
error_window.append(1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# ====================== 抖音 → HDMI 转播(FIFO + mpv 常驻 + 卡死自动重启 + 超强稳定重连) ======================
|
# ====================== 抖音 → HDMI 转播(FIFO + mpv 常驻 + 卡死自动重启 + 超强稳定重连) ======================
|
||||||
import queue
|
import queue
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import traceback
|
import traceback
|
||||||
# import os
|
# import os
|
||||||
# import datetime
|
# import datetime
|
||||||
|
|
||||||
class StreamEnded(Exception):
|
class StreamEnded(Exception):
|
||||||
"""主播真实下播时抛出,用于彻底退出循环"""
|
"""主播真实下播时抛出,用于彻底退出循环"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def sanitize_title(text: str, max_len: int = 120) -> str:
|
def sanitize_title(text: str, max_len: int = 120) -> str:
|
||||||
if not text:
|
if not text:
|
||||||
return ""
|
return ""
|
||||||
@@ -1641,7 +1638,6 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
text = ''.join(ch for ch in text if unicodedata.category(ch)[0] != 'C')
|
text = ''.join(ch for ch in text if unicodedata.category(ch)[0] != 'C')
|
||||||
text = ''.join(ch for ch in text if ord(ch) <= 0xFFFF)
|
text = ''.join(ch for ch in text if ord(ch) <= 0xFFFF)
|
||||||
return text.strip()[:max_len]
|
return text.strip()[:max_len]
|
||||||
|
|
||||||
def _stderr_reader_thread(proc, q):
|
def _stderr_reader_thread(proc, q):
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -1659,7 +1655,6 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
q.put(None)
|
q.put(None)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def cleanup_proc(proc):
|
def cleanup_proc(proc):
|
||||||
try:
|
try:
|
||||||
if proc and proc.poll() is None:
|
if proc and proc.poll() is None:
|
||||||
@@ -1671,39 +1666,32 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
proc.kill()
|
proc.kill()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# ====================== 配置参数 ======================
|
# ====================== 配置参数 ======================
|
||||||
MAX_RETRY_DELAY = 90 # 最大重连等待时间(秒),指数退避上限,防止抖音风控
|
MAX_RETRY_DELAY = 90 # 最大重连等待时间(秒),指数退避上限,防止抖音风控
|
||||||
INITIAL_RETRY_DELAY = 3 # 首次重连等待时间(秒),从这个值开始翻倍
|
INITIAL_RETRY_DELAY = 3 # 首次重连等待时间(秒),从这个值开始翻倍
|
||||||
STDERR_QUEUE_TIMEOUT = 1.0 # 读取 FFmpeg stderr 的队列超时(秒),用于及时发现卡顿
|
STDERR_QUEUE_TIMEOUT = 1.0 # 读取 FFmpeg stderr 的队列超时(秒),用于及时发现卡顿
|
||||||
|
NO_FRAME_TIMEOUT = 120 # 连续多少秒无新帧(frame= 未增长)就判定主播下播(秒)
|
||||||
NO_FRAME_TIMEOUT = 120 # 连续多少秒无新帧(frame= 未增长)就判定主播下播(秒)
|
START_CHECK_AFTER = 25 # 开播后至少等多少秒才开始无帧检测,避免刚开播误判(秒)
|
||||||
START_CHECK_AFTER = 25 # 开播后至少等多少秒才开始无帧检测,避免刚开播误判(秒)
|
STALE_OUTPUT_SECONDS = 120 # FFmpeg 连续多少秒完全无日志输出就认为僵死,需要重启(秒)
|
||||||
STALE_OUTPUT_SECONDS = 120 # FFmpeg 连续多少秒完全无日志输出就认为僵死,需要重启(秒)
|
|
||||||
|
|
||||||
END_KEYWORDS = [
|
END_KEYWORDS = [
|
||||||
"connection reset by peer", "failed to write trailer", "invalid data found",
|
"connection reset by peer", "failed to write trailer", "invalid data found",
|
||||||
"server returned 403", "server returned 404", "ingestion error",
|
"server returned 403", "server returned 404", "ingestion error",
|
||||||
"access denied", "packet too short", "client disconnected",
|
"access denied", "packet too short", "client disconnected",
|
||||||
"live has ended", "no route to host", "connection timed out",
|
"live has ended", "no route to host", "connection timed out",
|
||||||
]
|
]
|
||||||
|
|
||||||
timestamp_str = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
|
timestamp_str = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
|
||||||
_clean_title = sanitize_title(title_in_name)
|
_clean_title = sanitize_title(title_in_name)
|
||||||
stream_title = sanitize_title(f"{anchor_name}{('_' + _clean_title) if _clean_title else ''}_{timestamp_str}")
|
stream_title = sanitize_title(f"{anchor_name}{('_' + _clean_title) if _clean_title else ''}_{timestamp_str}")
|
||||||
print(f"[INFO] {rec_info} 开始转播到 HDMI: {stream_title}")
|
print(f"[INFO] {rec_info} 开始转播到 HDMI: {stream_title}")
|
||||||
|
|
||||||
# 编码参数(完全保留原样)
|
# 编码参数(完全保留原样)
|
||||||
codec_v = "libx264"
|
codec_v = "libx264"
|
||||||
preset_v = "ultrafast"
|
preset_v = "ultrafast"
|
||||||
tune_param = ["-tune", "zerolatency"]
|
tune_param = ["-tune", "zerolatency"]
|
||||||
|
|
||||||
douyin_headers = (
|
douyin_headers = (
|
||||||
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15\r\n"
|
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15\r\n"
|
||||||
"Referer: https://live.douyin.com/\r\n"
|
"Referer: https://live.douyin.com/\r\n"
|
||||||
"Origin: https://live.douyin.com\r\n"
|
"Origin: https://live.douyin.com\r\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ====================== FIFO ======================
|
# ====================== FIFO ======================
|
||||||
FIFO_PATH = "/tmp/douyin_hdmi.fifo"
|
FIFO_PATH = "/tmp/douyin_hdmi.fifo"
|
||||||
if not os.path.exists(FIFO_PATH):
|
if not os.path.exists(FIFO_PATH):
|
||||||
@@ -1712,30 +1700,27 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
print(f"[INFO] 创建 FIFO 文件: {FIFO_PATH}")
|
print(f"[INFO] 创建 FIFO 文件: {FIFO_PATH}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[ERROR] 创建 FIFO 失败(可能已存在): {e}")
|
print(f"[ERROR] 创建 FIFO 失败(可能已存在): {e}")
|
||||||
|
|
||||||
# ====================== mpv 命令 ======================
|
# ====================== mpv 命令 ======================
|
||||||
|
# ====================== mpv ======================
|
||||||
mpv_cmd = [
|
mpv_cmd = [
|
||||||
"mpv",
|
"mpv",
|
||||||
FIFO_PATH,
|
FIFO_PATH,
|
||||||
"--vo=drm", # 不支持会自动降级
|
"--vo=drm",
|
||||||
"--hwdec=v4l2m2m",
|
"--hwdec=v4l2m2m",
|
||||||
"--profile=low-latency",
|
"--profile=low-latency",
|
||||||
"--cache=yes",
|
"--cache=yes",
|
||||||
"--cache-secs=20",
|
"--cache-secs=5", # 缓存越小延迟越低
|
||||||
"--demuxer-lavf-format=flv",
|
"--demuxer-lavf-format=flv",
|
||||||
"--demuxer-lavf-o=fflags=+nobuffer",
|
"--demuxer-lavf-o=fflags=+nobuffer",
|
||||||
"--untimed",
|
|
||||||
"--framedrop=decoder",
|
"--framedrop=decoder",
|
||||||
"--video-sync=display-resample",
|
"--video-sync=audio", # 音频同步视频
|
||||||
"--quiet",
|
"--quiet",
|
||||||
"--loop-file=no",
|
"--loop-file=no",
|
||||||
"--no-input-default-bindings",
|
"--no-input-default-bindings",
|
||||||
"--vf=transpose=2,colorspace=all=bt709:iall=bt709,"
|
"--fullscreen",
|
||||||
"eq=gamma_r=0.95:gamma_g=1.0:gamma_b=1.05:"
|
"--video-unscaled=no"
|
||||||
"brightness=0.03:saturation=0.85:contrast=1.05"
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# ====================== FFmpeg 命令(输出到 FIFO) ======================
|
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg",
|
"ffmpeg",
|
||||||
"-y",
|
"-y",
|
||||||
@@ -1752,13 +1737,14 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
"-fflags", "+genpts+discardcorrupt+nobuffer+flush_packets",
|
"-fflags", "+genpts+discardcorrupt+nobuffer+flush_packets",
|
||||||
"-flags", "low_delay",
|
"-flags", "low_delay",
|
||||||
"-err_detect", "ignore_err",
|
"-err_detect", "ignore_err",
|
||||||
|
|
||||||
"-headers", douyin_headers,
|
"-headers", douyin_headers,
|
||||||
"-i", real_url,
|
"-i", real_url,
|
||||||
|
# ================= 视频处理(保持比例 + 顺时针旋转90度 + 全屏 HDMI 720x1280) =================
|
||||||
"-vf", "fps=30,scale=720:1280:force_original_aspect_ratio=decrease:flags=lanczos,"
|
"-vf", (
|
||||||
"pad=720:1280:(ow-iw)/2:(oh-ih)/2:black",
|
"transpose=-1," # 顺时针旋转90度
|
||||||
|
"scale=iw*min(720/iw\\,1280/ih):ih*min(720/iw\\,1280/ih):flags=lanczos,"
|
||||||
|
"pad=720:1280:(720-iw*min(720/iw\\,1280/ih))/2:(1280-ih*min(720/iw\\,1280/ih))/2:black"
|
||||||
|
),
|
||||||
"-c:v", codec_v,
|
"-c:v", codec_v,
|
||||||
"-preset", preset_v,
|
"-preset", preset_v,
|
||||||
*tune_param,
|
*tune_param,
|
||||||
@@ -1774,26 +1760,34 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
"-sc_threshold", "0",
|
"-sc_threshold", "0",
|
||||||
"-x264-params", "force-cfr=1:scenecut=0:open_gop=0:sync-lookahead=0",
|
"-x264-params", "force-cfr=1:scenecut=0:open_gop=0:sync-lookahead=0",
|
||||||
"-pix_fmt", "yuv420p",
|
"-pix_fmt", "yuv420p",
|
||||||
|
# ================= 音频处理 =================
|
||||||
"-c:a", "aac",
|
"-c:a", "aac",
|
||||||
"-b:a", "128k",
|
"-b:a", "128k",
|
||||||
"-ar", "44100",
|
"-ar", "48000",
|
||||||
"-ac", "2",
|
"-ac", "2",
|
||||||
"-af", "aresample=async=1:first_pts=0",
|
"-af", "aresample=async=1:first_pts=0",
|
||||||
|
# ================= 输出 =================
|
||||||
"-flags", "+global_header",
|
|
||||||
"-flvflags", "no_duration_filesize",
|
"-flvflags", "no_duration_filesize",
|
||||||
"-max_muxing_queue_size", "1024",
|
"-max_muxing_queue_size", "1024",
|
||||||
"-f", "flv",
|
"-f", "flv",
|
||||||
FIFO_PATH
|
FIFO_PATH
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ====================== 主循环 ======================
|
# ====================== 主循环 ======================
|
||||||
proc = None
|
proc = None
|
||||||
mpv_proc = None
|
mpv_proc = None
|
||||||
retry_delay = INITIAL_RETRY_DELAY
|
retry_delay = INITIAL_RETRY_DELAY
|
||||||
last_buffer_warning_time = 0 # 避免频繁重启
|
last_buffer_warning_time = 0 # 避免频繁重启
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# 启动/重启 mpv(常驻)
|
# 启动/重启 mpv(常驻)
|
||||||
@@ -1807,17 +1801,14 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL
|
stderr=subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
time.sleep(1) # 给 mpv 启动时间
|
time.sleep(1) # 给 mpv 启动时间
|
||||||
|
|
||||||
# 重连等待
|
# 重连等待
|
||||||
if retry_delay > INITIAL_RETRY_DELAY:
|
if retry_delay > INITIAL_RETRY_DELAY:
|
||||||
print(f"[WARN] 等待 {retry_delay} 秒后重连抖音源...")
|
print(f"[WARN] 等待 {retry_delay} 秒后重连抖音源...")
|
||||||
time.sleep(retry_delay)
|
time.sleep(retry_delay)
|
||||||
retry_delay = min(retry_delay * 2, MAX_RETRY_DELAY)
|
retry_delay = min(retry_delay * 2, MAX_RETRY_DELAY)
|
||||||
|
|
||||||
# 清理旧 FFmpeg
|
# 清理旧 FFmpeg
|
||||||
cleanup_proc(proc)
|
cleanup_proc(proc)
|
||||||
|
|
||||||
print(f"[INFO][{datetime.datetime.now():%H:%M:%S}] 启动 FFmpeg 拉流 → FIFO")
|
print(f"[INFO][{datetime.datetime.now():%H:%M:%S}] 启动 FFmpeg 拉流 → FIFO")
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
ffmpeg_command,
|
ffmpeg_command,
|
||||||
@@ -1829,15 +1820,12 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
universal_newlines=True
|
universal_newlines=True
|
||||||
)
|
)
|
||||||
proc.last_out_ts = time.time()
|
proc.last_out_ts = time.time()
|
||||||
|
|
||||||
stderr_q = queue.Queue()
|
stderr_q = queue.Queue()
|
||||||
reader_t = threading.Thread(target=_stderr_reader_thread, args=(proc, stderr_q))
|
reader_t = threading.Thread(target=_stderr_reader_thread, args=(proc, stderr_q))
|
||||||
reader_t.daemon = True
|
reader_t.daemon = True
|
||||||
reader_t.start()
|
reader_t.start()
|
||||||
|
|
||||||
start_time = last_frame_time = time.time()
|
start_time = last_frame_time = time.time()
|
||||||
last_buffer_warning_time = 0
|
last_buffer_warning_time = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
line = stderr_q.get(timeout=STDERR_QUEUE_TIMEOUT)
|
line = stderr_q.get(timeout=STDERR_QUEUE_TIMEOUT)
|
||||||
@@ -1846,19 +1834,15 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
print("[WARN] FFmpeg 长时间无输出,强制重启 FFmpeg")
|
print("[WARN] FFmpeg 长时间无输出,强制重启 FFmpeg")
|
||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line is None:
|
if line is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line.startswith("__ERR__READER__"):
|
if line.startswith("__ERR__READER__"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line:
|
if line:
|
||||||
proc.last_out_ts = time.time()
|
proc.last_out_ts = time.time()
|
||||||
print(f"[FFmpeg] {line}")
|
print(f"[FFmpeg] {line}")
|
||||||
line_lower = line.lower()
|
line_lower = line.lower()
|
||||||
|
|
||||||
# 关键:检测 mpv 卡死(buffers queued 1000)
|
# 关键:检测 mpv 卡死(buffers queued 1000)
|
||||||
if "1000 buffers queued" in line_lower:
|
if "1000 buffers queued" in line_lower:
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
@@ -1867,35 +1851,28 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
last_buffer_warning_time = current_time
|
last_buffer_warning_time = current_time
|
||||||
if mpv_proc and mpv_proc.poll() is None:
|
if mpv_proc and mpv_proc.poll() is None:
|
||||||
mpv_proc.kill()
|
mpv_proc.kill()
|
||||||
mpv_proc = None # 外层循环会重新启动
|
mpv_proc = None # 外层循环会重新启动
|
||||||
break # 退出内循环,触发重启
|
break # 退出内循环,触发重启
|
||||||
|
|
||||||
# 真实下播检测
|
# 真实下播检测
|
||||||
if any(kw in line_lower for kw in END_KEYWORDS):
|
if any(kw in line_lower for kw in END_KEYWORDS):
|
||||||
print("[INFO] 检测到主播下播关键字,停止转播")
|
print("[INFO] 检测到主播下播关键字,停止转播")
|
||||||
raise StreamEnded()
|
raise StreamEnded()
|
||||||
|
|
||||||
# 有新帧到来
|
# 有新帧到来
|
||||||
if re.search(r'frame=\s*\d+', line_lower):
|
if re.search(r'frame=\s*\d+', line_lower):
|
||||||
last_frame_time = time.time()
|
last_frame_time = time.time()
|
||||||
retry_delay = INITIAL_RETRY_DELAY
|
retry_delay = INITIAL_RETRY_DELAY
|
||||||
|
|
||||||
# 无帧超时下播判定
|
# 无帧超时下播判定
|
||||||
if time.time() - start_time > START_CHECK_AFTER:
|
if time.time() - start_time > START_CHECK_AFTER:
|
||||||
if time.time() - last_frame_time > NO_FRAME_TIMEOUT:
|
if time.time() - last_frame_time > NO_FRAME_TIMEOUT:
|
||||||
print(f"[INFO] {NO_FRAME_TIMEOUT}s 无新帧,判定主播已下播")
|
print(f"[INFO] {NO_FRAME_TIMEOUT}s 无新帧,判定主播已下播")
|
||||||
raise StreamEnded()
|
raise StreamEnded()
|
||||||
|
|
||||||
except StreamEnded:
|
except StreamEnded:
|
||||||
print(f"[INFO] 主播已下播,停止 HDMI 转播 → {record_name}")
|
print(f"[INFO] 主播已下播,停止 HDMI 转播 → {record_name}")
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("[INFO] 用户手动中断 (Ctrl+C)")
|
print("[INFO] 用户手动中断 (Ctrl+C)")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[FATAL] HDMI 转播异常: {e}")
|
print(f"[FATAL] HDMI 转播异常: {e}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
cleanup_proc(proc)
|
cleanup_proc(proc)
|
||||||
if mpv_proc and mpv_proc.poll() is None:
|
if mpv_proc and mpv_proc.poll() is None:
|
||||||
@@ -1905,7 +1882,6 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
mpv_proc.wait(timeout=5)
|
mpv_proc.wait(timeout=5)
|
||||||
except:
|
except:
|
||||||
mpv_proc.kill()
|
mpv_proc.kill()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
recording.discard(record_name)
|
recording.discard(record_name)
|
||||||
recording_time_list.pop(record_name, None)
|
recording_time_list.pop(record_name, None)
|
||||||
@@ -1916,15 +1892,15 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
|
|||||||
clear_record_info(record_name, record_url)
|
clear_record_info(record_name, record_url)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
color_obj.print_colored(f"[{record_name}] HDMI 转播已彻底停止并清理完毕\n", color_obj.GREEN)
|
color_obj.print_colored(f"[{record_name}] HDMI 转播已彻底停止并清理完毕\n", color_obj.GREEN)
|
||||||
|
|
||||||
count_time = time.time()
|
count_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
|
logger.error(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
|
||||||
with max_request_lock:
|
with max_request_lock:
|
||||||
|
|||||||
Reference in New Issue
Block a user