's''
This commit is contained in:
52
live.py
52
live.py
@@ -12,7 +12,7 @@ import spider
|
||||
import stream
|
||||
|
||||
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):
|
||||
self.processes = {}
|
||||
self.stop_flag = threading.Event()
|
||||
@@ -49,37 +49,20 @@ class AdaptiveHLS2YouTube:
|
||||
"-f", "mpegts",
|
||||
"-"
|
||||
]
|
||||
|
||||
while not self.stop_flag.is_set():
|
||||
try:
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||||
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():
|
||||
data = proc.stdout.read(188*20)
|
||||
if data:
|
||||
data = proc.stdout.read(188*50)
|
||||
if not data:
|
||||
time.sleep(0.05)
|
||||
continue
|
||||
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:
|
||||
print(f"[拉流] 异常: {e}, 5 秒后重试")
|
||||
time.sleep(5)
|
||||
print(f"拉流异常: {e}")
|
||||
|
||||
def _push_thread(self, youtube_key, room_name):
|
||||
min_buffer = max(1, self.delay_sec // self.segment_time)
|
||||
|
||||
while not self.stop_flag.is_set():
|
||||
if self.buffer_queue.qsize() < min_buffer:
|
||||
time.sleep(0.05)
|
||||
@@ -95,7 +78,7 @@ class AdaptiveHLS2YouTube:
|
||||
"-tune", "zerolatency",
|
||||
"-b:v", 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",
|
||||
"-g", "60",
|
||||
"-c:a", "aac",
|
||||
@@ -106,7 +89,6 @@ class AdaptiveHLS2YouTube:
|
||||
"-rtmp_live", "live",
|
||||
f"rtmp://a.rtmp.youtube.com/live2/{youtube_key}"
|
||||
]
|
||||
|
||||
try:
|
||||
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
self.processes[room_name] = proc
|
||||
@@ -118,33 +100,23 @@ class AdaptiveHLS2YouTube:
|
||||
print(f"[{room_name} FFmpeg]: {line.decode(errors='ignore').strip()}")
|
||||
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():
|
||||
data = self.buffer_queue.get()
|
||||
try:
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.flush()
|
||||
except BrokenPipeError:
|
||||
print(f"[{room_name}] FFmpeg stdin 已断开,重启推流")
|
||||
break
|
||||
else:
|
||||
time.sleep(0.01)
|
||||
|
||||
if proc.poll() is not None:
|
||||
print(f"[{room_name}] 推流 FFmpeg 退出,5 秒后重启...")
|
||||
time.sleep(5)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[{room_name}] 推流异常: {e}, 5 秒后重试")
|
||||
time.sleep(5)
|
||||
print(f"[{room_name}] 推流异常: {e}")
|
||||
|
||||
def _abr_thread(self):
|
||||
while not self.stop_flag.is_set():
|
||||
queue_len = self.buffer_queue.qsize()
|
||||
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:
|
||||
new_bitrate = min(self.max_bitrate, self.current_bitrate + 500)
|
||||
new_bitrate = min(self.max_bitrate, self.current_bitrate + 300)
|
||||
else:
|
||||
new_bitrate = self.current_bitrate
|
||||
if new_bitrate != self.current_bitrate:
|
||||
|
||||
Reference in New Issue
Block a user