From 587c0e10b41b0a96a400c8c0a7dc93d9ddb89ad2 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 19 Mar 2026 23:32:57 -0500 Subject: [PATCH] 's' --- config/youtube.ini | 5 ++- douyin_youtube_ffplay.py | 95 ++++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/config/youtube.ini b/config/youtube.ini index c3e43c6..819e905 100644 --- a/config/youtube.ini +++ b/config/youtube.ini @@ -7,4 +7,7 @@ key = qxvb-r47b-r5ju-6ud3-6k7z ; rtmps = 否 # 可选:视频比特率 kbps,YouTube 推荐 2500,可填 3000-4500 提高画质 -; bitrate = 2500 \ No newline at end of file +; bitrate = 2500 + +# 可选:软编 speed 长期小于 1 时开启——轻量音频链(无 arnndn/长 EQ),减轻 CPU。填 1 或 是 +fast_audio = 1 \ No newline at end of file diff --git a/douyin_youtube_ffplay.py b/douyin_youtube_ffplay.py index 24ac200..6a655df 100644 --- a/douyin_youtube_ffplay.py +++ b/douyin_youtube_ffplay.py @@ -84,8 +84,10 @@ def start_douyin_youtube_ffplay( rtmps_base = "rtmps://a.rtmp.youtube.com/live2/" rtmp_base = "rtmp://a.rtmp.youtube.com/live2/" use_rtmps = True - b_v, maxrate, bufsize = "2500k", "3000k", "5000k" + # 720p 竖屏 YouTube 建议 4–6 Mbps,偏低易触发「数据不足/卡顿」提示 + b_v, maxrate, bufsize = "4000k", "4500k", "8000k" stream_key = None + fast_audio = False # config: fast_audio=1 可跳过 arnndn+长 EQ,显著减轻 CPU if os.path.exists(config_file): config = configparser.ConfigParser() @@ -95,6 +97,8 @@ def start_douyin_youtube_ffplay( v = (v or "").strip() if k == "rtmps" and v.lower() in ("0", "false", "否"): use_rtmps = False + elif k == "fast_audio" and v.lower() in ("1", "true", "yes", "是", "开", "on"): + fast_audio = True elif k == "bitrate" and v: try: n = int(v) @@ -155,7 +159,8 @@ def start_douyin_youtube_ffplay( # ====================== NVENC 检测(符合 YouTube 推荐比特率)====================== codec_v = "libx264" - video_args = ["-preset", "fast", "-profile:v", "high", "-level", "4.0", + # ultrafast+zerolatency:在软编下尽量让 speed≥1.0;画质略降、直播可接受 + video_args = ["-preset", "ultrafast", "-tune", "zerolatency", "-profile:v", "high", "-level", "4.0", "-b:v", b_v, "-maxrate", maxrate, "-bufsize", bufsize] try: if platform.system().lower() in ["windows", "linux"]: @@ -172,65 +177,71 @@ def start_douyin_youtube_ffplay( print(f"[WARN] NVENC 检测失败,回退软件编码: {e}") # ====================== 开源音频处理:背景音乐弱化 + 防版权 ====================== - # 优先 RNNoise(arnndn),无模型则用 afftdn;aphaser 相位扰动可干扰 Content ID 指纹 _script_dir = os.path.dirname(os.path.abspath(__file__)) - _arnndn_paths = [ - os.path.join(_script_dir, "arnndn-models", "cb.rnnn"), - os.path.join(os.path.dirname(_script_dir), "arnndn-models", "cb.rnnn"), - "arnndn-models/cb.rnnn", - ] - _arnndn_model = None - for _p in _arnndn_paths: - if os.path.isfile(_p): - _arnndn_model = os.path.normpath(_p) - break _ffmpeg_cwd = None - if _arnndn_model: - print(f"[INFO] 使用 RNNoise 降噪模型: {_arnndn_model}") - # 跨平台:用相对路径避免 Windows 下 C: 冒号破坏 FFmpeg 滤镜解析;FFmpeg 以模型所在父目录为 cwd - _arnndn_base = os.path.dirname(os.path.dirname(os.path.abspath(_arnndn_model))) - _af_denoise = "arnndn=m=arnndn-models/cb.rnnn:mix=0.82," - _ffmpeg_cwd = _arnndn_base + if fast_audio: + print("[INFO] fast_audio=开:轻量音频链(无 arnndn/长 EQ),减轻 CPU 以利 speed≥1.0") + _af_chain = "afftdn=nf=-26:tn=1,aresample=async=1:first_pts=0" else: - print("[INFO] 未找到 arnndn 模型,使用 afftdn 降噪(可克隆 richardpl/arnndn-models 到 arnndn-models/)") - _af_denoise = "afftdn=nf=-26:tn=1," + # 优先 RNNoise(arnndn),无模型则用 afftdn;aphaser 相位扰动可干扰 Content ID 指纹 + _arnndn_paths = [ + os.path.join(_script_dir, "arnndn-models", "cb.rnnn"), + os.path.join(os.path.dirname(_script_dir), "arnndn-models", "cb.rnnn"), + "arnndn-models/cb.rnnn", + ] + _arnndn_model = None + for _p in _arnndn_paths: + if os.path.isfile(_p): + _arnndn_model = os.path.normpath(_p) + break + if _arnndn_model: + print(f"[INFO] 使用 RNNoise 降噪模型: {_arnndn_model}") + _arnndn_base = os.path.dirname(os.path.dirname(os.path.abspath(_arnndn_model))) + _af_denoise = "arnndn=m=arnndn-models/cb.rnnn:mix=0.82," + _ffmpeg_cwd = _arnndn_base + else: + print("[INFO] 未找到 arnndn 模型,使用 afftdn 降噪(可克隆 richardpl/arnndn-models 到 arnndn-models/)") + _af_denoise = "afftdn=nf=-26:tn=1," - # 强化版:收紧带通 + 频率偏移 + 音高微调 + 多段EQ + 相位扰动(纯 FFmpeg 理论上限 ~70%) - _af_chain = ( - _af_denoise - + "highpass=f=200,lowpass=f=3800," # 收紧人声区 200-3800Hz,削弱更多音乐 - + "equalizer=f=150:width_type=o:width=2.5:g=-14," # 强衰减低频 - + "equalizer=f=350:width_type=o:width=2:g=-10," - + "equalizer=f=600:width_type=o:width=1.8:g=-8," - + "equalizer=f=1000:width_type=o:width=1.5:g=-6," - + "equalizer=f=2500:width_type=o:width=1.2:g=-4," # 多段 EQ 破坏音乐结构 - + "acompressor=threshold=-26dB:ratio=5:attack=8:release=80:makeup=5," - + "afreqshift=shift=22," # 频率整体偏移,破坏指纹 - + "asetrate=48000*1.035,aresample=48000," # 音高微升 3.5% - + "aphaser=type=t:decay=0.4:delay=3:speed=0.7," # 加强相位扰动 - + "volume=1.12," - + "aresample=async=1:first_pts=0" - ) + # 强化版:收紧带通 + 频率偏移 + 音高微调 + 多段EQ + 相位扰动(纯 FFmpeg 理论上限 ~70%) + _af_chain = ( + _af_denoise + + "highpass=f=200,lowpass=f=3800," + + "equalizer=f=150:width_type=o:width=2.5:g=-14," + + "equalizer=f=350:width_type=o:width=2:g=-10," + + "equalizer=f=600:width_type=o:width=1.8:g=-8," + + "equalizer=f=1000:width_type=o:width=1.5:g=-6," + + "equalizer=f=2500:width_type=o:width=1.2:g=-4," + + "acompressor=threshold=-26dB:ratio=5:attack=8:release=80:makeup=5," + + "afreqshift=shift=22," + + "asetrate=48000*1.035,aresample=48000," + + "aphaser=type=t:decay=0.4:delay=3:speed=0.7," + + "volume=1.12," + + "aresample=async=1:first_pts=0" + ) # 720p 竖屏,符合 YouTube 推荐:4–6 Mbps、2s 关键帧、AAC 128k _video_mid = ["-vsync", "cfr", "-g", "60", "-keyint_min", "60", "-r", "30", "-bf", "0", "-sc_threshold", "0"] if codec_v == "libx264": _video_mid += ["-x264-params", "force-cfr=1:scenecut=0:open_gop=0:sync-lookahead=0"] + # -re:按源时间戳节奏拉流,避免突发过快;不用 realtime/arealtime(输入抖动时易让 YouTube 判为数据不足) ffmpeg_command = [ - "ffmpeg", "-y", "-loglevel", "info", "-nostdin", "-re", + "ffmpeg", "-y", "-loglevel", "info", "-nostdin", "-stats", "-stats_period", "1", "-rw_timeout", "50000000", "-reconnect", "1", "-reconnect_at_eof", "1", "-reconnect_streamed", "1", "-reconnect_delay_max", str(RECONNECT_DELAY_MAX), - "-fflags", "+genpts+discardcorrupt+nobuffer+flush_packets", - "-flags", "low_delay", + "-fflags", "+genpts+discardcorrupt", "-err_detect", "ignore_err", - "-max_delay", "80000", + "-max_delay", "500000", "-thread_queue_size", "2048", + "-filter_threads", "4", "-headers", douyin_headers, + "-re", "-i", real_url, - "-vf", "fps=30,scale=720:1280:force_original_aspect_ratio=decrease:flags=lanczos,pad=720:1280:(ow-iw)/2:(oh-ih)/2:black", + # bilinear 比 lanczos 轻很多,利于 speed 稳定在 ≥1.0x + "-vf", "fps=30,scale=720:1280:force_original_aspect_ratio=decrease:flags=bilinear,pad=720:1280:(ow-iw)/2:(oh-ih)/2:black", "-c:v", codec_v, *video_args, *_video_mid,