sh2/d2ypp2/src/youtube_runtime.py
2026-06-02 03:19:23 -05:00

85 lines
4.1 KiB
Python

from __future__ import annotations
LOCAL_RECORD_SAVE_TYPES = frozenset({"FLV", "MKV", "MP4", "MP3音频", "M4A音频", "MP3", "M4A"})
YOUTUBE_PUSH_SAVE_TYPES = frozenset({"YOUTUBE", "YOUTUBE_PUSH", "PUSH", "RTMP", "RTMPS"})
ARM_MACHINES = frozenset({"aarch64", "arm64", "armv7l", "armv8l"})
def normalize_youtube_video_save_type(raw_value: str, process_name: str) -> str:
normalized = (raw_value or "").strip().upper()
low_process = (process_name or "").strip().lower()
force_youtube_push = low_process.startswith("youtube") or low_process.startswith("douyin_youtube")
if normalized in YOUTUBE_PUSH_SAVE_TYPES:
return "YOUTUBE"
if normalized in LOCAL_RECORD_SAVE_TYPES:
return normalized
if force_youtube_push:
return "YOUTUBE"
return "TS"
def resolve_live_video_encoder(
requested_encoder: str,
ffmpeg_encoders_text: str,
machine: str,
*,
has_mpp_service: bool = False,
has_render_node: bool = False,
has_v4l2_device: bool = False,
usable_encoders: set[str] | None = None,
) -> tuple[str, str]:
requested = (requested_encoder or "").strip()
blob = ffmpeg_encoders_text or ""
machine_norm = (machine or "").strip().lower()
is_arm = machine_norm in ARM_MACHINES
def has_encoder(name: str) -> bool:
return bool(name) and name in blob
def is_usable(name: str) -> bool:
return usable_encoders is None or name in usable_encoders
arm_candidates: list[str] = []
if has_encoder("h264_rkmpp") and has_mpp_service and is_usable("h264_rkmpp"):
arm_candidates.append("h264_rkmpp")
if has_encoder("h264_v4l2m2m") and has_v4l2_device and is_usable("h264_v4l2m2m"):
arm_candidates.append("h264_v4l2m2m")
if is_arm:
if requested == "h264_vaapi":
if arm_candidates:
return arm_candidates[0], f"LIVE_VIDEO_ENCODER=h264_vaapi is not stable on ARM, fallback to {arm_candidates[0]}"
return "", "LIVE_VIDEO_ENCODER=h264_vaapi is not supported on ARM, fallback to libx264"
if requested == "h264_v4l2m2m" and not has_v4l2_device:
return "", "LIVE_VIDEO_ENCODER=h264_v4l2m2m requires /dev/video* on this ARM host, fallback to libx264"
if requested and has_encoder(requested) and usable_encoders is not None and requested not in usable_encoders:
if arm_candidates:
return arm_candidates[0], f"LIVE_VIDEO_ENCODER={requested} failed encoder smoke test, fallback to {arm_candidates[0]}"
return "", f"LIVE_VIDEO_ENCODER={requested} failed encoder smoke test, fallback to libx264"
if requested:
if requested in arm_candidates:
return requested, ""
if requested.startswith("h264_") and has_encoder(requested) and is_usable(requested):
return requested, ""
if arm_candidates:
return arm_candidates[0], f"LIVE_VIDEO_ENCODER={requested} is unavailable on this ARM host, fallback to {arm_candidates[0]}"
return "", f"LIVE_VIDEO_ENCODER={requested} is unavailable on this ARM host, fallback to libx264"
if arm_candidates:
return arm_candidates[0], ""
return "", ""
if requested == "h264_qsv":
if has_encoder("h264_vaapi") and has_render_node and is_usable("h264_vaapi"):
return "h264_vaapi", "LIVE_VIDEO_ENCODER=h264_qsv is not enabled in this Linux stack yet, fallback to h264_vaapi"
return "", "LIVE_VIDEO_ENCODER=h264_qsv is not enabled in this Linux stack yet, fallback to libx264"
if requested == "h264_vaapi" and not has_render_node:
return "", "LIVE_VIDEO_ENCODER=h264_vaapi requires /dev/dri/renderD*; fallback to libx264"
if requested and has_encoder(requested) and usable_encoders is not None and requested not in usable_encoders:
return "", f"LIVE_VIDEO_ENCODER={requested} failed encoder smoke test, fallback to libx264"
if requested and requested.startswith("h264_") and has_encoder(requested) and is_usable(requested):
return requested, ""
if requested:
return "", f"LIVE_VIDEO_ENCODER={requested} is unavailable on this host, fallback to libx264"
return "", ""