This commit is contained in:
eric
2025-09-13 10:21:42 +08:00
parent 64abdc03be
commit 88c53c37ff

52
live.py
View File

@@ -12,7 +12,7 @@ import spider
import stream import stream
class AdaptiveHLS2YouTube: class AdaptiveHLS2YouTube:
def __init__(self, max_bitrate=6000, min_bitrate=3000, audio_bitrate=192, def __init__(self, max_bitrate=2500, min_bitrate=1500, audio_bitrate=128,
delay_sec=30, segment_time=2, check_interval=5): delay_sec=30, segment_time=2, check_interval=5):
self.processes = {} self.processes = {}
self.stop_flag = threading.Event() self.stop_flag = threading.Event()
@@ -49,37 +49,20 @@ class AdaptiveHLS2YouTube:
"-f", "mpegts", "-f", "mpegts",
"-" "-"
] ]
while not self.stop_flag.is_set():
try: try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
print("[拉流] FFmpeg 启动成功") print("[拉流] FFmpeg 启动成功")
def read_stderr():
while proc.poll() is None:
line = proc.stderr.readline()
if line:
print(f"[拉流 FFmpeg]: {line.decode(errors='ignore').strip()}")
threading.Thread(target=read_stderr, daemon=True).start()
while not self.stop_flag.is_set(): while not self.stop_flag.is_set():
data = proc.stdout.read(188*20) data = proc.stdout.read(188*50)
if data: if not data:
time.sleep(0.05)
continue
self.buffer_queue.put(data) self.buffer_queue.put(data)
else:
time.sleep(0.01)
if proc.poll() is not None:
print("[拉流] FFmpeg 退出5 秒后重启...")
time.sleep(5)
except Exception as e: except Exception as e:
print(f"[拉流] 异常: {e}, 5 秒后重试") print(f"拉流异常: {e}")
time.sleep(5)
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)
while not self.stop_flag.is_set(): while not self.stop_flag.is_set():
if self.buffer_queue.qsize() < min_buffer: if self.buffer_queue.qsize() < min_buffer:
time.sleep(0.05) time.sleep(0.05)
@@ -95,7 +78,7 @@ class AdaptiveHLS2YouTube:
"-tune", "zerolatency", "-tune", "zerolatency",
"-b:v", f"{self.current_bitrate}k", "-b:v", f"{self.current_bitrate}k",
"-maxrate", f"{self.current_bitrate}k", "-maxrate", f"{self.current_bitrate}k",
"-bufsize", f"{self.current_bitrate*4}k", "-bufsize", f"{self.current_bitrate*2}k",
"-r", "30", "-r", "30",
"-g", "60", "-g", "60",
"-c:a", "aac", "-c:a", "aac",
@@ -106,7 +89,6 @@ class AdaptiveHLS2YouTube:
"-rtmp_live", "live", "-rtmp_live", "live",
f"rtmp://a.rtmp.youtube.com/live2/{youtube_key}" f"rtmp://a.rtmp.youtube.com/live2/{youtube_key}"
] ]
try: try:
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE) proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
self.processes[room_name] = proc self.processes[room_name] = proc
@@ -118,33 +100,23 @@ class AdaptiveHLS2YouTube:
print(f"[{room_name} FFmpeg]: {line.decode(errors='ignore').strip()}") print(f"[{room_name} FFmpeg]: {line.decode(errors='ignore').strip()}")
threading.Thread(target=read_stderr, daemon=True).start() threading.Thread(target=read_stderr, daemon=True).start()
while not self.stop_flag.is_set() and proc.poll() is None: while not self.stop_flag.is_set():
if not self.buffer_queue.empty(): if not self.buffer_queue.empty():
data = self.buffer_queue.get() data = self.buffer_queue.get()
try:
proc.stdin.write(data) proc.stdin.write(data)
proc.stdin.flush() proc.stdin.flush()
except BrokenPipeError:
print(f"[{room_name}] FFmpeg stdin 已断开,重启推流")
break
else: else:
time.sleep(0.01) time.sleep(0.01)
if proc.poll() is not None:
print(f"[{room_name}] 推流 FFmpeg 退出5 秒后重启...")
time.sleep(5)
except Exception as e: except Exception as e:
print(f"[{room_name}] 推流异常: {e}, 5 秒后重试") print(f"[{room_name}] 推流异常: {e}")
time.sleep(5)
def _abr_thread(self): def _abr_thread(self):
while not self.stop_flag.is_set(): while not self.stop_flag.is_set():
queue_len = self.buffer_queue.qsize() queue_len = self.buffer_queue.qsize()
if queue_len > 100: if queue_len > 100:
new_bitrate = max(self.min_bitrate, self.current_bitrate - 500) new_bitrate = max(self.min_bitrate, self.current_bitrate - 300)
elif queue_len < 30: elif queue_len < 30:
new_bitrate = min(self.max_bitrate, self.current_bitrate + 500) new_bitrate = min(self.max_bitrate, self.current_bitrate + 300)
else: else:
new_bitrate = self.current_bitrate new_bitrate = self.current_bitrate
if new_bitrate != self.current_bitrate: if new_bitrate != self.current_bitrate: