's'
This commit is contained in:
26
live.py
26
live.py
@@ -19,7 +19,7 @@ class AdaptiveHLS2YouTube:
|
||||
self.delay_sec = delay_sec
|
||||
self.segment_time = segment_time
|
||||
self.check_interval = check_interval
|
||||
self.buffer_queue = queue.Queue()
|
||||
self.buffer_queue = queue.Queue(maxsize=500) # 增大队列,防止阻塞
|
||||
self.max_bitrate = max_bitrate
|
||||
self.min_bitrate = min_bitrate
|
||||
self.audio_bitrate = audio_bitrate
|
||||
@@ -53,11 +53,14 @@ class AdaptiveHLS2YouTube:
|
||||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||||
print("[拉流] FFmpeg 启动成功")
|
||||
while not self.stop_flag.is_set():
|
||||
data = proc.stdout.read(188*500)
|
||||
data = proc.stdout.read(188*2000) # 大块读取,减少空跑
|
||||
if not data:
|
||||
time.sleep(0.05)
|
||||
continue
|
||||
self.buffer_queue.put(data)
|
||||
try:
|
||||
self.buffer_queue.put_nowait(data) # 非阻塞写入队列
|
||||
except queue.Full:
|
||||
continue # 队列满直接丢掉,保证不卡
|
||||
except Exception as e:
|
||||
print(f"拉流异常: {e}")
|
||||
|
||||
@@ -81,7 +84,7 @@ class AdaptiveHLS2YouTube:
|
||||
"-bufsize", f"{self.current_bitrate*4}k",
|
||||
"-r", "30",
|
||||
"-g", "60",
|
||||
# 不固定分辨率,保持抖音原始分辨率
|
||||
"-s", "720x1280",
|
||||
"-c:a", "aac",
|
||||
"-b:a", f"{self.audio_bitrate}k",
|
||||
"-ar", "44100",
|
||||
@@ -103,9 +106,12 @@ class AdaptiveHLS2YouTube:
|
||||
|
||||
while not self.stop_flag.is_set():
|
||||
try:
|
||||
data = self.buffer_queue.get(timeout=1)
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.flush()
|
||||
data = self.buffer_queue.get(timeout=0.5)
|
||||
try:
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.flush()
|
||||
except (BrokenPipeError, OSError):
|
||||
break # 管道断开,退出推流循环
|
||||
except queue.Empty:
|
||||
continue
|
||||
except Exception as e:
|
||||
@@ -123,7 +129,7 @@ class AdaptiveHLS2YouTube:
|
||||
if new_bitrate != self.current_bitrate:
|
||||
print(f"[ABR] 调整码率: {self.current_bitrate}k -> {new_bitrate}k")
|
||||
self.current_bitrate = new_bitrate
|
||||
time.sleep(self.check_interval)
|
||||
time.sleep(max(self.check_interval, 10)) # 降低 ABR 更新频率,避免频繁干扰
|
||||
|
||||
def stop_all(self):
|
||||
print("停止所有推流...")
|
||||
@@ -177,9 +183,9 @@ def monitor_panel_thread(hls_instance, interval=2, max_width=50):
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
# ----------------- 主程序 -----------------
|
||||
# ----------------- 示例 -----------------
|
||||
if __name__ == "__main__":
|
||||
DOUYIN_URL = "https://live.douyin.com/258299812685"
|
||||
DOUYIN_URL = "https://live.douyin.com/125742860435"
|
||||
YOUTUBE_KEY = "qxvb-r47b-r5ju-6ud3-6k7z"
|
||||
|
||||
hls_instance = start_douyin_to_youtube(DOUYIN_URL, YOUTUBE_KEY, delay_sec=40)
|
||||
|
||||
Reference in New Issue
Block a user