's'
This commit is contained in:
36
live.py
36
live.py
@@ -33,6 +33,9 @@ class AdaptiveHLS2YouTube:
|
|||||||
threading.Thread(target=self._abr_thread, daemon=True).start()
|
threading.Thread(target=self._abr_thread, daemon=True).start()
|
||||||
|
|
||||||
def _pull_thread(self, real_url):
|
def _pull_thread(self, real_url):
|
||||||
|
max_retry = 3 # 最大重试次数
|
||||||
|
retry_count = 0
|
||||||
|
while not self.stop_flag.is_set():
|
||||||
cmd = [
|
cmd = [
|
||||||
"ffmpeg",
|
"ffmpeg",
|
||||||
"-rw_timeout", "50000000",
|
"-rw_timeout", "50000000",
|
||||||
@@ -49,20 +52,41 @@ class AdaptiveHLS2YouTube:
|
|||||||
"-f", "mpegts",
|
"-f", "mpegts",
|
||||||
"-"
|
"-"
|
||||||
]
|
]
|
||||||
|
proc = None
|
||||||
try:
|
try:
|
||||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||||||
print("[拉流] FFmpeg 启动成功")
|
print("[拉流] FFmpeg 启动成功")
|
||||||
|
retry_count = 0 # 成功拉流重置重试计数
|
||||||
while not self.stop_flag.is_set():
|
while not self.stop_flag.is_set():
|
||||||
data = proc.stdout.read(188*2000) # 大块读取,减少空跑
|
data = proc.stdout.read(188*2000) # 大块读取
|
||||||
if not data:
|
if not data:
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
self.buffer_queue.put_nowait(data) # 非阻塞写入队列
|
self.buffer_queue.put_nowait(data)
|
||||||
except queue.Full:
|
except queue.Full:
|
||||||
continue # 队列满直接丢掉,保证不卡
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"拉流异常: {e}")
|
print(f"拉流异常: {e}")
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
if proc and proc.poll() is None:
|
||||||
|
proc.kill()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 轮训逻辑:直播未开播时重试
|
||||||
|
retry_count += 1
|
||||||
|
if retry_count >= max_retry:
|
||||||
|
print(f"[拉流] 连续 {max_retry} 次没有拉到流,停止推流")
|
||||||
|
self.stop_all()
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(f"[拉流] 等待 10 秒后重试 ({retry_count}/{max_retry})...")
|
||||||
|
for _ in range(6): # 每分钟 6 次,10 秒/次
|
||||||
|
if self.stop_flag.is_set():
|
||||||
|
break
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
def _push_thread(self, youtube_key, room_name):
|
def _push_thread(self, youtube_key, room_name):
|
||||||
min_buffer = max(1, self.delay_sec // self.segment_time)
|
min_buffer = max(1, self.delay_sec // self.segment_time)
|
||||||
@@ -111,7 +135,7 @@ class AdaptiveHLS2YouTube:
|
|||||||
proc.stdin.write(data)
|
proc.stdin.write(data)
|
||||||
proc.stdin.flush()
|
proc.stdin.flush()
|
||||||
except (BrokenPipeError, OSError):
|
except (BrokenPipeError, OSError):
|
||||||
break # 管道断开,退出推流循环
|
break
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
continue
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -129,7 +153,7 @@ class AdaptiveHLS2YouTube:
|
|||||||
if new_bitrate != self.current_bitrate:
|
if new_bitrate != self.current_bitrate:
|
||||||
print(f"[ABR] 调整码率: {self.current_bitrate}k -> {new_bitrate}k")
|
print(f"[ABR] 调整码率: {self.current_bitrate}k -> {new_bitrate}k")
|
||||||
self.current_bitrate = new_bitrate
|
self.current_bitrate = new_bitrate
|
||||||
time.sleep(max(self.check_interval, 10)) # 降低 ABR 更新频率,避免频繁干扰
|
time.sleep(max(self.check_interval, 10))
|
||||||
|
|
||||||
def stop_all(self):
|
def stop_all(self):
|
||||||
print("停止所有推流...")
|
print("停止所有推流...")
|
||||||
@@ -185,7 +209,7 @@ def monitor_panel_thread(hls_instance, interval=2, max_width=50):
|
|||||||
|
|
||||||
# ----------------- 示例 -----------------
|
# ----------------- 示例 -----------------
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
DOUYIN_URL = "https://live.douyin.com/525514386431"
|
DOUYIN_URL = "https://live.douyin.com/722725928418"
|
||||||
YOUTUBE_KEY = "qxvb-r47b-r5ju-6ud3-6k7z"
|
YOUTUBE_KEY = "qxvb-r47b-r5ju-6ud3-6k7z"
|
||||||
|
|
||||||
hls_instance = start_douyin_to_youtube(DOUYIN_URL, YOUTUBE_KEY, delay_sec=40)
|
hls_instance = start_douyin_to_youtube(DOUYIN_URL, YOUTUBE_KEY, delay_sec=40)
|
||||||
|
|||||||
Reference in New Issue
Block a user