Compare commits

1 Commits
main ... dev

Author SHA1 Message Date
eric
e3a56743fa 's' 2025-09-18 23:23:15 +08:00

22
live.py
View File

@@ -23,7 +23,7 @@ IS_WINDOWS = platform.system() == "Windows"
# ----------------- Douyin Recorder ----------------- # ----------------- Douyin Recorder -----------------
class DouyinRecorder: class DouyinRecorder:
def __init__(self, audio_bitrate=128, max_minutes=8): def __init__(self, audio_bitrate=128, max_minutes=8, loop_record=False):
self.record_proc = None self.record_proc = None
self.stop_flag = threading.Event() self.stop_flag = threading.Event()
self.audio_bitrate = audio_bitrate self.audio_bitrate = audio_bitrate
@@ -32,6 +32,7 @@ class DouyinRecorder:
self.out_file = None self.out_file = None
self.temp_file = None self.temp_file = None
self.max_minutes = max_minutes self.max_minutes = max_minutes
self.loop_record = loop_record # <-- 新增循环开关
os.makedirs("records", exist_ok=True) os.makedirs("records", exist_ok=True)
# ---- 构建 FFmpeg 命令 ---- # ---- 构建 FFmpeg 命令 ----
@@ -85,8 +86,10 @@ class DouyinRecorder:
threading.Thread(target=monitor_proc, args=(self.record_proc,), daemon=True).start() threading.Thread(target=monitor_proc, args=(self.record_proc,), daemon=True).start()
# ---- 录制循环 ---- # ---- 录制循环 ----
def _run_record_loop(self, real_url): def _run_record_loop(self, real_url):
first_run = True
while not self.stop_flag.is_set(): while not self.stop_flag.is_set():
try: try:
self.start_time = time.time() self.start_time = time.time()
@@ -94,11 +97,23 @@ class DouyinRecorder:
while not self.stop_flag.is_set(): while not self.stop_flag.is_set():
elapsed = time.time() - self.start_time elapsed = time.time() - self.start_time
if elapsed >= self.max_minutes * 60:
# 如果循环开关关闭,则只录一次,达到 max_minutes 就停止
if not self.loop_record and elapsed >= self.max_minutes * 60:
logging.info(f"[{self.room_name}] 达到 {self.max_minutes} 分钟,录制完成(循环关闭)")
self.stop_flag.set()
# break
self.stop()
# ⚠️ 这里自动退出整个程序
os._exit(0)
# 如果循环开启,达到 max_minutes 就切分录制
if self.loop_record and elapsed >= self.max_minutes * 60:
logging.info(f"[{self.room_name}] 达到 {self.max_minutes} 分钟,切分录制段") logging.info(f"[{self.room_name}] 达到 {self.max_minutes} 分钟,切分录制段")
self._finalize_recording() self._finalize_recording()
self.start_time = time.time() self.start_time = time.time()
self._start_new_record(real_url) self._start_new_record(real_url)
if self.record_proc.poll() is not None: if self.record_proc.poll() is not None:
logging.warning(f"[{self.room_name}] FFmpeg 进程退出1秒后重启") logging.warning(f"[{self.room_name}] FFmpeg 进程退出1秒后重启")
time.sleep(1) time.sleep(1)
@@ -107,7 +122,6 @@ class DouyinRecorder:
except Exception as e: except Exception as e:
logging.error(f"[{self.room_name}] FFmpeg异常: {e}") logging.error(f"[{self.room_name}] FFmpeg异常: {e}")
time.sleep(1) time.sleep(1)
# ---- 停止 FFmpeg ---- # ---- 停止 FFmpeg ----
def _terminate_proc(self): def _terminate_proc(self):
if self.record_proc and self.record_proc.poll() is None: if self.record_proc and self.record_proc.poll() is None:
@@ -207,7 +221,7 @@ def start_douyin_record(record_url, room_name=None, proxy_addr=None, cookies='',
# ----------------- Main ----------------- # ----------------- Main -----------------
if __name__ == "__main__": if __name__ == "__main__":
DOUYIN_URL = "https://live.douyin.com/846050151095" DOUYIN_URL = "https://live.douyin.com/163813589919"
recorder_instance = start_douyin_record(DOUYIN_URL) recorder_instance = start_douyin_record(DOUYIN_URL)
if not recorder_instance: if not recorder_instance:
logging.error("录制实例创建失败,程序退出") logging.error("录制实例创建失败,程序退出")