perf(video): 优化视频处理性能监控和音频播放
- 添加视频处理性能计时和统计功能 - 实现帧处理时间监控和慢帧警告 - 添加音频文件静音修剪功能 - 优化Windows平台音频播放实现 - 调整默认日志输出频率减少冗余信息 - 修复MediaPipe GPU委托在Windows上的兼容性问题
This commit is contained in:
+62
-25
@@ -5,6 +5,7 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from loguru import logger
|
||||
@@ -24,12 +25,14 @@ class RepAnnouncer:
|
||||
self.max_count = max_count
|
||||
self.audio_dir = Path(audio_dir)
|
||||
|
||||
self._queue: queue.Queue[int | None] = queue.Queue()
|
||||
self._queue: queue.Queue[tuple[int, float] | None] = queue.Queue()
|
||||
self._thread: threading.Thread | None = None
|
||||
self._current_process: subprocess.Popen | None = None
|
||||
self._closed = False
|
||||
self._play_lock = threading.Lock()
|
||||
|
||||
self._platform = sys.platform
|
||||
self._direct_playback = self._platform.startswith("win")
|
||||
|
||||
if self.enabled:
|
||||
self._start()
|
||||
@@ -41,8 +44,26 @@ class RepAnnouncer:
|
||||
if count <= 0 or count > self.max_count:
|
||||
return
|
||||
|
||||
requested_at = time.perf_counter()
|
||||
if self._direct_playback:
|
||||
audio_file = self._audio_path(count)
|
||||
if not audio_file.exists():
|
||||
logger.warning("Rep audio file missing: {}", audio_file)
|
||||
return
|
||||
try:
|
||||
self._play(audio_file)
|
||||
logger.info(
|
||||
"Rep audio submitted immediately: count={}, submit_ms={:.1f}",
|
||||
count,
|
||||
(time.perf_counter() - requested_at) * 1000,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to play rep count {}: {}", count, exc)
|
||||
return
|
||||
|
||||
self._clear_pending_counts()
|
||||
self._queue.put(count)
|
||||
self._queue.put((count, requested_at))
|
||||
logger.info("Rep audio queued: count={}", count)
|
||||
|
||||
def close(self) -> None:
|
||||
"""停止播报线程并释放资源"""
|
||||
@@ -62,17 +83,24 @@ class RepAnnouncer:
|
||||
"""启动后台播报线程"""
|
||||
self.audio_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if self._direct_playback:
|
||||
import winsound
|
||||
|
||||
logger.info("Rep announcer initialized in direct Windows mode, audio_dir={}", self.audio_dir)
|
||||
return
|
||||
|
||||
self._thread = threading.Thread(target=self._run, name="RepAnnouncer", daemon=True)
|
||||
self._thread.start()
|
||||
|
||||
logger.info("Rep announcer initialized, audio_dir={}", self.audio_dir)
|
||||
logger.info("Rep announcer initialized in queued mode, audio_dir={}", self.audio_dir)
|
||||
|
||||
def _run(self) -> None:
|
||||
"""后台线程:从队列取次数,播放对应音频文件"""
|
||||
while True:
|
||||
count = self._queue.get()
|
||||
if count is None:
|
||||
item = self._queue.get()
|
||||
if item is None:
|
||||
return
|
||||
count, requested_at = item
|
||||
|
||||
audio_file = self._audio_path(count)
|
||||
if not audio_file.exists():
|
||||
@@ -81,33 +109,42 @@ class RepAnnouncer:
|
||||
|
||||
try:
|
||||
self._play(audio_file)
|
||||
logger.info(
|
||||
"Rep audio submitted from queue: count={}, queue_ms={:.1f}",
|
||||
count,
|
||||
(time.perf_counter() - requested_at) * 1000,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to play rep count {}: {}", count, exc)
|
||||
|
||||
def _play(self, audio_file: Path) -> None:
|
||||
"""播放音频文件(平台自适应)"""
|
||||
self._stop_current_playback()
|
||||
with self._play_lock:
|
||||
self._stop_current_playback()
|
||||
|
||||
if self._platform == "darwin":
|
||||
self._current_process = subprocess.Popen(
|
||||
["afplay", str(audio_file)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
elif self._platform.startswith("win"):
|
||||
import winsound
|
||||
if self._platform == "darwin":
|
||||
self._current_process = subprocess.Popen(
|
||||
["afplay", str(audio_file)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
elif self._platform.startswith("win"):
|
||||
import winsound
|
||||
|
||||
winsound.PlaySound(str(audio_file), winsound.SND_FILENAME | winsound.SND_ASYNC)
|
||||
else:
|
||||
player = shutil.which("paplay") or shutil.which("aplay")
|
||||
if player is None:
|
||||
logger.warning("No audio player found")
|
||||
return
|
||||
self._current_process = subprocess.Popen(
|
||||
[player, str(audio_file)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
winsound.PlaySound(
|
||||
str(audio_file),
|
||||
winsound.SND_FILENAME | winsound.SND_ASYNC | winsound.SND_NODEFAULT,
|
||||
)
|
||||
else:
|
||||
player = shutil.which("paplay") or shutil.which("aplay")
|
||||
if player is None:
|
||||
logger.warning("No audio player found")
|
||||
return
|
||||
self._current_process = subprocess.Popen(
|
||||
[player, str(audio_file)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
def _stop_current_playback(self) -> None:
|
||||
"""中断当前正在播放的声音"""
|
||||
|
||||
Reference in New Issue
Block a user