54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""本机能力探测:GPU 等(供控制台 UI)。"""
|
||
from __future__ import annotations
|
||
|
||
import platform
|
||
import socket
|
||
import subprocess
|
||
from typing import Any
|
||
|
||
|
||
def detect_nvidia_gpu() -> bool:
|
||
if platform.system().lower() not in ("windows", "linux"):
|
||
return False
|
||
try:
|
||
_kw: dict = {"capture_output": True, "timeout": 4}
|
||
if platform.system().lower() == "windows":
|
||
_kw["creationflags"] = subprocess.CREATE_NO_WINDOW
|
||
r = subprocess.run(["nvidia-smi"], **_kw)
|
||
return r.returncode == 0
|
||
except Exception:
|
||
return False
|
||
|
||
|
||
def detect_nvenc_available() -> bool:
|
||
if not detect_nvidia_gpu():
|
||
return False
|
||
try:
|
||
_kw = {"capture_output": True, "text": True, "timeout": 8}
|
||
if platform.system().lower() == "windows":
|
||
_kw["creationflags"] = subprocess.CREATE_NO_WINDOW
|
||
r = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"], **_kw)
|
||
return r.returncode == 0 and "h264_nvenc" in (r.stdout or "")
|
||
except Exception:
|
||
return False
|
||
|
||
|
||
def gpu_status_payload() -> dict[str, Any]:
|
||
nv = detect_nvidia_gpu()
|
||
enc = detect_nvenc_available()
|
||
return {
|
||
"nvidia_detected": nv,
|
||
"nvenc_available": enc,
|
||
}
|
||
|
||
|
||
def machine_summary_payload() -> dict[str, Any]:
|
||
"""供远程 App 遥测:本机标识与系统信息。"""
|
||
return {
|
||
"hostname": socket.gethostname(),
|
||
"os": platform.system(),
|
||
"os_release": platform.release(),
|
||
"machine": platform.machine(),
|
||
"python": platform.python_version(),
|
||
}
|