# -*- coding: utf-8 -*- """YouTube live relay encoding presets — switchable; 720p keeps the original profile.""" from __future__ import annotations from typing import Any DEFAULT_RELAY_QUALITY_PRESET = "vps_ultra" VPS_4C_RELAY_QUALITY_PRESET = "vps_ultra" # YouTube 侧更在意:稳定帧率、不卡顿、足够观看时长;分辨率低于 720p 仍可正常推荐, # 但 speed<1 的「假 720p」比稳定的 480p/540p 更伤体验与留存。 RELAY_QUALITY_PRESETS: dict[str, dict[str, Any]] = { "720p": { "id": "720p", "label": "720p 标准", "description": "720×1280 · 30fps · veryfast · 码率跟随设置(默认 2500k)", "width": 720, "height": 1280, "fps": 30, "x264_preset": "veryfast", "bitrate_kbps": None, "audio_bitrate_kbps": 128, "gop": 60, "profile": "high", "level": "4.0", "x264_threads": 0, "scale_flags": "bilinear", "recommended_for_vps": False, }, "vps_ultra": { "id": "vps_ultra", "label": "VPS Ultra(4核专供)", "description": "480×854 · 24fps · ultrafast · 1500k · 限 2 线程(稳定优先,省 CPU)", "width": 480, "height": 854, "fps": 24, "x264_preset": "ultrafast", "bitrate_kbps": 1500, # YouTube Live Studio warns if audio < 128 Kbps (shows as 0 when under-delivered). "audio_bitrate_kbps": 128, "gop": 48, "profile": "main", "level": "3.1", "x264_threads": 2, "scale_flags": "fast_bilinear", "recommended_for_vps": True, }, } def list_relay_quality_presets(*, active_id: str = "") -> list[dict[str, Any]]: aid = str(active_id or DEFAULT_RELAY_QUALITY_PRESET).strip() or DEFAULT_RELAY_QUALITY_PRESET out: list[dict[str, Any]] = [] for pid, row in RELAY_QUALITY_PRESETS.items(): out.append( { "id": pid, "label": row["label"], "description": row["description"], "width": row["width"], "height": row["height"], "fps": row["fps"], "x264_preset": row["x264_preset"], "bitrate_kbps": row.get("bitrate_kbps"), "audio_bitrate_kbps": row.get("audio_bitrate_kbps"), "recommended_for_vps": bool(row.get("recommended_for_vps")), "active": pid == aid, } ) return out def resolve_relay_encoding(cfg: dict[str, Any]) -> dict[str, Any]: """Merge DB settings with the selected quality preset.""" preset_id = str(cfg.get("youtube_relay_quality_preset") or DEFAULT_RELAY_QUALITY_PRESET).strip() if preset_id not in RELAY_QUALITY_PRESETS: preset_id = DEFAULT_RELAY_QUALITY_PRESET base = dict(RELAY_QUALITY_PRESETS[preset_id]) bitrate = base.get("bitrate_kbps") if bitrate is None: bitrate = int(cfg.get("youtube_relay_bitrate") or 2500) base["preset_id"] = preset_id base["bitrate_kbps"] = max(800, min(int(bitrate), 9000)) base["audio_bitrate_kbps"] = max(128, min(int(base.get("audio_bitrate_kbps") or 128), 192)) base["x264_threads"] = max(0, int(base.get("x264_threads") or 0)) return base